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
15 html.templates.chloe
\r
16 html.templates.chloe.syntax ;
\r
23 : render-validation-messages ( -- )
\r
25 dup empty? [ drop ] [
\r
26 <ul "errors" =class ul>
\r
27 [ <li> escape-string write </li> ] each
\r
31 CHLOE: validation-messages drop render-validation-messages ;
\r
33 TUPLE: action rest authorize init display validate submit ;
\r
35 : new-action ( class -- action )
\r
36 new [ ] >>init [ ] >>validate [ ] >>authorize ; inline
\r
38 : <action> ( -- action )
\r
41 : set-nested-form ( form name -- )
\r
48 unclip [ set-nested-form ] nest-form
\r
52 : restore-validation-errors ( -- )
\r
54 nested-forms fget set-nested-form
\r
57 : handle-get ( action -- response )
\r
62 [ authorize>> call ]
\r
63 [ drop restore-validation-errors ]
\r
67 ] with-exit-continuation ;
\r
69 : param ( name -- value )
\r
72 : revalidate-url-key "__u" ;
\r
74 : revalidate-url ( -- url/f )
\r
75 revalidate-url-key param
\r
76 dup [ >url [ same-host? ] keep and ] when ;
\r
78 : validation-failed ( -- * )
\r
79 post-request? revalidate-url and
\r
81 nested-forms-key param " " split harvest nested-forms set
\r
82 { form nested-forms } <flash-redirect>
\r
86 : handle-post ( action -- response )
\r
90 [ authorize>> call ]
\r
94 ] with-exit-continuation ;
\r
96 : handle-rest ( path action -- assoc )
\r
97 rest>> dup [ [ "/" join ] dip associate ] [ 2drop f ] if ;
\r
99 : init-action ( path action -- )
\r
102 request get request-params assoc-union params set ;
\r
104 M: action call-responder* ( path action -- response )
\r
105 [ init-action ] keep
\r
106 request get method>> {
\r
107 { "GET" [ handle-get ] }
\r
108 { "HEAD" [ handle-get ] }
\r
109 { "POST" [ handle-post ] }
\r
112 M: action modify-form
\r
113 drop url get revalidate-url-key hidden-form-field ;
\r
115 : check-validation ( -- )
\r
116 validation-failed? [ validation-failed ] when ;
\r
118 : validate-params ( validators -- )
\r
119 params get swap validate-values check-validation ;
\r
121 : validate-integer-id ( -- )
\r
122 { { "id" [ v-number ] } } validate-params ;
\r
124 TUPLE: page-action < action template ;
\r
126 : <chloe-content> ( path -- response )
\r
127 resolve-template-path <chloe> "text/html" <content> ;
\r
129 : <page-action> ( -- page )
\r
130 page-action new-action
\r
131 dup '[ , template>> <chloe-content> ] >>display ;
\r