web-gen-pdf.lisp 4.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
(mccme-helpers/packages:file mccme-helpers/web-gen-pdf
  :use (
        mccme-helpers/emb-helpers
        mccme-helpers/ht
        mccme-helpers/data-transformer
        mccme-helpers/lisp
        mccme-helpers/csv
        mccme-helpers/trivial
        mccme-helpers/cookie
        mccme-helpers/struct
        mccme-helpers/auth-pages
        mccme-helpers/tex
        :iterate
        )
  :export (
           #:make-pdf-web-form
    )
  )

(defmacro 
  make-pdf-web-form
  (handler-symbol url-base page-template tex-template file-name
                  file-field-name view-log-field-name
                  data-set-name entry-names
                  &key
                  (force-field-names nil) (extra-entry-names nil)
                  (extra-data-set-name :extra-data) (temp-tex-dir "tex/"))
  `(progn
    (define-emb-page 
     (,(concatenate-symbol-names handler-symbol :-request) 
       ,(concatenate 'string url-base "/request") ,page-template)
     )
    (hunchentoot:define-easy-handler
     (,(concatenate-symbol-names handler-symbol :-submit)
       :uri ,(concatenate 'string url-base "/submit")
      )
     ()
     (mccme-helpers/functional:backtraceable
      (verify-login)
      (let*
       (
        (schema 
         `(
           ((:code-name ,,file-field-name)
            (:type :string)
            (:html-input-type :file)
            )
           ((:code-name ,,view-log-field-name)
            (:type :bool)
            (:html-input-type :checkbox)
            )
           ))
        (dt (data-transformer schema))
        (dummy1 (transformer-http-post-collect dt))
        (csv-content
         (multiple-value-list
          (load-csv-file-with-headers 
           (-> dt ,file-field-name) ,(if extra-entry-names 1 0)
           ,(if force-field-names 0 1) 0 :force-field-names ,force-field-names
           ,@(when extra-entry-names (list :extra-data-position 1))
           )
          ))
        (hts (first csv-content))
        (hted (second csv-content))
        (data `(,,data-set-name
                ,(iterate (for ht :in hts) 
                          (collect
                           (iterate
                            (for x in ,entry-names)
                            (when (-> ht (first x))
                                  (collect (second x))
                                  (collect (-> ht (first x)))
                                  )
                            )
                           ))
                ,,extra-data-set-name
                ,(iterate
                  (for x in ,extra-entry-names)
                  (when (-> hted (first x))
                        (collect (second x))
                        (collect (-> hted (first x)))
                        )
                  )
                ))
        (pdf-name 
         (third
          (multiple-value-list
           (tex-to-pdf ,tex-template data ,temp-tex-dir))))
        )
       (declare
        (ignore dummy1))
       (set-secure-cookie "target-pdf-file" pdf-name)
       (if 
         (-> dt ,view-log-field-name)
         (ht:redirect ,(concatenate 'string url-base "/" file-name ".log"))
         (ht:redirect ,(concatenate 'string url-base "/" file-name))
         )
       )
      ))
    (hunchentoot:define-easy-handler
     (,(concatenate-symbol-names handler-symbol :-pdf)
       :uri ,(concatenate 'string url-base "/" file-name)
      )
     ()
     (mccme-helpers/functional:backtraceable
      (verify-login)
      (let* 
       (
        (target-pdf-file (get-secure-cookie "target-pdf-file"))
        )
       (set-secure-cookie "target-pdf-file" nil)
       (setf (ht:content-type*) "application/x-pdf")
       (setf (ht:header-out "Content-Disposition") (format nil "attachment; filename=\"~a\"" ,file-name))
       (file-contents target-pdf-file)
       )
      ))
    (hunchentoot:define-easy-handler
     (,(concatenate-symbol-names handler-symbol :-log)
       :uri ,(concatenate 'string url-base "/" file-name ".log")
      )
     ()
     (mccme-helpers/functional:backtraceable
      (verify-login)
      (let* 
       (
        (target-pdf-file (get-secure-cookie "target-pdf-file"))
        )
       (set-secure-cookie "target-pdf-file" nil)
       (setf (ht:content-type*) "text/plain; charset=cp1251")
       (file-contents (cl-ppcre:regex-replace-all "[.]pdf$" target-pdf-file ".log"))
       )
      ))
    ))