Skip to content

Commit

Permalink
Merge pull request #303 from lucyparsons/multi-dept-submission
Browse files Browse the repository at this point in the history
Add multi-department support for photo submission
  • Loading branch information
b-meson authored Sep 19, 2017
2 parents 949f0d4 + 0f06116 commit c3e06cb
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 17 deletions.
35 changes: 35 additions & 0 deletions OpenOversight/app/db_repository/versions/004_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from sqlalchemy import *
from migrate import *


from migrate.changeset import schema
pre_meta = MetaData()
post_meta = MetaData()


raw_images = Table('raw_images', post_meta,
Column('id', Integer, primary_key=True, nullable=False),
Column('filepath', String(length=255)),
Column('hash_img', String(length=120)),
Column('date_image_inserted', DateTime),
Column('date_image_taken', DateTime),
Column('contains_cops', Boolean),
Column('user_id', Integer),
Column('is_tagged', Boolean, default=ColumnDefault(False)),
Column('department_id', Integer),
)


def upgrade(migrate_engine):
# Upgrade operations go here. Don't create your own engine; bind
# migrate_engine to your metadata
pre_meta.bind = migrate_engine
post_meta.bind = migrate_engine
post_meta.tables['raw_images'].columns['department_id'].create()


def downgrade(migrate_engine):
# Operations to reverse the above upgrade go here.
pre_meta.bind = migrate_engine
post_meta.bind = migrate_engine
post_meta.tables['raw_images'].columns['department_id'].drop()
16 changes: 12 additions & 4 deletions OpenOversight/app/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,23 @@ def submit_complaint():
@main.route('/submit', methods=['GET', 'POST'])
@limiter.limit('5/minute')
def submit_data():
return render_template('submit.html')
departments = Department.query.all()
return render_template('submit_deptselect.html', departments=departments)


@main.route('/submit/department/<int:department_id>', methods=['GET', 'POST'])
@limiter.limit('5/minute')
def submit_department_images(department_id=1):
department = Department.query.filter_by(id=department_id).one()
return render_template('submit_department.html', department=department)

@main.route('/upload', methods=['POST'])

@main.route('/upload/department/<int:department_id>', methods=['POST'])
@limiter.limit('250/minute')
def upload():
def upload(department_id):
file_to_upload = request.files['file']
if not allowed_file(file_to_upload.filename):
return jsonify(error="File type not allowed!"), 415

original_filename = secure_filename(file_to_upload.filename)
image_data = file_to_upload.read()

Expand Down Expand Up @@ -374,6 +381,7 @@ def upload():
# Update the database to add the image
new_image = Image(filepath=url, hash_img=hash_img, is_tagged=False,
date_image_inserted=datetime.datetime.now(),
department_id=department_id,
# TODO: Get the following field from exif data
date_image_taken=datetime.datetime.now())
db.session.add(new_image)
Expand Down
2 changes: 2 additions & 0 deletions OpenOversight/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class Image(db.Model):
user = db.relationship('User', backref='raw_images')
is_tagged = db.Column(db.Boolean, default=False, unique=False, nullable=True)

department_id = db.Column(db.Integer)

def __repr__(self):
return '<Image ID {}: {}>'.format(self.id, self.filepath)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
<link href="{{ url_for('static', filename='css/dropzone.css') }}" rel="stylesheet">

<div class="container theme-showcase" role="main">

<div class="page-header">
<h1>Submit Images <small>containing faces/badge numbers of uniformed police officers</small></h1>
</div>

<h3>Drop images here to submit</h3>
<h3>Drop images here to submit photos of officers in {{ department.name }}</h3>

<form action="/upload" class="dropzone" id="my-cop-dropzone">
<form action="/upload/department/{{ department.id }}" class="dropzone" id="my-cop-dropzone">
</form>

