Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move models to its own package #963

Merged
merged 40 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b615454
Create __init__.py
michplunkett Jul 6, 2023
45b678a
models -> database
michplunkett Jul 6, 2023
5fbf865
Update database.py
michplunkett Jul 6, 2023
06fc935
name change
michplunkett Jul 6, 2023
346f86b
name change
michplunkett Jul 6, 2023
f4919fa
bump
michplunkett Jul 6, 2023
c52e496
Reference updates
michplunkett Jul 6, 2023
3b3854b
Update database_imports.py
michplunkett Jul 6, 2023
25946ea
Update conftest.py
michplunkett Jul 6, 2023
33997ae
Update test_utils.py
michplunkett Jul 6, 2023
97b9105
Update test_utils.py
michplunkett Jul 6, 2023
5678348
Update commands.py
michplunkett Jul 6, 2023
9afd1c3
Update commands.py
michplunkett Jul 6, 2023
7d97dd4
Update 2a9064a2507c_remove_dots_middle_initial.py
michplunkett Jul 6, 2023
4133bd5
Update 59e9993c169c_change_faces_to_thumbnails.py
michplunkett Jul 6, 2023
22cf543
Update forms.py
michplunkett Jul 6, 2023
580ab95
Merge branch 'develop' into create_models_package
michplunkett Jul 7, 2023
f66e852
Close
michplunkett Jul 7, 2023
aff37c2
Create emails.py
michplunkett Jul 7, 2023
6f76960
Add emails to file
michplunkett Jul 7, 2023
f7eb8fa
Merge branch 'develop' into create_models_package
michplunkett Jul 7, 2023
44fd6bd
Update email_client.py
michplunkett Jul 7, 2023
ad373cd
Update test_functional.py
michplunkett Jul 7, 2023
1ccc248
Update config.py
michplunkett Jul 7, 2023
3c4e283
Update conftest.py
michplunkett Jul 10, 2023
6aaaac8
Update commands.py
michplunkett Jul 10, 2023
3c27750
Update __init__.py
michplunkett Jul 10, 2023
2df0dc0
Update config.py
michplunkett Jul 10, 2023
436ed36
Update __init__.py
michplunkett Jul 10, 2023
6d504ad
Update utils.py
michplunkett Jul 10, 2023
addf5ab
Update auth.py
michplunkett Jul 10, 2023
4b813c6
Path changes
michplunkett Jul 10, 2023
c4b56b9
Update forms.py
michplunkett Jul 10, 2023
7779620
Update views.py
michplunkett Jul 10, 2023
47964bc
Update test_descriptions.py
michplunkett Jul 10, 2023
eb25fc4
Update test_image_tagging.py
michplunkett Jul 10, 2023
4e8b7a2
Update test_incidents.py
michplunkett Jul 10, 2023
61d02a2
Update test_user_api.py
michplunkett Jul 10, 2023
1e81bf7
Update forms.py
michplunkett Jul 10, 2023
e2631be
Update views.py
michplunkett Jul 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions OpenOversight/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from flask_wtf.csrf import CSRFProtect
from markupsafe import Markup

from .config import config
from OpenOversight.app.config import config
from OpenOversight.app.models.database import db


bootstrap = Bootstrap()
Expand All @@ -40,7 +41,6 @@ def create_app(config_name="default"):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
from .models import db

bootstrap.init_app(app)
mail.init_app(app)
Expand Down
3 changes: 2 additions & 1 deletion OpenOversight/app/auth/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from wtforms.validators import DataRequired, Email, EqualTo, Length, Optional, Regexp
from wtforms_sqlalchemy.fields import QuerySelectField

from ..models import User
from OpenOversight.app.models.database import User

from ..utils.db import dept_choices


Expand Down
2 changes: 1 addition & 1 deletion OpenOversight/app/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
)
from flask_login import current_user, login_required, login_user, logout_user

from OpenOversight.app.models.database import User, db
from OpenOversight.app.utils.constants import HTTP_METHOD_GET, HTTP_METHOD_POST
from OpenOversight.app.utils.forms import set_dynamic_default
from OpenOversight.app.utils.general import validate_redirect_url

