(in-package #:cl-user) (defpackage #:utils (:use #:cl #:caveman2 #:log4cl) (: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) (:documentation "Utilities that do not depend on models.")) (in-package #:utils) (defun asciify (string) (str:downcase (slug:asciify 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))