diff --git a/OpenOversight/app/models/database.py b/OpenOversight/app/models/database.py index 00fb10922..ccd93ee79 100644 --- a/OpenOversight/app/models/database.py +++ b/OpenOversight/app/models/database.py @@ -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 @@ -110,7 +113,7 @@ 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) @@ -118,7 +121,7 @@ def to_dict(self): 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 @@ -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( @@ -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) @@ -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) @@ -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") @@ -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) @@ -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) @@ -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) @@ -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) @@ -504,7 +507,7 @@ def __repr__(self): return f"" -class Face(BaseModel, TrackUpdates): +class Face(BaseModel, TrackUpdates, Serializable): __tablename__ = "faces" id = db.Column(db.Integer, primary_key=True) @@ -556,7 +559,7 @@ def __repr__(self): return f"" -class Image(BaseModel, TrackUpdates): +class Image(BaseModel, TrackUpdates, Serializable): __tablename__ = "raw_images" id = db.Column(db.Integer, primary_key=True) @@ -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) @@ -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) @@ -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) @@ -731,7 +734,7 @@ 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) @@ -739,10 +742,10 @@ class Incident(BaseModel, TrackUpdates): 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(