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

Restructure utils.py to its own package #915

Merged
merged 26 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7bc69c5
Move utils to its own package
michplunkett Apr 12, 2023
aa629af
Move constants to own file
michplunkett Apr 12, 2023
ba67866
Update references
michplunkett Apr 12, 2023
28f9a1c
Update models.py
michplunkett Apr 12, 2023
f67e9c9
pre-check
michplunkett Apr 12, 2023
03d035b
Update constants.py
michplunkett Apr 12, 2023
53c9e5c
References
michplunkett Apr 12, 2023
abc28a8
Fixed pre-checkn
michplunkett Apr 12, 2023
65a910e
Use relative paths
michplunkett Apr 12, 2023
f4577bc
Update Makefile
michplunkett Apr 12, 2023
1c0aaec
Fix @patch string
michplunkett Apr 12, 2023
eb1d0df
Update forms.py
michplunkett Apr 12, 2023
c6d0471
Update test_utils.py
michplunkett Apr 12, 2023
e59db30
Use absolute path to avoid error
michplunkett Apr 12, 2023
56fe1ef
Use absolute imports
michplunkett Apr 12, 2023
756c2b5
Update 59e9993c169c_change_faces_to_thumbnails.py
michplunkett Apr 12, 2023
7a72b0a
util -> utils
michplunkett Apr 23, 2023
42e3af7
More util -> utils
michplunkett Apr 23, 2023
fedf57f
Comment and pathway things
michplunkett Apr 23, 2023
a291e91
Update general.py
michplunkett Apr 23, 2023
6a90c0f
Update 59e9993c169c_change_faces_to_thumbnails.py
michplunkett Apr 23, 2023
b1ee101
Revert "Update 59e9993c169c_change_faces_to_thumbnails.py"
michplunkett Apr 23, 2023
befcfff
Update .gitignore
michplunkett Apr 23, 2023
7824d7f
Update 59e9993c169c_change_faces_to_thumbnails.py
michplunkett Apr 23, 2023
1ec0659
Merge branch 'develop' into util_restructure
michplunkett Apr 24, 2023
c7666fc
Merge branch 'develop' into util_restructure
abandoned-prototype Apr 24, 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ vagrant/puppet/.tmp
.vscode/**
*.code-workspace

# Operating system files
**/.DS_Store
Comment on lines +96 to +97
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was getting this file if I opened up anything in finder on my comp.


node_modules/
OpenOverSight/app/static/dist/
static/
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ help: ## Print this message and exit
| sort \
| column -s ':' -t

.PHONY: attach
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a missing PHONY.

attach:
docker-compose exec postgres psql -h localhost -U openoversight openoversight-dev
2 changes: 1 addition & 1 deletion OpenOversight/app/auth/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from wtforms.validators import DataRequired, Email, EqualTo, Length, Optional, Regexp

from ..models import User
from ..utils import dept_choices
from ..utils.db import dept_choices


class LoginForm(Form):
Expand Down
10 changes: 4 additions & 6 deletions OpenOversight/app/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
)
from flask_login import current_user, login_required, login_user, logout_user

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 ..utils import (
HTTP_METHOD_GET,
HTTP_METHOD_POST,
set_dynamic_default,
validate_redirect_url,
)
from . import auth
from .forms import (
ChangeDefaultDepartmentForm,
Expand Down
11 changes: 4 additions & 7 deletions OpenOversight/app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
from flask import current_app
from flask.cli import with_appcontext

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
from .utils import (
ENCODING_UTF_8,
get_officer,
normalize_gender,
prompt_yes_no,
str_is_true,
)


@click.command()
Expand Down
3 changes: 2 additions & 1 deletion OpenOversight/app/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
ValidationError,
)

from OpenOversight.app.utils.db import dept_choices, unit_choices

