Browse Source

add dates, week view

master
Mohamad Safadieh 2 months ago
parent
commit
9030e31af2
Signed by: mhmd <me@msafadieh.com> GPG Key ID: 2F3FDF2EAE1E3C36
7 changed files with 64 additions and 22 deletions
  1. +2
    -1
      .gitignore
  2. +13
    -2
      guineapigs/app.py
  3. +7
    -7
      guineapigs/database.py
  4. +5
    -0
      guineapigs/static/style.css
  5. +2
    -1
      guineapigs/templates/base.html
  6. +13
    -11
      guineapigs/templates/index.html
  7. +22
    -0
      guineapigs/utils.py

+ 2
- 1
.gitignore View File

@@ -1,3 +1,4 @@
.env
.vscode
__pycache__
*.sh
__pycache__

+ 13
- 2
guineapigs/app.py View File

@@ -4,6 +4,7 @@ import os
import random
from flask import Flask, make_response, render_template, redirect, request
from guineapigs.database import Database
from guineapigs.utils import split_dates, start_of_day_and_week

TITLE = os.environ["TITLE"]

@@ -50,9 +51,19 @@ def render(*args, **kwargs):
@check_cookie
def index():
older = bool(request.args.get("older"))
start_day, start_week = start_of_day_and_week(os.environ["TIMEZONE"])
foods = database.get_foods(start_week if older else start_day)
foods = split_dates(foods)
if foods:
date, entries = foods.popitem(last=False)
key = "today" if entries[0]["time"] >= start_day else date
foods[key] = entries
foods.move_to_end(key, last=False)

return render("index.html",
foods=database.get_foods(older),
entries=foods,
food_options=FOOD_OPTIONS)

@app.route("/setname", methods=["GET", "POST"])
@@ -87,7 +98,7 @@ def submit():
@app.route("/vitaminc")
@check_cookie
def vitaminc():
database.add_food("vitamin c 🧡", "", request.cookies["name"])
database.add_food("vitamin c 🌻", "", request.cookies["name"])
return redirect("/")

@app.route("/delete", methods=["POST"])


+ 7
- 7
guineapigs/database.py View File

@@ -28,13 +28,13 @@ class Database:
}
self.foods.insert_one(food)

def get_foods(self, older):
day_begin = datetime.now(self.timezone).replace(hour=0,
minute=0,
second=0,
microsecond=0)
filter_ = {"time": {"$gt": day_begin - timedelta(days=6) if older else day_begin}}
def get_foods(self, gt=None):
filter_ = {}
if gt:
filter_["time"] = {"$gt": gt}
return self.timezone_aware.find(filter_).sort("time", -1)

def delete_food(self, _id):
self.foods.delete_one({"_id": ObjectId(_id)})
self.foods.delete_one({"_id": ObjectId(_id)})

+ 5
- 0
guineapigs/static/style.css View File

@@ -0,0 +1,5 @@
@media only screen and (max-width: 576px) {
h3 {
font-size: 1.5rem;
}
}

+ 2
- 1
guineapigs/templates/base.html View File

@@ -5,12 +5,13 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="/static/bootstrap.min.css">
<link rel="stylesheet" href="/static/style.css">
<title>{{ title }}</title>
{% endblock %}
</head>
<body>
<nav class="navbar navbar-light bg-light">
<div class="container px-0">
<div class="container px-0 justify-content-center flex-column flex-sm-row justify-content-sm-between">
<a class="navbar-brand" href="/">{{ title }}</a>
{% block navbar %}{% endblock %}
</div>


+ 13
- 11
guineapigs/templates/index.html View File

@@ -9,9 +9,11 @@
<p class="mt-2 text-right">
Hi, {{request.cookies["name"]}}! <a href="/removename">Not you?</a>
</p>
<div class="list-group list-group-flush">
{% for food in foods %}
<div class="list-group-item d-flex w-100 justify-content-between">
{% for day, foods in entries.items() %}
<h3 class="mt-2">{{ day }}</h3>
<div class="list-group list-group-flush">
{% for food in foods %}
<div class="list-group-item d-flex w-100 justify-content-between px-0">
<div class="w-100">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1 align-self-center">{{ food["name"] }}</h5>
@@ -29,25 +31,25 @@
<input type="hidden" name="id" value="{{ food["_id"] }}">
<button type="submit" style="font-size: 1.8rem;text-decoration: none;" class="btn p-0 ml-1 btn-lg btn-link text-danger">&times;</button>
</form>
</div>
{% endfor %}
</div>
{% else %}
<h4 class="text-center mb-4">
no food yet &gt;:(
</h4>
<img src="/static/hangry.png" width="500px" class="mx-auto img-fluid" alt="Blurry image showing an Abyssian and American Guinea pig excited behind cage grids">
{% endfor %}
</div>
<img src="/static/hangry.png" width="500px" class="mx-auto img-fluid d-block mx-auto" alt="Blurry image showing an Abyssian and American Guinea pig excited behind cage grids">
{% endfor %}
<form class="text-center mt-3">
{% if not request.args.get("older") %}
<input type="hidden" name="older" value="1">
{% endif %}
<button type="submit" class="btn btn-primary">
{% if request.args.get("older") %}
today &rightarrow;
{% else %}
&leftarrow; last week
{% else %}
<button type="submit" class="btn btn-secondary">
today &rightarrow;
{% endif %}
</button>
</button>
</form>
{% endblock %}
{% block footer %}


+ 22
- 0
guineapigs/utils.py View File

@@ -0,0 +1,22 @@
from collections import OrderedDict
from datetime import datetime, timedelta
import pytz

def start_of_day_and_week(timezone):
day_begin = datetime.now(pytz.timezone(timezone)).replace(hour=0,
minute=0,
second=0,
microsecond=0)
week_begin = day_begin - timedelta(days=6)
return day_begin, week_begin

def split_dates(entries):
days = OrderedDict()

for entry in entries:
date = entry["time"].strftime("%A, %B %d").lower()
days.setdefault(date, [])
days[date].append(entry)

return days


Loading…
Cancel
Save