admin/sync: tiny refactor

This commit is contained in:
nemunaire 2018-01-08 01:34:22 +01:00
parent 5edccf21cd
commit 494ccb740b
2 changed files with 14 additions and 9 deletions

View File

@ -60,9 +60,7 @@ func SyncExerciceKeys(i Importer, exercice fic.Exercice) []string {
return errs
}
func SyncExerciceMCQ(i Importer, exercice fic.Exercice) []string {
var errs []string
func SyncExerciceMCQ(i Importer, exercice fic.Exercice) (errs []string) {
if _, err := exercice.WipeMCQs(); err != nil {
errs = append(errs, err.Error())
return errs
@ -70,7 +68,9 @@ func SyncExerciceMCQ(i Importer, exercice fic.Exercice) []string {
// Unique Choice Questions (checkbox)
if ucq, err := getFileContent(i, path.Join(exercice.Path, "flags-ucq.txt")); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to read ucq: %s", path.Base(exercice.Path), err))
if i.exists(path.Join(exercice.Path, "flags-ucq.txt")) {
errs = append(errs, fmt.Sprintf("%q: unable to read ucq: %s", path.Base(exercice.Path), err))
}
} else if flag, err := exercice.AddMCQ(""); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to add ucq: %s", path.Base(exercice.Path), err))
} else {
@ -98,7 +98,9 @@ func SyncExerciceMCQ(i Importer, exercice fic.Exercice) []string {
// Multiple Choice Questions (radio)
if mcq, err := getFileContent(i, path.Join(exercice.Path, "flags-mcq.txt")); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to read mcq: %s", path.Base(exercice.Path), err))
if i.exists(path.Join(exercice.Path, "flags-mcq.txt")) {
errs = append(errs, fmt.Sprintf("%q: unable to read mcq: %s", path.Base(exercice.Path), err))
}
} else {
for nline, quest := range strings.Split(mcq, "\n") {
quest_splt := strings.Split(string(quest), "\t")

View File

@ -15,11 +15,11 @@ import (
var oneDeepSync sync.Mutex
func SyncDeep(i Importer) map[string][]string {
func SyncDeep(i Importer) (errs map[string][]string) {
oneDeepSync.Lock()
defer oneDeepSync.Unlock()
errs := map[string][]string{}
errs = map[string][]string{}
errs["_date"] = []string{fmt.Sprintf("%v", time.Now())}
errs["_themes"] = SyncThemes(i)
@ -52,6 +52,8 @@ func SyncDeep(i Importer) map[string][]string {
errs["_date"] = append(errs["_date"], fmt.Sprintf("%v", time.Now()))
errs["_regeneration"] = []string{}
if fdto, err := os.Create("../htdocs-admin/full_import_report.json"); err == nil {
defer fdto.Close()
@ -59,17 +61,18 @@ func SyncDeep(i Importer) map[string][]string {
if out, err := json.Marshal(errs); err == nil {
fdto.Write(out)
} else {
errs["_regeneration"] = append(errs["_regeneration"], err.Error())
log.Println(err)
}
fdto.Write([]byte(");"))
} else {
errs["_regeneration"] = append(errs["_regeneration"], err.Error())
log.Println(err)
}
errs["_regeneration"] = []string{}
if err := settings.ForceRegeneration(); err != nil {
errs["_regeneration"] = append(errs["_regeneration"], err.Error())
}
return errs
return
}