我的程序允许用户选择要导入数据库的文件。在此之前,它只允许他们一次导入一个文件。让它们一次导入多个文件很容易。但我遇到的问题是,如果数据库已经包含具有相同标题的页面,我希望在用导入的版本覆盖数据库中的版本之前显示一条警告并得到确认。
这是我到目前为止所掌握的。它很大程度上遵循了我在导入单个文件时所做的工作。
;; Handler for the POST "/import" route.
(defn- post-import-page
"Import the file(s) specified in the upload dialog. Checks the page
title of the import file against existing pages in the database. If
a page of the same name already exists, asks for confirmation before
importing."
[{{file-info "file-info"
referer "referer"} :multipart-params :as req}]
(let [file-vec (if (map? file-info)
(conj [] file-info)
file-info)]
(doseq [fm file-vec]
(let [file-name (:filename fm)
import-map (files/load-markdown-from-file (:tempfile fm))
page-title (get-in import-map [:meta :title])
id-exists? (db/title->page-id page-title)]
(if id-exists?
(build-response
(layout/compose-import-existing-page-warning
import-map file-name referer) req)
(do-the-import import-map file-name req))))))此函数导入数据库中尚不存在的任何文件,但不会导入任何会覆盖具有相同标题的现有数据库条目的内容。它也不会显示要求确认的警告页面。
警告页面的结构如下:
(defn compose-import-existing-page-warning
"Return a page stating that a page with the same title already exists
in the wiki. Ask the user to proceed or cancel the import."
[import-map file-name referer]
(short-form-template
[:div {:class "cwiki-form"}
(form-to {:enctype "multipart/form-data"
:autocomplete "off"}
[:post "proceed-with-import"]
(hidden-field "import-map" import-map)
(hidden-field "file-name" file-name)
(hidden-field "referer" referer)
[:p {:class "form-title"} "Page Already Exists"]
[:div {:class "form-group"}
[:p (str "A page with the title \"" (get-in import-map [:meta :title])
"\" already exists in the wiki.")]
[:p (str "Click \"Proceed\" to delete the existing page and "
"replace it with the contents of the imported file.")]
[:div {:class "button-bar-container"}
(submit-button {:id "proceed-with-import-button"
:class "form-button button-bar-item"}
"Proceed")
[:input {:type "button" :name "cancel-button"
:value "Cancel"
:class "form-button button-bar-item"
:autofocus "autofocus"
:onclick "window.history.back();"}]]])]))程序如何在doseq (或其他循环函数)中途暂停,以显示确认页面并等待用户做出选择?
发布于 2020-04-12 06:15:00
只需在循环中间使用read-line,然后使用if来选择您想要的分支。以下是您可能会发现有用的其他documentation的列表,尤其是Clojure CheatSheet。
https://stackoverflow.com/questions/61161252
复制相似问题