Remove notes and refactor the error pages of the notepad.

This commit is contained in:
Feufochmar 2021-05-06 17:26:34 +02:00
parent ede7ce01ae
commit ce1311b2aa
2 changed files with 74 additions and 30 deletions

View File

@ -81,6 +81,7 @@
("list" weblet pages:notepad:page-list)
("show/{page}" matching-weblet pages:notepad:page-show)
("edit/{page}" matching-weblet pages:notepad:page-edit)
("delete/{page}" matching-weblet pages:notepad:page-delete)
)
("user" symlink "/user/list"
("list" weblet pages:notepad:user-list)
@ -127,6 +128,7 @@
("Bloc-Note" "/notes/list" #t
(#f "/notes/show" #f)
(#f "/notes/edit" #f)
(#f "/notes/delete" #f)
("Utilisateurs" "/user/list" #t)
(#f "/user/show" #f)
(#f "/user/login" #f)

View File

@ -18,6 +18,7 @@
pages:notepad:page-list
pages:notepad:page-show
pages:notepad:page-edit
pages:notepad:page-delete
pages:notepad:user-list
pages:notepad:user-show
pages:notepad:user-login
@ -40,31 +41,33 @@
(or dev?
(eq? 'https (weblet-parameter-protocol param))))
; Page not found
(define (not-found)
; Error pages
; Type to code + title + message
(define *error-types*
(make-immutable-hash
'((error-code . 404)
(author . "404")
(title . "Not found")
(content . (article "Sorry, there is nothing here."))
'((not-found . (404 "Not found" "Sorry, there is nothing here."))
(unauthorized . (401 "Unauthorized" "Sorry, you can't go there."))
(method-not-allowed . (405 "Not allowed" "Sorry, this method is not allowed."))
)))
; As page
(define (pages:notepad:error param error-type)
(define err (hash-ref *error-types* error-type))
((pages:template
#:error-code (car err)
#:author (number->string (car err))
#:title (cadr err)
#:content `(article ,(caddr err))
)
param))
; As result for pages:adaptable-template
(define (notepad:error error-type)
(define err (hash-ref *error-types* error-type))
(make-immutable-hash
`((error-code . ,(car err))
(author . ,(number->string (car err)))
(title . ,(cadr err))
(content . (article ,(caddr err)))
)))
; 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
#:error-code 405
#:author "405"
#:title "Not allowed"
#:content '(article "Sorry, this method is not allowed.")
))
; Notepad pages
; /notes/list
@ -188,7 +191,7 @@
))
result)
(#t
(not-found))))))
(notepad:error 'not-found))))))
; /notes/new
; Create a new page. User must be logged in.
@ -254,10 +257,49 @@
see-other))
( #t
; No such page
(pages:notepad:not-found param))))
(pages:notepad:error param 'not-found))))
; /notes/delete/xxx
; Remove an existing page. User must be logged in.
; Get => ask confirmation
; Post => remove
(define (pages:notepad:page-delete 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 has-page? connected-usr secured? (eq? method 'get))
; Method get => ask for confirmation
( (pages:template
#:title (string-append "Suppression de la page " page)
#:author (user-name connected-usr)
#:content
`(article
(form ((action ,(string-append "/notes/delete/" page))
(method "post"))
(h3 "Supprimer la page " ,page " ? ")
"Cette action est irréversible." (br)
(input ((type "submit")
(value "Oui, supprimer la page")))
(input ((type "submit")(formaction ,(string-append "/notes/show/" page))
(formmethod "get")(value "Non, garder la page")))
)))
param))
( (and has-page? connected-usr secured? (eq? method 'post))
; Method post => remove
(delete-file file)
(redirect-to
"/notes/list"
see-other))
( has-page?
; Unauthorized
(pages:notepad:error param 'unauthorized))
( #t
; No such page
(pages:notepad:error param 'not-found))))
; /media/list
; Lists all medias of the notepad.
@ -337,7 +379,7 @@
""))
)))))
(#t
(not-found))))))
(notepad:error 'not-found))))))
; /user/login/xxx
; Login page for user xxx.
@ -359,7 +401,7 @@
; redirect to https
(redirect-to-https-weblet param))
(#t
(pages:notepad:method-not-allowed param))))
(pages:notepad:error param 'method-not-allowed))))
; Already connected
(define (pages:notepad:user-login:already-connected usr param)
((pages:template
@ -393,7 +435,7 @@
)
)))))
(#t
(not-found))))))
(notepad:error 'not-found))))))
; Process login
(define (pages:notepad:user-login:sent-form param)
(define usr (get-user-by-name (weblet-parameter-ref param 'name #f)))
@ -412,7 +454,7 @@
(string-append "/user/login/" (user-name usr) "?incorrect=t")
see-other))
(#t
(pages:notepad:not-found param))))
(pages:notepad:error param 'not-found))))
; /user/edit/xxx
; Edit page for the user xxx. Must be logged in as user xxx.
@ -437,7 +479,7 @@
(string-append "/user/show/" (user-name usr))
see-other))
(#t
((pages:adaptable-template (not-found)) param))))
(pages:notepad:error param 'not-found))))
; /user/logout
; Logout page.