models.py (3529B) download
1from datetime import datetime, timedelta
2from email.policy import default
3from typing import Optional
4from flask_login import UserMixin
5
6from .app import db, login_manager
7
8
9class User(db.Model, UserMixin):
10 id: int = db.Column(db.Integer, primary_key=True)
11 admin: bool = db.Column(db.Boolean, nullable=False, default=False)
12 email: str = db.Column(db.String(120), unique=True, nullable=False)
13 name: str = db.Column(db.String(20), unique=True, nullable=False)
14 password: str = db.Column(db.String(60), nullable=False)
15 image_file: str = db.Column(db.String(20), nullable=False,
16 default='default.jpg')
17 phone: str = db.Column(db.Text, nullable=False)
18 address: Optional[str] = db.Column(db.Text)
19
20 contact: Optional[int] = db.Column(
21 db.Integer, db.ForeignKey('user.id')) # set if user
22
23 def contact_class(self):
24 return User.query.filter_by(id=self.contact).first()
25
26
27class Trap(db.Model):
28 id: int = db.Column(db.Integer, primary_key=True)
29 token: str = db.Column(db.String(16), unique=True, nullable=False)
30 owner: Optional[int] = db.Column(db.Integer, db.ForeignKey('user.id'))
31 owned_date: Optional[datetime] = db.Column(db.DateTime)
32 name: str = db.Column(db.Text, nullable=False, default='n/a')
33
34 last_status: datetime = db.Column(db.DateTime, nullable=False)
35 caught: bool = db.Column(db.Boolean, nullable=False, default=False)
36 battery: int = db.Column(db.Integer, nullable=False, default=0)
37 charging: bool = db.Column(db.Boolean, nullable=False, default=False)
38 temperature: int = db.Column(db.Integer, nullable=False, default=0)
39 location_search: bool = db.Column(db.Boolean, nullable=False, default=True)
40 location_searching: bool = db.Column(
41 db.Boolean, nullable=False, default=True)
42 location_acc: float = db.Column(db.Float, nullable=False, default=0)
43 location_lat: Optional[float] = db.Column(db.Float)
44 location_lon: Optional[float] = db.Column(db.Float)
45 location_satellites: Optional[int] = db.Column(db.Integer)
46
47 def owner_class(self) -> Optional[User]:
48 return User.query.get(self.owner)
49
50 def offline(self):
51 return datetime.now() - self.last_status > timedelta(minutes=10)
52
53 def to_json(self):
54 owner = self.owner_class()
55 owner_name = owner.name if owner else 'n/a'
56
57 return dict(
58 id=self.id,
59 name=self.name,
60 offline=self.offline(),
61 locationSearch=self.location_search,
62 locationSearching=self.location_searching,
63 latitude=self.location_lat,
64 longitude=self.location_lon,
65 accuracy=round(self.location_acc, 1),
66 satellites=self.location_satellites,
67 activated=self.caught,
68 owner=owner_name,
69 battery=self.battery,
70 charging=self.charging,
71 temperature=self.temperature,
72 lastStatus=self.last_status.strftime('%d-%m-%y %H:%M'),
73 ownedDate=self.owned_date.strftime(
74 '%d-%m-%y %H:%M') if self.owned_date else '-'
75 )
76
77
78class Statistic(db.Model):
79 id: int = db.Column(db.Integer, primary_key=True)
80 user: int = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
81 trap: int = db.Column(db.Integer, db.ForeignKey('trap.id'), nullable=False)
82 date: datetime = db.Column(db.DateTime, nullable=False)
83
84
85@login_manager.user_loader
86def load_user(user_id: int) -> User:
87 return User.query.get(user_id)