1 ! Copyright (C) 2008 Slava Pestov.
\r
2 ! See http://factorcode.org/license.txt for BSD license.
\r
3 USING: accessors sequences kernel assocs combinators
\r
4 validators http hashtables namespaces fry continuations locals
\r
5 io arrays math boxes splitting urls
\r
8 http.server.responses
\r
11 furnace.conversations
\r
16 html.templates.chloe
\r
17 html.templates.chloe.syntax ;
\r
24 : render-validation-messages ( -- )
\r
27 <ul "errors" =class ul>
\r
28 [ <li> escape-string write </li> ] each
\r
32 CHLOE: validation-messages drop render-validation-messages ;
\r
34 TUPLE: action rest authorize init display validate submit ;
\r
36 : new-action ( class -- action )
\r
37 new [ ] >>init [ ] >>validate [ ] >>authorize ; inline
\r
39 : <action> ( -- action )
\r
42 : merge-forms ( form -- )
\r
44 [ [ errors>> ] bi@ push-all ]
\r
45 [ [ values>> ] bi@ swap update ]
\r
46 [ swap validation-failed>> >>validation-failed drop ]
\r
49 : set-nested-form ( form name -- )
\r
53 unclip [ set-nested-form ] nest-form
\r
56 : restore-validation-errors ( -- )
\r
58 nested-forms cget set-nested-form
\r
61 : handle-get ( action -- response )
\r
66 [ authorize>> call ]
\r
67 [ drop restore-validation-errors ]
\r
71 ] with-exit-continuation ;
\r
73 : param ( name -- value )
\r
76 : revalidate-url-key "__u" ;
\r
78 : revalidate-url ( -- url/f )
\r
79 revalidate-url-key param
\r
80 dup [ >url [ same-host? ] keep and ] when ;
\r
82 : validation-failed ( -- * )
\r
83 post-request? revalidate-url and [
\r
85 nested-forms-key param " " split harvest nested-forms cset
\r
91 : handle-post ( action -- response )
\r
95 [ authorize>> call ]
\r
99 ] with-exit-continuation ;
\r
101 : handle-rest ( path action -- assoc )
\r
102 rest>> dup [ [ "/" join ] dip associate ] [ 2drop f ] if ;
\r
104 : init-action ( path action -- )
\r
107 request get request-params assoc-union params set ;
\r
109 M: action call-responder* ( path action -- response )
\r
110 [ init-action ] keep
\r
111 request get method>> {
\r
112 { "GET" [ handle-get ] }
\r
113 { "HEAD" [ handle-get ] }
\r
114 { "POST" [ handle-post ] }
\r
117 M: action modify-form
\r
118 drop url get revalidate-url-key hidden-form-field ;
\r
120 : check-validation ( -- )
\r
121 validation-failed? [ validation-failed ] when ;
\r
123 : validate-params ( validators -- )
\r
124 params get swap validate-values check-validation ;
\r
126 : validate-integer-id ( -- )
\r
127 { { "id" [ v-number ] } } validate-params ;
\r
129 TUPLE: page-action < action template ;
\r
131 : <chloe-content> ( path -- response )
\r
132 resolve-template-path <chloe> "text/html" <content> ;
\r
134 : <page-action> ( -- page )
\r
135 page-action new-action
\r
136 dup '[ _ template>> <chloe-content> ] >>display ;
\r