oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "net/http"
5 "strconv"
6)
7
8// handleViewSave stores a tab's sort and filter.
9//
10// Editor, not viewer: the view is persisted on the tab and everybody
11// looking at that tab sees it, so changing it changes what colleagues
12// see. That is a write, and it is gated like one.
13func (a *App) handleViewSave(w http.ResponseWriter, r *http.Request) {
14 s, ok := a.sheetFromURL(w, r)
15 if !ok {
16 return
17 }
18 viewer, ok := a.actor(r)
19 if !ok {
20 a.notFound(w, r)
21 return
22 }
23 if !a.mayWrite(s.ID, viewer) {
24 a.notFound(w, r)
25 return
26 }
27
28 spec := emptyView()
29 if v := r.FormValue("sortCol"); v != "" && v != "-1" {
30 n, err := strconv.Atoi(v)
31 if err != nil {
32 a.tableRedirect(w, r, s)
33 return
34 }
35 spec.SortCol = n
36 spec.SortDsc = r.FormValue("sortDesc") != ""
37 }
38 if v := r.FormValue("filterCol"); v != "" && v != "-1" {
39 n, err := strconv.Atoi(v)
40 if err != nil {
41 a.tableRedirect(w, r, s)
42 return
43 }
44 spec.FilterCol = n
45 spec.FilterOp = r.FormValue("filterOp")
46 spec.FilterVal = r.FormValue("filterVal")
47 }
48 // An invalid spec leaves the stored one alone rather than clearing
49 // it: losing somebody's filter because they mistyped a column is a
50 // worse answer than ignoring the submission.
51 if err := spec.Validate(); err != nil {
52 if wantsNative(r) {
53 http.Error(w, "Invalid sort or filter.", 400)
54 return
55 }
56 a.tableRedirect(w, r, s)
57 return
58 }
59 if err := a.saveView(s.TabID, spec, viewer); err != nil {
60 a.fail(w, r, err)
61 return
62 }
63 a.tableRedirect(w, r, s)
64}
65
66func (a *App) tableRedirect(w http.ResponseWriter, r *http.Request, s SheetSummary) {
67 if nativeNotice(w, r, "saved") {
68 return
69 }
70 http.Redirect(w, r, "/sheets/"+strconv.FormatUint(s.ID, 10)+"/table", http.StatusSeeOther)
71}
72