def page_links():
    over = [
        {
            "title": u"Forsiden",
            "path": "/"
        },
        {
            "title": u"Finn aktør",
            "path": "/organisations"
        }
    ]

    under = []

    if not current_user.is_authenticated() or current_user.is_idporten_user() or current_user.is_aktorregister_admin():
        over.append({
            "title": u"Registrer aktør",
            "path": "/register_org"
        })

    if current_user.is_authenticated():
        if current_user.is_aktorregister_admin():
            over.append({
                "title": u"Oppdater medlemsdata",
                "path": "/organisations/updatememberdata"
            })
            over.append({
                "title": u"Paraplyorganisasjoner",
                "path": "/umbrella_organisations"
            })

        if current_user.is_idporten_user():
            under.append(
                {
                    "title": u"Min profil",
                    "path": "/profile",
                    "right": True,
                    "requires_login": True,
                }
            )

    over.append({
        "title": u"Om Aktørbasen",
        "path": "https://www.trondheim.kommune.no/aktorbasen/",
        "external": True
    })

    links = {
        "over": over,
        "under": under
    }

    return links
def organisation_update_member_data():
    if not (current_user.is_authenticated() and current_user.is_aktorregister_admin()):
        abort(401)

    try:
        # Not ideal, we should have a fixed setting
        # Will cause 413 if exceeded
        app.config['MAX_CONTENT_LENGTH'] = 20 * 1024 * 1024  # 20 MB

        allowed_extensions = ['xml']
        messages = []
        updated_organisations = []

        if request.method == 'POST' and len(request.files) > 0:
            file = request.files['document']
            filename = file.filename
            if '.' in filename and filename.rsplit('.', 1)[1] in allowed_extensions:
                organisations = xmlParser.get_organisations_from_nif_idrettsraad_xml(file.stream)
                organisations_service = proxy.gui_service_name_to_service_proxy['organisations']
                updated_organisations = organisations_service.update_organisations(organisations, auth_token_username=current_user.user_id)
                messages.append({'status': 'success', 'message': 'Filen ble parset. Se under for hvilke organisasjoner som ble oppdatert.'})
            else:
                messages.append({'status': 'error', 'message': u'Ugyldig filtype. Filnavnet må være på formatet "filnavn.xml"'})

        return render_flod_template(
            'organisations_updatemembers.html',
            messages=messages,
            updated_organisations=updated_organisations
        )

    except requests.exceptions.ConnectionError:
        app.logger.exception('Request failed')
        return "", 500
def render_flod_template(template, **kwargs):
    stripped_user = None
    stripped_person = None

    user_mode = None

    if not current_user.is_anonymous():

        if current_user.is_idporten_user():
            person = authentication.get_current_person()
            stripped_person = {
                "name": person['name'],
                "uri": "/persons/%d" % person['person_id'],
                "id": person['person_id']
            }
            user_mode = 'soker'
        elif current_user.is_adfs_user() and current_user.is_aktorregister_admin():
            user_mode = 'admin'

        stripped_user = {
            "id": current_user.user_id,
            "private_id": current_user.private_id
        }

    return render_template(
        template,
        person=stripped_person,
        user=stripped_user,
        pages=page_links(),
        app_name=APP_NAME,
        user_mode=user_mode,
        **kwargs
    )
def umbrella_organisations_list():
    if not (current_user.is_authenticated() and current_user.is_aktorregister_admin()):
        abort(401)

    try:
        umbrella_organisations = repo.get_all_umbrella_organisations(
            auth_token_username=current_user.user_id
        )

        return render_flod_template(
            'umbrella_organisations_list.html',
            umbrella_organisations=umbrella_organisations
        )
    except requests.exceptions.ConnectionError:
        app.logger.exception('Request failed')
        return "", 500
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 home():
    """Render home page."""
    return render_flod_template(
        'home.html',
        can_register=not current_user.is_authenticated() or current_user.is_idporten_user() or current_user.is_aktorregister_admin()
    )