Skip to content

Instantly share code, notes, and snippets.

@zahardzhan
Created May 28, 2010 11:38
Show Gist options
  • Save zahardzhan/417061 to your computer and use it in GitHub Desktop.
Save zahardzhan/417061 to your computer and use it in GitHub Desktop.
(ns app
(:gen-class :extends javax.servlet.http.HttpServlet)
(:use (compojure.http routes servlet helpers)
compojure.html)
(:use com.freiheit.gae.datastore.datastore-access-dsl
com.freiheit.gae.datastore.datastore-query-dsl
com.freiheit.gae.datastore.datastore-types)
(:import (com.google.appengine.api.users User UserService UserServiceFactory)
(com.google.appengine.api.datastore Query)
java.util.Date))
(in-ns 'app)
(defmacro with-head [& body]
`(html
[:html
[:head
[:title "DV-Chan"]
[:meta {:http-equiv "content-type" :content "text/html; charset=utf-8"}]
[:meta {:http-equiv "reply-to" :content "[email protected]"}]
[:meta {:name "author" :content "zahardzhan"}]
[:meta {:name "copyright" :content "Copyright 2010, zahardzhan"}]
[:meta {:name "description" :content "Testing"}]
[:meta {:name "keywords" :content "Testing"}]
[:link {:rel "stylesheet" :href "/css/main.css" :type "text/css"}]
[:link {:rel "icon" :href "/img/favicon.png" :type "image/png"}]]
[:body ~@body]]))
(defentity commentary [:key] [:author] [:comment] [:date])
(comment
(with-app-engine (make-commentary :author "zahardzhan" :comment "Racked Snack!" :date (Date.)))
(with-app-engine (store-entities! [(make-commentary :author "zahardzhan" :comment "Trinity Dub." :date (Date.))]))
(with-app-engine (select (where commentary [])))
)
(defn render-commentary [{:keys [author comment]}]
(html [:table {:border 0 :cellpadding 0 :cellspacing 0 :width "100%"}
[:tr {:valign :top}
[:td {:width "15%" :align :right}
(cond author (html [:span.author author] \:)
:else (html [:span.anonymous "Неизвестный" ] \:))]
[:td {:width "1%"} [:br]]
[:td {:width "51%" :align :left} [:p.comment comment]]
[:td {:width "33%"} [:br]]]]))
(defn index []
(with-head
(let [user-service (UserServiceFactory/getUserService)
user (.getCurrentUser user-service)
commentaries (select (where commentary []))]
(html
[:div.main
[:div.top
[:div.topmenu
[:div.leftpart
[:ul (for [[link menu-item item-helper]
[["/" "Все" nil]
["/" "Лучшие" nil]
["/" "Фильмы" nil]
["/" "Анимэ" nil]
["/" "Музыка" (html [:small '▼])]]]
(html [:li (link-to link menu-item) (when item-helper
(html " " item-helper))]))]]
[:span.rightpart
(cond user (html [:b (.getNickname user)] " | "
(link-to "/" "Сообщения") \space [:span.mailcount 3] " | "
(link-to (.createLogoutURL user-service "/") "Выйти"))
:else (html (link-to (.createLoginURL user-service "/") "Войти")))]]]
(if (empty? commentaries)
[:p "The guestbook has no messages."]
(for [commentary commentaries] (render-commentary commentary)))
(form-to [:post "/sign"]
[:div (text-area "commentary" "")]
[:div (submit-button "Post Greeting")])
[:div.footer2
[:div.legal
(link-to "/" "Домой")]]]))))
(defn sign-guestbook [params]
(store-entities!
[(make-commentary :author (let [user (.getCurrentUser (UserServiceFactory/getUserService))]
(if user (.getNickname user)))
:comment (params :commentary)
:date (Date.))])
(redirect-to "/"))
(defroutes app
(GET "/favicon.ico" "/img/favicon.png")
(GET "/" (index))
(POST "/sign" (sign-guestbook params)))
;; ONLINE
(defservice app)
;; OFFLINE
;; remove "appengine-local-runtime.jar" and "appengine-api-stubs.jar"
;; (use 'appengine-local)
;; (appengine-local/defgaeserver app-server app)
;; (appengine-local/start-gae-server app-server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment