def organisation(organisation_id):
    try:

        organisation = repo.get_organisation(
            organisation_id, getattr(current_user, 'user_id', None))

        recruiting_districts = repo.get_districts()
        districts = repo.get_districts_without_whole_trd()

        try:
            organisation["area"] = next(
                district["name"] for district in districts if district["id"] == organisation.get("area"))
        except StopIteration:
            organisation["area"] = None

        try:
            organisation["recruitment_area"] = next(
                district["name"] for district in recruiting_districts if district["id"] == organisation.get("recruitment_area"))
        except StopIteration:
            organisation["recruitment_area"] = None

        brreg_activity_codes = repo.get_brreg_activity_codes()

        organisation["brreg_activity_code"] = [code for code in brreg_activity_codes if
                                               code["code"] in organisation.get("brreg_activity_code", [])]

        activity_types = [type["flod_activity_types"] for type in organisation.get("brreg_activity_code")]
        activity_types = [y for x in activity_types for y in x]

        organisation["flod_activity_type"] = [type for type in activity_types if
                                              type["id"] in organisation.get("flod_activity_type", [])]

        for key, value in organisation.items():
            if value == "" or value is None:
                del organisation[key]

        return render_org_template(
            'org_info.html',
            organisation_id,
            requires_owner=False,
            organisation=organisation
        )
    except requests.exceptions.ConnectionError:
        app.logger.exception('Request failed')
        return "", 500
    except requests.exceptions.HTTPError as e:
        abort(e.response.status_code)
def umbrella_organisation_new():
    if not (current_user.is_authenticated() and current_user.is_aktorregister_admin()):
        abort(401)

    flod_activity_types = repo.get_flod_activity_types()
    brreg_activity_codes = repo.get_brreg_activity_codes()

    try:
        return render_flod_template(
            'umbrella_organisation_detail.html',
            umbrella_organisation=json.dumps(None),
            auth=repo.get_user(current_user.user_id, current_user.user_id),
            brreg_activity_codes=brreg_activity_codes,
            flod_activity_types=flod_activity_types
        )
    except requests.exceptions.ConnectionError:
        app.logger.exception('Request failed')
        return "", 500
def register_org():
    if not (current_user.is_idporten_user() or current_user.is_aktorregister_admin()):
        abort(401)

    """Render home page."""
    try:
        recruiting_districts = repo.get_districts()
        districts = repo.get_districts_without_whole_trd()

        brreg_activity_codes = repo.get_brreg_activity_codes()

        return render_flod_template(
            'register_org.html',
            districts=json.dumps(districts),
            recruiting_districts=json.dumps(recruiting_districts),
            brreg_activity_codes=json.dumps(brreg_activity_codes)
        )
    except requests.exceptions.ConnectionError:
        app.logger.exception('Request failed')
        return "", 500
def organisations_list():
    allowed_args = ["name", "brreg_activity_code", "flod_activity_type", "area"]

    params = {}
    for arg in allowed_args:
        if arg in request.args and request.args[arg]:
            params[arg] = request.args[arg]

    try:
        flod_activity_types = repo.get_flod_activity_types()
        brreg_activity_codes = repo.get_brreg_activity_codes()
        districts = repo.get_districts_without_whole_trd()

        if params:
            if current_user.is_anonymous():
                user_id = None
            else:
                user_id = current_user.user_id

            organisations = repo.get_all_organisations(params, user_id)
        else:
            organisations = []

        emails = []
        if current_user.is_authenticated() and current_user.is_aktorregister_admin():
            emails = [email for email in (o.get('email_address') for o in organisations) if email]
            emails += [email for email in (o.get('local_email_address') for o in organisations) if email]

        return render_flod_template(
            'organisations_list.html',
            organisations=organisations,
            params=params,
            emails=json.dumps(emails),
            brreg_activity_codes=brreg_activity_codes,
            flod_activity_types=flod_activity_types,
            districts=districts
        )
    except requests.exceptions.ConnectionError:
        app.logger.exception('Request failed')
        return "", 500
def edit_organisation(organisation_id):
    try:
        recruiting_districts = repo.get_districts()
        districts = repo.get_districts_without_whole_trd()
        brreg_activity_codes = repo.get_brreg_activity_codes()

        organisation = repo.get_organisation(
            organisation_id, getattr(current_user, 'user_id', None))

        return render_org_template(
            'edit_org.html',
            organisation_id,
            organisation=json.dumps(organisation),
            districts=json.dumps(districts),
            recruiting_districts=json.dumps(recruiting_districts),
            brreg_activity_codes=json.dumps(brreg_activity_codes)
        )
    except requests.exceptions.ConnectionError:
        app.logger.exception('Request failed')
        return "", 500
    except requests.exceptions.HTTPError as e:
        abort(e.response.status_code)