mirror of
https://github.com/1f349/tulip.git
synced 2024-11-12 14:51:32 +00:00
More improvements to the edit page
This commit is contained in:
parent
65f77dbe79
commit
498e42ebe8
@ -91,7 +91,8 @@ func normalLoad(startUp startUpConfig, wd string) {
|
||||
|
||||
exit_reload.ExitReload("Tulip", func() {}, func() {
|
||||
// stop http server
|
||||
srv.Close()
|
||||
_ = srv.Close()
|
||||
_ = db.Close()
|
||||
})
|
||||
}
|
||||
|
||||
@ -112,6 +113,7 @@ func checkDbHasUser(db *database.DB) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to start transaction: %w", err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err := tx.HasUser(); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
err := tx.InsertUser("Admin", "admin", "admin", "admin@localhost")
|
||||
|
@ -2,6 +2,7 @@ package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/MrMelon54/pronouns"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/text/language"
|
||||
@ -36,7 +37,8 @@ type UserPatch struct {
|
||||
Locale language.Tag
|
||||
}
|
||||
|
||||
func (u *UserPatch) ParseFromForm(v url.Values) (err error) {
|
||||
func (u *UserPatch) ParseFromForm(v url.Values) (safeErrs []error) {
|
||||
var err error
|
||||
u.Name = v.Get("name")
|
||||
u.Picture = v.Get("picture")
|
||||
u.Website = v.Get("website")
|
||||
@ -45,16 +47,16 @@ func (u *UserPatch) ParseFromForm(v url.Values) (err error) {
|
||||
} else {
|
||||
u.Pronouns, err = pronouns.FindPronoun(v.Get("pronouns"))
|
||||
if err != nil {
|
||||
return err
|
||||
safeErrs = append(safeErrs, fmt.Errorf("invalid pronoun selected"))
|
||||
}
|
||||
}
|
||||
if v.Has("reset_birthdate") {
|
||||
if v.Has("reset_birthdate") || v.Get("birthdate") == "" {
|
||||
u.Birthdate = sql.NullTime{}
|
||||
} else {
|
||||
u.Birthdate = sql.NullTime{Valid: true}
|
||||
u.Birthdate.Time, err = time.Parse(time.DateOnly, v.Get("birthdate"))
|
||||
if err != nil {
|
||||
return err
|
||||
safeErrs = append(safeErrs, fmt.Errorf("invalid time selected"))
|
||||
}
|
||||
}
|
||||
if v.Has("reset_zoneinfo") {
|
||||
@ -62,7 +64,7 @@ func (u *UserPatch) ParseFromForm(v url.Values) (err error) {
|
||||
} else {
|
||||
u.ZoneInfo, err = time.LoadLocation(v.Get("zoneinfo"))
|
||||
if err != nil {
|
||||
return err
|
||||
safeErrs = append(safeErrs, fmt.Errorf("invalid timezone selected"))
|
||||
}
|
||||
}
|
||||
if v.Has("reset_locale") {
|
||||
@ -70,8 +72,8 @@ func (u *UserPatch) ParseFromForm(v url.Values) (err error) {
|
||||
} else {
|
||||
u.Locale, err = language.Parse(v.Get("locale"))
|
||||
if err != nil {
|
||||
return err
|
||||
safeErrs = append(safeErrs, fmt.Errorf("invalid language selected"))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
@ -35,3 +35,7 @@ func (d *DB) BeginCtx(ctx context.Context) (*Tx, error) {
|
||||
}
|
||||
return &Tx{begin}, err
|
||||
}
|
||||
|
||||
func (d *DB) Close() error {
|
||||
return d.db.Close()
|
||||
}
|
||||
|
@ -22,16 +22,16 @@
|
||||
</div>
|
||||
<div>
|
||||
<label for="field_website">Website</label>
|
||||
<input type="text" name="website" id="field_website" value="{{.User.Picture}}">
|
||||
<input type="text" name="website" id="field_website" value="{{.User.Website}}">
|
||||
</div>
|
||||
<div>
|
||||
<label for="field_pronouns">Pronouns</label>
|
||||
<select name="pronouns" id="field_pronouns">
|
||||
<option value="they/them" selected>They/Them</option>
|
||||
<option value="he/him">He/Him</option>
|
||||
<option value="she/her">She/Her</option>
|
||||
<option value="it/its">It/Its</option>
|
||||
<option value="one/one's">One/One's</option>
|
||||
<option value="they/them" {{if eq "they/them" .FieldPronoun}}selected{{end}}>They/Them</option>
|
||||
<option value="he/him" {{if eq "he/him" .FieldPronoun}}selected{{end}}>He/Him</option>
|
||||
<option value="she/her" {{if eq "she/her" .FieldPronoun}}selected{{end}}>She/Her</option>
|
||||
<option value="it/its" {{if eq "it/its" .FieldPronoun}}selected{{end}}>It/Its</option>
|
||||
<option value="one/one's" {{if eq "one/one's" .FieldPronoun}}selected{{end}}>One/One's</option>
|
||||
</select>
|
||||
<label>Reset? <input type="checkbox" name="reset_pronouns"></label>
|
||||
</div>
|
||||
@ -58,7 +58,7 @@
|
||||
<option value="{{.Value}}">{{.Label}}</option>
|
||||
{{end}}
|
||||
</datalist>
|
||||
<label>Reset? <input type="checkbox" name="reset_zoneinfo"></label>
|
||||
<label>Reset? <input type="checkbox" name="reset_locale"></label>
|
||||
</div>
|
||||
<button type="submit">Edit</button>
|
||||
</form>
|
||||
|
@ -234,6 +234,7 @@ func NewHttpServer(listen, domain string, db *database.DB, privKey []byte, clien
|
||||
if err := pages.RenderPageTemplate(rw, "edit", map[string]any{
|
||||
"User": user,
|
||||
"Nonce": lNonce,
|
||||
"FieldPronoun": user.Pronouns.String(),
|
||||
"ListZoneInfo": lists.ListZoneInfo(),
|
||||
"ListLocale": lists.ListLocale(),
|
||||
}); err != nil {
|
||||
@ -243,13 +244,22 @@ func NewHttpServer(listen, domain string, db *database.DB, privKey []byte, clien
|
||||
r.POST("/edit", hs.RequireAuthentication("403 Forbidden", http.StatusForbidden, func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, auth UserAuth) {
|
||||
if req.ParseForm() != nil {
|
||||
rw.WriteHeader(http.StatusBadRequest)
|
||||
_, _ = rw.Write([]byte("400 Bad Request\n"))
|
||||
return
|
||||
}
|
||||
|
||||
var patch database.UserPatch
|
||||
err := patch.ParseFromForm(req.Form)
|
||||
if err != nil {
|
||||
errs := patch.ParseFromForm(req.Form)
|
||||
if len(errs) > 0 {
|
||||
rw.WriteHeader(http.StatusBadRequest)
|
||||
_, _ = fmt.Fprintln(rw, "<!DOCTYPE html>\n<html>\n<body>")
|
||||
_, _ = fmt.Fprintln(rw, "<p>400 Bad Request: Failed to parse form data, press the back button in your browser, check your inputs and try again.</p>")
|
||||
_, _ = fmt.Fprintln(rw, "<ul>")
|
||||
for _, i := range errs {
|
||||
_, _ = fmt.Fprintf(rw, " <li>%s</li>\n", i)
|
||||
}
|
||||
_, _ = fmt.Fprintln(rw, "</ul>")
|
||||
_, _ = fmt.Fprintln(rw, "</body>\n</html>")
|
||||
return
|
||||
}
|
||||
if hs.DbTx(rw, func(tx *database.Tx) error {
|
||||
@ -260,7 +270,7 @@ func NewHttpServer(listen, domain string, db *database.DB, privKey []byte, clien
|
||||
}) {
|
||||
return
|
||||
}
|
||||
http.Redirect(rw, req, "/", http.StatusFound)
|
||||
http.Redirect(rw, req, "/edit", http.StatusFound)
|
||||
}))
|
||||
r.GET("/userinfo", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
||||
token, err := oauthSrv.ValidationBearerToken(req)
|
||||
|
Loading…
Reference in New Issue
Block a user