<commit_before>(ns cadence.views.training
  (:require [cadence.views.common :as common]
            [cadence.model :as model]
            [cadence.model.flash :as flash]
            [cadence.pattern-recognition :as patrec]
            [cadence.model.validators :as is-valid]
            [noir.validation :as vali]
            (noir [response :as resp]
                  [session :as sess]))
  (:use noir.core
        clojure.walk
        hiccup.core
        hiccup.page-helpers))

(defpage training "/user/training" []
  (common/layout
    [:div.page-header
     [:h1 "Training"]]
    [:p "If you don't know what this is for please checkout the "
     (link-to "/#training" "blurb on the front page") "."]
    (if-let [phrase-doc (or (sess/get :training-phrase)
                            (get
                              (sess/put! :possible-training-phrase
                                         (model/get-phrase (:_id (model/get-auth))
                                                           false))
                              "possible-training-phrase"))]
      ; Found a training phrase in the session or grabbed a new one from the
      ; database.
      (let [phrase (:phrase phrase-doc)]
        (html
          [:h2 "Let's get down to business!"]
          [:div#train_well.well.container-fluid
           [:p.help "Simply type the following phrase in the form repeatedly
                    until I tell you to stop."]
           ; Show a phrase to type in and an input field to for the user to copy
           ; it.
           (common/phrase-fields "trainer" phrase)
           [:div#feedback.row-fluid]
           [:div#completion.row-fluid
            (let [trcount (count (patrec/kept-cadences))]
              (html
                [:div
                 {:class (str "progress progress-success progress-striped
                              span10"
                              (when (>= trcount @patrec/training-min)
                                " active"))}
                 [:div.bar
                  {:style (str "width: "
                               (min 100
                                    (* 100.0
                                       (/ trcount @patrec/training-min)))
                               "%;")}]]
                (if (>= trcount @patrec/training-min)
                  [:div.span2 [:a.btn.btn-large.btn-success
                               {:href "/user/profile"} "Already done!"]]
                  [:div.span2 [:a.btn.btn-large.disabled
                               {:href "#"} "Complete"]])))]]))
      ; If there is no training-phrase in the session and the current user has
      ; no untrained phrases then the user cannot do more training.
      (common/alert :info [:h2 "You are already trained!"]
                    (html [:p "There is a limited number of phrases and you
                              have done the training for all of them."]
                          [:p "Currently, there is no support for redoing your
                              training for a given phrase, but hopefully there
                              will be soon."]) false))))

(defpage post-training [:post "/user/training"] {:as unkeyed-cad}
  ; I like maps with keyword keys
  (let [cadence (keywordize-keys unkeyed-cad)]
    ; When training-phrase is unset, set it with the value of
    ; possible-training-phrase (set when /user/training is accessed via GET). If
    ; the client somehow manipulates this the worst case scenario is that
    ; :training-phrase will be null wich should be caught by the cadence?
    ; validator.
    (when (nil? (sess/get :training-phrase))
      (sess/put! :training-phrase (let [tp (sess/get :possible-training-phrase)]
                                    (sess/remove! :possible-training-phrase)
                                    tp)))
    ; Ensure that the supplied data is really what I want it to be and not some
    ; glitch or fabrication. (This uses noir.validation)
    (if (is-valid/cadence? cadence false)
      (if (patrec/keep-cadence cadence)
        (let [trnmin @patrec/training-min
              kept (patrec/kept-cadences)
              trcount (count kept)
              done (>= trcount trnmin)]
          (resp/json {:success true
                      :done done
                      :session kept
                      :progress (* 100.0 (/ trcount trnmin))}))
        (resp/json {:success true
                    :done false
                    :progress 0}))
      ; If bad data was received then tell the client we were not successful
      (resp/json {:success false
                  ; Grab errors put on the cadence field using noir.validation
                  :errors (vali/get-errors :cadence)
                  :progress 0}))))

(defpage auth "/user/auth" []
  (common/layout
    (let [phrase (model/get-phrase (:_id (model/get-auth)) true)]
      (common/phrase-fields "authenticate" phrase))))

(defpage auth-check [:post "/user/auth"] {:as unkeyed-cadence}
   (let [cadence (keywordize-keys unkeyed-cadence)]
     (if (is-valid/cadence? cadence true)
       (if (patrec/cadence-matches? (model/get-classifier (:_id (model/get-auth))
                                                          (:phrase cadence))
                                    cadence)
         (flash/put! "Would you look at that, you're you!")
         (flash/put! "Are you a sneaking spy?"))
       (flash/put! "There was an error with the cadence you submitted."))))<commit_msg>Adds markup and responses for auth.<commit_after>(ns cadence.views.training
  (:require [cadence.views.common :as common]
            [cadence.model :as model]
            [cadence.model.flash :as flash]
            [cadence.pattern-recognition :as patrec]
            [cadence.model.validators :as is-valid]
            [noir.validation :as vali]
            (noir [response :as resp]
                  [session :as sess]))
  (:use noir.core
        clojure.walk
        hiccup.core
        hiccup.page-helpers))

(defpage training "/user/training" []
  (common/layout
    [:div.page-header
     [:h1 "Training"]]
    [:p "If you don't know what this is for please checkout the "
     (link-to "/#training" "blurb on the front page") "."]
    (if-let [phrase-doc (or (sess/get :training-phrase)
                            (get
                              (sess/put! :possible-training-phrase
                                         (model/get-phrase (:_id (model/get-auth))
                                                           false))
                              "possible-training-phrase"))]
      ; Found a training phrase in the session or grabbed a new one from the
      ; database.
      (let [phrase (:phrase phrase-doc)]
        (html
          [:h2 "Let's get down to business!"]
          [:div#train_well.well.container-fluid
           [:p.help "Simply type the following phrase in the form repeatedly
                    until I tell you to stop."]
           ; Show a phrase to type in and an input field to for the user to copy
           ; it.
           (common/phrase-fields "trainer" phrase)
           [:div#feedback.row-fluid]
           [:div#completion.row-fluid
            (let [trcount (count (patrec/kept-cadences))]
              (html
                [:div
                 {:class (str "progress progress-success progress-striped
                              span10"
                              (when (>= trcount @patrec/training-min)
                                " active"))}
                 [:div.bar
                  {:style (str "width: "
                               (min 100
                                    (* 100.0
                                       (/ trcount @patrec/training-min)))
                               "%;")}]]
                (if (>= trcount @patrec/training-min)
                  [:div.span2 [:a.btn.btn-large.btn-success
                               {:href "/user/profile"} "Already done!"]]
                  [:div.span2 [:a.btn.btn-large.disabled
                               {:href "#"} "Complete"]])))]]))
      ; If there is no training-phrase in the session and the current user has
      ; no untrained phrases then the user cannot do more training.
      (common/alert :info [:h2 "You are already trained!"]
                    (html [:p "There is a limited number of phrases and you
                              have done the training for all of them."]
                          [:p "Currently, there is no support for redoing your
                              training for a given phrase, but hopefully there
                              will be soon."]) false))))

(defpage post-training [:post "/user/training"] {:as unkeyed-cad}
  ; I like maps with keyword keys
  (let [cadence (keywordize-keys unkeyed-cad)]
    ; When training-phrase is unset, set it with the value of
    ; possible-training-phrase (set when /user/training is accessed via GET). If
    ; the client somehow manipulates this the worst case scenario is that
    ; :training-phrase will be null wich should be caught by the cadence?
    ; validator.
    (when (nil? (sess/get :training-phrase))
      (sess/put! :training-phrase (let [tp (sess/get :possible-training-phrase)]
                                    (sess/remove! :possible-training-phrase)
                                    tp)))
    ; Ensure that the supplied data is really what I want it to be and not some
    ; glitch or fabrication. (This uses noir.validation)
    (if (is-valid/cadence? cadence false)
      (if (patrec/keep-cadence cadence)
        (let [trnmin @patrec/training-min
              kept (patrec/kept-cadences)
              trcount (count kept)
              done (>= trcount trnmin)]
          (resp/json {:success true
                      :done done
                      :session kept
                      :progress (* 100.0 (/ trcount trnmin))}))
        (resp/json {:success true
                    :done false
                    :progress 0}))
      ; If bad data was received then tell the client we were not successful
      (resp/json {:success false
                  ; Grab errors put on the cadence field using noir.validation
                  :errors (vali/get-errors :cadence)
                  :progress 0}))))

(defpage auth "/user/auth" []
  (common/layout
    (let [phrase (:phrase (model/get-phrase (:_id (model/get-auth)) true))]
      (html
        [:div.page-header [:h1 "Authencticate"]]
        [:p "If you don't know what this is for please checkout the "
         (link-to "/#auth" "blurb on the front page") "."]
        [:div#auth_well.well.container-fluid
         (common/phrase-fields "authenticate" phrase)
         [:div#feedback.row-fluid]]))))

(defpage auth-check [:post "/user/auth"] {:as unkeyed-cadence}
   (let [cadence (keywordize-keys unkeyed-cadence)]
     (if (is-valid/cadence? cadence true)
       (if (patrec/cadence-matches? (model/get-classifier (:_id (model/get-auth))
                                                          (:phrase cadence))
                                    cadence)
         (resp/json {:success true
                     :conclusive true
                     :legit true})
         (resp/json {:sucess true
                     :conclusive true
                     :legit false}))
       (resp/json {:success false
                   :conclusive false
                   :legit false}))))
{'fraction_non_alphanumeric': 0.10647053673058401, 'fraction_numerical': 0.00358987829437002, 'mean_word_length': 1.4616379310344827, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2951233', 'n_tokens_mistral': 3141, 'n_tokens_neox': 2951, 'n_words': 955}