butiful devbar

integration-tests
Josha von Gizycki 7 years ago
parent da8fb77fc0
commit a2bd971596

@ -12,8 +12,8 @@
:exclusions [commons-codec]]] :exclusions [commons-codec]]]
:plugins [[lein-ring "0.9.7"]] :plugins [[lein-ring "0.9.7"]]
:ring {:handler wanijo.handler/app} :ring {:handler wanijo.handler/app}
:profiles :profiles {:dev {:dependencies
{:dev {:dependencies [[javax.servlet/servlet-api "2.5"] [[javax.servlet/servlet-api "2.5"]
[ring/ring-mock "0.3.0"]] [ring/ring-mock "0.3.0"]]
:plugins [[lein-less "1.7.5"]]}} :plugins [[lein-less "1.7.5"]]}}
:less {:source-paths ["resources/app/stylesheets"] :less {:source-paths ["resources/app/stylesheets"]

@ -0,0 +1,24 @@
.devbar {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
font-family: monospace;
background-color: #ccc;
ol {
list-style-type: none;
padding: 0;
margin: 0;
li {
padding: .5rem;
border: 1px solid black;
margin: .5rem;
pre {
margin: 0;
}
}
}
}

@ -0,0 +1,45 @@
(ns wanijo.devmode
(:require [hiccup.core :as hcore]))
(def bar-entries (atom []))
(defn send-to-bar [msg]
(swap! bar-entries #(conj % msg)))
(defn devmode-on? [req]
(let [query-param (get-in req [:query-params "dev"])
cookie (get-in req [:cookies "devmode" :value])]
(or (= query-param "on")
(and (= cookie "1")
(not= query-param "off")))))
(defn devbar []
(hcore/html
[:section.devbar
[:ol
(for [entry @bar-entries]
[:li [:pre entry]])]]))
(defn append-devbar [resp]
(let [body (:body resp)
new-body (clojure.string/replace
body "</body>" (str (devbar) "</body>"))]
(assoc resp :body new-body)))
(defn wrap-devmode [handler]
(fn [req]
(let [on? (devmode-on? req)
query-param? (get-in req [:query-params "dev"])
new-req (assoc-in req [:session :devmode] on?)
resp (handler new-req)
new-resp (cond
on? (-> resp
append-devbar
(assoc-in [:cookies :devmode] 1))
(some? query-param?) (assoc-in
resp
[:cookies :devmode]
(if on? 1 0))
:else resp)]
(reset! bar-entries [])
new-resp)))

@ -1,10 +1,11 @@
(ns wanijo.domain.schema (ns wanijo.domain.schema
(:require [clojurewerkz.neocons.rest.cypher :as cypher] (:require [clojurewerkz.neocons.rest.cypher :as cypher]
[clojure.spec.alpha :as spec] [clojure.spec.alpha :as spec]
[wanijo.neo4j :as neo4j])) [wanijo.neo4j :as neo4j]
[wanijo.devmode :as devmode]))
(spec/def ::name (spec/def ::name
(spec/and string? not-empty)) (spec/and string? not-empty int?))
(spec/def ::created-at (spec/def ::created-at
(spec/or :int (spec/and int? (spec/or :int (spec/and int?
@ -13,12 +14,12 @@
#(re-matches #"\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}" %)))) #(re-matches #"\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}" %))))
(defn all-created-by! [ident] (defn all-created-by! [ident]
(->> (neo4j/query-rawdata!
(cypher/tquery :query
neo4j/conn "MATCH (s:schema)-[:created_by]->(u:user)
"MATCH (s:schema)-[created_by]->(u:user)
WHERE u.ident = {ident} WHERE u.ident = {ident}
RETURN s RETURN s
ORDER BY s.name" ORDER BY s.name"
{:ident ident}) :params {:ident ident}
(map #(:data (get % "s"))))) :alias "s"
))

@ -3,7 +3,7 @@
[clojure.spec.alpha :as spec] [clojure.spec.alpha :as spec]
[wanijo.view :as view])) [wanijo.view :as view]))
(defn spec-to-errmsg [label spec-data] (defn spec-to-errmsg [label spec-key field-value]
(view/flash-error (view/flash-error
(map (map
(fn [prob] (fn [prob]
@ -12,24 +12,27 @@
[:span.flash__field label] [:span.flash__field label]
" must comply to " " must comply to "
[:span.flash__pred (:pred prob)]]) [:span.flash__pred (:pred prob)]])
(:clojure.spec.alpha/problems spec-data)))) (:clojure.spec.alpha/problems
(spec/explain-data spec-key field-value)))))
(defn field-valid? [value spec-key req]
(or (empty? (:form-params req))
(spec/valid? spec-key value)))
(defn field [form-def field req] (defn field [form-def field req]
(let [field-value (get-in req [:params field]) (let [field-value (get-in req [:params field])
field-def (get-in form-def [:fields field]) field-def (get-in form-def [:fields field])
{:keys [label required] spec-key :spec} field-def {:keys [label required] spec-key :spec} field-def]
spec-valid? (or (empty? (:params req))
(spec/valid? spec-key field-value))]
(list (list
(when-not spec-valid? (when-not (field-valid? field-value spec-key req)
(spec-to-errmsg label (spec-to-errmsg label spec-key field-value))
(spec/explain-data spec-key field-value)))
(hform/label field label) (hform/label field label)
(hform/text-field {:required (when required "required")} (hform/text-field {:required (when required "required")}
field field
(get-in req [:params field]))))) field-value))))
(defn valid? [req form-def] (defn valid? [form-def req]
(clojure.pprint/pprint req)
(reduce-kv (reduce-kv
(fn [result field value] (fn [result field value]
(if (spec/valid? (if (spec/valid?

@ -7,13 +7,14 @@
[ring.middleware.session.cookie :as session-cookie] [ring.middleware.session.cookie :as session-cookie]
[wanijo.auth.routes :refer [auth-routes]] [wanijo.auth.routes :refer [auth-routes]]
[wanijo.home.routes :refer [home-routes]] [wanijo.home.routes :refer [home-routes]]
[wanijo.schema.routes :refer [schema-routes]])) [wanijo.schema.routes :refer [schema-routes]]
[wanijo.devmode :as devmode]))
(defn- wrap-login-redirect [handler] (defn- wrap-login-redirect [handler]
(fn [req] (fn [req]
(if-not (get-in req [:session :ident]) (if (get-in req [:session :ident])
(rur/redirect "/login") (handler req)
(handler req)))) (rur/redirect "/login"))))
(defroutes app-routes (defroutes app-routes
(routes auth-routes) (routes auth-routes)
@ -24,6 +25,9 @@
(def app (def app
(-> app-routes (-> app-routes
(wrap-defaults (assoc-in site-defaults devmode/wrap-devmode
(wrap-defaults
(assoc-in site-defaults
[:session :store] [:session :store]
(session-cookie/cookie-store {:key "1234567890123456"}))))) (session-cookie/cookie-store
{:key "1234567890123456"})))))

@ -1,14 +1,36 @@
(ns wanijo.neo4j (ns wanijo.neo4j
(:require [clojurewerkz.neocons.rest :as nr] (:require [clojurewerkz.neocons.rest :as nr]
[clojurewerkz.neocons.rest.nodes :as nrn] [clojurewerkz.neocons.rest.nodes :as nrn]
[clojurewerkz.neocons.rest.cypher :as nrc])) [clojurewerkz.neocons.rest.cypher :as cypher]
[wanijo.devmode :as devmode]))
(def conn (nr/connect "http://neo4j:b@localhost:7474/db/data")) (def conn
(delay (nr/connect "http://neo4j:b@localhost:7474/db/data")))
(defn conn! []
@conn)
(defn butiful-query [qry]
(->> qry
clojure.string/trim-newline
clojure.string/split-lines
(map clojure.string/trim)
(filter #(> (count %) 0))
(clojure.string/join \newline)))
(defn query-rawdata!
[& {:keys [query alias params]
:or {params {}}}]
(devmode/send-to-bar (str (butiful-query query) "<br>---Params---<br>" params))
(->>
(cypher/tquery (conn!) query params)
(map #(get % alias))
(map :data)))
(defn find-user! [ident] (defn find-user! [ident]
(some-> (some->
(nrc/tquery (cypher/tquery
conn (conn!)
"MATCH (n:user) WHERE n.ident = {ident} RETURN n" "MATCH (n:user) WHERE n.ident = {ident} RETURN n"
{:ident ident}) {:ident ident})
first first

@ -15,16 +15,17 @@
:or {content [] :or {content []
title nil title nil
session {}}}] session {}}}]
(println session)
(let [ident (:ident session) (let [ident (:ident session)
authed? (some? ident)] authed? (some? ident)
devmode? (:devmode session)]
(html5 (html5
[:head [:head
[:meta {:charset "utf-8"}] [:meta {:charset "utf-8"}]
[:meta {:name "viewport" [:meta {:name "viewport"
:content "width=device-width,initial-scale=1,shrink-to-fit=no"}] :content "width=device-width,initial-scale=1,shrink-to-fit=no"}]
[:title (str "wan ijo" (when title (str " - " title)))] [:title (str "wan ijo" (when title (str " - " title)))]
(include-css "/css/app.css")] (include-css "/css/app.css")
(when devmode? (include-css "/css/devmode.css"))]
[:body [:body
[:section.grid [:section.grid
[:header [:header

Loading…
Cancel
Save