<script>
$(document).ready(function(){
Dropzone.options.myCopDropzone = {
url: "/upload",
url: "/upload/department/{{ department.id }}",
method: "POST",
uploadMultiple: false,
parallelUploads: 50,
Expand All @@ -31,6 +32,7 @@ <h3>Drop images here to submit</h3>

});
</script>

</div>

<div class="container">
Expand All @@ -42,5 +44,4 @@ <h3>High Security Submissions</h3>
<script src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/dropzone.js') }}"></script>


{% endblock %}
21 changes: 21 additions & 0 deletions OpenOversight/app/templates/submit_deptselect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "base.html" %}
{% block content %}

<link href="{{ url_for('static', filename='css/dropzone.css') }}" rel="stylesheet">

<div class="container theme-showcase" role="main">

<div class="text-center frontpage-leads">
<h1><small>Submit images of officers in action in: </small></h1>
{% for department in departments %}
<p>
<a href="/submit/department/{{ department.id }}" class="btn btn-lg btn-primary">
<span class="glyphicon glyphicon-upload" aria-hidden="true"></span>
{{ department.name }}</a>
</p>
{% endfor %}
</div>

</div>

{% endblock %}
10 changes: 10 additions & 0 deletions OpenOversight/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,15 @@ def make_admin_user():
email))


@manager.command
def link_images_to_department():
# Link existing images to first department
from app.models import Image, db
images = Image.query.all()
for image in images:
image.department_id = 1
db.session.commit()


if __name__ == "__main__":
manager.run()
12 changes: 8 additions & 4 deletions OpenOversight/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,14 @@ def mockdata(session, request):
SEED = current_app.config['SEED']
random.seed(SEED)

image1 = models.Image(filepath='static/images/test_cop1.png')
image2 = models.Image(filepath='static/images/test_cop2.png')
image3 = models.Image(filepath='static/images/test_cop3.png')
image4 = models.Image(filepath='static/images/test_cop4.png')
image1 = models.Image(filepath='static/images/test_cop1.png',
department_id=1)
image2 = models.Image(filepath='static/images/test_cop2.png',
department_id=1)
image3 = models.Image(filepath='static/images/test_cop3.png',
department_id=1)
image4 = models.Image(filepath='static/images/test_cop4.png',
department_id=1)

unit1 = models.Unit(descrip="test")

Expand Down
14 changes: 14 additions & 0 deletions OpenOversight/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
('/tagger_find'),
('/privacy'),
('/submit'),
('/submit/department/1'),
('/label'),
('/departments'),
('/officer/3'),
Expand Down Expand Up @@ -671,3 +672,16 @@ def test_admin_can_see_department_list(mockdata, client, session):
)

assert 'Departments' in rv.data


def test_expected_dept_appears_in_submission_dept_selection(mockdata, client,
session):
with current_app.test_request_context():
login_admin(client)

rv = client.get(
url_for('main.submit_data'),
follow_redirects=True
)

assert 'Springfield Police Department' in rv.data
15 changes: 10 additions & 5 deletions test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,16 @@ def populate():
""" Populate database with test data"""

# Add images from Springfield Police Department
image1 = models.Image(filepath='static/images/test_cop1.png')
image2 = models.Image(filepath='static/images/test_cop2.png')
image3 = models.Image(filepath='static/images/test_cop3.png')
image4 = models.Image(filepath='static/images/test_cop4.png')
image5 = models.Image(filepath='static/images/test_cop5.jpg')
image1 = models.Image(filepath='static/images/test_cop1.png',
department_id=1)
image2 = models.Image(filepath='static/images/test_cop2.png',
department_id=1)
image3 = models.Image(filepath='static/images/test_cop3.png',
department_id=1)
image4 = models.Image(filepath='static/images/test_cop4.png',
department_id=1)
image5 = models.Image(filepath='static/images/test_cop5.jpg',
department_id=1)

test_images = [image1, image2, image3, image4, image5]
db.session.add_all(test_images)
Expand Down

0 comments on commit c3e06cb

Please sign in to comment.