Skip to content

Commit

Permalink
Officer Model Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
DMalone87 committed Mar 10, 2024
1 parent b87dbf4 commit 7228ea9
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 63 deletions.
2 changes: 2 additions & 0 deletions backend/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from .models.attachment import *
from .models.perpetrator import *
from .models.officer import *
from .models.employment import *
from .models.accusation import *
from .models.participant import *
from .models.tag import *
from .models.result_of_stop import *
Expand Down
17 changes: 0 additions & 17 deletions backend/database/models/_assoc_tables.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .. import db
from backend.database.models.officer import Rank


incident_agency = db.Table(
Expand All @@ -17,19 +16,3 @@
primary_key=True),
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True)
)

perpetrator_officer = db.Table(
'perpetrator_officer',
db.Column('perpetrator_id', db.Integer, db.ForeignKey('perpetrator.id'),
primary_key=True),
db.Column('officer_id', db.Integer, db.ForeignKey('officer.id'),
primary_key=True),
db.Column('accuser_id', db.Integer, db.ForignKey('user.id'),
primary_key=True),
db.Column('date_created', db.Text),
db.Column('basis', db.Text),
db.Column('attachments',
db.relationship("Attachment", backref="accusation"))
# Can we reuse the attachements Table here?

)
3 changes: 1 addition & 2 deletions backend/database/models/accusation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ class Accusation(db.Model):
id = db.Column(db.Integer, primary_key=True)
perpetrator_id = db.Column(db.Integer, db.ForeignKey("perpetrator.id"))
officer_id = db.Column(db.Integer, db.ForeignKey("officer.id"))
accuser_id = db.Column(db.Integer, db.ForeignKey("accuser.id"))
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
date_created = db.Column(db.Text)
basis = db.Column(db.Text)

attachments = db.relationship("Attachment", backref="accusation")
perpetrator = db.relationship("Perpetrator", back_populates="suspects")
officer = db.relationship("Officer", back_populates="accusations")
accuser = db.relationship("User", back_populates="accusations")

def __repr__(self):
return f"<Employment {self.id}>"
1 change: 0 additions & 1 deletion backend/database/models/agency.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import enum
from backend.database.models._assoc_tables import agency_officer
from .. import db


Expand Down
16 changes: 15 additions & 1 deletion backend/database/models/employment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import enum
from .. import db
from backend.database.models.officer import Rank


class Rank(str, enum.Enum):
# TODO: Is this comprehensive?
TECHNICIAN = "TECHNICIAN"
OFFICER = "OFFICER"
DETECTIVE = "DETECTIVE"
CORPORAL = "CORPORAL"
SERGEANT = "SERGEANT"
LIEUTENANT = "LIEUTENANT"
CAPTAIN = "CAPTAIN"
DEPUTY = "DEPUTY"
CHIEF = "CHIEF"
COMMISSIONER = "COMMISSIONER"


class Employment(db.Model):
Expand Down
39 changes: 5 additions & 34 deletions backend/database/models/officer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,6 @@
from .. import db


class Rank(str, enum.Enum):
# TODO: Is this comprehensive?
TECHNICIAN = "TECHNICIAN"
OFFICER = "OFFICER"
DETECTIVE = "DETECTIVE"
CORPORAL = "CORPORAL"
SERGEANT = "SERGEANT"
LIEUTENANT = "LIEUTENANT"
CAPTAIN = "CAPTAIN"
DEPUTY = "DEPUTY"
CHIEF = "CHIEF"


class State(str, enum.Enum):
AL = "AL"
AK = "AK"
Expand Down Expand Up @@ -72,8 +59,8 @@ class State(str, enum.Enum):
class StateID(db.Model):
"""
Represents a Statewide ID that follows an offcier even as they move between
law enforcement agencies. for an officer. For example, in New York, this
would be the Tax ID Number.
law enforcement agencies. For example, in New York, this would be
the Tax ID Number.
"""
id = db.Column(db.Integer, primary_key=True)
officer_id = db.Column(
Expand All @@ -86,24 +73,6 @@ def __repr__(self):
return f"<StateID {self.id}>"


class Employment(db.Model):
id = db.Column(db.Integer, primary_key=True)
officer_id = db.Column(db.Integer, db.ForeignKey("officer.id"))
agency_id = db.Column(db.Integer, db.ForeignKey("agency.id"))
earliest_employment = db.Column(db.Text)
latest_employment = db.Column(db.Text)
badge_number = db.Column(db.Text)
unit = db.Column(db.Text)
highest_rank = db.Column(db.Enum(Rank))
currently_employed = db.Column(db.Boolean)

officer = db.relationship("Officer", back_populates="known_employers")
agency = db.relationship("Agency", back_populates="known_officers")

def __repr__(self):
return f"<Employment {self.id}>"


class Officer(db.Model):
id = db.Column(db.Integer, primary_key=True) # officer id
first_name = db.Column(db.Text)
Expand All @@ -113,7 +82,9 @@ class Officer(db.Model):
ethnicity = db.Column(db.Text)
gender = db.Column(db.Text)
date_of_birth = db.Column(db.Date)
known_employers = db.relationship("Employment")
known_employers = db.relationship("Employment", back_populates="officer")
accusations = db.relationship("Accusation", back_populates="officer")
state_ids = db.relationship("StateID", backref="officer")

def __repr__(self):
return f"<Officer {self.id}>"
4 changes: 2 additions & 2 deletions backend/database/models/perpetrator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from backend.database.models._assoc_tables import perpetrator_officer
from backend.database.models.officer import Rank, State
from backend.database.models.officer import State
from backend.database.models.employment import Rank
from .. import db


Expand Down
3 changes: 3 additions & 0 deletions backend/database/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class User(db.Model, UserMixin, CrudMixin):
"PartnerMember", back_populates="user", lazy="select")
member_of = association_proxy("partner_association", "partner")

