From 0f6b627156aa6fa779825d69050c8da122e3e48e Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Wed, 21 Jun 2023 20:46:27 -0700 Subject: [PATCH] Spit and polish * Refactor pagination into a DRY template func * Better guide users with no profile pic to upload one --- pkg/controller/photo/upload.go | 11 ++++++++ pkg/templates/template_funcs.go | 34 ++++++++++++++++++++++++ web/templates/account/block_list.html | 21 ++------------- web/templates/account/search.html | 18 ++++--------- web/templates/admin/certification.html | 20 ++------------ web/templates/admin/feedback.html | 22 ++------------- web/templates/base.html | 1 + web/templates/forum/admin.html | 21 ++------------- web/templates/forum/board_index.html | 21 ++------------- web/templates/forum/newest.html | 21 ++------------- web/templates/forum/thread.html | 21 ++------------- web/templates/friend/friends.html | 21 ++------------- web/templates/partials/simple_pager.html | 31 +++++++++++++++++++++ web/templates/photo/gallery.html | 30 +++------------------ web/templates/photo/private.html | 21 ++------------- web/templates/photo/upload.html | 14 +++++++++- 16 files changed, 116 insertions(+), 212 deletions(-) create mode 100644 web/templates/partials/simple_pager.html diff --git a/pkg/controller/photo/upload.go b/pkg/controller/photo/upload.go index 68c190a..5acdffb 100644 --- a/pkg/controller/photo/upload.go +++ b/pkg/controller/photo/upload.go @@ -40,6 +40,17 @@ func Upload() http.HandlerFunc { vars["PhotoCount"] = photoCount vars["PhotoQuota"] = photoQuota + // If they do not have a profile picture currently set (and are not uploading one now), + // the front-end should point this out to them. + if (user.ProfilePhotoID == nil || *user.ProfilePhotoID == 0) && vars["Intent"] != "profile_pic" { + // If they have no photo at all, make the default intent to upload one. + if photoCount == 0 { + templates.Redirect(w, r.URL.Path+"?intent=profile_pic") + return + } + vars["NoProfilePicture"] = true + } + // Are they POSTing? if r.Method == http.MethodPost { var ( diff --git a/pkg/templates/template_funcs.go b/pkg/templates/template_funcs.go index d41a58e..76d4b35 100644 --- a/pkg/templates/template_funcs.go +++ b/pkg/templates/template_funcs.go @@ -1,6 +1,7 @@ package templates import ( + "bytes" "encoding/json" "fmt" "html/template" @@ -11,6 +12,7 @@ import ( "code.nonshy.com/nonshy/website/pkg/config" "code.nonshy.com/nonshy/website/pkg/markdown" + "code.nonshy.com/nonshy/website/pkg/models" "code.nonshy.com/nonshy/website/pkg/photo" "code.nonshy.com/nonshy/website/pkg/session" "code.nonshy.com/nonshy/website/pkg/utility" @@ -57,6 +59,7 @@ func TemplateFuncs(r *http.Request) template.FuncMap { "SubtractInt": SubtractInt, "UrlEncode": UrlEncode, "QueryPlus": QueryPlus(r), + "SimplePager": SimplePager(r), } } @@ -185,3 +188,34 @@ func QueryPlus(r *http.Request) func(...interface{}) template.URL { return template.URL(strings.Join(parts, "&")) } } + +// SimplePager creates a paginator row (partial template). +// +// Use it like: {{SimplePager .Pager}} +// +// It runs the template partial 'simple_pager.html' to customize it for the site theme. +func SimplePager(r *http.Request) func(*models.Pagination) template.HTML { + return func(pager *models.Pagination) template.HTML { + tmpl, err := template.New("index").Funcs(template.FuncMap{ + "QueryPlus": QueryPlus(r), + }).ParseFiles(config.TemplatePath + "/partials/simple_pager.html") + if err != nil { + return template.HTML(err.Error()) + } + + var ( + vars = struct { + Pager *models.Pagination + Request *http.Request + }{pager, r} + + buf = bytes.NewBuffer([]byte{}) + ) + + err = tmpl.ExecuteTemplate(buf, "SimplePager", vars) + if err != nil { + return template.HTML(err.Error()) + } + return template.HTML(buf.String()) + } +} diff --git a/web/templates/account/block_list.html b/web/templates/account/block_list.html index 34f3485..6b0ef32 100644 --- a/web/templates/account/block_list.html +++ b/web/templates/account/block_list.html @@ -18,24 +18,7 @@
- + {{SimplePager .Pager}}
@@ -90,4 +73,4 @@
-{{end}} \ No newline at end of file +{{end}} diff --git a/web/templates/account/search.html b/web/templates/account/search.html index b070d69..4b2de64 100644 --- a/web/templates/account/search.html +++ b/web/templates/account/search.html @@ -22,18 +22,6 @@ Found {{.Pager.Total}} user{{Pluralize64 .Pager.Total}} (page {{.Pager.Page}} of {{.Pager.Pages}}). -
- - -
@@ -173,6 +161,8 @@
+ {{SimplePager .Pager}} +
{{range .Users}} @@ -274,7 +264,9 @@ {{end}}
+ {{SimplePager .Pager}} + -{{end}} \ No newline at end of file +{{end}} diff --git a/web/templates/admin/certification.html b/web/templates/admin/certification.html index fe02f78..186e2a3 100644 --- a/web/templates/admin/certification.html +++ b/web/templates/admin/certification.html @@ -57,23 +57,7 @@ {{if .Pager}} - + {{SimplePager .Pager}} {{end}}
@@ -164,4 +148,4 @@
-{{end}} \ No newline at end of file +{{end}} diff --git a/web/templates/admin/feedback.html b/web/templates/admin/feedback.html index 98a692d..329ef8b 100644 --- a/web/templates/admin/feedback.html +++ b/web/templates/admin/feedback.html @@ -48,25 +48,7 @@ - {{if .Pager}} - - {{end}} + {{SimplePager .Pager}}
{{range .Feedback}} @@ -187,4 +169,4 @@
-{{end}} \ No newline at end of file +{{end}} diff --git a/web/templates/base.html b/web/templates/base.html index 1233518..9812664 100644 --- a/web/templates/base.html +++ b/web/templates/base.html @@ -1,4 +1,5 @@ {{define "title"}}Untitled{{end}} +{{define "content"}}{{end}} {{define "scripts"}}{{end}} {{define "base"}} diff --git a/web/templates/forum/admin.html b/web/templates/forum/admin.html index e93b7c2..21cb0a3 100644 --- a/web/templates/forum/admin.html +++ b/web/templates/forum/admin.html @@ -28,24 +28,7 @@

- + {{SimplePager .Pager}}
@@ -124,4 +107,4 @@
{{end}} -{{end}} \ No newline at end of file +{{end}} diff --git a/web/templates/forum/board_index.html b/web/templates/forum/board_index.html index 9cef992..b75b863 100644 --- a/web/templates/forum/board_index.html +++ b/web/templates/forum/board_index.html @@ -53,24 +53,7 @@ {{end}}
- + {{SimplePager .Pager}}
{{$Root := .}} @@ -158,4 +141,4 @@ {{end}} -{{end}} \ No newline at end of file +{{end}} diff --git a/web/templates/forum/newest.html b/web/templates/forum/newest.html index fec9ffd..c3fb393 100644 --- a/web/templates/forum/newest.html +++ b/web/templates/forum/newest.html @@ -38,24 +38,7 @@
- + {{SimplePager .Pager}}
@@ -273,4 +256,4 @@
{{end}} -{{end}} \ No newline at end of file +{{end}} diff --git a/web/templates/forum/thread.html b/web/templates/forum/thread.html index e847670..e85661c 100644 --- a/web/templates/forum/thread.html +++ b/web/templates/forum/thread.html @@ -97,24 +97,7 @@

- + {{SimplePager .Pager}}
{{$Root := .}} @@ -497,4 +480,4 @@ document.addEventListener('DOMContentLoaded', function() { {{end}} -{{end}} \ No newline at end of file +{{end}} diff --git a/web/templates/friend/friends.html b/web/templates/friend/friends.html index f8430dc..48e94e3 100644 --- a/web/templates/friend/friends.html +++ b/web/templates/friend/friends.html @@ -51,24 +51,7 @@
- + {{SimplePager .Pager}}
@@ -148,4 +131,4 @@
-{{end}} \ No newline at end of file +{{end}} diff --git a/web/templates/partials/simple_pager.html b/web/templates/partials/simple_pager.html new file mode 100644 index 0000000..c23613c --- /dev/null +++ b/web/templates/partials/simple_pager.html @@ -0,0 +1,31 @@ + +{{define "SimplePager"}} + +{{end}} diff --git a/web/templates/photo/gallery.html b/web/templates/photo/gallery.html index 84dbe80..0283018 100644 --- a/web/templates/photo/gallery.html +++ b/web/templates/photo/gallery.html @@ -79,30 +79,6 @@ {{end}} - -{{define "pager"}} -{{if .Pager.Total}} - -{{end}} -{{end}} - {{define "content"}}
@@ -361,7 +337,7 @@ {{end}} {{end}} - {{template "pager" .}} + {{SimplePager .Pager}} {{if eq .ViewStyle "full"}} @@ -576,7 +552,7 @@
{{end}} - {{template "pager" .}} + {{SimplePager .Pager}} @@ -601,4 +577,4 @@ return false; } -{{end}} \ No newline at end of file +{{end}} diff --git a/web/templates/photo/private.html b/web/templates/photo/private.html index 85ff5fc..22154c6 100644 --- a/web/templates/photo/private.html +++ b/web/templates/photo/private.html @@ -69,24 +69,7 @@ {{end}}
- + {{SimplePager .Pager}}
@@ -146,4 +129,4 @@
-{{end}} \ No newline at end of file +{{end}} diff --git a/web/templates/photo/upload.html b/web/templates/photo/upload.html index 4190afe..d70af99 100644 --- a/web/templates/photo/upload.html +++ b/web/templates/photo/upload.html @@ -88,6 +88,18 @@ {{end}} + + {{if .NoProfilePicture}} +
+

+ Notice: you currently do not have a Default Profile Picture set up on your + account. You can click here to upload a new one + or click the Edit button on one of your existing photos + to crop a square profile picture from one of them. +

+
+ {{end}} + {{if or .EditPhoto (lt .PhotoCount .PhotoQuota)}}
@@ -558,4 +570,4 @@
-{{end}} \ No newline at end of file +{{end}}