mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 08:21:13 -05:00
Add user details to template schema dump.
This commit is contained in:
@@ -123,9 +123,19 @@ class TemplateSchema(BaseTemplateSchema):
|
|||||||
raise ValidationError('Invalid template subject', 'subject')
|
raise ValidationError('Invalid template subject', 'subject')
|
||||||
|
|
||||||
|
|
||||||
class TemplateHistorySchema(BaseTemplateSchema):
|
class TemplateHistorySchema(BaseSchema):
|
||||||
|
|
||||||
created_by = field_for(models.Template, 'created_by', required=True)
|
class Meta:
|
||||||
|
# Use the base model class that the history class is created from
|
||||||
|
model = models.Template
|
||||||
|
# We have to use a method here because the relationship field on the
|
||||||
|
# history object is not created.
|
||||||
|
created_by = fields.Method("populate_created_by", dump_only=True)
|
||||||
|
created_at = field_for(models.Template, 'created_at', format='%Y-%m-%d %H:%M:%S.%f')
|
||||||
|
|
||||||
|
def populate_created_by(self, data):
|
||||||
|
usr = models.User.query.filter_by(id=data.created_by_id).one()
|
||||||
|
return {'id': str(usr.id), 'name': usr.name, 'email_address': usr.email_address}
|
||||||
|
|
||||||
|
|
||||||
class NotificationsStatisticsSchema(BaseSchema):
|
class NotificationsStatisticsSchema(BaseSchema):
|
||||||
|
|||||||
@@ -99,12 +99,13 @@ def get_template_by_id_and_service_id(service_id, template_id):
|
|||||||
|
|
||||||
@template.route('/<uuid:template_id>/version/<int:version>')
|
@template.route('/<uuid:template_id>/version/<int:version>')
|
||||||
def get_template_version(service_id, template_id, version):
|
def get_template_version(service_id, template_id, version):
|
||||||
fetched_template = dao_get_template_by_id_and_service_id(
|
data, errors = template_history_schema.dump(
|
||||||
template_id=template_id,
|
dao_get_template_by_id_and_service_id(
|
||||||
service_id=service_id,
|
template_id=template_id,
|
||||||
version=version
|
service_id=service_id,
|
||||||
|
version=version
|
||||||
|
)
|
||||||
)
|
)
|
||||||
data, errors = template_history_schema.dump(fetched_template)
|
|
||||||
if errors:
|
if errors:
|
||||||
return json_resp(result='error', message=errors), 400
|
return json_resp(result='error', message=errors), 400
|
||||||
return jsonify(data=data)
|
return jsonify(data=data)
|
||||||
@@ -112,8 +113,10 @@ def get_template_version(service_id, template_id, version):
|
|||||||
|
|
||||||
@template.route('/<uuid:template_id>/version')
|
@template.route('/<uuid:template_id>/version')
|
||||||
def get_template_versions(service_id, template_id):
|
def get_template_versions(service_id, template_id):
|
||||||
fetched_templates = dao_get_template_versions(service_id, template_id)
|
data, errors = template_history_schema.dump(
|
||||||
data, errors = template_history_schema.dump(fetched_templates, many=True)
|
dao_get_template_versions(service_id, template_id),
|
||||||
|
many=True
|
||||||
|
)
|
||||||
if errors:
|
if errors:
|
||||||
return json_resp(result='error', message=errors), 400
|
return json_resp(result='error', message=errors), 400
|
||||||
return jsonify(data=data)
|
return jsonify(data=data)
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
import json
|
import json
|
||||||
|
from datetime import (datetime, date)
|
||||||
from flask import url_for
|
from flask import url_for
|
||||||
from app.models import Template
|
from app.models import Template
|
||||||
|
from freezegun import freeze_time
|
||||||
from app.dao.templates_dao import dao_update_template
|
from app.dao.templates_dao import dao_update_template
|
||||||
from tests import create_authorization_header
|
from tests import create_authorization_header
|
||||||
|
|
||||||
|
|
||||||
def test_template_history_version(notify_api, sample_template):
|
def test_template_history_version(notify_api, sample_user, sample_template):
|
||||||
with notify_api.test_request_context():
|
with notify_api.test_request_context():
|
||||||
with notify_api.test_client() as client:
|
with notify_api.test_client() as client:
|
||||||
auth_header = create_authorization_header()
|
auth_header = create_authorization_header()
|
||||||
@@ -23,6 +25,8 @@ def test_template_history_version(notify_api, sample_template):
|
|||||||
assert json_resp['data']['id'] == str(sample_template.id)
|
assert json_resp['data']['id'] == str(sample_template.id)
|
||||||
assert json_resp['data']['content'] == sample_template.content
|
assert json_resp['data']['content'] == sample_template.content
|
||||||
assert json_resp['data']['version'] == 1
|
assert json_resp['data']['version'] == 1
|
||||||
|
assert json_resp['data']['created_by']['name'] == sample_user.name
|
||||||
|
assert datetime.strptime(json_resp['data']['created_at'], '%Y-%m-%d %H:%M:%S.%f').date() == date.today()
|
||||||
|
|
||||||
|
|
||||||
def test_previous_template_history_version(notify_api, sample_template):
|
def test_previous_template_history_version(notify_api, sample_template):
|
||||||
|
|||||||
Reference in New Issue
Block a user