from .. import sitemap
from ..email import send_email
from ..models import User, db
from . import auth
from .forms import (
ChangeDefaultDepartmentForm,
Expand Down
13 changes: 10 additions & 3 deletions OpenOversight/app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@
from flask import current_app
from flask.cli import with_appcontext

from OpenOversight.app.csv_imports import import_csv_files
from OpenOversight.app.models.database import (
Assignment,
Department,
Job,
Officer,
Salary,
User,
db,
)
from OpenOversight.app.utils.constants import ENCODING_UTF_8
from OpenOversight.app.utils.db import get_officer
from OpenOversight.app.utils.general import normalize_gender, prompt_yes_no, str_is_true

from .csv_imports import import_csv_files
from .models import Assignment, Department, Job, Officer, Salary, User, db


@click.command()
@with_appcontext
Expand Down
24 changes: 12 additions & 12 deletions OpenOversight/app/csv_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@

from sqlalchemy.exc import SQLAlchemyError

from .model_imports import (
from OpenOversight.app.models.database import (
Assignment,
Department,
Incident,
Job,
Link,
Officer,
Salary,
Unit,
db,
)
from OpenOversight.app.models.database_imports import (
create_assignment_from_dict,
create_incident_from_dict,
create_link_from_dict,
Expand All @@ -18,17 +29,6 @@
update_officer_from_dict,
update_salary_from_dict,
)
from .models import (
Assignment,
Department,
Incident,
Job,
Link,
Officer,
Salary,
Unit,
db,
)


def _create_or_update_model(
Expand Down
2 changes: 1 addition & 1 deletion OpenOversight/app/main/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from flask import Response, abort
from sqlalchemy.orm import Query

from ..models import (
from OpenOversight.app.models.database import (
Assignment,
Department,
Description,
Expand Down
2 changes: 1 addition & 1 deletion OpenOversight/app/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
)
from wtforms_sqlalchemy.fields import QuerySelectField

from OpenOversight.app.models.database import Officer
from OpenOversight.app.utils.db import dept_choices, unit_choices

from ..formfields import TimeField
from ..models import Officer
from ..widgets import BootstrapListWidget, FormFieldWidget
from .choices import (
AGE_CHOICES,
Expand Down
2 changes: 1 addition & 1 deletion OpenOversight/app/main/model_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from flask_sqlalchemy.model import DefaultMeta
from flask_wtf import FlaskForm as Form

from OpenOversight.app.models.database import db
from OpenOversight.app.utils.constants import HTTP_METHOD_GET, HTTP_METHOD_POST
from OpenOversight.app.utils.db import add_department_query
from OpenOversight.app.utils.forms import set_dynamic_default

from ..auth.utils import ac_or_admin_required
from ..models import db


class ModelView(MethodView):
Expand Down
36 changes: 18 additions & 18 deletions OpenOversight/app/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@
from sqlalchemy.orm import contains_eager, joinedload, selectinload
from sqlalchemy.orm.exc import NoResultFound

from OpenOversight.app.models.database import (
Assignment,
Department,
Description,
Face,
Image,
Incident,
Job,
LicensePlate,
Link,
Location,
Note,
Officer,
Salary,
Unit,
User,
db,
)
from OpenOversight.app.utils.cloud import crop_image, upload_image_to_s3_and_store_in_db
from OpenOversight.app.utils.constants import HTTP_METHOD_GET, HTTP_METHOD_POST
from OpenOversight.app.utils.db import (
Expand Down Expand Up @@ -54,24 +72,6 @@
from .. import limiter, sitemap
from ..auth.forms import LoginForm
from ..auth.utils import ac_or_admin_required, admin_required
from ..models import (
Assignment,
Department,
Description,
Face,
Image,
Incident,
Job,
LicensePlate,
Link,
Location,
Note,
Officer,
Salary,
Unit,
User,
db,
)
from . import downloads, main
from .choices import AGE_CHOICES, GENDER_CHOICES, RACE_CHOICES
from .forms import (
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
from sqlalchemy.orm import validates
from werkzeug.security import check_password_hash, generate_password_hash

from . import login_manager
from .utils.constants import ENCODING_UTF_8
from .validators import state_validator, url_validator
from OpenOversight.app.utils.constants import ENCODING_UTF_8
from OpenOversight.app.validators import state_validator, url_validator


db = SQLAlchemy()
Expand Down Expand Up @@ -168,7 +167,7 @@ def full_name(self):
def race_label(self):
if self.race is None:
return "Data Missing"
from .main.choices import RACE_CHOICES
from OpenOversight.app.main.choices import RACE_CHOICES

for race, label in RACE_CHOICES:
if self.race == race:
Expand All @@ -177,7 +176,7 @@ def race_label(self):
def gender_label(self):
if self.gender is None:
return "Data Missing"
from .main.choices import GENDER_CHOICES
from OpenOversight.app.main.choices import GENDER_CHOICES

for gender, label in GENDER_CHOICES:
if self.gender == gender:
Expand Down Expand Up @@ -614,8 +613,3 @@ def is_active(self):

def __repr__(self):
return "<User %r>" % self.username


@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
Comment on lines -620 to -622
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved to prevent a circular import error.

Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

import dateutil.parser

from OpenOversight.app.utils.general import get_or_create, str_is_true

from .main import choices
from .models import (
from OpenOversight.app import login_manager
from OpenOversight.app.main import choices
from OpenOversight.app.models.database import (
Assignment,
Incident,
LicensePlate,
Link,
Location,
Officer,
Salary,
User,
db,
)
from .validators import state_validator, url_validator
from OpenOversight.app.utils.general import get_or_create, str_is_true
from OpenOversight.app.validators import state_validator, url_validator


if TYPE_CHECKING:
Expand Down Expand Up @@ -70,7 +71,6 @@ def parse_str(value: Optional[str], default: Optional[str] = "") -> Optional[str


def create_officer_from_dict(data: Dict[str, Any], force_id: bool = False) -> Officer:

officer = Officer(
department_id=int(data["department_id"]),
last_name=parse_str(data.get("last_name", "")),
Expand All @@ -94,7 +94,6 @@ def create_officer_from_dict(data: Dict[str, Any], force_id: bool = False) -> Of


def update_officer_from_dict(data: Dict[str, Any], officer: Officer) -> Officer:

if "department_id" in data.keys():
officer.department_id = int(data["department_id"])
if "last_name" in data.keys():
Expand Down Expand Up @@ -124,7 +123,6 @@ def update_officer_from_dict(data: Dict[str, Any], officer: Officer) -> Officer:
def create_assignment_from_dict(
data: Dict[str, Any], force_id: bool = False
) -> Assignment:

assignment = Assignment(
officer_id=int(data["officer_id"]),
star_no=parse_str(data.get("star_no"), None),
Expand Down Expand Up @@ -320,3 +318,8 @@ def update_incident_from_dict(data: Dict[str, Any], incident: Incident) -> Incid
incident.license_plates = data["license_plate_objects"] or []
db.session.flush()
return incident


@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
2 changes: 1 addition & 1 deletion OpenOversight/app/utils/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from PIL import Image as Pimage
from PIL.PngImagePlugin import PngImageFile

from ..models import Image, db
from OpenOversight.app.models.database import Image, db


def compute_hash(data_to_hash):
Expand Down
11 changes: 10 additions & 1 deletion OpenOversight/app/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

from sqlalchemy import func

from ..models import Assignment, Department, Face, Image, Officer, Unit, User, db
from OpenOversight.app.models.database import (
Assignment,
Department,
Face,
Image,
Officer,
Unit,
User,
db,
)


def add_department_query(form, current_user):
Expand Down
5 changes: 3 additions & 2 deletions OpenOversight/app/utils/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from sqlalchemy.orm import selectinload
from sqlalchemy.sql.expression import cast

from ...app.main.choices import GENDER_CHOICES, RACE_CHOICES
from ..models import (
from OpenOversight.app.models.database import (
Assignment,
Description,
Face,
Expand All @@ -20,6 +19,8 @@
Unit,
db,
)

from ...app.main.choices import GENDER_CHOICES, RACE_CHOICES
from .general import get_or_create


Expand Down
Loading