Add note edition.
This commit is contained in:
parent
97ac116753
commit
4b2739f516
2
main.rkt
2
main.rkt
|
@ -80,6 +80,7 @@
|
|||
("notes" symlink "/notes/list"
|
||||
("list" weblet pages:notepad:page-list)
|
||||
("show/{page}" matching-weblet pages:notepad:page-show)
|
||||
("edit/{page}" matching-weblet pages:notepad:page-edit)
|
||||
)
|
||||
("user" symlink "/user/list"
|
||||
("list" weblet pages:notepad:user-list)
|
||||
|
@ -125,6 +126,7 @@
|
|||
)
|
||||
("Bloc-Note" "/notes/list" #t
|
||||
(#f "/notes/show" #f)
|
||||
(#f "/notes/edit" #f)
|
||||
("Utilisateurs" "/user/list" #t)
|
||||
(#f "/user/show" #f)
|
||||
(#f "/user/login" #f)
|
||||
|
|
|
@ -11,11 +11,13 @@
|
|||
web-server/http/redirect
|
||||
web-server/http/cookie
|
||||
web-server/http/request-structs
|
||||
racket/port
|
||||
)
|
||||
|
||||
(provide
|
||||
pages:notepad:page-list
|
||||
pages:notepad:page-show
|
||||
pages:notepad:page-edit
|
||||
pages:notepad:user-list
|
||||
pages:notepad:user-show
|
||||
pages:notepad:user-login
|
||||
|
@ -47,6 +49,14 @@
|
|||
(content . (article "Sorry, there is nothing here."))
|
||||
)))
|
||||
|
||||
; Not found (as page)
|
||||
(define pages:notepad:not-found
|
||||
(pages:template
|
||||
#:error-code 404
|
||||
#:author "404"
|
||||
#:title "Not found"
|
||||
#:content '(article "Sorry, there is nothing here.")
|
||||
))
|
||||
; Unallowed method (as page)
|
||||
(define pages:notepad:method-not-allowed
|
||||
(pages:template
|
||||
|
@ -81,6 +91,9 @@
|
|||
(define pages:notepad:page-show
|
||||
(pages:adaptable-template
|
||||
(lambda (param)
|
||||
(define connected-usr (get-user-from-weblet-parameter param))
|
||||
(define secured? (check-secured? param))
|
||||
(define can-edit? (and connected-usr secured?))
|
||||
(define page (weblet-parameter-ref param 'page #f))
|
||||
(define file (and page (string-append notepad-dir "/" page)))
|
||||
(define has-page? (file-exists? file))
|
||||
|
@ -164,7 +177,15 @@
|
|||
(recur parsed))
|
||||
))))
|
||||
(define result (make-hash))
|
||||
(hash-set! result 'content `(article ,@(collect-result parsed result)))
|
||||
(hash-set! result 'content
|
||||
`(article
|
||||
,@(collect-result parsed result)
|
||||
,@(if can-edit?
|
||||
`((hr)
|
||||
(a ((href ,(string-append "/notes/edit/" page))) "Éditer") " — "
|
||||
(a ((href ,(string-append "/notes/delete/" page))) "Supprimer"))
|
||||
'())
|
||||
))
|
||||
result)
|
||||
(#t
|
||||
(not-found))))))
|
||||
|
@ -174,6 +195,66 @@
|
|||
|
||||
; /notes/edit/xxx
|
||||
; Edit an existing page, or create a new page with a given title. User must be logged in.
|
||||
; Get => Form to edit page
|
||||
; Post => Save page
|
||||
(define (pages:notepad:page-edit param)
|
||||
(define connected-usr (get-user-from-weblet-parameter param))
|
||||
(define secured? (check-secured? param))
|
||||
(define method (weblet-parameter-method param))
|
||||
(define page (weblet-parameter-ref param 'page #f))
|
||||
(define file (and page (string-append notepad-dir "/" page)))
|
||||
(define has-page? (file-exists? file))
|
||||
(cond
|
||||
( (and page connected-usr secured? (eq? method 'get))
|
||||
; User connected, get method : read the page
|
||||
(define content (if has-page? (port->string (open-input-file file)) ""))
|
||||
( (pages:template
|
||||
#:title (string-append "Édition de la page " page)
|
||||
#:author (user-name connected-usr)
|
||||
#:content
|
||||
; Display the page as a form
|
||||
`(article
|
||||
(form ((action ,(string-append "/notes/edit/" page))
|
||||
(method "post"))
|
||||
(label ((for "pagename")) "Nom de la note") (br)
|
||||
(input ((id "pagename")(name "pagename")(type "text")(value ,page))) (br)
|
||||
(label ((for "pagecontent")) "Contenu de la note") (br)
|
||||
(textarea ((rows "10")(cols "80")(id "pagecontent")(name "pagecontent"))
|
||||
,content) (br)
|
||||
(input ((type "submit")
|
||||
(value "Sauver et quitter l'édition")))
|
||||
(input ((type "submit")(formaction ,(string-append "/notes/edit/" page "?continue=t"))
|
||||
(value "Sauver et continuer l'édition")))
|
||||
)))
|
||||
param))
|
||||
( (and page connected-usr secured? (eq? method 'post))
|
||||
(define continue? (equal? "t" (weblet-parameter-ref param 'continue #f)))
|
||||
(define page-name (weblet-parameter-ref param 'pagename #f))
|
||||
(define new-page-content (weblet-parameter-ref param 'pagecontent #f))
|
||||
(define new-page-name (or (and (not (equal? "" page-name)) page-name)
|
||||
page))
|
||||
(define new-file (string-append notepad-dir "/" new-page-name))
|
||||
; Save page to file
|
||||
(call-with-output-file
|
||||
new-file
|
||||
(lambda (out)
|
||||
(display new-page-content out))
|
||||
#:exists 'truncate/replace)
|
||||
; Name has changed ? If yes, remove the old page
|
||||
(when (not (equal? file new-file))
|
||||
(delete-file file))
|
||||
; Redirect
|
||||
(redirect-to
|
||||
(string-append (if continue? "/notes/edit/" "/notes/show/") new-page-name)
|
||||
see-other))
|
||||
( page
|
||||
; Redirect to /notes/show/, as edition is not allowed
|
||||
(redirect-to
|
||||
(string-append "/notes/show/" page)
|
||||
see-other))
|
||||
( #t
|
||||
; No such page
|
||||
(pages:notepad:not-found param))))
|
||||
|
||||
; /notes/delete/xxx
|
||||
; Remove an existing page. User must be logged in.
|
||||
|
@ -331,7 +412,7 @@
|
|||
(string-append "/user/login/" (user-name usr) "?incorrect=t")
|
||||
see-other))
|
||||
(#t
|
||||
((pages:adaptable-template (not-found)) param))))
|
||||
(pages:notepad:not-found param))))
|
||||
|
||||
; /user/edit/xxx
|
||||
; Edit page for the user xxx. Must be logged in as user xxx.
|
||||
|
|
Loading…
Reference in New Issue