list.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. tpl, err := tpl_util.LoadTextTemplate("./generator/generators/list/list.tpl.md", funcMap)
  35. if err != nil {
  36. panic(err)
  37. }
  38. f, err := os.Create(filename + ".md")
  39. if err != nil {
  40. panic(err)
  41. }
  42. defer f.Close()
  43. err = tpl.Execute(f, teachers)
  44. if err != nil {
  45. panic(err)
  46. }
  47. odtFilename := fileutil.ReplaceExt(filename, "odt")
  48. log.Println("Generate", odtFilename)
  49. if err := pandoc.Convert(filename+".md", odtFilename, "--data-dir", "./generator/generators/list/"); err != nil {
  50. panic(err)
  51. }
  52. if err := libreoffice.Convert(filename+".odt", "pdf", "--outdir", outputPath); err != nil {
  53. panic(err)
  54. }
  55. }
  56. func comma(classes []*orm.Class) string {
  57. var names []string
  58. for _, c := range classes {
  59. names = append(names, c.Name)
  60. }
  61. return strings.Join(names, ",")
  62. }
  63. func abbrev(name string) string {
  64. var result string
  65. splits := strings.Split(name, " ")
  66. for i := len(splits) - 1; i >= 0; i-- {
  67. if i == len(splits)-1 {
  68. result += splits[i] + " "
  69. continue
  70. }
  71. result += strings.ToUpper(string(splits[i][0]) + ".")
  72. }
  73. return result
  74. }
  75. func nbsp(num int, text string) string {
  76. return text + strings.Repeat(" ", num)
  77. }
  78. func groupClasses(classes []*orm.Class) string {
  79. var groups []string
  80. groupByAddresses := make(map[string][]string)
  81. addressesAbbrev := map[string]string{
  82. "Linguistico": "LIN",
  83. "Classico": "CLA",
  84. "Musicale": "M",
  85. "Economico sociale": "ES",
  86. "Scienze umane": "SU",
  87. }
  88. for _, c := range classes {
  89. groupByAddresses[c.Field] = append(groupByAddresses[c.Field], fmt.Sprintf("%d%s", c.Year, c.Section))
  90. }
  91. for address, classes := range groupByAddresses {
  92. var group string
  93. if len(classes) > 1 {
  94. group = fmt.Sprintf("[%s]%s", strings.Join(classes, ","), addressesAbbrev[address])
  95. } else {
  96. group = fmt.Sprintf("%s %s", strings.Join(classes, ","), addressesAbbrev[address])
  97. }
  98. groups = append(groups, group)
  99. }
  100. return strings.Join(groups, ",")
  101. }
  102. func (generator *ListGenerator) Run(jobId uint) {
  103. ncClient, err := cloud.Dial(
  104. "https://cloud.carducci-dante.gov.it/remote.php/webdav/",
  105. "andrea.fazzi",
  106. "6eQn}9l>=A",
  107. )
  108. if err != nil {
  109. panic(err)
  110. }
  111. log.Printf("Connecting to karmen...")
  112. client, err := karmen_client.Dial(
  113. "http://localhost:3000",
  114. "admin",
  115. "admin",
  116. )
  117. if err != nil {
  118. log.Println(err)
  119. }
  120. job, err := client.GetJob(jobId)
  121. if err != nil {
  122. panic(err)
  123. }
  124. job.Start = time.Now()
  125. err = client.UpdateJob(job)
  126. if err != nil {
  127. panic(err)
  128. }
  129. teachers, err := client.GetTeachers()
  130. if err != nil {
  131. panic(err)
  132. }
  133. outputPath := filepath.Join(config.Config.Documents.OutputPath, fmt.Sprintf("%d/%d", job.DocumentID, job.ID))
  134. if err := os.MkdirAll(outputPath, 0777); err != nil {
  135. panic(err)
  136. }
  137. generator.generate(outputPath, teachers, funcMap)
  138. job.End = time.Now()
  139. job.Files = append(job.Files, &orm.File{Path: "elenco_docenti.pdf"})
  140. err = client.UpdateJob(job)
  141. if err != nil {
  142. panic(err)
  143. }
  144. log.Println("Uploading files to the cloud...")
  145. src, err := ioutil.ReadFile(filepath.Join(outputPath, "elenco_docenti.pdf"))
  146. if err != nil {
  147. panic(err)
  148. }
  149. err = ncClient.Upload(src, "Carducci Dante/Docenti/Elenchi/elenco_docenti_da_karmen.pdf")
  150. if err != nil {
  151. panic(err)
  152. }
  153. }