forked from civictechdc/us-congress-pizza-flag-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OfficeActions.py
54 lines (46 loc) · 1.66 KB
/
OfficeActions.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
from models import OfficeModel
from flask_sqlalchemy import sqlalchemy
from config import db
import uuid
class OfficeActions():
# Table actions:
@classmethod
def create(cls, usa_state: str, office_code: str):
new_office = OfficeModel(usa_state=usa_state, office_code=office_code)
db.session.add(new_office)
db.session.commit()
return new_office
@ classmethod
def delete(cls):
OfficeModel.query.delete()
db.session.commit()
@ classmethod
def get_states(cls):
query = db.session.query(OfficeModel.usa_state.distinct().label("usa_state"))
return [row.usa_state for row in query.all()]
@ classmethod
def get_offices_by_state(cls, usa_state: str):
offices = OfficeModel.query.filter(OfficeModel.usa_state == usa_state)
return [row.office_code for row in offices]
# Unneccesary method ???
@ classmethod
def get_offices(cls):
offices = OfficeModel.query.all()
return [
{"usa_state": office.usa_state,
"office_code": office.office_code
}
for office in offices]
# Unneccesary method ???
@ classmethod
def get_by_code(cls, office_code: str):
return OfficeModel.query.filter(OfficeModel.office_code == office_code).first()
# Unneccesary method ??? (no uuid currently)
@ classmethod
def update_office(cls, uuid, usa_state, office_number , office_code):
office = cls.get_office_by_uuid(uuid)
office.office_number = office_number
office.usa_state = usa_state
office.office_code = office_code
db.session.commit()
return office