mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 18:31:13 -05:00
Merge pull request #266 from alphagov/add-created-by-for-jobs
Add created by for jobs
This commit is contained in:
@@ -7,8 +7,7 @@ from flask import (
|
|||||||
from app.dao.jobs_dao import (
|
from app.dao.jobs_dao import (
|
||||||
dao_create_job,
|
dao_create_job,
|
||||||
dao_get_job_by_service_id_and_job_id,
|
dao_get_job_by_service_id_and_job_id,
|
||||||
dao_get_jobs_by_service_id,
|
dao_get_jobs_by_service_id
|
||||||
dao_update_job
|
|
||||||
)
|
)
|
||||||
|
|
||||||
from app.dao.services_dao import (
|
from app.dao.services_dao import (
|
||||||
@@ -57,16 +56,3 @@ def create_job(service_id):
|
|||||||
dao_create_job(job)
|
dao_create_job(job)
|
||||||
process_job.apply_async([str(job.id)], queue="process-job")
|
process_job.apply_async([str(job.id)], queue="process-job")
|
||||||
return jsonify(data=job_schema.dump(job).data), 201
|
return jsonify(data=job_schema.dump(job).data), 201
|
||||||
|
|
||||||
|
|
||||||
@job.route('/<job_id>', methods=['POST'])
|
|
||||||
def update_job(service_id, job_id):
|
|
||||||
fetched_job = dao_get_job_by_service_id_and_job_id(service_id, job_id)
|
|
||||||
current_data = dict(job_schema.dump(fetched_job).data.items())
|
|
||||||
current_data.update(request.get_json())
|
|
||||||
|
|
||||||
update_dict, errors = job_schema.load(current_data)
|
|
||||||
if errors:
|
|
||||||
return jsonify(result="error", message=errors), 400
|
|
||||||
dao_update_job(update_dict)
|
|
||||||
return jsonify(data=job_schema.dump(update_dict).data), 200
|
|
||||||
|
|||||||
@@ -247,6 +247,8 @@ class Job(db.Model):
|
|||||||
index=False,
|
index=False,
|
||||||
unique=False,
|
unique=False,
|
||||||
nullable=True)
|
nullable=True)
|
||||||
|
created_by = db.relationship('User')
|
||||||
|
created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), index=True, nullable=False)
|
||||||
|
|
||||||
|
|
||||||
VERIFY_CODE_TYPES = ['email', 'sms']
|
VERIFY_CODE_TYPES = ['email', 'sms']
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ class CreatedBySchema(ma.Schema):
|
|||||||
if not isinstance(data.get('created_by'), models.User):
|
if not isinstance(data.get('created_by'), models.User):
|
||||||
created_by = models.User.query.filter_by(id=data.get('created_by')).one()
|
created_by = models.User.query.filter_by(id=data.get('created_by')).one()
|
||||||
except:
|
except:
|
||||||
raise ValidationError('Invalid template created_by: {}'.format(data))
|
raise ValidationError('Invalid created_by: {}'.format(data))
|
||||||
|
|
||||||
@post_load
|
@post_load
|
||||||
def format_created_by(self, item):
|
def format_created_by(self, item):
|
||||||
|
|||||||
34
migrations/versions/0009_created_by_for_jobs.py
Normal file
34
migrations/versions/0009_created_by_for_jobs.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: 0009_created_by_for_jobs
|
||||||
|
Revises: 0008_archive_template
|
||||||
|
Create Date: 2016-04-26 14:54:56.852642
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '0009_created_by_for_jobs'
|
||||||
|
down_revision = '0008_archive_template'
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.add_column('jobs', sa.Column('created_by_id', postgresql.UUID(as_uuid=True), nullable=True))
|
||||||
|
op.create_index(op.f('ix_jobs_created_by_id'), 'jobs', ['created_by_id'], unique=False)
|
||||||
|
op.create_foreign_key(None, 'jobs', 'users', ['created_by_id'], ['id'])
|
||||||
|
op.get_bind()
|
||||||
|
op.execute('UPDATE jobs SET created_by_id = \
|
||||||
|
(SELECT user_id FROM user_to_service WHERE jobs.service_id = user_to_service.service_id LIMIT 1)')
|
||||||
|
op.alter_column('jobs', 'created_by_id', nullable=False)
|
||||||
|
### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_constraint(None, 'jobs', type_='foreignkey')
|
||||||
|
op.drop_index(op.f('ix_jobs_created_by_id'), table_name='jobs')
|
||||||
|
op.drop_column('jobs', 'created_by_id')
|
||||||
|
### end Alembic commands ###
|
||||||
@@ -235,7 +235,8 @@ def sample_job(notify_db,
|
|||||||
'template_id': template.id,
|
'template_id': template.id,
|
||||||
'original_file_name': 'some.csv',
|
'original_file_name': 'some.csv',
|
||||||
'notification_count': notification_count,
|
'notification_count': notification_count,
|
||||||
'created_at': created_at
|
'created_at': created_at,
|
||||||
|
'created_by': service.created_by
|
||||||
}
|
}
|
||||||
job = Job(**data)
|
job = Job(**data)
|
||||||
dao_create_job(job)
|
dao_create_job(job)
|
||||||
@@ -275,7 +276,8 @@ def sample_email_job(notify_db,
|
|||||||
'service': service,
|
'service': service,
|
||||||
'template_id': template.id,
|
'template_id': template.id,
|
||||||
'original_file_name': 'some.csv',
|
'original_file_name': 'some.csv',
|
||||||
'notification_count': 1
|
'notification_count': 1,
|
||||||
|
'created_by': service.created_by
|
||||||
}
|
}
|
||||||
job = Job(**data)
|
job = Job(**data)
|
||||||
dao_create_job(job)
|
dao_create_job(job)
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ def test_create_job(sample_template):
|
|||||||
'service_id': sample_template.service.id,
|
'service_id': sample_template.service.id,
|
||||||
'template_id': sample_template.id,
|
'template_id': sample_template.id,
|
||||||
'original_file_name': 'some.csv',
|
'original_file_name': 'some.csv',
|
||||||
'notification_count': 1
|
'notification_count': 1,
|
||||||
|
'created_by': sample_template.created_by
|
||||||
}
|
}
|
||||||
|
|
||||||
job = Job(**data)
|
job = Job(**data)
|
||||||
|
|||||||
@@ -97,7 +97,8 @@ def test_create_job(notify_api, sample_template, mocker, fake_uuid):
|
|||||||
'service': str(sample_template.service.id),
|
'service': str(sample_template.service.id),
|
||||||
'template': str(sample_template.id),
|
'template': str(sample_template.id),
|
||||||
'original_file_name': 'thisisatest.csv',
|
'original_file_name': 'thisisatest.csv',
|
||||||
'notification_count': 1
|
'notification_count': 1,
|
||||||
|
'created_by': str(sample_template.created_by.id)
|
||||||
}
|
}
|
||||||
path = '/service/{}/job'.format(sample_template.service.id)
|
path = '/service/{}/job'.format(sample_template.service.id)
|
||||||
auth_header = create_authorization_header(
|
auth_header = create_authorization_header(
|
||||||
@@ -179,35 +180,6 @@ def test_create_job_returns_404_if_missing_service(notify_api, sample_template,
|
|||||||
assert resp_json['message'] == 'No result found'
|
assert resp_json['message'] == 'No result found'
|
||||||
|
|
||||||
|
|
||||||
def test_get_update_job(notify_api, sample_job):
|
|
||||||
assert sample_job.status == 'pending'
|
|
||||||
|
|
||||||
job_id = str(sample_job.id)
|
|
||||||
service_id = str(sample_job.service.id)
|
|
||||||
|
|
||||||
update_data = {
|
|
||||||
'status': 'in progress'
|
|
||||||
}
|
|
||||||
|
|
||||||
with notify_api.test_request_context():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
path = '/service/{}/job/{}'.format(service_id, job_id)
|
|
||||||
|
|
||||||
auth_header = create_authorization_header(
|
|
||||||
service_id=service_id,
|
|
||||||
path=path,
|
|
||||||
method='POST',
|
|
||||||
request_body=json.dumps(update_data))
|
|
||||||
|
|
||||||
headers = [('Content-Type', 'application/json'), auth_header]
|
|
||||||
|
|
||||||
response = client.post(path, headers=headers, data=json.dumps(update_data))
|
|
||||||
|
|
||||||
resp_json = json.loads(response.get_data(as_text=True))
|
|
||||||
assert response.status_code == 200
|
|
||||||
assert resp_json['data']['status'] == 'in progress'
|
|
||||||
|
|
||||||
|
|
||||||
def _setup_jobs(notify_db, notify_db_session, template, number_of_jobs=5):
|
def _setup_jobs(notify_db, notify_db_session, template, number_of_jobs=5):
|
||||||
for i in range(number_of_jobs):
|
for i in range(number_of_jobs):
|
||||||
create_job(
|
create_job(
|
||||||
|
|||||||
Reference in New Issue
Block a user