2018-02-07 14:43:09 +00:00
|
|
|
from app import db
|
|
|
|
|
from app.dao.dao_utils import transactional
|
2018-02-20 17:09:16 +00:00
|
|
|
from app.models import Organisation, InvitedOrganisationUser, User
|
2018-02-07 14:43:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_organisations():
|
|
|
|
|
return Organisation.query.order_by(
|
|
|
|
|
Organisation.active.desc(), Organisation.name.asc()
|
|
|
|
|
).all()
|
|
|
|
|
|
|
|
|
|
|
2018-02-10 01:34:37 +00:00
|
|
|
def dao_get_organisation_services(organisation_id):
|
|
|
|
|
return Organisation.query.filter_by(
|
|
|
|
|
id=organisation_id
|
|
|
|
|
).one().services
|
|
|
|
|
|
|
|
|
|
|
2018-02-07 14:43:09 +00:00
|
|
|
def dao_get_organisation_by_id(organisation_id):
|
|
|
|
|
return Organisation.query.filter_by(id=organisation_id).one()
|
|
|
|
|
|
|
|
|
|
|
2018-02-10 01:34:37 +00:00
|
|
|
def dao_get_organisation_by_service_id(service_id):
|
2018-02-12 18:26:51 +00:00
|
|
|
return Organisation.query.join(Organisation.services).filter_by(id=service_id).first()
|
2018-02-10 01:34:37 +00:00
|
|
|
|
|
|
|
|
|
2018-02-07 14:43:09 +00:00
|
|
|
@transactional
|
|
|
|
|
def dao_create_organisation(organisation):
|
|
|
|
|
db.session.add(organisation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@transactional
|
2018-02-09 11:17:13 +00:00
|
|
|
def dao_update_organisation(organisation_id, **kwargs):
|
|
|
|
|
return Organisation.query.filter_by(id=organisation_id).update(
|
|
|
|
|
kwargs
|
|
|
|
|
)
|
2018-02-10 01:34:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@transactional
|
|
|
|
|
def dao_add_service_to_organisation(service, organisation_id):
|
|
|
|
|
organisation = Organisation.query.filter_by(
|
|
|
|
|
id=organisation_id
|
|
|
|
|
).one()
|
|
|
|
|
|
|
|
|
|
organisation.services.append(service)
|
2018-02-20 17:09:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_invited_organisation_user(user_id):
|
2018-02-21 11:49:21 +00:00
|
|
|
return InvitedOrganisationUser.query.filter_by(id=user_id).one()
|
2018-02-20 17:09:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_get_users_for_organisation(organisation_id):
|
|
|
|
|
return User.query.filter(
|
|
|
|
|
User.user_to_organisation.any(id=organisation_id),
|
|
|
|
|
User.state == 'active'
|
2018-02-21 11:49:21 +00:00
|
|
|
).order_by(User.created_at).all()
|
2018-02-20 17:09:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_add_user_to_organisation(organisation_id, user_id):
|
|
|
|
|
organisation = dao_get_organisation_by_id(organisation_id)
|
|
|
|
|
user = User.query.get(user_id)
|
|
|
|
|
organisation.users.append(user)
|
|
|
|
|
db.session.add(organisation)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return user
|