public
Description: Simple blog application in common lisp using the framework Weblocks
Homepage: http://obakechan.net/lisp/blog-app/
Clone URL: git://github.com/evanmonroig/cl-blogapp.git
cl-blogapp / blog-record-v1.lisp
100644 202 lines (167 sloc) 6.236 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
;;;; blog-v1: adding models and a gridedit interface
 
;;;; We will work inside the blog/ directory, which now has a data/
;;;; directory (will store the posts and user data), pub/ (for
;;;; javascript scripts and css stylesheets), conf/ for configuration
;;;; of the store (by default a prevalence store that stores data in
;;;; xml files under data/), and src/ which will contain the lisp
;;;; source code of the blog.
;;;;
;;;; So let's start working by creating a post and a user class:
 
;;; src/models.lisp
(in-package :blog)
 
(defclass user ()
  ((id :documentation "this is automatically assigned by cl-prevalence
when we persist the post object")
   (name :accessor user-name
:initarg :name
:initform ""
:type string)))
 
(defclass post ()
  ((id)
   (short-text :accessor post-short-text
:initarg :short-text
:initform ""
:type string
:documentation "short text of the post, to be shown on
the main page of the blog")
   (text :accessor post-text
:initarg :text
:initform ""
:type string
:documentation "long text of the post, shown when the user
clicks on the link after the short text")
   (time :accessor post-time
:initarg :time
:initform (get-universal-time)
:documentation "time at which the post was created")
   (author :accessor post-author
:initarg :author
:initform nil
:type user)))
 
;;;; Now we'll follow the weblocks-demo and play a little with the
;;;; data by using the gridedit widgets.
;;;;
;;;; Gridedit needs three views for its data, corresponding to the
;;;; grid view, the data view to view the details of an item, and the
;;;; form view to edit an item.
;;;;
;;;; We put one gridedit widget for the USER and one for the POST
;;;; class on the same page for convenience, by putting them inside a
;;;; composite widget.
;;;;
;;;; Each of the gridedits has three associated views that we'll
;;;; define thereafter. A simple title is also added before the grid,
;;;; by using a lambda function and the :WIDGET-PREFIX-FN argument to
;;;; GRIDEDIT.
 
;;; src/layout.lisp
(in-package :blog)
 
(defun make-users-gridedit ()
  (make-instance 'gridedit
:name 'users-grid
:data-class 'user
:view 'user-table-view
:widget-prefix-fn (lambda (&rest args)
(declare (ignore args))
(with-html (:h1 "Users")))
:item-data-view 'user-data-view
:item-form-view 'user-form-view))
 
(defun make-posts-gridedit ()
  (make-instance 'gridedit
:name 'posts-grid
:data-class 'post
:widget-prefix-fn (lambda (&rest args)
(declare (ignore args))
(with-html (:h1 "Posts")))
:view 'post-table-view
:item-data-view 'post-data-view
:item-form-view 'post-form-view))
 
(defun make-admin-page ()
  (make-instance 'composite
:widgets
(list (make-users-gridedit)
(lambda ()
;; gridedit widgets were probably
;; not intended to be put 2 on the
;; same page, so I add an HR tag
;; between the two
(with-html (:div (:hr :style "margin: 2em;"))))
(make-posts-gridedit))))
 
 
;;;; Now we need to modify the file src/init-session.lisp to call the
;;;; function MAKE-ADMIN-PAGE.
 
;;; src/init-session.lisp
(in-package :blog)
 
;; Define our application
(defwebapp 'blog
    :description "A web application based on Weblocks")
 
;; Set public files directory to blog/pub
(setf *public-files-path* (compute-public-files-path :blog))
 
(defun init-user-session (comp)
  (setf (composite-widgets comp)
(make-admin-page)))
 
;;;; Finally, we need to define the views to be used in the GRIDEDIT.
;;;; The most straightforward way is to scaffold them based on the
;;;; USER and POST slots, see what the result is and modify the views
;;;; after.
 
;;; src/views.lisp
(in-package :blog)
 
(defview user-table-view (:type table :inherit-from '(:scaffold user)))
(defview user-data-view (:type data :inherit-from '(:scaffold user)))
(defview user-form-view (:type form :inherit-from '(:scaffold user)))
 
(defview post-table-view (:type table :inherit-from '(:scaffold post)))
(defview post-data-view (:type data :inherit-from '(:scaffold post)))
(defview post-form-view (:type form :inherit-from '(:scaffold post)))
 
 
;;;; For completeness, here is the modified blog.asd file.
 
;;; blog.asd
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
(defpackage #:blog-asd
  (:use :cl :asdf))
 
(in-package :blog-asd)
 
(defsystem blog
    :name "blog"
    :version "0.0.1"
    :maintainer ""
    :author ""
    :licence ""
    :description "blog"
    :depends-on (:weblocks)
    :components ((:file "blog")
(:module conf
:components ((:file "stores"))
:depends-on ("blog"))
(:module src
:components ((:file "init-session" :depends-on ("layout"))
(:file "layout" :depends-on ("models" "views"))
(:file "models")
(:file "views" :depends-on ("models")))
:depends-on ("blog" conf))))
 
 
;;;; Now compile the files or load them from asdf, and restart the
;;;; blog from the REPL.
 
(blog:stop-blog)
(blog:start-blog :debug t)
 
;;;; The two gridedit widgets show "no information available" but we
;;;; can add a user using the "Add" button.
;;;;
;;;; A few remarks:
;;;;
;;;; - For some reason unknown to me the title that I added in front
;;;; of each widget disappears.
;;;;
;;;; - The scaffold defines fields from all slots for which there is a
;;;; slot reader when in data/table view, and all fields for which
;;;; there is a slot writer when in form view. So the ID slots are
;;;; not shown but all other slots are shown.
;;;;
;;;; - The data are validated by using the :TYPE information in the
;;;; slots. So you can add a user, but no post as there is no way
;;;; to enter a user object directly. Let's change this by
;;;; modifying POST-FORM-VIEW.
 
 
;;;; ChangeLog
blog-v1:
 
* src/views.lisp (user-table-view, user-data-view, user-form-view)
(post-table-view, post-data-view, post-form-view): scaffolded views
for the gridedit interface
 
* src/init-session.lisp (init-user-session): call MAKE-ADMIN-PAGE
 
* src/layout.lisp (make-users-gridedit, make-posts-gridedit)
(make-admin-page): add simple gridedit interface for the two
models
 
* src/models.lisp (user, post): USER and POST models