Public archive for the Return to Ritherdon project.
https://www.nicolaellisandritherdon.com
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.6 KiB
82 lines
2.6 KiB
(in-package #:cl-user) |
|
(defpackage #:utils |
|
(:use #:cl |
|
#:caveman2 |
|
#:log4cl |
|
#:app-constants) |
|
(:export #:i18n-load |
|
#:_ |
|
#:parse-iso-date |
|
#:request-params |
|
#:string-is-nil-or-empty? |
|
#:separate-files-in-web-request |
|
#:set-alert |
|
#:get-alert |
|
#:get-and-reset-alert |
|
#:checkbox-to-bool |
|
#:asciify |
|
#:slugify) |
|
(:documentation "Utilities that do not depend on models.")) |
|
|
|
(in-package #:utils) |
|
|
|
(defun asciify (string) |
|
(str:downcase (slug:asciify string))) |
|
|
|
(defun slugify (string) |
|
"Turns a string of text into a slug." |
|
(str:downcase (slug:slugify string))) |
|
|
|
(defun request-params (request) |
|
(loop :for (key . value) :in request |
|
:collect (let ((*package* (find-package :keyword))) |
|
(read-from-string key)) |
|
:collect value)) |
|
|
|
(defun string-is-nil-or-empty? (string-to-test) |
|
"Tests to see if `STRING-TO-TEST' is empty of just whitespace. |
|
This is essentially the 'IsNullOrWhiteSpace' function I use in C#. It |
|
expands the 'empty string' check to include a check to see if there is |
|
string with just a '(white) space' in it." |
|
(if (or (string= string-to-test " ") |
|
(zerop (length string-to-test)) |
|
(null string-to-test)) |
|
t |
|
nil)) |
|
|
|
(defun separate-files-in-web-request (request &optional request-value) |
|
"Creates a new list of 'upload' files from a web `REQUEST'. |
|
You will mostly use this for processing a multi-file upload (HTML) |
|
form. The standard value for the 'name' attribute in (file) input tag |
|
in the HTML form is `CONTENT-FILES' but you can use a different |
|
name. Just specify it in this function's `REQUEST-VALUE' argument." |
|
(loop :for item :in request |
|
if (or (string= "CONTENT-FILES" (car item)) |
|
(string= request-value (car item))) |
|
collect item)) |
|
|
|
(defun set-alert (message) |
|
"Sets the alert `MESSAGE' stored in session, provide info. to users. |
|
The intention is store a `MESSAGE' across a redirect during a HTTP |
|
POST request." |
|
(setf (gethash :alert ningle:*session*) message)) |
|
|
|
(defun get-alert () |
|
"Get alert message from session data." |
|
(gethash :alert ningle:*session*)) |
|
|
|
(defun get-and-reset-alert () |
|
"Returns the `ALERT' message and clears its content from the session hash." |
|
(let ((message (get-alert))) |
|
(set-alert nil) |
|
message)) |
|
|
|
(defun checkbox-to-bool (value) |
|
"Converts a HTML Checkbox `VALUE' to a Boolean. |
|
The `VALUE' will either be 'on' or 'off'. 'Boolean' in this instance |
|
is assuming you are using SQLite and need to convert `VALUE' to an |
|
integer/number. If you are needing a traditional Boolean value, DO NOT USE |
|
THIS FUNCTION." |
|
(cond ((or (string= "checked" value) (string= "on" value)) +true+) |
|
((or (string= "off" value) (null value)) +false+) |
|
((null value) +false+)))
|
|
|