git.fiddlerwoaroof.com
Browse code

Reorg + refine

fiddlerwoaroof authored on 01/02/2016 05:33:59
Showing 10 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,15 @@
1
+(defpackage :inangulis.blog
2
+  (:use :cl :anaphora :alexandria :inangulis.web))
3
+
4
+(in-package :inangulis.blog)
5
+
6
+(defclass blog-post ()
7
+  ((id :col-type serial)
8
+   (title :col-type text)
9
+   (author :col-type int)
10
+   (name :initarg :name :col-type text)
11
+   (moderator :initarg :moderator :initform nil :col-type bool))
12
+  (:metaclass dao-class)
13
+  (:keys id))
14
+
15
+(define-controller)
... ...
@@ -22,8 +22,11 @@
22 22
                 #:ningle)
23 23
   :serial t
24 24
   :components ((:file "package")
25
-               (:file "tables")
26 25
                (:file "web")
26
+               (:file "utils")
27
+               (:file "user")
28
+               (:file "links")
29
+               (:file "tables")
27 30
                (:file "inangulis")))
28 31
 
29 32
 ;; vim: set ft=lisp:
... ...
@@ -1,68 +1,14 @@
1 1
 (declaim (optimize (debug 3) (speed 0 ) (safety 3)) )
2
-
3
-;;;; inangulis.lisp
4
-
5 2
 (in-package #:inangulis)
6
-(defparameter *submissions* nil)
3
+
7 4
 (defparameter *by-distinct* (make-hash-table :test #'equalp))
8 5
 (defparameter *users* (make-hash-table :test #'equalp))
9
-(defparameter *persist* t)
10
-
11
-(defmacro cdr-assoc (&whole lis item alist &key key test test-not)
12
-  `(cdr (assoc ,@(cdr lis))))
13
-
14
-(defun str-assoc (param params &key (test #'equal) key)
15
-  (cdr-assoc param params :test test :key key))
16
-
17
-;;; "inangulis" goes here. Hacks and glory await!
18
-
19
-(defun current-date-string ()
20
-  "Returns current date as a string."
21
-  (local-time:format-timestring nil (local-time:now) 
22
-                                :format local-time:+rfc-1123-format+))
23
-
24
-(defclass user ()
25
-  ((uid :initarg :uid)
26
-   (email :initarg :email)
27
-   (name :initarg :name)
28
-   (moderator :initarg :moderator :initform nil)))
29
-
30
-(defun user-alist (user)
31
-  (with-slots ((email inangulis.tables::email)
32
-               (name inangulis.tables::name)
33
-               (moderator inangulis.tables::moderator)) user
34
-    `(("name" . ,name)
35
-      ("email" . ,email)
36
-      ("moderator" . ,moderator))))
37 6
 
38 7
 (defmethod print-object ((obj user) s)
39 8
   (print-unreadable-object (obj s :type t :identity t)
40 9
     (with-slots (uid email name moderator) obj
41 10
       (format s "U: ~s E: ~s N: ~s M: ~s" uid email name moderator))))
42 11
 
43
-(defun make-submission (headline url &key (approved ""))
44
-  (alet 'inangulis.tables:submission
45
-    (make-instance it :headline headline :url url :approved approved :date (current-date-string))))
46
-
47
-(defun submission-alist (submission)
48
-  `(("headline". ,(s-headline submission))
49
-    ("url" . ,(s-url submission))
50
-    ("date" . ,(s-url submission))
51
-    ("approved" . ,(s-approved submission))))
52
-
53
-(defun get-by-key (headline url)
54
-  (car (postmodern:select-dao 'inangulis.tables::submission (:and (:= 'headline headline) (:= 'url url)))))
55
-
56
-
57
-(defun alist-submission (alist &key nil-if-exists)
58
-  (let* ((headline (cdr-assoc :headline alist :test #'string-equal))
59
-         (url (cdr-assoc :url alist :test #'string-equal))
60
-         (approved (or (cdr-assoc :approved alist :test #'string-equal) ""))   
61
-         (result (make-submission headline url :approved approved)))
62
-    (with-slots ((headline inangulis.tables:headline) (url inangulis.tables:url)) result
63
-      (aif (get-by-key headline url)
64
-        (unless nil-if-exists it)
65
-        result))))
66 12
 
67 13
 
68 14
 (defun get-moderated (feeds)
... ...
@@ -95,22 +41,8 @@
95 41
      (declare (ignorable ,@args))
96 42
      ,@body))
97 43
 
98
-(defmacro with-db (&body b)
99
-  `(postmodern:with-connection (ubiquitous:value 'db)
100
-     ,@b))
101
-
102
-(defmacro with-submissions (&body b)
103
-  `(with-db
104
-     (let ((*submissions* (postmodern:select-dao 'inangulis.tables::submission t "date desc")))
105
-       ,@b)))
106
-
107
-
108 44
 
109 45
 
110
-(defun submit (params)
111
-  (awhen (alist-submission params :nil-if-exists t)
112
-    (postmodern:insert-dao it)
113
-    (push it *submissions*)))
114 46
 
115 47
 (defun get-feed-guid (item)
116 48
   (with-slots (alimenta:title alimenta:link) item
... ...
@@ -125,28 +57,6 @@
125 57
     (pomo:with-transaction ()
126 58
       (call-next-method))))
127 59
 
128
-;; View Controllers
129
-(define-controller murmur (params)
130
-  (sleep 0.01)
131
-  (submit params))
132
-
133
-(define-view murmur (model)
134
-  '(302 (:location "/") ("Done"))) 
135
-  
136
-(define-controller curate (params)
137
-  (let ((*submissions* (postmodern:select-dao 'inangulis.tables::submission t "date desc")))
138
-    (cl-oid-connect.utils:require-login
139
-      (alet (alist-submission params)
140
-        (let ((approval (string-downcase (str-assoc "approved" params :test #'equalp))))
141
-          (setf (s-approved it)
142
-                (if (equal approval "+") "approved"
143
-                  (if (equal approval "-") "rejected")))
144
-          (when *persist*
145
-            (postmodern:update-dao it)))))))
146
-
147
-(define-view curate (params)
148
-  '(302 (:location "/") ("Done")))
149
-
150 60
 (define-view login-page (model)
151 61
   `(200
152 62
     ()
... ...
@@ -171,7 +81,7 @@
171 81
   (let ((feed (alimenta::make-feed :title "In Angulis" :link "http://in-angulis.com/feed"
172 82
                                    :description "Locus in quo sunt illi qui murmurant in angulis")))
173 83
     (prog1 feed
174
-      (pomo:do-select-dao (('inangulis.tables::submission submission)
84
+      (pomo:do-select-dao (('inangulis.links:submission submission)
175 85
                            (:raw (if moderated (pomo:sql (:= 'approved "approved")) "'t'"))
176 86
                            (:desc 'date))
177 87
         (alimenta::add-item-to-feed feed
... ...
@@ -186,21 +96,6 @@
186 96
 (define-view get-feed (feed)
187 97
   `(200 (:content-type "application/rss+xml") (,(plump:serialize (alimenta:generate-xml feed) nil))))
188 98
 
189
-(mustache-view root (user . links) #p"static/index.mustache.html"
190
-  :links (mapcar #'submission-alist links)
191
-  :user (when user (user-alist user)))
192
-
193
-(define-controller root (params)
194
-  (setf *tmp* ningle.context:*request*)
195
-  (with-submissions
196
-    (ningle:with-context-variables (session)
197
-      (handler-case
198
-        (cl-oid-connect.utils:ensure-logged-in
199
-          (cl-oid-connect.utils:redirect-if-necessary session
200
-            (cons (gethash :app-user session)
201
-                  *submissions*)))
202
-        (cl-oid-connect.utils:user-not-logged-in (c) (cons nil *submissions*))))))
203
-
204 99
 (defmethod controller :around ((name (eql 'headlines)) params &key (moderated t moderated-p))
205 100
   (unless moderated-p
206 101
     (awhen (str-assoc "moderated" params :test #'string-equal)
... ...
@@ -214,8 +109,8 @@
214 109
   (let (result)
215 110
     (ningle.context:with-context-variables (session)
216 111
       (let* ((app-user (gethash :app-user session))
217
-             (moderator-p (and app-user (slot-value app-user 'inangulis.tables::moderator))))
218
-        (pomo:do-select-dao (('inangulis.tables::submission submission)
112
+             (moderator-p (and app-user (slot-value app-user 'inangulis.user::moderator))))
113
+        (pomo:do-select-dao (('inangulis.links:submission submission)
219 114
                              (:raw (cond 
220 115
                                      ((and moderator-p (not moderated-p)) "'t'")
221 116
                                      (moderated (pomo:sql (:= 'approved "approved")))
... ...
@@ -246,12 +141,12 @@
246 141
   (declare (ignore args))
247 142
   (let ((id (cdr (assoc :id userinfo))))
248 143
     (with-db
249
-      (aif (car (postmodern:select-dao 'inangulis.tables::user (:= 'uid id)))
144
+      (aif (car (postmodern:select-dao 'inangulis.user:user (:= 'uid id)))
250 145
         it
251
-        (alet (make-instance 'inangulis.tables::user)
252
-          (with-slots ((uid inangulis.tables::uid)
253
-                       (name inangulis.tables::name)
254
-                       (email inangulis.tables::email)) it
146
+        (alet (make-instance 'inangulis.user:user)
147
+          (with-slots ((uid inangulis.user::uid)
148
+                       (name inangulis.user::name)
149
+                       (email inangulis.user::email)) it
255 150
             (prog1 it
256 151
               (setf uid id
257 152
                     name (cdr (assoc :name userinfo))
258 153
new file mode 100644
... ...
@@ -0,0 +1,114 @@
1
+(in-package :inangulis.links)
2
+
3
+(defparameter *submissions* nil)
4
+
5
+(defclass submission ()
6
+  ((headline  :initarg :headline  :col-type text :initform ""  :accessor s-headline)
7
+   (url       :initarg :url       :col-type text :initform ""  :accessor s-url)
8
+   (date      :initarg :date      :col-type timestamptz :initform "" :accessor s-date)
9
+   (submitter :initarg :submitter :col-type (or s-sql:db-null integer) :accessor s-submitter)
10
+   (curator   :initarg :curator   :col-type (or s-sql:db-null integer) :accessor s-curator)
11
+   (approved  :initarg :approved  :col-type text :initform "" :accessor s-approved))
12
+  (:metaclass pomo:dao-class)
13
+  (:keys headline url))
14
+
15
+(pomo:deftable submission
16
+  (pomo:!dao-def) 
17
+  (pomo:!foreign "public.user" "submitter" "id" :on-delete :set-null :on-update :cascade)
18
+  (pomo:!foreign "public.user" "curator" "id" :on-delete :set-null :on-update :cascade))
19
+
20
+#|
21
+ |(pomo:with-connection (ubiquitous:value :db)
22
+ |   (pomo:create-table 'submission))
23
+ |#
24
+
25
+(defun make-submission (headline url &key (approved ""))
26
+  (alet 'submission
27
+    (make-instance it :headline headline :url url :approved approved :date (current-date-string))))
28
+
29
+(defun get-by-key (headline url)
30
+  (car (postmodern:select-dao 'submission (:and (:= 'headline headline) (:= 'url url)))))
31
+
32
+(defun alist-submission (alist &key nil-if-exists)
33
+  (let* ((headline (cdr-assoc :headline alist :test #'string-equal))
34
+         (url (cdr-assoc :url alist :test #'string-equal))
35
+         (approved (or (cdr-assoc :approved alist :test #'string-equal) ""))   
36
+         (result (make-submission headline url :approved approved)))
37
+    (with-slots ((headline inangulis.links:headline) (url inangulis.links:url)) result
38
+      (aif (get-by-key headline url)
39
+        (unless nil-if-exists it)
40
+        result))))
41
+
42
+(defmethod s-date ((object submission))
43
+  (with-slots (date) object
44
+    (local-time:format-timestring nil date :format local-time:+rfc-1123-format+)))
45
+
46
+(defmethod print-object ((obj submission) s)
47
+  (print-unreadable-object (obj s :type t :identity t)
48
+    (with-slots (headline url approved) obj
49
+      (format s "H: ~s U: ~s A: ~s" headline url approved))))
50
+
51
+(defmacro with-submissions (&body b)
52
+  `(with-db
53
+     (let ((*submissions* (postmodern:select-dao 'submission t "date desc")))
54
+       ,@b)))
55
+
56
+(defun submission-alist (submission)
57
+  (slots-to-pairs submission
58
+                  (headline
59
+                    url
60
+                    date
61
+                    approved)))
62
+
63
+
64
+(mustache-view root (user . links) #p"static/index.mustache.html"
65
+  :links (mapcar #'submission-alist links)
66
+  :user (when user (inangulis.user:user-alist user)))
67
+
68
+(define-controller root (params)
69
+  (with-submissions
70
+    (ningle:with-context-variables (session)
71
+      (handler-case
72
+        (cl-oid-connect.utils:ensure-logged-in
73
+          (cl-oid-connect.utils:redirect-if-necessary session
74
+            (cons (gethash :app-user session)
75
+                  *submissions*)))
76
+        (cl-oid-connect.utils:user-not-logged-in (c) (cons nil *submissions*))))))
77
+
78
+(defun submit (params)
79
+  (ningle:with-context-variables (session)
80
+    (let* ((app-user (gethash :app-user session)))
81
+      (awhen (alist-submission params :nil-if-exists t)
82
+        (when app-user
83
+          (setf (s-submitter it)
84
+                (slot-value app-user 'inangulis.user:id)))
85
+        (postmodern:insert-dao it)
86
+        (push it *submissions*))))) 
87
+
88
+;; View Controllers
89
+(define-controller murmur (params)
90
+  (sleep 0.01)
91
+  (submit params))
92
+
93
+(define-view murmur (model)
94
+  '(302 (:location "/") ("Done"))) 
95
+
96
+(defmethod controller :around ((name (eql 'curate)) params &key)
97
+  (cl-oid-connect.utils:require-login
98
+    (call-next-method)))
99
+
100
+(define-controller curate (params)
101
+    (let* ((session (ningle:context :session))
102
+           (app-user (gethash :app-user session))
103
+           (approval (string-downcase (str-assoc "approved" params :test #'equalp)))
104
+           (submission (alist-submission params)))
105
+      (setf1 ((s-curator submission) (slot-value app-user 'inangulis.user:id))
106
+             ((s-approved submission) (ccase (elt approval 0)
107
+                                     (#\+ "approved")
108
+                                     (#\- "rejected"))))
109
+      (postmodern:update-dao submission)))
110
+
111
+(define-view curate (params)
112
+  '(302 (:location "/") ("Done")))
113
+
114
+
... ...
@@ -1,8 +1,11 @@
1 1
 ;;;; package.lisp
2 2
 
3 3
 (defpackage #:inangulis.tables
4
-  (:use #:cl #:anaphora #:postmodern)
5
-  (:export #:submission #:headline #:url #:date #:approved))
4
+  (:use #:cl #:anaphora #:postmodern))
5
+
6
+(defpackage #:inangulis.utils
7
+  (:use #:cl #:anaphora)
8
+  (:export #:with-db #:cdr-assoc #:str-assoc #:current-date-string))
6 9
 
7 10
 (defpackage #:inangulis.web
8 11
   (:use #:cl)
... ...
@@ -10,9 +13,20 @@
10 13
            #:controller #:view #:run-route #:mustache-view #:render-mustache
11 14
            #:setf1))
12 15
 
16
+(defpackage #:inangulis.user
17
+  (:use #:cl #:anaphora #:alexandria #:inangulis.web)
18
+  (:export #:user #:user-alist #:id #:uid #:name #:email #:moderator))
19
+
20
+(defpackage #:inangulis.links
21
+  (:use #:cl #:anaphora #:alexandria #:inangulis.web #:inangulis.utils #:fwoar.lisputils)
22
+  (:export #:root #:murmur #:curate *submissions* #:submission #:s-headline #:s-url
23
+           #:s-date #:s-submitter #:s-curator #:s-approved #:headline #:url #:date #:submitter
24
+           #:curator #:approved))
25
+
13 26
 (defpackage #:inangulis
14
-  (:use #:cl #:anaphora #:inangulis.tables #:inangulis.web))
27
+  (:use #:cl #:anaphora #:inangulis.tables #:inangulis.web #:inangulis.links
28
+        #:inangulis.user #:inangulis.links #:inangulis.utils))
15 29
 
16 30
 (defpackage #:inangulis-user
17
-  (:use #:cl #:inangulis.tables #:inangulis.web #:inangulis))
31
+  (:use #:cl #:inangulis.utils #:inangulis.user #:inangulis.links #:inangulis.tables #:inangulis.web #:inangulis))
18 32
 
... ...
@@ -1,5 +1,6 @@
1 1
 /*@import(url(/static/css/baseline.css));*/
2 2
 /*@import url("/css/formalize.css");*/
3
+/*// TODO: embed newer version*/
3 4
 @import url(https://fonts.googleapis.com/css?family=Alegreya+Sans|MedievalSharp&subset=latin,latin-ext);
4 5
 
5 6
 input {
... ...
@@ -34,15 +35,22 @@ h1 {
34 35
   letter-spacing: 6px;
35 36
   padding: 0.5em;
36 37
   margin: 0px;
37
-  margin-top: 0.5em;
38
-  margin-bottom: 1em;
38
+  padding-top: 2.75em;
39
+  /*margin-left: 1em;*/
40
+  padding-bottom: 1em;
41
+  margin-bottom: 0.5em;
39 42
   font-size: 3em;
40 43
   background: white;
44
+  font-weight: normal;
41 45
   width: 100%;
42 46
   z-index: 100;
43 47
   text-align: center;
44 48
   font-variant: small-caps;
45
-  font-family: 'MedievalSharp'
49
+  font-family: 'MedievalSharp';
50
+
51
+  color: #000;
52
+  background: #eee;
53
+
46 54
 }
47 55
 
48 56
 main {
... ...
@@ -261,6 +269,6 @@ main > h2 {
261 269
   position:relative;
262 270
   left: -6.25vw;
263 271
   width: 87.5vw;
264
-  margin-top: 1.5em;
272
+  margin-top: 4.5em;
265 273
   letter-spacing: 1px;
266 274
 }
... ...
@@ -33,7 +33,7 @@
33 33
     <form action="/murmuro" id="submission" name="submission" method="POST">
34 34
       <input type="text" name="headline" placeholder="Headline" v-model="newLink.headline">
35 35
       <input type="text" name="url" placeholder="URL" v-model="newLink.url">
36
-      <input type="submit" value="Go" title='Murmuro'>
36
+      <input type="submit" value="Submit" title='Murmuro'>
37 37
     </form>
38 38
 
39 39
     {{#user}}
... ...
@@ -3,42 +3,4 @@
3 3
 (eval-when (:compile-toplevel :load-toplevel :execute)
4 4
   (local-time:set-local-time-cl-postgres-readers))
5 5
 
6
-(defclass user ()
7
-  ((id :col-type serial)
8
-   (uid :initarg :uid :col-type text)
9
-   (email :initarg :email :col-type text)
10
-   (name :initarg :name :col-type text)
11
-   (moderator :initarg :moderator :initform nil :col-type bool))
12
-  (:metaclass dao-class)
13
-  (:keys id))
14 6
 
15
-(deftable user
16
-  (!dao-def))
17
-
18
-(defclass submission ()
19
-  ((headline :initarg :headline :col-type text :initform ""  :accessor inangulis::s-headline)
20
-   (url      :initarg :url      :col-type text :initform ""  :accessor inangulis::s-url)
21
-   (date     :initarg :date     :col-type timestamptz :initform "")
22
-   (approved :initarg :approved :col-type text :initform "" :accessor inangulis::s-approved))
23
-  (:metaclass dao-class)
24
-  (:keys headline url))
25
-
26
-(deftable submission
27
-  (!dao-def))
28
-
29
-;; (ubiquitous:restore :inangulis)
30
-
31
-;; (with-connection (ubiquitous:value 'db)
32
-;;   (create-table 'user))
33
-
34
-;; (with-connection (ubiquitous:value 'db)
35
-;;   (create-table 'submission))
36
-
37
-(defmethod inangulis::s-date ((object submission))
38
-  (with-slots (date) object
39
-    (local-time:format-timestring nil date :format local-time:+rfc-1123-format+)))
40
-
41
-(defmethod print-object ((obj submission) s)
42
-  (print-unreadable-object (obj s :type t :identity t)
43
-    (with-slots (headline url approved) obj
44
-      (format s "H: ~s U: ~s A: ~s" headline url approved))))
45 7
new file mode 100644
... ...
@@ -0,0 +1,18 @@
1
+(in-package :inangulis.user)
2
+
3
+(defclass user () 
4
+  ((id :col-type serial)
5
+   (uid :initarg :uid :col-type text)
6
+   (email :initarg :email :col-type text)
7
+   (name :initarg :name :col-type text)
8
+   (moderator :initarg :moderator :initform nil :col-type bool))
9
+  (:metaclass pomo:dao-class)
10
+  (:keys id))
11
+
12
+(pomo:deftable user
13
+  (pomo:!dao-def))
14
+
15
+(defun user-alist (user)
16
+  (with-slots (email name moderator) user
17
+    (pairlis '("name" "email" "moderator")
18
+             `(,name  ,email  ,moderator))))
0 19
new file mode 100644
... ...
@@ -0,0 +1,18 @@
1
+(in-package #:inangulis.utils)
2
+
3
+(defmacro with-db (&body b)
4
+  `(postmodern:with-connection (ubiquitous:value :db)
5
+     ,@b))
6
+
7
+(defmacro cdr-assoc (&whole lis item alist &key key test test-not)
8
+  `(cdr (assoc ,@(cdr lis))))
9
+
10
+(defun str-assoc (param params &key (test #'equal) key)
11
+  (cdr-assoc param params :test test :key key))
12
+
13
+(defun current-date-string ()
14
+  "Returns current date as a string."
15
+  (local-time:format-timestring nil (local-time:now) 
16
+                                :format local-time:+rfc-1123-format+))
17
+
18
+