Anonymous avatar Anonymous committed 9609456

search

Comments (0)

Files changed (9)

foodoptimizr/__init__.py

 
 DEBUG = True
 SECRET_KEY = 'secret'
-SQLALCHEMY_DATABASE_URI = "sqlite:///foodoptimizer.db"
+SQLALCHEMY_DATABASE_URI = "postgresql://postgres@localhost/foodoptimizr"
 SQLALCHEMY_ECHO = False
 
 def create_app(settings=None):

foodoptimizr/forms.py

         if User.query.filter(User.email==field.data.lower()).count():
             raise ValidationError, "This email is already taken"
 
+
 class FoodForm(Form):
 
     description = TextField("Description", validators=[required()])

foodoptimizr/models.py

     id = db.Column(db.Integer, primary_key=True)
     user_id = db.Column(db.Integer, db.ForeignKey(User.id))
     entered_on = db.Column(db.Date, default=date.today)
-    day = db.Column(db.Enum("green", "red"))
+    day = db.Column(db.Enum("green", "red", name="day"))
     total_sins = db.Column(db.Float)
     weight = db.Column(db.Float)
     

foodoptimizr/templates/add_food.html

 {% block content %}
 
 <form method="POST" action="" class="span-12 append-12">
+    {{ form.hidden_tag() }}
     <fieldset>
-        <label>Description</label><br>
-        <input type="text" size="50">
+        {{ form.description.label }}<br>
+        {{ form.description(size=50) }}
     </fieldset>
 
     <fieldset>
         <legend>Sins</legend>
-        <label>Green days <input value="0" type="text" size="5"></label>
-        <label>Red days <input value="0" type="text" size="5"></label>
+        <label>Green days {{ form.green_sins(size=5) }}</label>
+        <label>Red days {{ form.red_sins(size=5) }}</label>
     </fieldset>
 
     <fieldset>
         <legend>Healthy extra</legend>
-        <label>Healthy extra option A <input type="checkbox"></label><br>
-        <label>Healthy extra option B (green days) <input type="checkbox"></label><br>
-        <label>Healthy extra option B (red days) <input type="checkbox"></label><br>
+        <label>Healthy extra option A {{ form.healthy_extra_a }}</label><br>
+        <label>Healthy extra option B (green days) {{ form.healthy_extra_b_green }}</label><br>
+        <label>Healthy extra option B (red days) {{ form.healthy_extra_b_red }}</label><br>
     </fieldset>
     <div class="submit">
-        <input type="submit" value="Enter">
+        {{ form.submit }}
     </div>
 </form>
 {% endblock %}

foodoptimizr/templates/index.html

 <div class="span-24 navigation calendar">
     <ul>
         <li class="previous">
-        <a href="">&lt;&lt; {{ last_month.strftime('%B %Y') }}</a>
+        <a href="{{ url_for('main.index', month=last_month.month, year=last_month.year) }}">&lt;&lt; {{ last_month.strftime('%B %Y') }}</a>
         </li>
         <li class="current selected">
-        <a href-"">{{ today.strftime('%B %Y') }}</a>
+        <a href-"{{ url_for('main.index', month=today.month, year=today.year) }}">{{ today.strftime('%B %Y') }}</a>
+
         </li>
         <li class="following">
-        <a href="">{{ next_month.strftime('%B %Y') }} &gt;&gt;</a>
+        <a href="{{ url_for('main.index', month=next_month.month, year=next_month.year) }}">{{ next_month.strftime('%B %Y') }} &gt;&gt;</a>
         </li>
     </ul>
 </div>
         </tr>
     </thead>
     <tbody>
-            {% for i in range(20) %}
-            <tr class="{{ ('red', 'green')|random }}">
-                <td><a href="">{{ 20 - i }}.11.2010</a></td>          
-                <td>{{ range(15)|random }}</td>
-                <td>120</td>
+        {% for entry in entries %}
+        <tr class="{{ entry.day }}">
+            <td><a href="">{{  entry.entered_on.strftime('%d.%m.%Y') }}</a></td>          
+            <td>{{ entry.total_sins }}</td>
+            <td>{{ entry.weight }}</td>
             </tr>
