scrumli/scrumli.lisp

220 lines
8.2 KiB
Common Lisp
Raw Normal View History

2013-06-30 22:38:05 +02:00
(in-package #:scrumli)
(defvar *scrumli-host* "http://localhost:8080"
"The host currently running Scrumli. Used by Mozilla Persona.")
2013-07-05 23:22:25 +02:00
(defvar *scrumli-bootstrap-css-location*
2013-06-30 22:38:05 +02:00
"http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css"
"The location of the twitter bootstrap CSS file.")
2013-07-05 23:22:25 +02:00
(defvar *scrumli-bootstrap-js-location*
2013-06-30 22:38:05 +02:00
"http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"
"The location of the twitter bootstrap JS file.")
2013-07-05 23:22:25 +02:00
(defvar *scrumli-font-awesome-css-location*
2013-06-30 22:38:05 +02:00
"http://netdna.bootstrapcdn.com/font-awesome/3.1.1/css/font-awesome.min.css"
"The location of the font awesome CSS file.")
2013-07-05 23:22:25 +02:00
(defvar *scrumli-jquery-js-location*
2013-06-30 22:38:05 +02:00
"http://code.jquery.com/jquery-2.0.0.min.js"
"The location of the jQuery JS file.")
2013-07-05 23:22:25 +02:00
(defvar *scrumli-react-js-location*
2013-06-30 22:38:05 +02:00
"http://cdnjs.cloudflare.com/ajax/libs/react/0.3.2/react.min.js"
"The location of the React JS file.")
2013-07-05 23:22:25 +02:00
(defvar *scrumli-jsxtransformer-js-location*
2013-06-30 22:38:05 +02:00
"http://cdnjs.cloudflare.com/ajax/libs/react/0.3.2/JSXTransformer.js"
"The location of the JSX Transformer JS file.")
(defun logged-in-p ()
(hunchentoot:session-value :username))
(defun page-title (title)
(<:title (concatenate 'string title " | scrumli")))
2013-07-05 23:22:25 +02:00
(defun css (&rest sheets)
(apply 'concatenate 'string
(mapcar (lambda (s)
(<:link :href s :rel "stylesheet" :type "text/css"))
sheets)))
(defun js (&rest scripts)
(apply 'concatenate 'string
(mapcar (lambda (s)
(<:script :type "text/javascript" :src s))
scripts)))
(defmacro navbar (&body body)
`(<:div :class "navbar navbar-static-top navbar-inverse"
(<:div :class "navbar-inner"
(<:div :class "container"
(<:a :class "brand" "scrumli")
,@body))))
2013-06-30 22:38:05 +02:00
(define-route main ("")
(if (logged-in-p)
(<:html
(<:head (page-title "Backlog")
2013-07-05 23:22:25 +02:00
(css *scrumli-bootstrap-css-location*
*scrumli-font-awesome-css-location*)
(js *scrumli-bootstrap-js-location*
*scrumli-jquery-js-location*
*scrumli-react-js-location*
*scrumli-jsxtransformer-js-location*))
2013-06-30 22:38:05 +02:00
(<:body
2013-07-05 23:22:25 +02:00
(navbar (<:div :class "pull-right"
(<:span :class "navbar-text"
(hunchentoot:session-value :username))
(<:ul :class "nav pull-right"
(<:li :class "divider-vertical")
(<:li (<:a :href (genurl 'logout-page)
"Logout"))
(<:li :class "divider-vertical"))))
2013-06-30 22:38:05 +02:00
(<:div :class "container"
(<:h1 "Backlog")
(<:div :id "content")
2013-07-05 23:22:25 +02:00
(<:script :type "text/jsx" :src (genurl 'main-js)))))
2013-06-30 22:38:05 +02:00
(redirect 'login-page)))
2013-07-05 23:22:25 +02:00
(defmacro serve-static (name relpath)
`(define-route ,name (,relpath :content-type "application/ecmascript")
(merge-pathnames ,relpath *static-directory*)))
2013-06-30 22:38:05 +02:00
2013-07-05 23:22:25 +02:00
(serve-static main-js "js/main.js")
(serve-static login-js "js/login.js")
2013-06-30 22:38:05 +02:00
(define-route stories-json ("stories" :content-type "text/json")
(if (logged-in-p)
(with-output-to-string (out)
(encode-json (get-all-stories) out))
403))
2013-07-05 23:22:25 +02:00
(defmacro with-post-parameters (parameters &body body)
`(let ,(mapcar (lambda (p)
(list (intern (string-upcase p))
`(hunchentoot:post-parameter ,p)))
parameters)
,@body))
(define-route stories-new ("stories/new" :method :post
:content-type "text/json")
2013-06-30 22:38:05 +02:00
(if (logged-in-p)
2013-07-05 23:22:25 +02:00
(with-post-parameters ("role" "necessity" "headline" "content")
(post-story role necessity headline content
2013-06-30 22:38:05 +02:00
(hunchentoot:session-value :username))
(with-output-to-string (out)
(encode-json '((status . "ok")) out)))
2013-06-30 22:38:05 +02:00
403))
(define-route tasks-new ("stories/tasks/new" :method :post)
2013-07-04 21:51:31 +02:00
(if (logged-in-p)
2013-07-05 23:22:25 +02:00
(with-post-parameters ("storyId" "description")
(post-task storyid description
(hunchentoot:session-value :username))
2013-07-04 21:51:31 +02:00
200)
403))
2013-06-30 22:38:05 +02:00
(define-route stories-state ("stories/state" :method :post)
(if (logged-in-p)
(let* ((id (hunchentoot:post-parameter "id"))
2013-07-04 23:55:43 +02:00
(current-state (story-get-state 'story id)))
(story-set-state 'story id (ecase (intern current-state :scrumli)
(todo "DOING")
(doing "DONE")
(done "TODO")))
2013-06-30 22:38:05 +02:00
200)
403))
2013-07-04 23:55:43 +02:00
(define-route task-state ("tasks/state" :method :post)
(if (logged-in-p)
(let* ((id (hunchentoot:post-parameter "id"))
(current-state (story-get-state 'task id)))
(story-set-state 'task id (ecase (intern current-state :scrumli)
(todo "DOING")
(doing "DONE")
(done "TODO"))))))
2013-06-30 22:38:05 +02:00
(define-route stories-priority ("stories/:dir" :method :post)
(if (logged-in-p)
(let* ((id (hunchentoot:post-parameter "id")))
2013-07-04 23:55:43 +02:00
(story-change-priority
'story id (intern (string-upcase dir) :keyword))
200)
403))
(define-route task-priority ("tasks/:dir" :method :post)
(if (logged-in-p)
(let* ((id (hunchentoot:post-parameter "id")))
(story-change-priority
'task id (intern (string-upcase dir) :keyword))
2013-06-30 22:38:05 +02:00
200)
403))
(define-route login-page ("login")
(if (not (logged-in-p))
(<:html :lang "en"
(<:head (<:meta :charset "utf-8")
(page-title "Login")
2013-07-05 23:22:25 +02:00
(css *scrumli-bootstrap-css-location*)
(js *scrumli-bootstrap-js-location*
"https://login.persona.org/include.js"
(genurl 'login-js)))
2013-06-30 22:38:05 +02:00
(<:body
2013-07-05 23:22:25 +02:00
(navbar (<:ul :class "nav pull-right"
(<:li :class "divider-vertical")
(<:li (<:a :href "javascript:login()"
"Login"))
(<:li :class "divider-vertical")))
(<:div :class "container"
(<:br)
(<:div :class "hero-unit"
(<:h1 "Scrumli")
(<:p "As a " (<:em "developer") " I "
(<:em "love") " to " (<:em "scrum")
"...")
(<:a :class "btn btn-primary btn-large"
:href "javascript:login()"
"Login")))
2013-06-30 22:38:05 +02:00
(<:form :id "login-form" :method "POST" :action ""
(<:input :id "assertion-field" :type "hidden"
:name "assertion" :value ""))))
2013-06-30 22:38:05 +02:00
(redirect 'main)))
(define-route logout-page ("logout")
(if (logged-in-p)
2013-07-05 23:22:25 +02:00
(setf (hunchentoot:session-value :username) nil))
2013-06-30 22:38:05 +02:00
(redirect 'login-page))
(defun json-to-string (obj)
(with-output-to-string (out)
(encode-json obj out)))
(defun verify-credentials (audience assertion)
(let ((response
(http-request "https://verifier.login.persona.org/verify"
:method :post :content-type "application/json"
:content (json-to-string
`(("assertion" . ,assertion)
("audience" . ,audience)))
:want-stream t)))
(decode-json response)))
(define-route login-page/post ("login" :method :post)
(let ((result (verify-credentials
*scrumli-host*
(hunchentoot:post-parameter "assertion"))))
(if (equal (cdr (assoc :status result)) "okay")
(progn
(hunchentoot:start-session)
(setf (hunchentoot:session-value :username)
(cdr (assoc :email result)))
(redirect 'main))
403)))
2013-07-05 23:22:25 +02:00
(define-route scrumli-story ("stories/:id")
2013-06-30 22:38:05 +02:00
(if (logged-in-p)
(with-output-to-string (out)
(encode-json (get-story id) out))
403))