# Officer Accusations
accusations = db.relationship("Accusation", backref="user")

def verify_password(self, pw):
return bcrypt.checkpw(pw.encode("utf8"), self.password.encode("utf8"))

Expand Down
5 changes: 3 additions & 2 deletions backend/routes/officers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
from backend.mixpanel.mix import track_to_mp
from mixpanel import MixpanelException
from backend.database.models.user import UserRole
from backend.database.models.employment import Employment
from flask import Blueprint, abort, request
from flask_jwt_extended.view_decorators import jwt_required
from pydantic import BaseModel

from ..database import Officer, db, agency_officer
from ..database import Officer, Employment, db
from ..schemas import (
officer_orm_to_json,
validate,
Expand Down Expand Up @@ -63,7 +64,7 @@ def search_officer():
if body.badgeNumber:
officer_ids = [
result.officer_id for result in db.session.query(
agency_officer
Employment
).filter_by(badge_number=body.badgeNumber).all()
]
query = Officer.query.filter(Officer.id.in_(officer_ids)).all()
Expand Down
19 changes: 16 additions & 3 deletions backend/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
from .database.models.partner import Partner, PartnerMember, MemberRole
from .database.models.incident import Incident, SourceDetails
from .database.models.agency import Agency
from .database.models.officer import Officer
from .database.models.officer import Officer, StateID
from .database.models.employment import Employment
from .database.models.accusation import Accusation
from .database.models.investigation import Investigation
from .database.models.legal_case import LegalCase
from .database.models.attachment import Attachment
Expand Down Expand Up @@ -164,7 +166,10 @@ def schema_create(model_type: DeclarativeMeta, **kwargs) -> ModelMetaclass:

_BaseCreatePartnerSchema = schema_create(Partner)
_BaseCreateIncidentSchema = schema_create(Incident)
CreateOfficerSchema = schema_create(Officer)
_BaseCreateOfficerSchema = schema_create(Officer)
CreateStateIDSchema = schema_create(StateID)
CreateEmploymentSchema = schema_create(Employment)
CreateAccusationSchema = schema_create(Accusation)
CreateAgencySchema = schema_create(Agency)
CreateVictimSchema = schema_create(Victim)
CreatePerpetratorSchema = schema_create(Perpetrator)
Expand Down Expand Up @@ -202,6 +207,12 @@ class CreatePartnerMemberSchema(BaseModel):
is_active: Optional[bool] = True


class CreateOfficerSchema(_BaseCreateOfficerSchema, _OfficerMixin):
known_employers: Optional[List[CreateEmploymentSchema]]
accusations: Optional[List[CreateAccusationSchema]]
state_ids: Optional[List[CreateStateIDSchema]]


AddMemberSchema = sqlalchemy_to_pydantic(
PartnerMember, exclude=["id", "date_joined", "partner", "user"]
)
Expand Down Expand Up @@ -246,7 +257,9 @@ class IncidentSchema(_BaseIncidentSchema, _IncidentMixin):


class OfficerSchema(_BaseOfficerSchema, _OfficerMixin):
reported_Officer: Optional[List[_BaseOfficerSchema]]
known_employers: List[CreateEmploymentSchema]
accusations: List[CreateAccusationSchema]
state_ids: List[CreateStateIDSchema]


class PartnerSchema(_BasePartnerSchema, _PartnerMixin):
Expand Down
3 changes: 2 additions & 1 deletion frontend/helpers/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export enum Rank {
LIEUTENANT = "Lieutenant",
CAPTAIN = "Captain",
DEPUTY = "Deputy",
CHIEF = "Chief"
CHIEF = "Chief",
COMMISSIONER = "Commissioner"
}

export interface Officer {
Expand Down

0 comments on commit 7228ea9

Please sign in to comment.