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-v2.lisp
c1c1a22e » evanmonroig 2008-04-16 initial import from first p... 1 (in-package :blog)
2
3 ;;;; blog-v2: define views
4
5 ;;;; We'll override some of the fields in the scaffolding. For each
6 ;;;; field, we can specify some arguments to specify how to present,
7 ;;;; obtain, write or parse the data. Note also that currently, if I
8 ;;;; override a field then I lose the associated scaffolding
9 ;;;; (e.g. that the field is required).
10
11 ;;; added in src/views.lisp
12 (defview post-form-view (:type form :inherit-from '(:scaffold post))
13 (time :hidep t)
14 ;; POST-AUTHOR-ID and ALL-USERS will be defined below
15 (author :reader #'post-author-id
16 :present-as (dropdown :choices #'all-users
17 :label-key #'user-name)
18 :parse-as (object-id :class-name 'user)
19 :requiredp t)
20 (short-text :present-as textarea
21 :requiredp t)
22 (text :present-as (textarea :cols 30)
23 :requiredp t))
24
25 ;;;; Here we don't really need to manually edit the time as it should
26 ;;;; be done automatically by the backend, so we hide the TIME field.
27 ;;;;
28 ;;;; We had better present the SHORT-TEXT and TEXT by using a TEXTAREA
29 ;;;; presentation so that they're easier to edit. It is actually a
30 ;;;; class named TEXTAREA-PRESENTATION located in the weblocks source
31 ;;;; in the file
32 ;;;; "cl-weblocks/src/views/types/presentations/textarea.lisp". Now
33 ;;;; is probably a good time to have a look. You will see that it has
34 ;;;; among others a COLS slot, which the DEFVIEW macro allows us to
35 ;;;; set by using the syntax above.
36 ;;;;
37 ;;;; Now for the AUTHOR. As is done in the weblocks-demo application,
38 ;;;; we present it as an HTML dropdown list. The idea is to show the
39 ;;;; user a list of the names of users, and each name has for value
40 ;;;; the ID of the user. We then map the selected user ID to the
41 ;;;; actual user object.
42 ;;;;
43 ;;;; This is done by using a DROPDOWN presentation (in a file next to
44 ;;;; the TEXTAREA one). To :CHOICES we assign a function that returns
45 ;;;; a list of objects to choose from, and :LABEL-KEY the function
46 ;;;; used to convert each object to a string. That's it for what
47 ;;;; we'll see in the browser.
48 ;;;;
49 ;;;; Under the cover we need to pass the USER objects as their ID's,
50 ;;;; and parse an ID into a USER object. The former is done by
51 ;;;; specifying a function as :READER (takes a POST as argument), and
52 ;;;; the latter by using a parser named OBJECT-ID. As for
53 ;;;; presentation, this is actually a class named OBJECT-ID-PARSER
54 ;;;; which you'll find in
55 ;;;; cl-weblocks/src/views/types/parsers/common.lisp at the end of the
56 ;;;; file. As before CLASS-NAME is one slot of OBJECT-ID-PARSER.
57 ;;;;
58 ;;;; Finally, before we make this work we need to define
59 ;;;; POST-AUTHOR-ID and ALL-USERS,
60
61 ;;; added in src/models.lisp:
62 (in-package :blog)
63
64 (defgeneric post-author-id (post)
65 (:method ((post post))
66 (when (post-author post)
67 (object-id (post-author post)))))
68
69 (defun all-users (&rest args)
70 "return all objects of class USER. ARGS is an added argument that
71 is ignored (needed for use in dropdown lists in views)."
72 (declare (ignore args))
73 (find-persistent-objects (class-store 'user) 'user))
74
75 ;;;; Now we can add and delete users and posts using the gridedit
76 ;;;; widgets. That should be enough of an introduction to views and
77 ;;;; presentations.
78
79
80 ;;;; ChangeLog
81 blog-v2
82
83 * src/models.lisp (post-author-id, all-users): functions used by
84 the views
85
86 * src/views.lisp (post-form-view): override some fields - textarea
87 for the texts, and dropdown list for the author
88
89 blog-v1:
90
723968c3 » evanmonroig 2008-06-07 adapted to new weblocks (la... 91 * src/views.lisp (user-table-view, user-data-view, user-form-view)
92 (post-table-view, post-data-view, post-form-view): scaffolded views
c1c1a22e » evanmonroig 2008-04-16 initial import from first p... 93 for the gridedit interface
94
95 * src/init-session.lisp (init-user-session): call MAKE-ADMIN-PAGE
96
97 * src/layout.lisp (make-users-gridedit, make-posts-gridedit)
98 (make-admin-page): add simple gridedit interface for the two
99 models
100
101 * src/models.lisp (user, post): USER and POST models