Skip to content

Commit

Permalink
Add create and last_update columns to models (#1032)
Browse files Browse the repository at this point in the history
## Fixes issue
#1008 

## Description of Changes
Add creation and last update columns to track when and by whom models
were updated.

## Notes for Deployment
Run alembic migration

## Screenshots (if appropriate)
N/A

## 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.
  • Loading branch information
sea-kelp committed Aug 22, 2023
1 parent 872a01a commit de93328
Show file tree
Hide file tree
Showing 15 changed files with 529 additions and 266 deletions.
2 changes: 1 addition & 1 deletion OpenOversight/app/main/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,5 @@ def descriptions_record_maker(description: Description) -> _Record:
"created_by": description.created_by,
"officer_id": description.officer_id,
"created_at": description.created_at,
"updated_at": description.updated_at,
"last_updated_at": description.last_updated_at,
}
30 changes: 14 additions & 16 deletions OpenOversight/app/main/model_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ def new(self, form=None):

if form.validate_on_submit():
new_obj = self.create_function(form, current_user)
if hasattr(new_obj, "created_by"):
new_obj.created_by = current_user.get_id()
if hasattr(new_obj, "last_updated_by"):
new_obj.last_updated_by = current_user.get_id()
db.session.add(new_obj)
db.session.commit()

match self.model.__name__:
case Incident.__name__:
Department(id=new_obj.department_id).remove_database_cache_entries(
Expand Down Expand Up @@ -117,20 +122,6 @@ def edit(self, obj_id, form=None):

if not form:
form = self.get_edit_form(obj)
# if the object doesn't have a creator id set it to current user
if (
hasattr(obj, "created_by")
and hasattr(form, "created_by")
and getattr(obj, "created_by")
):
form.created_by.data = obj.created_by
elif hasattr(form, "created_by"):
form.created_by.data = current_user.get_id()

# if the object keeps track of who updated it last, set to current user
if hasattr(obj, "last_updated_by") and hasattr(form, "last_updated_by"):
form.last_updated_by.data = current_user.get_id()
form.last_updated_at.data = datetime.datetime.now()

if hasattr(form, "department"):
add_department_query(form, current_user)
Expand Down Expand Up @@ -225,8 +216,15 @@ def get_department_id(self, obj):

def populate_obj(self, form, obj):
form.populate_obj(obj)
if hasattr(obj, "updated_at"):
obj.updated_at = datetime.datetime.now()

# if the object doesn't have a creator id set it to current user
if hasattr(obj, "created_by") and not getattr(obj, "created_by"):
obj.created_by = current_user.get_id()
# if the object keeps track of who updated it last, set to current user
if hasattr(obj, "last_updated_at"):
obj.last_updated_at = datetime.datetime.now()
obj.last_updated_by = current_user.get_id()

db.session.add(obj)
db.session.commit()

Expand Down
2 changes: 1 addition & 1 deletion OpenOversight/app/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,7 @@ def download_dept_descriptions_csv(department_id: int):
"created_by",
"officer_id",
"created_at",
"updated_at",
"last_updated_at",
]
return make_downloadable_csv(
notes, department_id, "Notes", field_names, descriptions_record_maker
Expand Down
Loading

0 comments on commit de93328

Please sign in to comment.