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 internal_notes(organisation_id):
    try:
        organisation = repo.get_organisation(
            organisation_id, getattr(current_user, 'user_id', None))

        notes = repo.get_notes(organisation_id, getattr(current_user, 'user_id', None))
        map_internal_notes_to_users(notes)

        return render_org_template(
            'internal_notes.html',
            organisation_id,
            organisation=json.dumps(organisation),
            internal_notes=json.dumps(notes)
        )
    except requests.exceptions.ConnectionError:
        app.logger.exception('Request failed')
    return "", 500
def add_org_members(organisation_id):
    try:
        organisation = repo.get_organisation(
            organisation_id, getattr(current_user, 'user_id', None))

        members = repo.get_members(
            organisation_id,
            auth_token_username=current_user.user_id
        )

        return render_org_template(
            'org_members.html',
            organisation_id,
            organisation=organisation,
            members=json.dumps(members)
        )
    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)