mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-11 09:27:56 -04:00
Merge pull request #1639 from alphagov/create-new-organisation-model
Create new organisation model
This commit is contained in:
@@ -107,6 +107,7 @@ def register_blueprint(application):
|
||||
from app.authentication.auth import requires_admin_auth, requires_auth, requires_no_auth
|
||||
from app.letters.rest import letter_job
|
||||
from app.billing.rest import billing_blueprint
|
||||
from app.organisation.rest import organisation_blueprint
|
||||
|
||||
service_blueprint.before_request(requires_admin_auth)
|
||||
application.register_blueprint(service_blueprint, url_prefix='/service')
|
||||
@@ -177,6 +178,9 @@ def register_blueprint(application):
|
||||
service_callback_blueprint.before_request(requires_admin_auth)
|
||||
application.register_blueprint(service_callback_blueprint)
|
||||
|
||||
organisation_blueprint.before_request(requires_admin_auth)
|
||||
application.register_blueprint(organisation_blueprint, url_prefix='/organisations')
|
||||
|
||||
|
||||
def register_v2_blueprints(application):
|
||||
from app.v2.inbound_sms.get_inbound_sms import v2_inbound_sms_blueprint as get_inbound_sms
|
||||
|
||||
25
app/dao/organisation_dao.py
Normal file
25
app/dao/organisation_dao.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from app import db
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.models import Organisation
|
||||
|
||||
|
||||
def dao_get_organisations():
|
||||
return Organisation.query.order_by(
|
||||
Organisation.active.desc(), Organisation.name.asc()
|
||||
).all()
|
||||
|
||||
|
||||
def dao_get_organisation_by_id(organisation_id):
|
||||
return Organisation.query.filter_by(id=organisation_id).one()
|
||||
|
||||
|
||||
@transactional
|
||||
def dao_create_organisation(organisation):
|
||||
db.session.add(organisation)
|
||||
|
||||
|
||||
@transactional
|
||||
def dao_update_organisation(organisation_id, **kwargs):
|
||||
return Organisation.query.filter_by(id=organisation_id).update(
|
||||
kwargs
|
||||
)
|
||||
@@ -303,6 +303,24 @@ class Service(db.Model, Versioned):
|
||||
return permission in [p.permission for p in self.permissions]
|
||||
|
||||
|
||||
class Organisation(db.Model):
|
||||
__tablename__ = "organisation"
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=False)
|
||||
name = db.Column(db.String(255), nullable=False, unique=True, index=True)
|
||||
active = db.Column(db.Boolean, nullable=False, default=True)
|
||||
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
||||
updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow)
|
||||
|
||||
def serialize(self):
|
||||
serialized = {
|
||||
"id": str(self.id),
|
||||
"name": self.name,
|
||||
"active": self.active,
|
||||
}
|
||||
|
||||
return serialized
|
||||
|
||||
|
||||
class AnnualBilling(db.Model):
|
||||
__tablename__ = "annual_billing"
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=False)
|
||||
|
||||
0
app/organisation/__init__.py
Normal file
0
app/organisation/__init__.py
Normal file
21
app/organisation/organisation_schema.py
Normal file
21
app/organisation/organisation_schema.py
Normal file
@@ -0,0 +1,21 @@
|
||||
post_create_organisation_schema = {
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "POST organisation schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": "string"},
|
||||
"active": {"type": ["boolean", "null"]}
|
||||
},
|
||||
"required": ["name"]
|
||||
}
|
||||
|
||||
post_update_organisation_schema = {
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "POST organisation schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": ["string", "null"]},
|
||||
"active": {"type": ["boolean", "null"]}
|
||||
},
|
||||
"required": []
|
||||
}
|
||||
57
app/organisation/rest.py
Normal file
57
app/organisation/rest.py
Normal file
@@ -0,0 +1,57 @@
|
||||
from flask import Blueprint, jsonify, request
|
||||
|
||||
from app.dao.organisation_dao import (
|
||||
dao_create_organisation,
|
||||
dao_get_organisations,
|
||||
dao_get_organisation_by_id,
|
||||
dao_update_organisation,
|
||||
)
|
||||
from app.errors import register_errors, InvalidRequest
|
||||
from app.models import Organisation
|
||||
from app.organisation.organisation_schema import (
|
||||
post_create_organisation_schema,
|
||||
post_update_organisation_schema,
|
||||
)
|
||||
from app.schema_validation import validate
|
||||
|
||||
organisation_blueprint = Blueprint('organisation', __name__)
|
||||
register_errors(organisation_blueprint)
|
||||
|
||||
|
||||
@organisation_blueprint.route('', methods=['GET'])
|
||||
def get_organisations():
|
||||
organisations = [
|
||||
org.serialize() for org in dao_get_organisations()
|
||||
]
|
||||
|
||||
return jsonify(organisations)
|
||||
|
||||
|
||||
@organisation_blueprint.route('/<uuid:organisation_id>', methods=['GET'])
|
||||
def get_organisation_by_id(organisation_id):
|
||||
organisation = dao_get_organisation_by_id(organisation_id)
|
||||
return jsonify(organisation.serialize())
|
||||
|
||||
|
||||
@organisation_blueprint.route('', methods=['POST'])
|
||||
def create_organisation():
|
||||
data = request.get_json()
|
||||
|
||||
validate(data, post_create_organisation_schema)
|
||||
|
||||
organisation = Organisation(**data)
|
||||
dao_create_organisation(organisation)
|
||||
|
||||
return jsonify(organisation.serialize()), 201
|
||||
|
||||
|
||||
@organisation_blueprint.route('/<uuid:organisation_id>', methods=['POST'])
|
||||
def update_organisation(organisation_id):
|
||||
data = request.get_json()
|
||||
validate(data, post_update_organisation_schema)
|
||||
result = dao_update_organisation(organisation_id, **data)
|
||||
|
||||
if result:
|
||||
return '', 204
|
||||
else:
|
||||
raise InvalidRequest("Organisation not found", 404)
|
||||
Reference in New Issue
Block a user