Create new pages in the notepad by modifying how edit pages works.

This commit is contained in:
Feufochmar 2021-05-06 18:14:53 +02:00
parent ce1311b2aa
commit 8c96fb25a1
2 changed files with 28 additions and 24 deletions

View File

@ -80,6 +80,7 @@
("notes" symlink "/notes/list"
("list" weblet pages:notepad:page-list)
("show/{page}" matching-weblet pages:notepad:page-show)
("edit" weblet pages:notepad:page-edit)
("edit/{page}" matching-weblet pages:notepad:page-edit)
("delete/{page}" matching-weblet pages:notepad:page-delete)
)

View File

@ -78,14 +78,22 @@
#:author "feuforeve.fr"
#:content
(lambda (param)
(define notes (map path->string (directory-list notepad-dir)))
(if (null? notes)
'(article "Pas de notes.")
`(article
,@(map
(lambda (x)
`(div (a ((href ,(string-append "/notes/show/" x))) ,x)))
notes))))
(define connected-usr (get-user-from-weblet-parameter param))
(define secured? (check-secured? param))
(define can-edit? (and connected-usr secured?))
(define notes (map path->string (directory-list notepad-dir)))
`(article
,@(if (null? notes)
'("Pas de notes.")
(map
(lambda (x)
`(div (a ((href ,(string-append "/notes/show/" x))) ,x)))
notes))
,@(if can-edit?
'((hr)
(a ((href "/notes/edit")) "Nouvelle note"))
'(""))
))
))
; /notes/show/xxx
@ -193,9 +201,6 @@
(#t
(notepad:error 'not-found))))))
; /notes/new
; Create a new page. User must be logged in.
; /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
@ -206,27 +211,28 @@
(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))
(define has-page? (and file (file-exists? file)))
(cond
( (and page connected-usr secured? (eq? method 'get))
( (and connected-usr secured? (eq? method 'get))
; User connected, get method : read the page
(define content (if has-page? (port->string (open-input-file file)) ""))
(define page-name (or page "nouvelle.note"))
( (pages:template
#:title (string-append "Édition de la page " page)
#:title (string-append "Édition de la page '" page-name "'")
#:author (user-name connected-usr)
#:content
; Display the page as a form
`(article
(form ((action ,(string-append "/notes/edit/" page))
(method "post"))
(form ((action ,(string-append "/notes/edit/" page-name))
(method "post"))
(label ((for "pagename")) "Nom de la note") (br)
(input ((id "pagename")(name "pagename")(type "text")(value ,page))) (br)
(input ((id "pagename")(name "pagename")(type "text")(value ,page-name))) (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"))
(input ((type "submit")(formaction ,(string-append "/notes/edit/" page-name "?continue=t"))
(value "Sauver et continuer l'édition")))
)))
param))
@ -244,17 +250,15 @@
(display new-page-content out))
#:exists 'truncate/replace)
; Name has changed ? If yes, remove the old page
(when (not (equal? file new-file))
(when (and (not (equal? file new-file)) has-page?)
(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))
; Edition is not allowed
(pages:notepad:error param 'unauthorized))
( #t
; No such page
(pages:notepad:error param 'not-found))))
@ -352,7 +356,6 @@
`((author . ,(user-name usr))
(title . ,(string-append "À propos de " (user-pseudo usr)))
(content . (article
(hr)
,(cond
( edition-possible?
`(form ((action ,(string-append "/user/edit/" (user-name usr)))