list.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package list
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "text/template"
  10. "time"
  11. "github.com/remogatto/cloud"
  12. karmen_client "gogs.carducci-dante.gov.it/karmen/client"
  13. "gogs.carducci-dante.gov.it/karmen/core/config"
  14. "gogs.carducci-dante.gov.it/karmen/core/orm"
  15. "gogs.carducci-dante.gov.it/karmen/util/fileutil"
  16. "gogs.carducci-dante.gov.it/karmen/util/libreoffice"
  17. "gogs.carducci-dante.gov.it/karmen/util/pandoc"
  18. tpl_util "gogs.carducci-dante.gov.it/karmen/util/template"
  19. )
  20. type ListGenerator struct {
  21. Config *config.ConfigT
  22. }
  23. func NewListGenerator(config *config.ConfigT) *ListGenerator {
  24. return &ListGenerator{config}
  25. }
  26. var funcMap template.FuncMap = template.FuncMap{
  27. "comma": comma,
  28. "groupClasses": groupClasses,
  29. "abbrev": abbrev,
  30. "nbsp": nbsp,
  31. }
  32. func (generator *ListGenerator) generate(outputPath string, teachers []*orm.Teacher, funcMap template.FuncMap) {
  33. filename := filepath.Join(outputPath, "elenco_docenti")
  34. tplContent, err := ncClient.Download("")
  35. tpl, err := tpl_util.LoadTextTemplateFromString("./generator/generators/list/list.tpl.md", funcMap)
  36. if err != nil {
  37. panic(err)
  38. }
  39. f, err := os.Create(filename + ".md")
  40. if err != nil {
  41. panic(err)
  42. }
  43. defer f.Close()
  44. err = tpl.Execute(f, teachers)
  45. if err != nil {
  46. panic(err)
  47. }
  48. odtFilename := fileutil.ReplaceExt(filename, "odt")
  49. log.Println("Generate", odtFilename)
  50. if err := pandoc.Convert(filename+".md", odtFilename, "--data-dir", "./generator/generators/list/"); err != nil {
  51. panic(err)
  52. }
  53. if err := libreoffice.Convert(filename+".odt", "pdf", "--outdir", outputPath); err != nil {
  54. panic(err)
  55. }
  56. }
  57. func comma(classes []*orm.Class) string {
  58. var names []string
  59. for _, c := range classes {
  60. names = append(names, c.Name)
  61. }
  62. return strings.Join(names, ",")
  63. }
  64. func abbrev(name string) string {
  65. var result string
  66. splits := strings.Split(name, " ")
  67. for i := len(splits) - 1; i >= 0; i-- {
  68. if i == len(splits)-1 {
  69. result += splits[i] + " "
  70. continue
  71. }
  72. result += strings.ToUpper(string(splits[i][0]) + ".")
  73. }
  74. return result
  75. }
  76. func nbsp(num int, text string) string {
  77. return text + strings.Repeat(" ", num)
  78. }
  79. func groupClasses(classes []*orm.Class) string {
  80. var groups []string
  81. groupByAddresses := make(map[string][]string)
  82. addressesAbbrev := map[string]string{
  83. "Linguistico": "LIN",
  84. "Classico": "CLA",
  85. "Musicale": "M",
  86. "Economico sociale": "ES",
  87. "Scienze umane": "SU",
  88. }
  89. for _, c := range classes {
  90. groupByAddresses[c.Field] = append(groupByAddresses[c.Field], fmt.Sprintf("%d%s", c.Year, c.Section))
  91. }
  92. for address, classes := range groupByAddresses {
  93. var group string
  94. if len(classes) > 1 {
  95. group = fmt.Sprintf("[%s]%s", strings.Join(classes, ","), addressesAbbrev[address])
  96. } else {
  97. group = fmt.Sprintf("%s %s", strings.Join(classes, ","), addressesAbbrev[address])
  98. }
  99. groups = append(groups, group)
  100. }
  101. return strings.Join(groups, ",")
  102. }
  103. func (generator *ListGenerator) Run(jobId uint) {
  104. ncClient, err := cloud.Dial(
  105. config.Config.Cloud.Url,
  106. config.Config.Cloud.Username,
  107. config.Config.Cloud.Password,
  108. )
  109. if err != nil {
  110. panic(err)
  111. }
  112. log.Printf("Connecting to karmen...")
  113. client, err := karmen_client.Dial(
  114. config.Config.Url,
  115. config.Config.Admin.Username,
  116. config.Config.Admin.Password,
  117. )
  118. if err != nil {
  119. log.Println(err)
  120. }
  121. job, err := client.GetJob(jobId)
  122. if err != nil {
  123. panic(err)
  124. }
  125. job.Start = time.Now()
  126. err = client.UpdateJob(job)
  127. if err != nil {
  128. panic(err)
  129. }
  130. teachers, err := client.GetTeachers()
  131. if err != nil {
  132. panic(err)
  133. }
  134. outputPath := filepath.Join(config.Config.Documents.OutputPath, fmt.Sprintf("%d/%d", job.DocumentID, job.ID))
  135. if err := os.MkdirAll(outputPath, 0777); err != nil {
  136. panic(err)
  137. }
  138. generator.generate(outputPath, teachers, funcMap)
  139. job.End = time.Now()
  140. job.Files = append(job.Files, &orm.File{Path: "elenco_docenti.pdf"})
  141. time.Sleep(10 * time.Second)
  142. err = client.UpdateJob(job)
  143. if err != nil {
  144. panic(err)
  145. }
  146. log.Println("Uploading files to the cloud...")
  147. src, err := ioutil.ReadFile(filepath.Join(outputPath, "elenco_docenti.pdf"))
  148. if err != nil {
  149. panic(err)
  150. }
  151. err = ncClient.Upload(src, "Documents/elenco_docenti.pdf")
  152. if err != nil {
  153. panic(err)
  154. }
  155. log.Println("Files were uploaded to the cloud...")
  156. }