Commits

Julio Biason  committed 4a9f2c8

did the roadwork to display current selections

  • Participants
  • Parent commits 76471fd

Comments (0)

Files changed (3)

File aweek/main/views.py

 from flask.views import MethodView
 
 from ..model import Picks
+from ..model import User
 
 import logging
+import datetime
 
 main = Blueprint('main',
                  __name__,
 
 class ListView(MethodView):
     def get(self):
-        objs = Picks.objects.all()  # for now, need to filter
-        return render_template('index.html', objs=objs)
+        # previous selections
+        today = datetime.datetime.utcnow()
+        last_4_weeks = today - datetime.timedelta(days=7 * 4)
+
+        query = {'date': {'$gte': last_4_weeks}}
+
+        objs = Picks.objects(query).order_by('+date')
+
+        # people who will pick it next
+        future = []
+        all_users = User.objects.all()
+
+        gather = False
+        for user in all_users:
+            if gather:
+                future.append(user)
+
+            if len(future) >= 4:
+                break
+
+            if user._id == objs.user:
+                gather = True
+
+        if len(future) < 4:
+            needed = 4 - len(future)
+            future.extend(all_users[:needed])
+
+        return render_template('index.html', previous=objs, next=future)
 
 
 main.add_url_rule('/', view_func=ListView.as_view('list'))

File aweek/model.py

 # -*- encoding: utf-8 -*-
 
 from mongoengine import Document
-from mongoengine import EmbeddedDocument
-from mongoengine import EmbeddedDocumentField
 from mongoengine import DateTimeField
 from mongoengine import StringField
 from mongoengine import ListField
+from mongoengine import ReferenceField
 
 
-class Animal(EmbeddedDocument):
+class Animal(Document):
     genus = StringField()
     species = StringField()
     date = DateTimeField()
 
 
-class User(EmbeddedDocument):
+class User(Document):
     name = StringField()
     joined = DateTimeField()
-    future_options = ListField(EmbeddedDocumentField(Animal))
+    future_options = ListField(ReferenceField(Animal))
 
 
 class Picks(Document):
     date = DateTimeField()
-    who = EmbeddedDocumentField(User)
-    animal = EmbeddedDocumentField(Animal)
+    who = ReferenceField(User)
+    animal = ReferenceField(Animal)

File aweek/templates/index.html

-{% extends "base.html" %}
+{% extends "base.html" %}
+
+{% block content %}
+    {% for old in previous %}
+        <div class="distance-{{ 4-loop.index|abs }}">
+            {{ old.animal }} <i>{{ old.who }}</i>
+        </div>
+    {% endfor %}
+
+    {% for record in future %}
+        <div class="distance-{{ loop.index }}">
+            <i>record.name</i>
+        </div>
+    {% endfor %}
+{% endblock %}