2009-03-15 19:19:29 -04:00
|
|
|
! Copyright (C) 2008, 2009 Slava Pestov.
|
2008-03-03 03:19:36 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-06-02 16:00:03 -04:00
|
|
|
USING: accessors sequences kernel assocs combinators
|
2008-05-26 01:47:27 -04:00
|
|
|
validators http hashtables namespaces fry continuations locals
|
2009-03-16 21:11:36 -04:00
|
|
|
io arrays math boxes splitting urls
|
2008-06-02 16:00:03 -04:00
|
|
|
xml.entities
|
|
|
|
http.server
|
|
|
|
http.server.responses
|
2008-11-24 21:26:11 -05:00
|
|
|
furnace.utilities
|
2008-07-10 00:41:45 -04:00
|
|
|
furnace.redirection
|
|
|
|
furnace.conversations
|
2009-04-06 23:05:17 -04:00
|
|
|
furnace.chloe-tags
|
2008-06-15 03:38:12 -04:00
|
|
|
html.forms
|
2008-06-02 16:00:03 -04:00
|
|
|
html.components
|
|
|
|
html.templates.chloe
|
2008-09-19 17:36:31 -04:00
|
|
|
html.templates.chloe.syntax
|
|
|
|
html.templates.chloe.compiler ;
|
2008-06-01 18:22:39 -04:00
|
|
|
IN: furnace.actions
|
2008-03-03 03:19:36 -05:00
|
|
|
|
2008-06-05 01:18:36 -04:00
|
|
|
SYMBOL: rest
|
2008-05-27 01:01:57 -04:00
|
|
|
|
2008-11-13 22:49:37 -05:00
|
|
|
TUPLE: action rest init authorize display validate submit ;
|
2008-03-03 03:19:36 -05:00
|
|
|
|
2008-05-26 01:47:27 -04:00
|
|
|
: new-action ( class -- action )
|
2008-06-13 01:47:47 -04:00
|
|
|
new [ ] >>init [ ] >>validate [ ] >>authorize ; inline
|
2008-03-03 03:19:36 -05:00
|
|
|
|
2008-05-26 01:47:27 -04:00
|
|
|
: <action> ( -- action )
|
|
|
|
action new-action ;
|
2008-03-03 05:40:29 -05:00
|
|
|
|
2008-07-10 00:41:45 -04:00
|
|
|
: merge-forms ( form -- )
|
2010-02-03 09:25:53 -05:00
|
|
|
[ form get ] dip
|
|
|
|
[ [ errors>> ] bi@ append! drop ]
|
|
|
|
[ [ values>> ] bi@ assoc-union! drop ]
|
|
|
|
[ validation-failed>> >>validation-failed drop ]
|
2008-07-10 00:41:45 -04:00
|
|
|
2tri ;
|
|
|
|
|
2008-06-15 03:38:12 -04:00
|
|
|
: set-nested-form ( form name -- )
|
2008-09-06 20:13:59 -04:00
|
|
|
[
|
|
|
|
merge-forms
|
2008-06-15 03:38:12 -04:00
|
|
|
] [
|
2008-07-10 00:41:45 -04:00
|
|
|
unclip [ set-nested-form ] nest-form
|
2008-09-06 20:13:59 -04:00
|
|
|
] if-empty ;
|
2008-06-15 03:38:12 -04:00
|
|
|
|
|
|
|
: restore-validation-errors ( -- )
|
2008-07-10 00:41:45 -04:00
|
|
|
form cget [
|
|
|
|
nested-forms cget set-nested-form
|
2008-06-15 03:38:12 -04:00
|
|
|
] when* ;
|
2008-06-04 20:54:05 -04:00
|
|
|
|
2008-05-26 01:47:27 -04:00
|
|
|
: handle-get ( action -- response )
|
2008-06-04 20:54:05 -04:00
|
|
|
'[
|
2008-09-10 23:11:40 -04:00
|
|
|
_ dup display>> [
|
2008-06-13 01:47:47 -04:00
|
|
|
{
|
2009-03-15 19:19:29 -04:00
|
|
|
[ init>> call( -- ) ]
|
|
|
|
[ authorize>> call( -- ) ]
|
2008-06-15 03:38:12 -04:00
|
|
|
[ drop restore-validation-errors ]
|
2009-03-15 19:19:29 -04:00
|
|
|
[ display>> call( -- response ) ]
|
2008-06-13 01:47:47 -04:00
|
|
|
} cleave
|
|
|
|
] [ drop <400> ] if
|
2008-06-04 20:54:05 -04:00
|
|
|
] with-exit-continuation ;
|
2008-03-11 04:39:09 -04:00
|
|
|
|
2009-02-23 22:40:17 -05:00
|
|
|
CONSTANT: revalidate-url-key "__u"
|
2008-05-27 01:01:57 -04:00
|
|
|
|
2008-06-04 20:54:05 -04:00
|
|
|
: revalidate-url ( -- url/f )
|
2008-06-13 23:05:41 -04:00
|
|
|
revalidate-url-key param
|
2008-09-29 23:33:06 -04:00
|
|
|
dup [ >url ensure-port [ same-host? ] keep and ] when ;
|
2008-06-04 20:54:05 -04:00
|
|
|
|
2008-07-10 00:41:45 -04:00
|
|
|
: validation-failed ( -- * )
|
|
|
|
post-request? revalidate-url and [
|
|
|
|
begin-conversation
|
|
|
|
nested-forms-key param " " split harvest nested-forms cset
|
|
|
|
form get form cset
|
2008-09-21 20:42:05 -04:00
|
|
|
<continue-conversation>
|
2008-07-10 00:41:45 -04:00
|
|
|
] [ <400> ] if*
|
2008-06-15 03:38:12 -04:00
|
|
|
exit-with ;
|
|
|
|
|
2008-06-04 20:54:05 -04:00
|
|
|
: handle-post ( action -- response )
|
|
|
|
'[
|
2008-09-10 23:11:40 -04:00
|
|
|
_ dup submit>> [
|
2009-03-15 19:19:29 -04:00
|
|
|
[ validate>> call( -- ) ]
|
|
|
|
[ authorize>> call( -- ) ]
|
|
|
|
[ submit>> call( -- response ) ]
|
2008-06-15 03:38:12 -04:00
|
|
|
tri
|
|
|
|
] [ drop <400> ] if
|
|
|
|
] with-exit-continuation ;
|
2008-06-04 20:54:05 -04:00
|
|
|
|
2009-08-03 15:58:18 -04:00
|
|
|
: handle-rest ( path action -- )
|
|
|
|
rest>> dup [ [ "/" join ] dip set-param ] [ 2drop ] if ;
|
2008-06-04 20:54:05 -04:00
|
|
|
|
|
|
|
: init-action ( path action -- )
|
2008-06-15 03:38:12 -04:00
|
|
|
begin-form
|
2009-08-03 15:58:18 -04:00
|
|
|
handle-rest ;
|
2008-06-04 20:54:05 -04:00
|
|
|
|
|
|
|
M: action call-responder* ( path action -- response )
|
|
|
|
[ init-action ] keep
|
|
|
|
request get method>> {
|
|
|
|
{ "GET" [ handle-get ] }
|
|
|
|
{ "HEAD" [ handle-get ] }
|
|
|
|
{ "POST" [ handle-post ] }
|
|
|
|
} case ;
|
|
|
|
|
|
|
|
M: action modify-form
|
2008-07-09 18:04:20 -04:00
|
|
|
drop url get revalidate-url-key hidden-form-field ;
|
2008-05-26 01:47:27 -04:00
|
|
|
|
|
|
|
: check-validation ( -- )
|
2008-07-10 00:41:45 -04:00
|
|
|
validation-failed? [ validation-failed ] when ;
|
2008-05-26 01:47:27 -04:00
|
|
|
|
|
|
|
: validate-params ( validators -- )
|
2008-06-15 03:38:12 -04:00
|
|
|
params get swap validate-values check-validation ;
|
2008-05-26 01:47:27 -04:00
|
|
|
|
|
|
|
: validate-integer-id ( -- )
|
|
|
|
{ { "id" [ v-number ] } } validate-params ;
|
|
|
|
|
|
|
|
TUPLE: page-action < action template ;
|
|
|
|
|
2008-06-01 18:22:39 -04:00
|
|
|
: <chloe-content> ( path -- response )
|
2014-04-22 16:47:25 -04:00
|
|
|
resolve-template-path <chloe> <html-content> ;
|
2008-06-01 18:22:39 -04:00
|
|
|
|
2008-05-26 01:47:27 -04:00
|
|
|
: <page-action> ( -- page )
|
|
|
|
page-action new-action
|
2008-09-10 23:11:40 -04:00
|
|
|
dup '[ _ template>> <chloe-content> ] >>display ;
|