Skip to content

Commit

Permalink
Update database.py
Browse files Browse the repository at this point in the history
  • Loading branch information
michplunkett committed Dec 20, 2024
1 parent 74f20d2 commit e8535cb
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions OpenOversight/app/models/database.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import operator
import re
import time
import uuid
from datetime import date, datetime, time, timezone
from datetime import date, datetime
from datetime import time as dt_time
from datetime import timezone
from typing import List, Optional

from authlib.jose import JoseError, JsonWebToken
Expand Down Expand Up @@ -110,15 +113,15 @@ def to_dict(self):
]

for column in inspect(self).mapper.column_attrs:
if column.key in excluded or column.key.startsWith("_"):
if column.key in excluded or column.key.startswith("_"):
continue

value = getattr(self, column.key)
if isinstance(value, (date, datetime)):
data[column.key] = value.isoformat()
elif isinstance(value, date):
data[column.key] = value.strftime("%Y-%m-%d")
elif isinstance(value, time):
elif isinstance(value, dt_time):
data[column.key] = value.strftime("%I:%M %p")
else:
data[column.key] = value
Expand Down Expand Up @@ -163,7 +166,7 @@ def creator(cls):
return db.relationship("User", foreign_keys=[cls.created_by])


class Department(BaseModel, TrackUpdates):
class Department(BaseModel, TrackUpdates, Serializable):
__tablename__ = "departments"
id: Mapped[int] = db.Column(db.Integer, primary_key=True)
name: Mapped[str] = db.Column(
Expand Down Expand Up @@ -221,7 +224,7 @@ def remove_database_cache_entries(self, update_types: List[str]) -> None:
remove_database_cache_entries(self, update_types)


class Job(BaseModel, TrackUpdates):
class Job(BaseModel, TrackUpdates, Serializable):
__tablename__ = "jobs"

id: Mapped[int] = db.Column(db.Integer, primary_key=True)
Expand Down Expand Up @@ -250,7 +253,7 @@ def __str__(self):
return self.job_title


class Note(BaseModel, TrackUpdates):
class Note(BaseModel, TrackUpdates, Serializable):
__tablename__ = "notes"

id: Mapped[int] = db.Column(db.Integer, primary_key=True)
Expand All @@ -261,7 +264,7 @@ class Note(BaseModel, TrackUpdates):
officer = db.relationship("Officer", back_populates="notes")


class Description(BaseModel, TrackUpdates):
class Description(BaseModel, TrackUpdates, Serializable):
__tablename__ = "descriptions"

officer = db.relationship("Officer", back_populates="descriptions")
Expand All @@ -272,7 +275,7 @@ class Description(BaseModel, TrackUpdates):
)


class Officer(BaseModel, TrackUpdates):
class Officer(BaseModel, TrackUpdates, Serializable):
__tablename__ = "officers"

id: Mapped[int] = db.Column(db.Integer, primary_key=True)
Expand Down Expand Up @@ -404,7 +407,7 @@ def currently_on_force(self):
return "Uncertain"


class Salary(BaseModel, TrackUpdates):
class Salary(BaseModel, TrackUpdates, Serializable):
__tablename__ = "salaries"

id: Mapped[int] = db.Column(db.Integer, primary_key=True)
Expand Down Expand Up @@ -440,7 +443,7 @@ def year_repr(self) -> str:
return str(self.year)


class Assignment(BaseModel, TrackUpdates):
class Assignment(BaseModel, TrackUpdates, Serializable):
__tablename__ = "assignments"

id: Mapped[int] = db.Column(db.Integer, primary_key=True)
Expand Down Expand Up @@ -485,7 +488,7 @@ def start_date_or_max(self):
return self.start_date or date.max


class Unit(BaseModel, TrackUpdates):
class Unit(BaseModel, TrackUpdates, Serializable):
__tablename__ = "unit_types"

id: Mapped[int] = db.Column(db.Integer, primary_key=True)
Expand All @@ -504,7 +507,7 @@ def __repr__(self):
return f"<Unit: {self.description}>"


class Face(BaseModel, TrackUpdates):
class Face(BaseModel, TrackUpdates, Serializable):
__tablename__ = "faces"

id = db.Column(db.Integer, primary_key=True)
Expand Down Expand Up @@ -556,7 +559,7 @@ def __repr__(self):
return f"<Tag ID {self.id}: {self.officer_id} - {self.img_id}>"


class Image(BaseModel, TrackUpdates):
class Image(BaseModel, TrackUpdates, Serializable):
__tablename__ = "raw_images"

id = db.Column(db.Integer, primary_key=True)
Expand Down Expand Up @@ -655,7 +658,7 @@ def __repr__(self):
)


class Location(BaseModel, TrackUpdates):
class Location(BaseModel, TrackUpdates, Serializable):
__tablename__ = "locations"

id: Mapped[int] = db.Column(db.Integer, primary_key=True)
Expand Down Expand Up @@ -698,7 +701,7 @@ def __repr__(self):
return f"{self.city} {self.state}"


class LicensePlate(BaseModel, TrackUpdates):
class LicensePlate(BaseModel, TrackUpdates, Serializable):
__tablename__ = "license_plates"

id: Mapped[int] = db.Column(db.Integer, primary_key=True)
Expand All @@ -713,7 +716,7 @@ def validate_state(self, key, state):
return state_validator(state)


class Link(BaseModel, TrackUpdates):
class Link(BaseModel, TrackUpdates, Serializable):
__tablename__ = "links"

id: Mapped[int] = db.Column(db.Integer, primary_key=True)
Expand All @@ -731,18 +734,18 @@ def validate_url(self, key, url):
return url_validator(url)


class Incident(BaseModel, TrackUpdates):
class Incident(BaseModel, TrackUpdates, Serializable):
__tablename__ = "incidents"

id: Mapped[int] = db.Column(db.Integer, primary_key=True)
date: Mapped[date] = db.Column(db.Date, unique=False, index=True)
time: Mapped[time] = db.Column(db.Time, unique=False, index=True)
report_number: Mapped[str] = db.Column(db.String(50), index=True)
description: Mapped[str] = db.Column(db.Text(), nullable=True)
address_id: Mapped[int] = db.Column(
address_id = db.Column(
db.Integer, db.ForeignKey("locations.id", name="incidents_address_id_fkey")
)
address: Mapped[str] = db.relationship(
address = db.relationship(
"Location", backref=db.backref("incidents", cascade_backrefs=False)
)
license_plates = db.relationship(
Expand Down

0 comments on commit e8535cb

Please sign in to comment.