From 9bc75c86f3202be8f52662a149b226bfbf02c0af Mon Sep 17 00:00:00 2001 From: akuma06 Date: Mon, 8 May 2017 01:46:30 +0200 Subject: [PATCH] Added logged badge + new User variable in each template variables --- db/gorm.go | 2 +- model/user.go | 7 ++++--- public/css/style.css | 16 +++++++++++++--- router/faqHandler.go | 2 +- router/homeHandler.go | 2 +- router/notFoundHandler.go | 2 +- router/router.go | 1 + router/searchHandler.go | 2 +- router/templateVariables.go | 15 +++++++++++++++ router/uploadHandler.go | 2 +- router/userHandler.go | 12 ++++++------ router/viewTorrentHandler.go | 2 +- templates/_badgemenu.html | 20 ++++++++++++++++++++ templates/index.html | 1 + 14 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 templates/_badgemenu.html diff --git a/db/gorm.go b/db/gorm.go index bbef9270..00d49b38 100644 --- a/db/gorm.go +++ b/db/gorm.go @@ -31,7 +31,7 @@ func GormInit(conf *config.Config) (*gorm.DB, error) { if config.Environment == "DEVELOPMENT" { db.LogMode(true) // db.DropTable(&model.User{}, "UserFollower") - db.AutoMigrate(&model.Torrents{}, &model.User{}, &model.Role{}, &model.Connection{}, &model.Language{}) + db.AutoMigrate(&model.Torrents{}, &model.User{}, &model.Role{}, &model.Language{}) // db.AutoMigrate(&model.Article{}, &model.Location{}, &model.Comment{}, &model.File{}) // db.Model(&model.User{}).AddIndex("idx_user_token", "token") diff --git a/model/user.go b/model/user.go index d121c305..0d1a43b9 100644 --- a/model/user.go +++ b/model/user.go @@ -44,7 +44,7 @@ type User struct { Likings []User `gorm:"foreignkey:userId;associationforeignkey:follower_id;many2many:users_followers;"` Liked []User `gorm:"foreignkey:follower_id;associationforeignkey:userId;many2many:users_followers;"` - Connections []Connection + //Connections []Connection Languages []Language `gorm:"many2many:user_languages;"` // Many To Many, user_languages is the join table Roles []Role `gorm:"many2many:users_roles;"` // Many To Many, users_roles @@ -79,13 +79,13 @@ type PublicUser struct { LastLoginIp omit `json:"lastLoginIp,omitempty",sql:"size:100"` CurrentLoginIp omit `json:"currentLoginIp,omitempty",sql:"size:100"` - Connections omit `json:"connections,omitempty"` + //Connections omit `json:"connections,omitempty"` Languages omit `json:"languages,omitempty"` Roles omit `json:"roles,omitempty"` Torrents omit `json:"articles,omitempty"` //should user torrents not be displayed? } -// Connection is a connection model for oauth. +/*// Connection is a connection model for oauth. type Connection struct { Id uint `json:"id"` UserId uint `json:"userId"` @@ -95,3 +95,4 @@ type Connection struct { ProfileUrl string `gorm:"column:profile_url", json:"profileUrl"` ImageUrl string `gorm:"column:image_url", json:"imageUrl"` } +*/ \ No newline at end of file diff --git a/public/css/style.css b/public/css/style.css index 05876b95..86db4521 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -20,19 +20,25 @@ } /* modified copy of NyaaTorrent theme */ -#mainmenu { +#mainmenu, #mainmenu .dropdown-menu{ background: #263238; - position: fixed; color: white; +} +#mainmenu { + position: fixed; width: 100%; z-index: 4; border: 0px solid white; } - #mainmenu a { + background: none; color: white; } +#mainmenu .divider { + background: black; +} + #container { padding-top: 1.25em; background: white; @@ -182,3 +188,7 @@ div.container div.blockBody:nth-of-type(2) table tr:first-of-type th:last-of-typ .navbar { border-radius: 0px; } +.navbar-nav>li>a.profile-image { + padding-top: 10px; + padding-bottom: 10px; +} \ No newline at end of file diff --git a/router/faqHandler.go b/router/faqHandler.go index 31b1e4c5..11b8f399 100644 --- a/router/faqHandler.go +++ b/router/faqHandler.go @@ -9,7 +9,7 @@ import ( func FaqHandler(w http.ResponseWriter, r *http.Request) { searchForm := NewSearchForm() searchForm.HideAdvancedSearch = true - err := faqTemplate.ExecuteTemplate(w, "index.html", FaqTemplateVariables{Navigation{}, searchForm, r.URL, mux.CurrentRoute(r)}) + err := faqTemplate.ExecuteTemplate(w, "index.html", FaqTemplateVariables{Navigation{}, searchForm, GetUser(r), r.URL, mux.CurrentRoute(r)}) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } diff --git a/router/homeHandler.go b/router/homeHandler.go index f8a643a6..ab0aa54b 100644 --- a/router/homeHandler.go +++ b/router/homeHandler.go @@ -35,7 +35,7 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) { } navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"} - htv := HomeTemplateVariables{b, NewSearchForm(), navigationTorrents, r.URL, mux.CurrentRoute(r)} + htv := HomeTemplateVariables{b, NewSearchForm(), navigationTorrents, GetUser(r), r.URL, mux.CurrentRoute(r)} err := homeTemplate.ExecuteTemplate(w, "index.html", htv) if err != nil { diff --git a/router/notFoundHandler.go b/router/notFoundHandler.go index 2bf91697..b13eaf52 100644 --- a/router/notFoundHandler.go +++ b/router/notFoundHandler.go @@ -19,7 +19,7 @@ func NotFoundHandler(w http.ResponseWriter, r *http.Request) { searchForm := NewSearchForm() searchForm.HideAdvancedSearch = true - err := notFoundTemplate.ExecuteTemplate(w, "index.html", NotFoundTemplateVariables{Navigation{}, searchForm, r.URL, mux.CurrentRoute(r)}) + err := notFoundTemplate.ExecuteTemplate(w, "index.html", NotFoundTemplateVariables{Navigation{}, searchForm, GetUser(r), r.URL, mux.CurrentRoute(r)}) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } diff --git a/router/router.go b/router/router.go index 84206748..57de30e6 100644 --- a/router/router.go +++ b/router/router.go @@ -35,6 +35,7 @@ func init() { Router.HandleFunc("/verify/email/{token}", UserVerifyEmailHandler).Name("user_verify").Methods("GET") Router.HandleFunc("/user/register", UserRegisterPostHandler).Name("user_register_post").Methods("POST") Router.HandleFunc("/user/login", UserLoginPostHandler).Name("user_login_post").Methods("POST") + Router.HandleFunc("/user/{id}", UserProfileHandler).Name("user_profile") Router.PathPrefix("/captcha").Methods("GET").HandlerFunc(captcha.ServeFiles) Router.NotFoundHandler = http.HandlerFunc(NotFoundHandler) diff --git a/router/searchHandler.go b/router/searchHandler.go index c44bf29e..49ebfadf 100644 --- a/router/searchHandler.go +++ b/router/searchHandler.go @@ -37,7 +37,7 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) { search_param.Order, false, } - htv := HomeTemplateVariables{b, searchForm, navigationTorrents, r.URL, mux.CurrentRoute(r)} + htv := HomeTemplateVariables{b, searchForm, navigationTorrents, GetUser(r), r.URL, mux.CurrentRoute(r)} err := searchTemplate.ExecuteTemplate(w, "index.html", htv) if err != nil { diff --git a/router/templateVariables.go b/router/templateVariables.go index 841fde51..32eb7b8f 100644 --- a/router/templateVariables.go +++ b/router/templateVariables.go @@ -2,9 +2,11 @@ package router import ( "net/url" + "net/http" "github.com/ewhal/nyaa/model" userForms "github.com/ewhal/nyaa/service/user/form" + "github.com/ewhal/nyaa/service/user" "github.com/gorilla/mux" ) @@ -17,6 +19,7 @@ import ( type FaqTemplateVariables struct { Navigation Navigation Search SearchForm + User model.User URL *url.URL // For parsing Url in templates Route *mux.Route // For getting current route in templates } @@ -24,6 +27,7 @@ type FaqTemplateVariables struct { type NotFoundTemplateVariables struct { Navigation Navigation Search SearchForm + User model.User URL *url.URL // For parsing Url in templates Route *mux.Route // For getting current route in templates } @@ -32,6 +36,7 @@ type ViewTemplateVariables struct { Torrent model.TorrentsJson Search SearchForm Navigation Navigation + User model.User URL *url.URL // For parsing Url in templates Route *mux.Route // For getting current route in templates } @@ -41,6 +46,7 @@ type UserRegisterTemplateVariables struct { FormErrors map[string][]string Search SearchForm Navigation Navigation + User model.User URL *url.URL // For parsing Url in templates Route *mux.Route // For getting current route in templates } @@ -49,6 +55,7 @@ type UserVerifyTemplateVariables struct { FormErrors map[string][]string Search SearchForm Navigation Navigation + User model.User URL *url.URL // For parsing Url in templates Route *mux.Route // For getting current route in templates } @@ -58,6 +65,7 @@ type UserLoginFormVariables struct { FormErrors map[string][]string Search SearchForm Navigation Navigation + User model.User URL *url.URL // For parsing Url in templates Route *mux.Route // For getting current route in templates } @@ -66,6 +74,7 @@ type HomeTemplateVariables struct { ListTorrents []model.TorrentsJson Search SearchForm Navigation Navigation + User model.User URL *url.URL // For parsing Url in templates Route *mux.Route // For getting current route in templates } @@ -74,6 +83,7 @@ type UploadTemplateVariables struct { Upload UploadForm Search SearchForm Navigation Navigation + User model.User URL *url.URL Route *mux.Route } @@ -123,3 +133,8 @@ func NewSearchForm(params ...string) (searchForm SearchForm) { } return } + +func GetUser(r *http.Request) model.User { + user, _ , _ := userService.RetrieveCurrentUser(r) + return user +} \ No newline at end of file diff --git a/router/uploadHandler.go b/router/uploadHandler.go index e2d11a56..c08e8497 100644 --- a/router/uploadHandler.go +++ b/router/uploadHandler.go @@ -55,7 +55,7 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) { } } else if r.Method == "GET" { uploadForm.CaptchaID = captcha.GetID() - htv := UploadTemplateVariables{uploadForm, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} + htv := UploadTemplateVariables{uploadForm, NewSearchForm(), Navigation{}, GetUser(r), r.URL, mux.CurrentRoute(r)} err = uploadTemplate.ExecuteTemplate(w, "index.html", htv) } else { w.WriteHeader(http.StatusMethodNotAllowed) diff --git a/router/userHandler.go b/router/userHandler.go index bf26c3ae..862db76b 100644 --- a/router/userHandler.go +++ b/router/userHandler.go @@ -22,7 +22,7 @@ func UserRegisterFormHandler(w http.ResponseWriter, r *http.Request) { modelHelper.BindValueForm(&b, r) b.CaptchaID = captcha.GetID() languages.SetTranslationFromRequest(viewRegisterTemplate, r, "en-us") - htv := UserRegisterTemplateVariables{b, form.NewErrors(), NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} + htv := UserRegisterTemplateVariables{b, form.NewErrors(), NewSearchForm(), Navigation{}, GetUser(r), r.URL, mux.CurrentRoute(r)} err := viewRegisterTemplate.ExecuteTemplate(w, "index.html", htv) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -38,7 +38,7 @@ func UserLoginFormHandler(w http.ResponseWriter, r *http.Request) { modelHelper.BindValueForm(&b, r) languages.SetTranslationFromRequest(viewLoginTemplate, r, "en-us") - htv := UserLoginFormVariables{b, form.NewErrors(), NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} + htv := UserLoginFormVariables{b, form.NewErrors(), NewSearchForm(), Navigation{}, GetUser(r), r.URL, mux.CurrentRoute(r)} err := viewLoginTemplate.ExecuteTemplate(w, "index.html", htv) if err != nil { @@ -78,7 +78,7 @@ func UserRegisterPostHandler(w http.ResponseWriter, r *http.Request) { if (len(err) == 0) { b := form.RegistrationForm{} languages.SetTranslationFromRequest(viewRegisterSuccessTemplate, r, "en-us") - htv := UserRegisterTemplateVariables{b, err, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} + htv := UserRegisterTemplateVariables{b, err, NewSearchForm(), Navigation{}, GetUser(r), r.URL, mux.CurrentRoute(r)} errorTmpl := viewRegisterSuccessTemplate.ExecuteTemplate(w, "index.html", htv) if errorTmpl != nil { http.Error(w, errorTmpl.Error(), http.StatusInternalServerError) @@ -90,7 +90,7 @@ func UserRegisterPostHandler(w http.ResponseWriter, r *http.Request) { if (len(err) > 0) { b.CaptchaID = captcha.GetID() languages.SetTranslationFromRequest(viewRegisterTemplate, r, "en-us") - htv := UserRegisterTemplateVariables{b, err, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} + htv := UserRegisterTemplateVariables{b, err, NewSearchForm(), Navigation{}, GetUser(r), r.URL, mux.CurrentRoute(r)} errorTmpl := viewRegisterTemplate.ExecuteTemplate(w, "index.html", htv) if errorTmpl != nil { http.Error(w, errorTmpl.Error(), http.StatusInternalServerError) @@ -107,7 +107,7 @@ func UserVerifyEmailHandler(w http.ResponseWriter, r *http.Request) { err["errors"] = append(err["errors"], errEmail.Error()) } languages.SetTranslationFromRequest(viewVerifySuccessTemplate, r, "en-us") - htv := UserVerifyTemplateVariables{err, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} + htv := UserVerifyTemplateVariables{err, NewSearchForm(), Navigation{}, GetUser(r), r.URL, mux.CurrentRoute(r)} errorTmpl := viewVerifySuccessTemplate.ExecuteTemplate(w, "index.html", htv) if errorTmpl != nil { http.Error(w, errorTmpl.Error(), http.StatusInternalServerError) @@ -125,7 +125,7 @@ func UserLoginPostHandler(w http.ResponseWriter, r *http.Request) { if (errorUser != nil) { err["errors"] = append(err["errors"], errorUser.Error()) languages.SetTranslationFromRequest(viewLoginTemplate, r, "en-us") - htv := UserLoginFormVariables{b, err, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} + htv := UserLoginFormVariables{b, err, NewSearchForm(), Navigation{}, GetUser(r), r.URL, mux.CurrentRoute(r)} errorTmpl := viewLoginTemplate.ExecuteTemplate(w, "index.html", htv) if errorTmpl != nil { http.Error(w, errorTmpl.Error(), http.StatusInternalServerError) diff --git a/router/viewTorrentHandler.go b/router/viewTorrentHandler.go index 67a9c4ce..a52b8483 100644 --- a/router/viewTorrentHandler.go +++ b/router/viewTorrentHandler.go @@ -19,7 +19,7 @@ func ViewHandler(w http.ResponseWriter, r *http.Request) { } b := torrent.ToJson() - htv := ViewTemplateVariables{b, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} + htv := ViewTemplateVariables{b, NewSearchForm(), Navigation{}, GetUser(r), r.URL, mux.CurrentRoute(r)} err = viewTemplate.ExecuteTemplate(w, "index.html", htv) if err != nil { diff --git a/templates/_badgemenu.html b/templates/_badgemenu.html new file mode 100644 index 00000000..265fa54f --- /dev/null +++ b/templates/_badgemenu.html @@ -0,0 +1,20 @@ +{{define "badge_user"}} +{{with .User}} + +{{end}} +{{end}} diff --git a/templates/index.html b/templates/index.html index 9b6c3391..19265138 100755 --- a/templates/index.html +++ b/templates/index.html @@ -55,6 +55,7 @@ {{block "search_button" .}}{{end}} + {{block "badge_user" .}}{{end}}