from ..formfields import TimeField
from ..models import Officer
from ..utils import dept_choices, unit_choices
from ..widgets import BootstrapListWidget, FormFieldWidget
from .choices import (
AGE_CHOICES,
Expand Down
10 changes: 4 additions & 6 deletions OpenOversight/app/main/model_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
from flask_sqlalchemy.model import DefaultMeta
from flask_wtf import FlaskForm as Form

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
from ..utils import (
HTTP_METHOD_GET,
HTTP_METHOD_POST,
add_department_query,
set_dynamic_default,
)


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

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 (
add_department_query,
add_unit_query,
compute_leaderboard_stats,
dept_choices,
unit_choices,
)
from OpenOversight.app.utils.forms import (
add_new_assignment,
add_officer_profile,
create_description,
create_incident,
create_note,
edit_existing_assignment,
edit_officer_profile,
filter_by_form,
set_dynamic_default,
)
from OpenOversight.app.utils.general import (
ac_can_edit_officer,
allowed_file,
get_or_create,
get_random_image,
replace_list,
serve_image,
validate_redirect_url,
)

from .. import limiter, sitemap
from ..auth.forms import LoginForm
from ..auth.utils import ac_or_admin_required, admin_required
Expand All @@ -42,33 +72,6 @@
User,
db,
)
from ..utils import (
HTTP_METHOD_GET,
HTTP_METHOD_POST,
ac_can_edit_officer,
add_department_query,
add_new_assignment,
add_officer_profile,
add_unit_query,
allowed_file,
compute_leaderboard_stats,
create_description,
create_incident,
create_note,
crop_image,
dept_choices,
edit_existing_assignment,
edit_officer_profile,
filter_by_form,
get_or_create,
get_random_image,
replace_list,
serve_image,
set_dynamic_default,
unit_choices,
upload_image_to_s3_and_store_in_db,
validate_redirect_url,
)
from . import downloads, main
from .choices import AGE_CHOICES, GENDER_CHOICES, RACE_CHOICES
from .forms import (
Expand Down Expand Up @@ -153,7 +156,8 @@ def get_officer():
else None,
rank=form.data["rank"] if form.data["rank"] != "Not Sure" else None,
unit=form.data["unit"] if form.data["unit"] != "Not Sure" else None,
current_job=form.data["current_job"] or None, # set to None if False
current_job=form.data["current_job"] or None,
# set to None if False
min_age=form.data["min_age"],
max_age=form.data["max_age"],
first_name=form.data["first_name"],
Expand Down
3 changes: 2 additions & 1 deletion OpenOversight/app/model_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import dateutil.parser

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

from .main import choices
from .models import (
Assignment,
Expand All @@ -13,7 +15,6 @@
Salary,
db,
)
from .utils import get_or_create, str_is_true
from .validators import state_validator, url_validator


Expand Down
8 changes: 4 additions & 4 deletions OpenOversight/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
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


db = SQLAlchemy()
model_encoding = "utf-8"
Copy link
Collaborator Author

@michplunkett michplunkett Apr 12, 2023

Choose a reason for hiding this comment

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

We could remove this now that the constant isn't a circular reference.

jwt = JsonWebToken("HS512")

BaseModel = db.Model # type: DefaultMeta
Expand Down Expand Up @@ -552,7 +552,7 @@ def verify_password(self, password):

def generate_confirmation_token(self, expiration=3600):
payload = {"confirm": self.id}
return self._jwt_encode(payload, expiration).decode(model_encoding)
return self._jwt_encode(payload, expiration).decode(ENCODING_UTF_8)

def confirm(self, token):
try:
Expand All @@ -572,7 +572,7 @@ def confirm(self, token):

def generate_reset_token(self, expiration=3600):
payload = {"reset": self.id}
return self._jwt_encode(payload, expiration).decode(model_encoding)
return self._jwt_encode(payload, expiration).decode(ENCODING_UTF_8)

def reset_password(self, token, new_password):
try:
Expand All @@ -588,7 +588,7 @@ def reset_password(self, token, new_password):

def generate_email_change_token(self, new_email, expiration=3600):
payload = {"change_email": self.id, "new_email": new_email}
return self._jwt_encode(payload, expiration).decode(model_encoding)
return self._jwt_encode(payload, expiration).decode(ENCODING_UTF_8)

def change_email(self, token):
try:
Expand Down
Loading