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

Merged
merged 2 commits into from Mar 20, 2012
View
1 .gitignore
@@ -1,3 +1,4 @@
+*~
pom.xml
*jar
/lib/
View
6 project.clj
@@ -1,8 +1,8 @@
(defproject noir-blog "0.1.0"
:description "A fully functional blog that serves an example of a noir project."
- :dependencies [[org.clojure/clojure "1.2.1"]
- [clj-time "0.3.0"]
- [noir "1.0.0"]
+ :dependencies [[org.clojure/clojure "1.3.0"]
+ [clj-time "0.3.7"]
+ [noir "1.2.2"]
[org.markdownj/markdownj "0.3.0-1.0.2b4"]
[simpledb "0.1.4"]]
:main noir-blog.server)
View
24 src/noir_blog/models/user.clj
@@ -23,14 +23,17 @@
(defn prepare [{password :password :as user}]
(assoc user :password (crypt/encrypt password)))
-(defn valid? [{:keys [username password]}]
+(defn valid-user? [username]
(vali/rule (not (db/get-in :users [username]))
[:username "That username is already taken"])
(vali/rule (vali/min-length? username 3)
[:username "Username must be at least 3 characters."])
+ (not (vali/errors? :username :password)))
+
+(defn valid-psw? [password]
(vali/rule (vali/min-length? password 5)
[:password "Password must be at least 5 characters."])
- (not (vali/errors? :username :password)))
+ (not (vali/errors? :password)))
;; Operations
@@ -46,14 +49,17 @@
(session/put! :username username))
(vali/set-error :username "Invalid username or password"))))
-(defn add! [{:keys [username password]}]
- (let [user (-> {:username username :password password}
- (prepare))]
- (when (valid? user)
- (store! user))))
+(defn add! [{:keys [username password] :as user}]
+ (when (valid-user? username)
+ (when (valid-psw? password)
+ (-> user (prepare) (store!)))))
-(defn edit! [user]
- (add! user))
+(defn edit! [{:keys [username old-name password]}]
+ (let [user {:username username :password password}]
+ (if (= username old-name)
+ (when (valid-psw? password)
+ (-> user (prepare) (store!)))
+ (add! user))))
(defn remove! [username]
(db/update! :users dissoc username))
View
9 src/noir_blog/views/admin.clj
@@ -19,7 +19,7 @@
;; Partials
(defpartial error-text [errors]
- [:p (string/join "<br/>" errors)])
+ [:span (string/join "" errors)])
(defpartial post-fields [{:keys [title body]}]
(vali/on-error :title error-text)
@@ -30,6 +30,7 @@
(defpartial user-fields [{:keys [username] :as usr}]
(vali/on-error :username error-text)
(text-field {:placeholder "Username"} :username username)
+ (vali/on-error :password error-text)
(password-field {:placeholder "Password"} :password))
(defpartial post-item [{:keys [title] :as post}]
@@ -120,12 +121,12 @@
[:ul.items
(map user-item (users/all))]))
-(defpage "/blog/admin/user/add" {}
+(defpage "/blog/admin/user/add" {:as user}
(common/admin-layout
(form-to [:post "/blog/admin/user/add"]
[:ul.actions
[:li (link-to {:class "submit"} "/" "Add")]]
- (user-fields {})
+ (user-fields user)
(submit-button {:class "submit"} "add user"))))
(defpage [:post "/blog/admin/user/add"] {:keys [username password] :as neue-user}
@@ -143,7 +144,7 @@
(user-fields user)))))
(defpage [:post "/blog/admin/user/edit/:old-name"] {:keys [old-name] :as user}
- (if (users/edit! old-name user)
+ (if (users/edit! user)
(resp/redirect "/blog/admin/users")
(render "/blog/admin/user/edit/:old-name" user)))