Skip to content

Commit

Permalink
Rename star_date and descrip (lucyparsons#977)
Browse files Browse the repository at this point in the history
## Fixes issues
lucyparsons#941
lucyparsons#427

## Description of Changes
Changed the following column names:
- `star_date` -> `start_date`
- `descrip` -> `description`

## Tests and linting
 - [x] This branch is up-to-date with the `develop` branch.
 - [x] `pytest` passes on my local development environment.
 - [x] `pre-commit` passes on my local development environment.
 - [x]  Data-migration output:

```shell
$ docker exec -it openoversight-web-1 bash
$ flask db stamp head
/usr/local/lib/python3.11/site-packages/flask_limiter/extension.py:293: UserWarning: Using the in-memory storage for tracking rate limits as no storage was explicitly specified. This is not recommended for production use. See: https://flask-limiter.readthedocs.io#configuring-a-storage-backend for documentation about configuring the storage backend.
  warnings.warn(
[2023-07-18 17:16:06,001] INFO in __init__: OpenOversight startup
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running stamp_revision  -> 93fc3e074dcc
$ flask db migrate -m "rename 'star_date'"
/usr/local/lib/python3.11/site-packages/flask_limiter/extension.py:293: UserWarning: Using the in-memory storage for tracking rate limits as no storage was explicitly specified. This is not recommended for production use. See: https://flask-limiter.readthedocs.io#configuring-a-storage-backend for documentation about configuring the storage backend.
  warnings.warn(
[2023-07-18 17:17:01,906] INFO in __init__: OpenOversight startup
...
  Generating /usr/src/app/OpenOversight/migrations/versions/2023-07-18-1717_9ce70d7ebd56_rename_star_date.py ...  done
$ flask db upgrade
[2023-07-18 17:18:49,546] INFO in __init__: OpenOversight startup
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 93fc3e074dcc -> 9ce70d7ebd56, rename 'star_date'

...

(env) % make start
docker-compose build
...
[+] Running 2/2
 ✔ Container openoversight-postgres-1  Started                                                                                                                                                             0.2s 
 ✔ Container openoversight-web-1       Started                                                                                                                                                             0.3s 
(env) % docker exec -it openoversight-web-1 bash
$ flask db stamp head
/usr/local/lib/python3.11/site-packages/flask_limiter/extension.py:293: UserWarning: Using the in-memory storage for tracking rate limits as no storage was explicitly specified. This is not recommended for production use. See: https://flask-limiter.readthedocs.io#configuring-a-storage-backend for documentation about configuring the storage backend.
  warnings.warn(
[2023-07-18 19:18:26,742] INFO in __init__: OpenOversight startup
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
$ flask db migrate -m "rename 'descrip' to 'description'"
...
INFO  [alembic.autogenerate.compare] Detected added column 'unit_types.description'
INFO  [alembic.autogenerate.compare] Detected removed index 'ix_unit_types_descrip' on 'unit_types'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_unit_types_description' on '['description']'
INFO  [alembic.autogenerate.compare] Detected removed column 'unit_types.descrip'
  Generating /usr/src/app/OpenOversight/migrations/versions/2023-07-18-1921_eb0266dc8588_rename_descrip_to_description.py ...  done
$ flask db upgrade
/usr/local/lib/python3.11/site-packages/flask_limiter/extension.py:293: UserWarning: Using the in-memory storage for tracking rate limits as no storage was explicitly specified. This is not recommended for production use. See: https://flask-limiter.readthedocs.io#configuring-a-storage-backend for documentation about configuring the storage backend.
  warnings.warn(
[2023-07-18 19:33:12,354] INFO in __init__: OpenOversight startup
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 9ce70d7ebd56 -> eb0266dc8588, rename 'descrip' to 'description'
$
```
  • Loading branch information
michplunkett authored and AetherUnbound committed Oct 9, 2023
1 parent 4f27885 commit c71ffd4
Show file tree
Hide file tree
Showing 22 changed files with 192 additions and 114 deletions.
6 changes: 3 additions & 3 deletions OpenOversight/app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def try_else_false(comparable):
def process_assignment(row, officer, compare=False):
assignment_fields = {
"required": [],
"optional": ["job_title", "star_no", "unit_id", "star_date", "resign_date"],
"optional": ["job_title", "star_no", "unit_id", "start_date", "resign_date"],
}

# See if the row has assignment data
Expand All @@ -342,7 +342,7 @@ def process_assignment(row, officer, compare=False):
assignment_fieldnames = [
"star_no",
"unit_id",
"star_date",
"start_date",
"resign_date",
]
i = 0
Expand Down Expand Up @@ -393,7 +393,7 @@ def process_assignment(row, officer, compare=False):
assignment.job_id = job.id
set_field_from_row(row, assignment, "star_no")
set_field_from_row(row, assignment, "unit_id")
set_field_from_row(row, assignment, "star_date", allow_blank=False)
set_field_from_row(row, assignment, "start_date", allow_blank=False)
set_field_from_row(row, assignment, "resign_date", allow_blank=False)
db.session.add(assignment)
db.session.flush()
Expand Down
22 changes: 12 additions & 10 deletions OpenOversight/app/csv_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ def _handle_officers_csv(
"birth_year",
"unique_internal_identifier",
"department_name",
# the following are unused, but allowed since they are included in the csv output
# the following are unused, but allowed since they are included in the
# csv output
"badge_number",
"unique_identifier",
"job_title",
Expand Down Expand Up @@ -162,7 +163,7 @@ def _handle_assignments_csv(
with _csv_reader(assignments_csv) as csv_reader:
field_names = csv_reader.fieldnames
if "start_date" in field_names:
field_names[field_names.index("start_date")] = "star_date"
field_names[field_names.index("start_date")] = "start_date"
if "badge_number" in field_names:
field_names[field_names.index("badge_number")] = "star_no"
if "end_date" in field_names:
Expand All @@ -181,7 +182,7 @@ def _handle_assignments_csv(
"star_no",
"unit_id",
"unit_name",
"star_date",
"start_date",
"resign_date",
"officer_unique_identifier",
],
Expand All @@ -193,8 +194,8 @@ def _handle_assignments_csv(
job_title_to_id = {
job.job_title.strip().lower(): job.id for job in jobs_for_department
}
unit_descrip_to_id = {
unit.descrip.strip().lower(): unit.id
unit_description_to_id = {
unit.description.strip().lower(): unit.id
for unit in Unit.query.filter_by(department_id=department_id).all()
}
if overwrite_assignments:
Expand All @@ -211,7 +212,8 @@ def _handle_assignments_csv(
)
if len(wrong_department) > 0:
raise Exception(
"Referenced {} officers in assignment csv that belong to different department. Example ids: {}".format(
"Referenced {} officers in assignment csv that belong to different "
"department. Example ids: {}".format(
len(wrong_department),
", ".join(map(str, list(wrong_department)[:3])),
)
Expand Down Expand Up @@ -253,17 +255,17 @@ def _handle_assignments_csv(
)
elif row.get("unit_name"):
unit_name = row["unit_name"].strip()
descrip = unit_name.lower()
unit_id = unit_descrip_to_id.get(descrip)
description = unit_name.lower()
unit_id = unit_description_to_id.get(description)
if unit_id is None:
unit = Unit(
descrip=unit_name,
description=unit_name,
department_id=officer.department_id,
)
db.session.add(unit)
db.session.flush()
unit_id = unit.id
unit_descrip_to_id[descrip] = unit_id
unit_description_to_id[description] = unit_id
row["unit_id"] = unit_id
job_title = row["job_title"].strip().lower()
job_id = job_title_to_id.get(job_title)
Expand Down
6 changes: 3 additions & 3 deletions OpenOversight/app/main/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def salary_record_maker(salary: Salary) -> _Record:
def officer_record_maker(officer: Officer) -> _Record:
if officer.assignments_lazy:
most_recent_assignment = max(
officer.assignments_lazy, key=lambda a: a.star_date or date.min
officer.assignments_lazy, key=lambda a: a.start_date or date.min
)
most_recent_title = most_recent_assignment.job and check_output(
most_recent_assignment.job.job_title
Expand Down Expand Up @@ -121,10 +121,10 @@ def assignment_record_maker(assignment: Assignment) -> _Record:
"officer unique identifier": officer and officer.unique_internal_identifier,
"badge number": assignment.star_no,
"job title": assignment.job and check_output(assignment.job.job_title),
"start date": assignment.star_date,
"start date": assignment.start_date,
"end date": assignment.resign_date,
"unit id": assignment.unit and assignment.unit.id,
"unit description": assignment.unit and assignment.unit.descrip,
"unit description": assignment.unit and assignment.unit.description,
}


Expand Down
16 changes: 8 additions & 8 deletions OpenOversight/app/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def validate_money(form, field):


def validate_end_date(form, field):
if form.data["star_date"] and field.data:
if form.data["star_date"] > field.data:
if form.data["start_date"] and field.data:
if form.data["start_date"] > field.data:
raise ValidationError("End date must come after start date.")


Expand Down Expand Up @@ -155,11 +155,11 @@ class AssignmentForm(Form):
"Unit",
validators=[Optional()],
query_factory=unit_choices,
get_label="descrip",
get_label="description",
allow_blank=True,
blank_text="None",
)
star_date = DateField("Assignment start date", validators=[Optional()])
start_date = DateField("Assignment start date", validators=[Optional()])
resign_date = DateField(
"Assignment end date", validators=[Optional(), validate_end_date]
)
Expand Down Expand Up @@ -321,7 +321,7 @@ class AddOfficerForm(Form):
"Unit",
validators=[Optional()],
query_factory=unit_choices,
get_label="descrip",
get_label="description",
allow_blank=True,
blank_text="None",
)
Expand Down Expand Up @@ -401,7 +401,7 @@ class EditOfficerForm(Form):


class AddUnitForm(Form):
descrip = StringField(
description = StringField(
"Unit name or description",
default="",
validators=[Regexp(r"\w*"), Length(max=120), DataRequired()],
Expand Down Expand Up @@ -563,8 +563,8 @@ class BrowseForm(Form):
unit = QuerySelectField(
"unit",
validators=[Optional()],
get_label="descrip",
get_pk=lambda unit: unit.descrip,
get_label="description",
get_pk=lambda unit: unit.description,
)
current_job = BooleanField("current_job", default=None, validators=[Optional()])
name = StringField("Last name")
Expand Down
16 changes: 9 additions & 7 deletions OpenOversight/app/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,9 @@ def list_officer(

unit_selections = ["Not Sure"] + [
uc[0]
for uc in db.session.query(Unit.descrip)
for uc in db.session.query(Unit.description)
.filter_by(department_id=department_id)
.order_by(Unit.descrip.asc())
.order_by(Unit.description.asc())
.all()
]
rank_selections = [
Expand Down Expand Up @@ -842,13 +842,13 @@ def get_dept_units(department_id=None):

if department_id:
units = Unit.query.filter_by(department_id=department_id)
units = units.order_by(Unit.descrip).all()
unit_list = [(unit.id, unit.descrip) for unit in units]
units = units.order_by(Unit.description).all()
unit_list = [(unit.id, unit.description) for unit in units]
else:
units = Unit.query.all()
# Prevent duplicate units
unit_list = sorted(
set((unit.id, unit.descrip) for unit in units),
set((unit.id, unit.description) for unit in units),
key=lambda x: x[1],
)

Expand Down Expand Up @@ -926,10 +926,12 @@ def add_unit():
set_dynamic_default(form.department, current_user.dept_pref_rel)

if form.validate_on_submit():
unit = Unit(descrip=form.descrip.data, department_id=form.department.data.id)
unit = Unit(
description=form.description.data, department_id=form.department.data.id
)
db.session.add(unit)
db.session.commit()
flash("New unit {} added to OpenOversight".format(unit.descrip))
flash("New unit {} added to OpenOversight".format(unit.description))
return redirect(url_for("main.get_started_labeling"))
else:
current_app.logger.info(form.errors)
Expand Down
20 changes: 10 additions & 10 deletions OpenOversight/app/models/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,26 +246,26 @@ def gender_label(self):
def job_title(self):
if self.assignments_lazy:
return max(
self.assignments_lazy, key=lambda x: x.star_date or date.min
self.assignments_lazy, key=lambda x: x.start_date or date.min
).job.job_title

def unit_descrip(self):
def unit_description(self):
if self.assignments_lazy:
unit = max(
self.assignments_lazy, key=lambda x: x.star_date or date.min
self.assignments_lazy, key=lambda x: x.start_date or date.min
).unit
return unit.descrip if unit else None
return unit.description if unit else None

def badge_number(self):
if self.assignments_lazy:
return max(
self.assignments_lazy, key=lambda x: x.star_date or date.min
self.assignments_lazy, key=lambda x: x.start_date or date.min
).star_no

def currently_on_force(self):
if self.assignments_lazy:
most_recent = max(
self.assignments_lazy, key=lambda x: x.star_date or date.min
self.assignments_lazy, key=lambda x: x.start_date or date.min
)
return "Yes" if most_recent.resign_date is None else "No"
return "Uncertain"
Expand Down Expand Up @@ -337,7 +337,7 @@ class Assignment(BaseModel):
job = db.relationship("Job")
unit_id = db.Column(db.Integer, db.ForeignKey("unit_types.id"), nullable=True)
unit = db.relationship("Unit")
star_date = db.Column(db.Date, index=True, unique=False, nullable=True)
start_date = db.Column(db.Date, index=True, unique=False, nullable=True)
resign_date = db.Column(db.Date, index=True, unique=False, nullable=True)
date_created = db.Column(db.DateTime, default=func.now())
date_updated = db.Column(
Expand All @@ -352,14 +352,14 @@ class Unit(BaseModel):
__tablename__ = "unit_types"

id = db.Column(db.Integer, primary_key=True)
descrip = db.Column(db.String(120), index=True, unique=False)
description = db.Column(db.String(120), index=True, unique=False)
department_id = db.Column(db.Integer, db.ForeignKey("departments.id"))
department = db.relationship(
"Department", backref="unit_types", order_by="Unit.descrip.asc()"
"Department", backref="unit_types", order_by="Unit.description.asc()"
)

def __repr__(self):
return "Unit: {}".format(self.descrip)
return "Unit: {}".format(self.description)


class Face(BaseModel):
Expand Down
6 changes: 3 additions & 3 deletions OpenOversight/app/models/database_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def create_assignment_from_dict(
star_no=parse_str(data.get("star_no"), None),
job_id=int(data["job_id"]),
unit_id=parse_int(data.get("unit_id")),
star_date=parse_date(data.get("star_date")),
start_date=parse_date(data.get("start_date")),
resign_date=parse_date(data.get("resign_date")),
)
if force_id and data.get("id"):
Expand All @@ -150,8 +150,8 @@ def update_assignment_from_dict(
assignment.job_id = int(data["job_id"])
if "unit_id" in data.keys():
assignment.unit_id = parse_int(data.get("unit_id"))
if "star_date" in data.keys():
assignment.star_date = parse_date(data.get("star_date"))
if "start_date" in data.keys():
assignment.start_date = parse_date(data.get("start_date"))
if "resign_date" in data.keys():
assignment.resign_date = parse_date(data.get("resign_date"))
db.session.flush()
Expand Down
2 changes: 1 addition & 1 deletion OpenOversight/app/templates/add_unit.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h1>Add Unit</h1>
<form class="form" method="post" role="form">
{{ form.hidden_tag() }}
{{ wtf.form_errors(form, hiddens="only") }}
{{ wtf.form_field(form.descrip, autofocus="autofocus") }}
{{ wtf.form_field(form.description, autofocus="autofocus") }}
{{ wtf.form_field(form.department) }}
{{ wtf.form_field(form.submit, id="submit", button_map={'submit':'primary'}) }}
</form>
Expand Down
2 changes: 1 addition & 1 deletion OpenOversight/app/templates/edit_assignment.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h1>Edit Officer Assignment</h1>
<h4>
<small><a href="{{ url_for("main.add_unit") }}">Don't see your unit? Add one!</a></small>
</h4>
{{ wtf.form_field(form.star_date) }}
{{ wtf.form_field(form.start_date) }}
{{ wtf.form_field(form.resign_date) }}
<input class="btn btn-primary btn-lg" type="submit" value="Edit" />
</form>
Expand Down
2 changes: 1 addition & 1 deletion OpenOversight/app/templates/list_officer.html
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ <h2>
</dd>
<dt>Unit</dt>
<dd>
{{ officer.unit_descrip() | default('Unknown') }}
{{ officer.unit_description() | default('Unknown') }}
</dd>
<dt>Currently on the Force</dt>
<dd>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ <h3>Assignment History</h3>
{% endif %}
</tr>
<tbody>
{% for assignment in assignments|rejectattr('star_date','ne',None) %}
{% for assignment in assignments|rejectattr('start_date','ne',None) %}
<tr>
<td>{{ assignment.job.job_title }}</td>
<td>{{ assignment.star_no }}</td>
<td>
{% if assignment.unit_id %}{{ assignment.unit.descrip }}{% endif %}
{% if assignment.unit_id %}{{ assignment.unit.description }}{% endif %}
</td>
<td>
{% if assignment.star_date %}
{{ assignment.star_date }}
{% if assignment.start_date %}
{{ assignment.start_date }}
{% else %}
Unknown
{% endif %}
Expand All @@ -50,16 +50,16 @@ <h3>Assignment History</h3>
</td>
</tr>
{% endfor %}
{% for assignment in assignments | rejectattr('star_date', 'none') | sort(attribute='star_date', reverse=True) %}
{% for assignment in assignments | rejectattr('start_date', 'none') | sort(attribute='start_date', reverse=True) %}
<tr>
<td>{{ assignment.job.job_title }}</td>
<td>{{ assignment.star_no }}</td>
<td>
{% if assignment.unit_id %}{{ assignment.unit.descrip }}{% endif %}
{% if assignment.unit_id %}{{ assignment.unit.description }}{% endif %}
</td>
<td>
{% if assignment.star_date: %}
{{ assignment.star_date }}
{% if assignment.start_date %}
{{ assignment.start_date }}
{% else %}
Unknown
{% endif %}
Expand Down Expand Up @@ -132,8 +132,8 @@ <h3>
<b>Start date of new assignment:</b>
</td>
<td>
{{ form.star_date }}
{% for error in form.star_date.errors %}
{{ form.start_date }}
{% for error in form.start_date.errors %}
<p>
<span style="color: red">[{{ error }}]</span>
</p>
Expand Down
8 changes: 4 additions & 4 deletions OpenOversight/app/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def add_unit_query(form, current_user):
if not current_user.is_administrator:
form.unit.query = Unit.query.filter_by(
department_id=current_user.ac_department_id
).order_by(Unit.descrip.asc())
).order_by(Unit.description.asc())
else:
form.unit.query = Unit.query.order_by(Unit.descrip.asc()).all()
form.unit.query = Unit.query.order_by(Unit.description.asc()).all()


def compute_leaderboard_stats(select_top=25):
Expand Down Expand Up @@ -82,7 +82,7 @@ def unit_choices(department_id: Optional[int] = None):
return (
db.session.query(Unit)
.filter_by(department_id=department_id)
.order_by(Unit.descrip.asc())
.order_by(Unit.description.asc())
.all()
)
return db.session.query(Unit).order_by(Unit.descrip.asc()).all()
return db.session.query(Unit).order_by(Unit.description.asc()).all()
Loading

0 comments on commit c71ffd4

Please sign in to comment.