+        {% else %}
+        <tr>
+            <td colspan="3" align=center>You haven't made any entries yet for this month.</td>
+        </tr>
             {% endfor %}
     </tbody>
 </table>
 
-<div class="navigation calendar">
+<div class="span-24 navigation calendar">
     <ul>
         <li class="previous">
-        	<a href="">&lt;&lt; October 2010</a>
+        <a href="{{ url_for('main.index', month=last_month.month, year=last_month.year) }}">&lt;&lt; {{ last_month.strftime('%B %Y') }}</a>
         </li>
         <li class="current selected">
-        <a href-"">November 2010</a>
+        <a href-"{{ url_for('main.index', month=today.month, year=today.year) }}">{{ today.strftime('%B %Y') }}</a>
+
         </li>
         <li class="following">
-        	<a href="">December 2010 &gt;&gt;</a>
+        <a href="{{ url_for('main.index', month=next_month.month, year=next_month.year) }}">{{ next_month.strftime('%B %Y') }} &gt;&gt;</a>
         </li>
     </ul>
 </div>
 
+
 {% endblock %}

foodoptimizr/templates/search.html

+{% block content %}
+<table>
+    {% for food in foods %}
+    <tr>
+        <td>{{ food.description }}</td>
+        <td>{{ food.green_sins }}</td>
+        <td>{{ food.red_sins }}</td>
+    </tr>
+    {% endfor %}
+</table>
+{% endblock %}

foodoptimizr/templates/signup.html

     {{ form.hidden_tag() }}
     <div class="span-24">
     <fieldset class="span-12">
-        {{ form.email.label }}<br>
+        {{ form.email.label }}
         {{ form.email(size=40) }}<br>
     </fieldset>
     <fieldset class="span-12">

foodoptimizr/views/main.py

 from datetime import date
 from dateutil.relativedelta import relativedelta
 
-from flask import Module, render_template, g
+from flask import Module, render_template, g, flash, abort
 
 from foodoptimizr.decorators import login_required
+from foodoptimizr.models import Food, db
+from foodoptimizr.forms import FoodForm
 
 main = Module(__name__)
 
     return render_template("index.html", 
                            today=today,
                            last_month=last_month,
-                           next_year=next_year,
+                           next_month=next_month,
                            entries=entries)
 
 
-@main.route("/add-food/")
+@main.route("/add-food/", methods=("GET", "POST"))
 def add_food():
-    return render_template("add_food.html")
+    form = FoodForm()
+    if form.validate_on_submit():
+        food = Food()
+        form.populate_obj(food)
 
+        db.session.add(food)
+        db.session.commit()
+
+        flash("%s added" % food.description)
+        return redirect(url_for("main.index"))
+    return render_template("add_food.html", form=form)
+
+
+@main.route("/search/")
+def search():
+    q = request.args.get('q')
+
+    if not q:
+        abort(404)
+
+    foods = Food.query.search(q).limit(30)
+
+    if request.is_xhr:
+        results = [(f.id, f.description) for f in foods])
+        return jsonify(results=results)
+   
+    return render_template("search.html", foods=foods)
 
 @main.route("/add-entry/")
 def add_entry():
+CREATE OR REPLACE FUNCTION MONTH(DATE) RETURNS INTEGER
+AS $$
+      SELECT EXTRACT(MONTH FROM $1)::INTEGER;
+$$ LANGUAGE SQL IMMUTABLE;
+
+
+CREATE OR REPLACE FUNCTION YEAR(DATE) RETURNS INTEGER
+AS $$
+      SELECT EXTRACT(YEAR FROM $1)::INTEGER;
+$$ LANGUAGE SQL IMMUTABLE;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.