-
Notifications
You must be signed in to change notification settings - Fork 79
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
Refactoring links forms #646
Changes from 10 commits
2250935
a27fa1d
c8a7428
e354f3d
df143b3
af6ce33
45a6340
946bae7
5388705
46bc5c6
b46d07a
0905899
10741fe
7f87290
91e4889
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,11 +23,10 @@ | |
crop_image, create_incident, get_or_create, dept_choices, | ||
upload_image_to_s3_and_store_in_db) | ||
|
||
|
||
from .forms import (FindOfficerForm, FindOfficerIDForm, AddUnitForm, | ||
FaceTag, AssignmentForm, DepartmentForm, AddOfficerForm, | ||
EditOfficerForm, IncidentForm, TextForm, EditTextForm, | ||
AddImageForm, EditDepartmentForm, BrowseForm, SalaryForm) | ||
AddImageForm, EditDepartmentForm, BrowseForm, SalaryForm, OfficerLinkForm) | ||
from .model_view import ModelView | ||
from .choices import GENDER_CHOICES, RACE_CHOICES, AGE_CHOICES | ||
from ..models import (db, Image, User, Face, Officer, Assignment, Department, | ||
|
@@ -567,7 +566,7 @@ def add_officer(): | |
jsloads = ['js/dynamic_lists.js', 'js/add_officer.js'] | ||
form = AddOfficerForm() | ||
for link in form.links: | ||
link.user_id.data = current_user.get_id() | ||
link.creator_id.data = current_user.id | ||
add_unit_query(form, current_user) | ||
add_department_query(form, current_user) | ||
set_dynamic_default(form.department, current_user.dept_pref_rel) | ||
|
@@ -595,9 +594,6 @@ def edit_officer(officer_id): | |
jsloads = ['js/dynamic_lists.js'] | ||
officer = Officer.query.filter_by(id=officer_id).one() | ||
form = EditOfficerForm(obj=officer) | ||
for link in form.links: | ||
if not link.user_id.data: | ||
link.user_id.data = current_user.get_id() | ||
|
||
if current_user.is_area_coordinator and not current_user.is_administrator: | ||
if not ac_can_edit_officer(officer, current_user): | ||
|
@@ -1116,7 +1112,7 @@ def get_new_form(self): | |
form.officers[0].oo_id.data = request.args.get('officer_id') | ||
|
||
for link in form.links: | ||
link.user_id.data = current_user.get_id() | ||
link.creator_id.data = current_user.id | ||
return form | ||
|
||
def get_edit_form(self, obj): | ||
|
@@ -1126,10 +1122,10 @@ def get_edit_form(self, obj): | |
no_links = len(obj.links) | ||
no_officers = len(obj.officers) | ||
for link in form.links: | ||
if link.user_id.data: | ||
if link.creator_id.data: | ||
continue | ||
else: | ||
link.user_id.data = current_user.get_id() | ||
link.creator_id.data = current_user.id | ||
|
||
for officer_idx, officer in enumerate(obj.officers): | ||
form.officers[officer_idx].oo_id.data = officer.id | ||
|
@@ -1304,3 +1300,99 @@ def dispatch_request(self, *args, **kwargs): | |
'/officer/<int:officer_id>/description/<int:obj_id>/delete', | ||
view_func=description_view, | ||
methods=['GET', 'POST']) | ||
|
||
|
||
# This API only applies to links attached to officer profiles, not links | ||
# attached to incidents. | ||
class OfficerLinkApi(ModelView): | ||
model = Link | ||
model_name = 'link' | ||
form = OfficerLinkForm | ||
department_check = True | ||
|
||
@property | ||
def officer(self): | ||
if not hasattr(self, '_officer'): | ||
self._officer = db.session.query(Officer).filter_by(id=self.officer_id).one() | ||
return self._officer | ||
|
||
@login_required | ||
@ac_or_admin_required | ||
def new(self, form=None): | ||
if not current_user.is_administrator and current_user.ac_department_id != self.officer.department_id: | ||
abort(403) | ||
if not form: | ||
form = self.get_new_form() | ||
if hasattr(form, 'creator_id') and not form.creator_id.data: | ||
form.creator_id.data = current_user.get_id() | ||
|
||
if form.validate_on_submit(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ModelView class it inherits from is written in the same way, so I wouldn't want to break that pattern here without doing it elsewhere, to keep things consistent. And I think it would be best to come to consensus on a solution for #786 and apply it in a separate PR. |
||
link = Link( | ||
title=form.title.data, | ||
url=form.url.data, | ||
link_type=form.link_type.data, | ||
description=form.description.data, | ||
author=form.author.data, | ||
creator_id=form.creator_id.data) | ||
self.officer.links.append(link) | ||
db.session.add(link) | ||
db.session.commit() | ||
flash('{} created!'.format(self.model_name)) | ||
return self.get_redirect_url(obj_id=link.id) | ||
|
||
return render_template('{}_new.html'.format(self.model_name), form=form) | ||
|
||
@login_required | ||
@ac_or_admin_required | ||
def delete(self, obj_id): | ||
obj = self.model.query.get_or_404(obj_id) | ||
if not current_user.is_administrator and current_user.ac_department_id != self.get_department_id(obj): | ||
abort(403) | ||
|
||
if request.method == 'POST': | ||
db.session.delete(obj) | ||
db.session.commit() | ||
flash('{} successfully deleted!'.format(self.model_name)) | ||
return self.get_post_delete_url() | ||
|
||
return render_template('{}_delete.html'.format(self.model_name), obj=obj, officer_id=self.officer_id) | ||
|
||
def get_new_form(self): | ||
form = self.form() | ||
form.officer_id.data = self.officer_id | ||
return form | ||
|
||
def get_edit_form(self, obj): | ||
form = self.form(obj=obj) | ||
form.officer_id.data = self.officer_id | ||
return form | ||
|
||
def get_redirect_url(self, *args, **kwargs): | ||
return redirect(url_for('main.officer_profile', officer_id=self.officer_id)) | ||
|
||
def get_post_delete_url(self, *args, **kwargs): | ||
return self.get_redirect_url() | ||
|
||
def get_department_id(self, obj): | ||
return self.officer.department_id | ||
|
||
def dispatch_request(self, *args, **kwargs): | ||
if 'officer_id' in kwargs: | ||
officer = Officer.query.get_or_404(kwargs['officer_id']) | ||
self.officer_id = kwargs.pop('officer_id') | ||
self.department_id = officer.department_id | ||
return super(OfficerLinkApi, self).dispatch_request(*args, **kwargs) | ||
|
||
|
||
main.add_url_rule( | ||
'/officer/<int:officer_id>/link/new', | ||
view_func=OfficerLinkApi.as_view('link_api_new'), | ||
methods=['GET', 'POST']) | ||
main.add_url_rule( | ||
'/officer/<int:officer_id>/link/<int:obj_id>/edit', | ||
view_func=OfficerLinkApi.as_view('link_api_edit'), | ||
methods=['GET', 'POST']) | ||
main.add_url_rule( | ||
'/officer/<int:officer_id>/link/<int:obj_id>/delete', | ||
view_func=OfficerLinkApi.as_view('link_api_delete'), | ||
methods=['GET', 'POST']) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block content %} | ||
<div class="container theme-showcase" role="main"> | ||
|
||
<div class="page-header"> | ||
<h1> | ||
Delete Link for officer {{ obj.officer_id }} | ||
</h1> | ||
<p> | ||
<a href="{{ obj.url }}">{{ obj.title or obj.url }}</a> | ||
{% if obj.description or obj.author %} | ||
<div> | ||
{% if obj.description %} | ||
{{ obj.description }} | ||
{% endif %} | ||
{% if obj.author %} | ||
{% if obj.description %}- {% endif %}<em>{{ obj.author }}</em> | ||
{% endif %} | ||
</div> | ||
{% endif %} | ||
</p> | ||
</div> | ||
<p class="lead"> | ||
Are you sure you want to delete this link? | ||
This cannot be undone. | ||
<form action="{{ url_for('main.link_api_delete', obj_id=obj.id, officer_id=officer_id) }}" method="post"> | ||
<button class='btn btn-danger' type="submit">Delete</button> | ||
</form> | ||
</p> | ||
</div> | ||
{% endblock content %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% extends "form.html" %} | ||
|
||
{% block page_title %} | ||
Update Link | ||
{% endblock page_title %} | ||
|
||
{% block form %} | ||
<p>For officer with OOID {{ form.officer_id.data }}.</p> | ||
{{ super() }} | ||
{% endblock form %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% extends 'form.html' %} | ||
|
||
{% block page_title %} | ||
New Link | ||
{% endblock page_title %} | ||
|
||
{% block form %} | ||
<p>For officer with OOID {{ form.officer_id.data }}.</p> | ||
{{ super() }} | ||
{% endblock form %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe move the code comment as a doc comment inside the class? e.g.