note editing via passwords enabled

This commit is contained in:
Christian Mueller
2012-07-14 22:34:11 +02:00
parent e53fad2319
commit 9203913886
9 changed files with 178 additions and 59 deletions
+2 -1
View File
@@ -5,6 +5,7 @@
[noir.core :only [defpartial]]
[noir.options :only [dev-mode?]]
[hiccup.util :only [escape-html]]
[ring.util.codec :only [url-encode]]
[hiccup.core]
[hiccup.page :only [include-js html5]]
[hiccup.element :only [javascript-tag]]))
@@ -12,7 +13,7 @@
(defn url
"Creates a local url from the given substrings"
[& args]
(apply str (interpose "/" (cons "" args))))
(apply str (interpose "/" (cons "" (map url-encode args)))))
; Creates the main html layout
(defpartial generate-layout
+16 -10
View File
@@ -29,6 +29,10 @@
:margin-left "auto"
:margin-right "auto"))
(defn thin-border [foreground]
(mixin :border-radius :3px
:border [:1px :solid foreground]))
; Resolves the theme name & tone parameter to a concrete color
(defn- color [& keys]
(get-in {:dark {:background :#333
@@ -70,8 +74,15 @@
(rule "&:visited"
:color link-visited))
(rule ".ui-border"
:border-radius :3px
:border [:1px :solid foreground])
(thin-border foreground))
(rule ".button"
:cursor :pointer)
(rule ".ui-elem"
helvetica-neue
(thin-border foreground)
:opacity 0.8
:font-size :1em
:background background)
(rule ".landing-button"
:box-shadow [0 :2px :5px :#aaa]
:text-decoration :none
@@ -139,6 +150,8 @@
:margin :2em))
(rule ".centered"
:text-align :center)
(rule ".bottom-space"
:margin-bottom :7em)
(rule "pre"
:border-radius :3px
:padding :0.5em
@@ -155,20 +168,13 @@
:height :500px)
(rule ".hidden"
:display :none)
(rule ".button"
:-webkit-appearance :none
helvetica-neue
:cursor :pointer
:opacity 0.8
:font-size :1em
:background background)
(rule ".central-element"
central-element)
(rule "fieldset"
:border :none)
(rule "h1"
:font-size :2em)
(rule ".dashed-line"
(rule "#dashed-line"
:border-bottom [:1px :dashed foreground-halftone]
:margin-top :3em
:margin-bottom :3em)
+43 -21
View File
@@ -9,11 +9,10 @@
[clojure.string :rename {replace sreplace} :only [split replace lower-case]]
[clojure.core.incubator :only [-?>]]
[hiccup.form]
[ring.util.codec :only [url-encode]]
[hiccup.core]
[hiccup.element]
[noir.response :only [redirect status content-type]]
[noir.core :only [defpage]]
[noir.core :only [defpage defpartial]]
[cheshire.core]
[noir.statuses])
(:import
@@ -30,7 +29,7 @@
; Sets a custom message for each needed HTTP status.
; The message to be assigned is extracted with a dynamically generated key
(doseq [code [400 404 500]]
(doseq [code [400 403 404 500]]
(set-page! code
(let [message (get-message (keyword (str "status-" code)))]
(layout message
@@ -44,13 +43,13 @@
(defn- wrap [short-url params md-text]
(when md-text
(layout params (params :title)
[:article (md-to-html md-text)]
[:article.bottom-space (md-to-html md-text)]
(let [links (map #(link-to
(if (= :short-url %)
(url short-url)
(str (params :title) "/" (name %)))
(get-message %))
[:stats :export :short-url])
[:stats :edit :export :short-url])
space (apply str (repeat 4 " "))
separator (str space "·" space)
links (interpose separator links)]
@@ -79,25 +78,42 @@
[:h2 (get-message :title)]
[:br]
[:a.landing-button {:href "/new" :style "color: white"} (get-message :new-page)]]
[:div.dashed-line]
[:div#dashed-line]
; dynamically generates three column, retrieving corresponding messages
[:article.helvetica-neue {:style "font-size: 1em"}
[:article.helvetica-neue.bottom-space {:style "font-size: 1em"}
(md-to-html (slurp "README.md"))]
[:div.centered.helvetica-neue (md-to-html (get-message :created-by))]))
; input form for the markdown text with a preview area
(defpartial input-form [form-url command fields content passwd-msg]
(let [css-class (when (= :publish command) :hidden)]
(layout {:js true} (get-message :new-note)
[:article#preview " "]
[:div#dashed-line {:class css-class}]
[:div.central-element.helvetica-neue {:style "margin-bottom: 3em"}
(form-to [:post form-url]
(hidden-field :action command)
(hidden-field :password)
fields
(text-area {:class :max-width} :draft content)
[:fieldset#input-elems {:class css-class}
(get-message passwd-msg)
(text-field {:class "ui-elem"} :plain-password)
(submit-button {:class "button ui-elem"
:id :publish-button} (get-message command))])])))
; Update Note Page
(defpage "/:year/:month/:day/:title/edit" {:keys [year month day title]}
(input-form "/update-note" :update
(html (hidden-field :key (build-key [year month day] title)))
(get-note [year month day] title) :enter-passwd))
; New Note Page
(defpage "/new" {}
(layout {:js true} (get-message :new-note)
[:article#preview " "]
[:div#preview-start-line.dashed-line.hidden]
[:div.central-element {:style "margin-bottom: 3em"}
(form-to [:post "/post-note"]
(hidden-field :session-key (create-session))
(hidden-field {:id :session-value} :session-value)
(text-area {:class :max-width} :draft (get-message :loading))
[:fieldset#input-elems.hidden
(submit-button {:class "button ui-border"
:id :publish-button} (get-message :publish))])]))
(input-form "/post-note" :publish
(html (hidden-field :session-key (create-session))
(hidden-field {:id :session-value} :session-value))
(get-message :loading) :set-passwd))
; Displays the note
(defpage "/:year/:month/:day/:title" {:keys [year month day title theme header-font text-font] :as params}
@@ -123,8 +139,14 @@
[:td (get-message :article-views)]
[:td views]]])))
; Updates a note
(defpage [:post "/update-note"] {:keys [key draft password]}
(if (update-note key draft password)
(redirect (apply url (split key #" ")))
(response 403)))
; New Note Posting — the most "complex" function in the entire app ;)
(defpage [:post "/post-note"] {:keys [draft session-key session-value]}
(defpage [:post "/post-note"] {:keys [draft password session-key session-value]}
; first we collect all info needed to evaluate the validity of the note creation request
(let [valid-session (invalidate-session session-key) ; was the note posted from a newly generated form?
valid-draft (not (ccs/blank? draft)) ; has the note a meaningful content?
@@ -151,8 +173,8 @@
(cons proposed-title
(map #(str proposed-title "-" (+ 2 %)) (range)))))]
(do
(set-note date title draft)
(redirect (url year month day (url-encode title)))))
(set-note date title draft password)
(redirect (url year month day title))))
(response 400))))
; Resolving of a short url