Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Updated dependencies. Fixed edit! and improved add! #6

Merged
merged 2 commits into from about 2 years ago

2 participants

Zoran Tomičić Chris Granger
Zoran Tomičić
zoka commented March 11, 2012

Hi Chris

Couple of small fixes:

  • Latest dependencies
  • Finer grained validation for add! and avoiding to clear Username field if validation fails
  • Display validation errors in span tag
  • Fixed exception in edit! and allowed either to change existing user password or create new one.
Chris Granger ibdknox merged commit 613798b into from March 20, 2012
Chris Granger ibdknox closed this March 20, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
1  .gitignore
... ...
@@ -1,3 +1,4 @@
  1
+*~
1 2
 pom.xml
2 3
 *jar
3 4
 /lib/
6  project.clj
... ...
@@ -1,8 +1,8 @@
1 1
 (defproject noir-blog "0.1.0"
2 2
             :description "A fully functional blog that serves an example of a noir project."
3  
-            :dependencies [[org.clojure/clojure "1.2.1"]
4  
-                           [clj-time "0.3.0"]
5  
-                           [noir "1.0.0"]
  3
+            :dependencies [[org.clojure/clojure "1.3.0"]
  4
+                           [clj-time "0.3.7"]
  5
+                           [noir "1.2.2"]
6 6
                            [org.markdownj/markdownj "0.3.0-1.0.2b4"]
7 7
                            [simpledb "0.1.4"]]
8 8
             :main noir-blog.server)
24  src/noir_blog/models/user.clj
@@ -23,14 +23,17 @@
23 23
 (defn prepare [{password :password :as user}]
24 24
   (assoc user :password (crypt/encrypt password)))
25 25
 
26  
-(defn valid? [{:keys [username password]}]
  26
+(defn valid-user? [username]
27 27
   (vali/rule (not (db/get-in :users [username]))
28 28
              [:username "That username is already taken"])
29 29
   (vali/rule (vali/min-length? username 3)
30 30
              [:username "Username must be at least 3 characters."])
  31
+  (not (vali/errors? :username :password)))
  32
+
  33
+(defn valid-psw? [password]
31 34
   (vali/rule (vali/min-length? password 5)
32 35
              [:password "Password must be at least 5 characters."])
33  
-  (not (vali/errors? :username :password)))
  36
+  (not (vali/errors? :password)))
34 37
 
35 38
 ;; Operations
36 39
 
@@ -46,14 +49,17 @@
46 49
         (session/put! :username username))
47 50
       (vali/set-error :username "Invalid username or password"))))
48 51
 
49  
-(defn add! [{:keys [username password]}]
50  
-  (let [user (-> {:username username :password password}
51  
-               (prepare))]
52  
-    (when (valid? user)
53  
-      (store! user))))
  52
+(defn add! [{:keys [username password] :as user}]
  53
+  (when (valid-user? username)
  54
+    (when (valid-psw? password)
  55
+      (-> user (prepare) (store!)))))
54 56
 
55  
-(defn edit! [user]
56  
-  (add! user))
  57
+(defn edit! [{:keys [username old-name password]}]
  58
+  (let [user {:username username :password password}]
  59
+    (if (= username old-name)
  60
+      (when (valid-psw? password)
  61
+        (-> user (prepare) (store!)))
  62
+      (add! user))))
57 63
 
58 64
 (defn remove! [username]
59 65
   (db/update! :users dissoc username))
9  src/noir_blog/views/admin.clj
@@ -19,7 +19,7 @@
19 19
 ;; Partials
20 20
 
21 21
 (defpartial error-text [errors]
22  
-            [:p (string/join "<br/>" errors)])
  22
+            [:span (string/join "" errors)])
23 23
 
24 24
 (defpartial post-fields [{:keys [title body]}]
25 25
             (vali/on-error :title error-text)
@@ -30,6 +30,7 @@
30 30
 (defpartial user-fields [{:keys [username] :as usr}]
31 31
             (vali/on-error :username error-text)
32 32
             (text-field {:placeholder "Username"} :username username)
  33
+            (vali/on-error :password error-text)
33 34
             (password-field {:placeholder "Password"} :password))
34 35
 
35 36
 (defpartial post-item [{:keys [title] :as post}]
@@ -120,12 +121,12 @@
120 121
            [:ul.items
121 122
             (map user-item (users/all))]))
122 123
 
123  
-(defpage "/blog/admin/user/add" {}
  124
+(defpage "/blog/admin/user/add" {:as user}
124 125
          (common/admin-layout
125 126
            (form-to [:post "/blog/admin/user/add"]
126 127
                       [:ul.actions
127 128
                         [:li (link-to {:class "submit"} "/" "Add")]]
128  
-                    (user-fields {})
  129
+                    (user-fields user)
129 130
                     (submit-button {:class "submit"} "add user"))))
130 131
 
131 132
 (defpage [:post "/blog/admin/user/add"] {:keys [username password] :as neue-user}
@@ -143,7 +144,7 @@
143 144
                       (user-fields user)))))
144 145
 
145 146
 (defpage [:post "/blog/admin/user/edit/:old-name"] {:keys [old-name] :as user}
146  
-         (if (users/edit! old-name user)
  147
+         (if (users/edit! user)
147 148
            (resp/redirect "/blog/admin/users")
148 149
            (render "/blog/admin/user/edit/:old-name" user)))
149 150
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.