[guadec-web-regcfp/develop] Voting works



commit 6b7e429af2cd97f8f01a209bc20e664b52f7c9d5
Author: Patrick Uiterwijk <puiterwijk redhat com>
Date:   Thu May 21 16:16:09 2015 +0200

    Voting works

 app.js                       |    9 ++++-
 models/user.js               |    1 +
 routes/papers.js             |   79 +++++++++++++++++++++++++++++++++++++++++-
 views/papers/vote.hbs        |   45 +++++++++++++++++------
 views/papers/vote_submit.hbs |    3 ++
 5 files changed, 123 insertions(+), 14 deletions(-)
---
diff --git a/app.js b/app.js
index a928a71..b3bb457 100644
--- a/app.js
+++ b/app.js
@@ -31,7 +31,14 @@ var hbs = handlebars.create({
       text = text.replace(/(\r\n|\n|\r)/gm, '<br>');
       return new handlebars.handlebars.SafeString(text);
     },
-    has_permission: utils.has_permission
+    has_permission: utils.has_permission,
+    ifEqual: function(v1, v2, options) {
+      if(v1 == v2) {
+        return options.fn(this);
+      } else {
+        return options.inverse(this);
+      }
+    }
   }
 });
 app.set('views', path.join(__dirname, 'views'));
diff --git a/models/user.js b/models/user.js
index 750b555..d107e4c 100644
--- a/models/user.js
+++ b/models/user.js
@@ -8,6 +8,7 @@ module.exports = function(sequelize, DataTypes) {
     classMethods: {
       associate: function(models) {
         User.hasMany(models.Paper);
+        User.hasMany(models.PaperVote);
       }
     }
   });
diff --git a/routes/papers.js b/routes/papers.js
index d7ef26c..691be3b 100644
--- a/routes/papers.js
+++ b/routes/papers.js
@@ -96,11 +96,88 @@ router.all('/admin/vote', utils.require_permission('papers/vote'));
 router.get('/admin/vote', function(req, res, next) {
   Paper.findAll({include: [User, PaperVote]})
     .complete(function(err, papers) {
-      res.render('papers/vote', { papers: papers});
+      paper_info = [];
+      for(paper in papers) {
+        paper = papers[paper];
+        ppr = {
+          id: paper.id,
+          title: paper.title,
+          summary: paper.summary,
+          User: paper.User
+        };
+        for(vote in paper.PaperVotes) {
+          vote = paper.PaperVotes[vote];
+          if(vote.UserId == req.user.id)
+          {
+            ppr.vote = {
+              vote: vote.vote,
+              comment: vote.comment,
+              abstained: vote.abstained
+            };
+            if(ppr.vote.abstained) {
+              ppr.vote.vote = null;
+            }
+          }
+        }
+        paper_info.push(ppr);
+      }
+      res.render('papers/vote', { papers: paper_info,
+                                  voteOptions: [-2, -1, 0, 1, 2]});
     });
 });
 
 router.post('/admin/vote', function(req, res, next) {
+  errors = [];
+  saved = [];
+
+  for(key in req.body) {
+    if(key.substring(0, 5) == "vote_")
+    {
+      var id = key.substring(5);
+      var vote_val = req.body[key];
+      var abstained = false;
+      if(vote_val == 'A')
+      {
+        abstained = true;
+        vote_val = null;
+      }
+      var comment = req.body["comment_" + key];
+
+      PaperVote.findOne({ where: {
+        UserId: req.user.id,
+        PaperId: id
+      }}).then(function(vote) {
+        if(vote == null) {
+          console.log('NEW VOTE');
+          PaperVote
+            .create({
+              comment: comment,
+              vote: vote_val,
+              abstained: abstained,
+            })
+            .complete(function(err, vote) {
+              if(!!err) {
+                errors.push({id: id, err: err});
+              } else {
+                Paper.findOne({where: {id: id}}).then(function(paper) {
+                  paper.addPaperVote(vote);
+                });
+                req.user.addPaperVote(vote);
+                saved.push(id);
+              }
+            });
+        } else {
+          console.log('Updating: ' + vote);
+          vote.comment = comment;
+          vote.vote = vote_val;
+          vote.abstained = abstained;
+          vote.save();
+        }
+      });
+    }
+  }
+  res.render('papers/vote_submit', { errors: JSON.stringify(errors),
+                                     saved: JSON.stringify(saved) });
 });
 
 module.exports = router;
diff --git a/views/papers/vote.hbs b/views/papers/vote.hbs
index 65e27f3..f95a4d5 100644
--- a/views/papers/vote.hbs
+++ b/views/papers/vote.hbs
@@ -1,15 +1,36 @@
-{{description}} submitted papers:<br /><br />
+Vote on papers:
 
-You: "{{user}}"
+<form action="/papers/admin/vote" method="POST">
+<table border="1">
+<tr><th>Title</th><th>Author</th><th>Vote</th><th>Comment</th></tr>
+    {{#each papers}}
+        <tr>
+            <th><span title="{{this.summary}}">{{ this.title }}</span></th>
+            <td>{{ this.User.name }}</td>
+            <td>
+                <input type="radio" name="vote_{{this.id}}" id="vote_{{this.id}}_A" value="A"
+                {{#if this.vote.abstained}}
+                checked
+                {{/if}}
+                >
+                <label for="vote_{{this.id}}_A">Abstain</label>
+                | 
+                {{#each ../voteOptions}}
+                    <input type="radio" name="vote_{{../this.id}}" id="vote_{{../this.id}}_{{this}}" 
value="{{this}}"
+                    {{#ifEqual ../this.vote.vote this}}
+                        checked
+                    {{/ifEqual}}
+                    >
+                    <label for="vote_{{../this.id}}_{{this}}">{{this}}</label>
+                {{/each}}
+            </td>
+            <td>
+                <input type="text" name="comment_vote_{{this.id}}" value="{{this.vote.comment}}">
+            </td>
+        </tr>
+    {{/each}}
+</table>
 
-{{#each papers}}
-    <b>{{ this.title }}</b>
-    {{#if ../showAuthors}}
-      ({{ this.User.name }} &lt;{{ this.User.email }}&gt;)
-    {{/if}}
-    - 
-    {{ breaklines this.summary }}
+<input type="submit" value="Submit">
+</form>
 
-    <br />
-    <br />
-{{/each}}
diff --git a/views/papers/vote_submit.hbs b/views/papers/vote_submit.hbs
new file mode 100644
index 0000000..e8c7db9
--- /dev/null
+++ b/views/papers/vote_submit.hbs
@@ -0,0 +1,3 @@
+Votes saved.<br />
+Saved: {{saved}}<br />
+Errors: {{errors}}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]