forked from OneOneFour/ICHack19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
78 lines (61 loc) · 2.67 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import requests
import json
from flask_login import UserMixin
from main import mongo_db, app, login
from datetime import datetime
from edmam_wrapper import receipe_lookup
from werkzeug.security import generate_password_hash, check_password_hash
import math
class User(UserMixin, mongo_db.Document):
username = mongo_db.StringField(required=True, unique=True)
first_name = mongo_db.StringField(required=True)
last_name = mongo_db.StringField(required=True)
password = mongo_db.StringField()
email = mongo_db.EmailField(required=True)
def set_password(self, password):
self.password = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password, password)
def __repr__(self):
return f"User {self.username}"
def get_distance(a, b):
R = 6371
delta_lat = (a[0] - b[0]) * (math.pi / 180)
delta_long = (a[1] - b[1]) * (math.pi / 180)
a = math.sin(delta_lat / 2) ** 2 + math.cos(a[0]) * math.cos(b[0]) * (math.sin(delta_long / 2) ** 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
return R * c
class Food(mongo_db.Document):
name = mongo_db.StringField(required=True)
barcode = mongo_db.IntField()
country_of_origin = mongo_db.PointField()
expiry_advice = mongo_db.StringField()
packaging = mongo_db.FloatField()
co2_emission_base = mongo_db.FloatField()
FOOD_EMISSION_SITE = "https://opendata.socrata.com/resource/8nz9-yn2p.json"
def save(self, *args, **kwargs):
# Trawl site for info if it is available
header = {'X-App-Token': app.config['SOCRATA_APP_TOKEN']}
params = {'food': self.name}
resp = requests.get(self.FOOD_EMISSION_SITE, params=params, headers=header)
if resp.status_code == 200:
food_data = json.loads(resp.text)
if len(food_data) > 0:
print(self.co2_emission_base)
self.co2_emission_base = float(food_data[0]["grams_co2e_per_serving"])
super(Food, self).save(*args, **kwargs)
def get_recipes(self):
return receipe_lookup(self.name)
class UserFood(mongo_db.Document):
food = mongo_db.ReferenceField(Food, required=True, reverse_delete_rule=mongo_db.DENY)
expiry = mongo_db.DateTimeField(required=True)
owner = mongo_db.ReferenceField(User, required=True, reverse_delete_rule=mongo_db.CASCADE)
def __str__(self):
return f"{self.name} expires on {self.expiry}"
def get_expiry_time(self):
return (self.expiry - datetime.now()).days
def has_expired(self):
return (self.expiry - datetime.now()).days < 0
@login.user_loader
def load_user(id):
return User.objects.get(id = id)