2018-02-20 11:22:17 +00:00
|
|
|
from flask import redirect, render_template, session, url_for
|
|
|
|
|
from flask_login import current_user, login_required
|
|
|
|
|
|
2018-03-07 18:13:27 +00:00
|
|
|
from app import user_api_client
|
2018-02-20 11:22:17 +00:00
|
|
|
from app.main import main
|
2016-07-15 15:23:23 +01:00
|
|
|
from app.notify_client.service_api_client import ServicesBrowsableItem
|
2018-03-07 18:13:27 +00:00
|
|
|
from app.notify_client.organisations_api_client import OrganisationBrowsableItem
|
2016-10-28 10:47:32 +01:00
|
|
|
from app.utils import is_gov_user
|
2016-01-13 14:43:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route("/services")
|
|
|
|
|
@login_required
|
2016-01-14 11:12:03 +00:00
|
|
|
def choose_service():
|
2018-03-07 18:13:27 +00:00
|
|
|
orgs_and_services = user_api_client.get_organisations_and_services_for_user(current_user)
|
|
|
|
|
from pprint import pprint
|
|
|
|
|
pprint(orgs_and_services)
|
|
|
|
|
orgs_and_services['organisations'] = [
|
|
|
|
|
OrganisationBrowsableItem(org) for org in orgs_and_services['organisations']
|
|
|
|
|
]
|
|
|
|
|
orgs_and_services['services_without_organisations'] = [
|
|
|
|
|
ServicesBrowsableItem(x) for x in orgs_and_services['services_without_organisations']
|
|
|
|
|
]
|
|
|
|
|
|
2016-01-18 11:15:14 +00:00
|
|
|
return render_template(
|
|
|
|
|
'views/choose-service.html',
|
2018-03-07 18:13:27 +00:00
|
|
|
organisations=orgs_and_services['organisations'],
|
|
|
|
|
services_without_organisations=orgs_and_services['services_without_organisations'],
|
2016-10-28 10:47:32 +01:00
|
|
|
can_add_service=is_gov_user(current_user.email_address)
|
2016-03-29 10:39:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route("/services-or-dashboard")
|
|
|
|
|
def show_all_services_or_dashboard():
|
2016-04-24 09:46:44 +01:00
|
|
|
|
2016-05-04 13:01:55 +01:00
|
|
|
if not current_user.is_authenticated:
|
2016-04-24 09:46:44 +01:00
|
|
|
return redirect(url_for('.index'))
|
|
|
|
|
|
2016-11-09 14:33:35 +00:00
|
|
|
services = service_api_client.get_active_services({'user_id': current_user.id})['data']
|
2016-03-29 10:39:01 +01:00
|
|
|
|
2016-03-29 22:50:40 +01:00
|
|
|
if 1 == len(services):
|
|
|
|
|
return redirect(url_for('.service_dashboard', service_id=services[0]['id']))
|
|
|
|
|
else:
|
2016-04-04 16:53:52 +01:00
|
|
|
service_id = session.get('service_id', None)
|
|
|
|
|
if any([service_id == x['id'] for x in services]):
|
|
|
|
|
return redirect(url_for('.service_dashboard', service_id=service_id))
|
2016-03-29 22:50:40 +01:00
|
|
|
return redirect(url_for('.choose_service'))
|