mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-05 02:41:14 -05:00
Refactor organisation rest to remove marshmallow
This commit is contained in:
@@ -137,17 +137,15 @@ class Organisation(db.Model):
|
|||||||
logo = db.Column(db.String(255), nullable=False)
|
logo = db.Column(db.String(255), nullable=False)
|
||||||
name = db.Column(db.String(255), nullable=True)
|
name = db.Column(db.String(255), nullable=True)
|
||||||
|
|
||||||
@classmethod
|
def serialize(self):
|
||||||
def from_json(cls, data):
|
serialized = {
|
||||||
"""
|
"id": str(self.id),
|
||||||
Assumption: data has been validated appropriately.
|
"colour": self.colour,
|
||||||
|
"logo": self.logo,
|
||||||
|
"name": self.name,
|
||||||
|
}
|
||||||
|
|
||||||
Returns a Organisation object based on the provided data.
|
return serialized
|
||||||
"""
|
|
||||||
# validate json with marshmallow
|
|
||||||
fields = data.copy()
|
|
||||||
|
|
||||||
return cls(**fields)
|
|
||||||
|
|
||||||
|
|
||||||
DVLA_ORG_HM_GOVERNMENT = '001'
|
DVLA_ORG_HM_GOVERNMENT = '001'
|
||||||
|
|||||||
11
app/organisation/organisation_schema.py
Normal file
11
app/organisation/organisation_schema.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
post_organisation_schema = {
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"description": "POST schema for getting organisation",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"colour": {"type": ["string", "null"], "format": "string"},
|
||||||
|
"name": {"type": ["string", "null"], "minimum": 1},
|
||||||
|
"logo": {"type": ["string", "null"], "minimum": 1}
|
||||||
|
},
|
||||||
|
"required": ["logo"]
|
||||||
|
}
|
||||||
@@ -1,12 +1,17 @@
|
|||||||
from flask import Blueprint, jsonify, request
|
from flask import Blueprint, jsonify, request
|
||||||
|
|
||||||
from app.dao.organisations_dao import dao_get_organisations, dao_get_organisation_by_id, dao_create_organisation
|
from app.dao.organisations_dao import (
|
||||||
from app.schemas import organisation_schema
|
dao_get_organisations,
|
||||||
|
dao_get_organisation_by_id,
|
||||||
|
dao_create_organisation
|
||||||
|
)
|
||||||
from app.errors import (
|
from app.errors import (
|
||||||
InvalidRequest,
|
InvalidRequest,
|
||||||
register_errors
|
register_errors
|
||||||
)
|
)
|
||||||
from app.models import Organisation
|
from app.models import Organisation
|
||||||
|
from app.organisation.organisation_schema import post_organisation_schema
|
||||||
|
from app.schema_validation import validate
|
||||||
|
|
||||||
organisation_blueprint = Blueprint('organisation', __name__)
|
organisation_blueprint = Blueprint('organisation', __name__)
|
||||||
register_errors(organisation_blueprint)
|
register_errors(organisation_blueprint)
|
||||||
@@ -14,28 +19,23 @@ register_errors(organisation_blueprint)
|
|||||||
|
|
||||||
@organisation_blueprint.route('', methods=['GET'])
|
@organisation_blueprint.route('', methods=['GET'])
|
||||||
def get_organisations():
|
def get_organisations():
|
||||||
data = organisation_schema.dump(dao_get_organisations(), many=True).data
|
organisations = [o.serialize() for o in dao_get_organisations()]
|
||||||
return jsonify(organisations=data)
|
return jsonify(organisations=organisations)
|
||||||
|
|
||||||
|
|
||||||
@organisation_blueprint.route('/<uuid:org_id>', methods=['GET'])
|
@organisation_blueprint.route('/<uuid:org_id>', methods=['GET'])
|
||||||
def get_organisation_by_id(org_id):
|
def get_organisation_by_id(org_id):
|
||||||
data = organisation_schema.dump(dao_get_organisation_by_id(org_id)).data
|
organisation = dao_get_organisation_by_id(org_id)
|
||||||
return jsonify(organisation=data)
|
return jsonify(organisation=organisation.serialize())
|
||||||
|
|
||||||
|
|
||||||
@organisation_blueprint.route('', methods=['POST'])
|
@organisation_blueprint.route('', methods=['POST'])
|
||||||
def post_organisation():
|
def post_organisation():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
if not data.get('logo', None):
|
|
||||||
errors = {'logo': ['Missing data for required field.']}
|
|
||||||
raise InvalidRequest(errors, status_code=400)
|
|
||||||
|
|
||||||
# validate json with marshmallow
|
validate(data, post_organisation_schema)
|
||||||
organisation_schema.load(request.get_json())
|
|
||||||
|
|
||||||
# unpack valid json into service object
|
organisation = Organisation(**data)
|
||||||
valid_organisation = Organisation.from_json(data)
|
|
||||||
|
|
||||||
dao_create_organisation(valid_organisation)
|
dao_create_organisation(organisation)
|
||||||
return jsonify(data=organisation_schema.dump(valid_organisation).data), 201
|
return jsonify(data=organisation.serialize()), 201
|
||||||
|
|||||||
@@ -600,12 +600,6 @@ class EventSchema(BaseSchema):
|
|||||||
strict = True
|
strict = True
|
||||||
|
|
||||||
|
|
||||||
class OrganisationSchema(BaseSchema):
|
|
||||||
class Meta:
|
|
||||||
model = models.Organisation
|
|
||||||
strict = True
|
|
||||||
|
|
||||||
|
|
||||||
class DaySchema(ma.Schema):
|
class DaySchema(ma.Schema):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
@@ -658,7 +652,6 @@ service_history_schema = ServiceHistorySchema()
|
|||||||
api_key_history_schema = ApiKeyHistorySchema()
|
api_key_history_schema = ApiKeyHistorySchema()
|
||||||
template_history_schema = TemplateHistorySchema()
|
template_history_schema = TemplateHistorySchema()
|
||||||
event_schema = EventSchema()
|
event_schema = EventSchema()
|
||||||
organisation_schema = OrganisationSchema()
|
|
||||||
provider_details_schema = ProviderDetailsSchema()
|
provider_details_schema = ProviderDetailsSchema()
|
||||||
provider_details_history_schema = ProviderDetailsHistorySchema()
|
provider_details_history_schema = ProviderDetailsHistorySchema()
|
||||||
day_schema = DaySchema()
|
day_schema = DaySchema()
|
||||||
|
|||||||
@@ -70,12 +70,12 @@ def test_create_organisation_without_logo_raises_error(client, notify_db, notify
|
|||||||
data=json.dumps(data)
|
data=json.dumps(data)
|
||||||
)
|
)
|
||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
|
json_resp = json.loads(response.get_data(as_text=True))
|
||||||
|
assert json_resp['errors'][0]['message'] == "logo is a required property"
|
||||||
|
|
||||||
|
|
||||||
def test_create_organisation_without_name_or_colour_is_valid(client, notify_db, notify_db_session):
|
def test_create_organisation_without_name_or_colour_is_valid(client, notify_db, notify_db_session):
|
||||||
data = {
|
data = {
|
||||||
'name': None,
|
|
||||||
'colour': None,
|
|
||||||
'logo': 'images/text_x2.png'
|
'logo': 'images/text_x2.png'
|
||||||
}
|
}
|
||||||
auth_header = create_authorization_header()
|
auth_header = create_authorization_header()
|
||||||
@@ -85,4 +85,4 @@ def test_create_organisation_without_name_or_colour_is_valid(client, notify_db,
|
|||||||
headers=[('Content-Type', 'application/json'), auth_header],
|
headers=[('Content-Type', 'application/json'), auth_header],
|
||||||
data=json.dumps(data)
|
data=json.dumps(data)
|
||||||
)
|
)
|
||||||
assert response.status_code == 400
|
assert response.status_code == 201
|
||||||
|
|||||||
Reference in New Issue
Block a user