add new endpoint to get organisations and services for a user

contains orgs, and unmapped services.

the orgs contain nested services - services the user is a part of that
belong to that org.

the unmapped services are any services that the user is a part of that
either don't have an org or have one that the user doesn't know about
This commit is contained in:
Leo Hemsted
2018-03-07 18:31:18 +00:00
parent efec57db01
commit 91fa475645
2 changed files with 40 additions and 1 deletions

View File

@@ -120,7 +120,7 @@ class User(db.Model):
organisations = db.relationship(
'Organisation',
secondary='user_to_organisation',
backref=db.backref('user_to_organisation', lazy='dynamic'))
backref=db.backref('users', lazy='dynamic'))
@property
def password(self):

View File

@@ -395,6 +395,45 @@ def update_password(user_id):
return jsonify(data=user.serialize()), 200
@user_blueprint.route('/<uuid:user_id>/organisations-and-services', methods=['GET'])
def get_organisations_and_services_for_user(user_id):
user = get_user_by_id(user_id=user_id)
data = {
'organisations': [
{
'name': org.name,
'id': org.id,
'services': [
{
'id': service.id,
'name': service.name
}
for service in org.services
if service.active and user in service.users
]
}
for org in user.organisations
],
'services_without_organisations': [
{
'id': service.id,
'name': service.name
} for service in user.services
if (
service.active and
# include services that either aren't in an organisation, or are in an organisation,
# but not one that the user can see.
(
not service.organisation or
user not in service.organisation.users
)
)
]
}
return jsonify(data)
def _create_reset_password_url(email):
data = json.dumps({'email': email, 'created_at': str(datetime.utcnow())})
url = '/new-password/'