-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_flavor.go
131 lines (114 loc) · 3.17 KB
/
new_flavor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package handle
import (
"net/url"
"strconv"
"strings"
"github.com/polyglottis/platform/content"
"github.com/polyglottis/platform/frontend"
"github.com/polyglottis/platform/i18n"
)
type newFlavorArgs struct {
Language string
MainComment string
OtherComment string
}
func (a *newFlavorArgs) CleanUp() {
a.MainComment = strings.TrimSpace(a.MainComment)
a.OtherComment = strings.TrimSpace(a.OtherComment)
}
func (w *Worker) NewFlavorPOST(context *frontend.Context, session *Session) ([]byte, error) {
errors := make(frontend.ErrorMap)
defaults := url.Values{}
if !context.LoggedIn() {
errors["FORM"] = i18n.Key("You must sign in to perform this action.")
}
e, err := w.readExtract(context)
if err != nil {
return nil, err
}
args := new(newFlavorArgs)
err = decoder.Decode(args, context.Form)
if err != nil {
return nil, err
}
args.CleanUp()
langCode, err := w.Language.GetCode(args.Language)
if err == nil {
defaults.Set("Language", args.Language)
} else {
defaults.Set("Language", "")
errors["Language"] = i18n.Key("Please select an option.")
}
var mainComment string
var otherFlavor *content.Flavor
if fByType, ok := e.Flavors[langCode]; ok {
if flavors, ok := fByType[content.Text]; ok {
if len(flavors) > 0 {
// MainComment only necessary if there are already other flavors.
if valid, msg := content.ValidLanguageComment(args.MainComment); valid {
mainComment = args.MainComment
} else {
errors["MainComment"] = msg
}
}
if len(flavors) == 1 {
// OtherComment only necessary if there is exactly one other flavor.
if valid, msg := content.ValidLanguageComment(args.OtherComment); valid {
otherFlavor = flavors[0]
otherFlavor.LanguageComment = args.OtherComment
} else {
errors["OtherComment"] = msg
}
}
}
}
if len(errors) != 0 {
session.SaveFlashErrors(errors)
defaults.Set("MainComment", args.MainComment)
defaults.Set("OtherComment", args.OtherComment)
session.SaveDefaults(defaults)
return nil, redirectToOther(context.Url)
}
if otherFlavor != nil {
err = w.Content.UpdateFlavor(context.User, otherFlavor)
if err != nil {
return nil, err
}
}
f := &content.Flavor{
ExtractId: e.Id,
Type: content.Text,
Language: langCode,
LanguageComment: mainComment,
}
err = w.Content.NewFlavor(context.User, f)
if err != nil {
return nil, err
}
session.ClearDefaults()
if context.Query.Get("b") == "" {
context.Query.Set("b", args.Language)
context.Query.Set("bt", strconv.Itoa(int(f.Id)))
context.Query.Set("focus", "b")
} else {
context.Query.Set("a", args.Language)
context.Query.Set("at", strconv.Itoa(int(f.Id)))
}
return nil, redirectToOther("/extract/edit/text/" + e.UrlSlug + "?" + context.Query.Encode())
}
func (w *Worker) NewFlavor(context *frontend.Context) ([]byte, error) {
extract, err := w.readExtract(context)
if err != nil {
return nil, err
}
// avoid sending too much over rpc
// TODO do not read the blocks at all in the database
for _, fByType := range extract.Flavors {
for _, flavors := range fByType {
for _, f := range flavors {
f.Blocks = nil
}
}
}
return w.Server.NewFlavor(context, extract)
}