Add flash messages #10

This commit is contained in:
Aaron Fischer 2017-03-29 22:14:55 +02:00
parent 81fe574b7b
commit c0270f5f4b
6 changed files with 30 additions and 19 deletions

View file

@ -54,6 +54,12 @@
</nav> </nav>
<div id="content" class="col-md-12"> <div id="content" class="col-md-12">
{% if flash.message %}
<div class="alert alert-{{ flash.type }}" role="alert">
{{ flash.message }}
</div>
{% endif %}
{% block content %} {% block content %}
{% endblock %} {% endblock %}
</div> </div>

View file

@ -23,14 +23,12 @@
(wrap-routes middleware/wrap-csrf) (wrap-routes middleware/wrap-csrf)
(wrap-routes middleware/wrap-formats) (wrap-routes middleware/wrap-formats)
(wrap-routes middleware/wrap-identity) (wrap-routes middleware/wrap-identity)
(wrap-routes middleware/wrap-auth) (wrap-routes middleware/wrap-auth))
)
(-> #'admin-routes (-> #'admin-routes
(wrap-routes middleware/wrap-csrf) (wrap-routes middleware/wrap-csrf)
(wrap-routes middleware/wrap-formats) (wrap-routes middleware/wrap-formats)
(wrap-routes middleware/wrap-identity) (wrap-routes middleware/wrap-identity)
(wrap-routes middleware/wrap-auth) (wrap-routes middleware/wrap-auth))
)
(route/not-found (route/not-found
(:body (:body
(error-page {:status 404 (error-page {:status 404

View file

@ -28,8 +28,7 @@
:page template :page template
:csrf-token *anti-forgery-token* :csrf-token *anti-forgery-token*
:servlet-context *app-context* :servlet-context *app-context*
:identity *identity* :identity *identity*)))
)))
"text/html; charset=utf-8")) "text/html; charset=utf-8"))
(defn error-page (defn error-page

View file

@ -36,13 +36,16 @@
(run! #(add-tag image-id %) all-tags))) (run! #(add-tag image-id %) all-tags)))
(defroutes admin-routes (defroutes admin-routes
(GET "/upload" [] (GET "/upload" [:as request]
(layout/render "admin/upload.html")) (-> (layout/render
"admin/upload.html"
{:flash (:flash request)})))
(POST "/upload" [file title description tags] (POST "/upload" [file title description tags]
(-> (upload-file file) (-> (upload-file file)
(images/process-image) (images/process-image)
(add-image-to-database title description tags)) (add-image-to-database title description tags))
(redirect "/")) (-> (redirect "/upload")
(assoc :flash {:message "Upload erfolgreich." :type "success"})))
(GET "/statistics" [] (GET "/statistics" []
(layout/render "statistics.html")) (layout/render "statistics.html"))

View file

@ -15,16 +15,20 @@
user-identity (valid-identity password)] user-identity (valid-identity password)]
(if user-identity (if user-identity
(let [updated-session (assoc session :identity user-identity)] (let [updated-session (assoc session :identity user-identity)]
(-> (redirect "/") (-> (redirect "/page/1")
(assoc :session updated-session)))))) (assoc :flash {:message "Erfolgreich eingeloggt." :type "success"})
(assoc :session updated-session)))
(-> (redirect "/login")
(assoc :flash {:message "Falsches Passwort." :type "danger"})))))
(defn logout! [request] (defn logout! [request]
(-> (redirect "/") (-> (redirect "/login")
(assoc :session {}))) (assoc :session {})
(assoc :flash {:message "Erfolgreich ausgeloggt." :type "success"})))
(defroutes auth-routes (defroutes auth-routes
(GET "/login" [] (GET "/login" [:as request]
(layout/render "login.html")) (layout/render "login.html", {:flash (:flash request)}))
(POST "/login" [] login!) (POST "/login" [] login!)
(GET "/logout" [] logout!)) (GET "/logout" [] logout!))

View file

@ -10,7 +10,7 @@
(int (Math/ceil (int (Math/ceil
(/ (:count (db/get-image-count)) images-on-page)))) (/ (:count (db/get-image-count)) images-on-page))))
(defn index-page [current-page] (defn index-page [current-page request]
(let [image-count 15 (let [image-count 15
offset (* (dec current-page) image-count) offset (* (dec current-page) image-count)
pages (number-of-pages image-count) pages (number-of-pages image-count)
@ -18,7 +18,8 @@
(layout/render "index.html" (layout/render "index.html"
{:images images {:images images
:current-page current-page :current-page current-page
:pages (range 1 (inc pages))}))) :pages (range 1 (inc pages))
:flash (:flash request)})))
(defn detail-page [image-id] (defn detail-page [image-id]
(let [image (db/get-image {:id image-id}) (let [image (db/get-image {:id image-id})
@ -39,8 +40,8 @@
(defroutes core-routes (defroutes core-routes
(GET "/" [] (GET "/" []
(redirect "/page/1")) (redirect "/page/1"))
(GET "/page/:page-number" [page-number] (GET "/page/:page-number" [page-number :as request]
(index-page (Integer. page-number))) (index-page (Integer. page-number) request))
(GET "/show/:image-id" [image-id] (GET "/show/:image-id" [image-id]
(detail-page image-id)) (detail-page image-id))
(GET ["/images/:type/:hash.:ext" (GET ["/images/:type/:hash.:ext"