<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2015 The Pycroft Authors. See the AUTHORS file.
# This file is part of the Pycroft project and licensed under the terms of
# the Apache License, Version 2.0. See the LICENSE file for details.
"""
    web.blueprints.facilities
    ~~~~~~~~~~~~~~

    This module defines view functions for /facilities
    :copyright: (c) 2012 by AG DSN.
"""
from operator import and_

from collections import defaultdict
from flask import (Blueprint, flash, jsonify, render_template, url_for,
                   redirect, request, abort)
from flask_login import current_user
from flask_wtf import FlaskForm as Form
from sqlalchemy.sql import and_, select, exists
from sqlalchemy.orm import joinedload, aliased

from pycroft import lib, config
from pycroft.helpers import facilities
from pycroft.helpers.i18n import gettext
from pycroft.helpers.net import sort_ports
from pycroft.lib.address import get_or_create_address
from pycroft.lib.facilities import get_overcrowded_rooms, create_room, edit_room, \
    RoomAlreadyExistsException, suggest_room_address_data
from pycroft.lib.infrastructure import create_patch_port, edit_patch_port, delete_patch_port, \
    PatchPortAlreadyExistsException
from pycroft.model import session
from pycroft.model.facilities import Room, Site, Building
from pycroft.model.port import PatchPort
from pycroft.model.property import CurrentProperty
from pycroft.model.user import User
from web.blueprints.access import BlueprintAccess
from web.blueprints.facilities.forms import (
    RoomLogEntry, PatchPortForm, CreateRoomForm, EditRoomForm)
from web.blueprints.helpers.log import format_room_log_entry
from web.blueprints.helpers.user import user_button
from web.blueprints.navigation import BlueprintNavigation
from .address import get_address_entity, address_entity_search_query
from .tables import (BuildingLevelRoomTable, RoomLogTable, SiteTable,
                     RoomOvercrowdedTable, PatchPortTable)

bp = Blueprint('facilities', __name__)
access = BlueprintAccess(bp, required_properties=['facilities_show'])
nav = BlueprintNavigation(bp, "Wohnheime", blueprint_access=access)

@bp.route('/')
def root():
    return redirect(url_for(".overview"))

@nav.navigate(u"Wohnheime")
@bp.route('/sites/')
def overview():
    return render_template(
        'facilities/site_overview.html',
        site_table=SiteTable(data_url=url_for('.overview_json')),
    )

@bp.route('/sites/json')
def overview_json():
    return jsonify(items=[{
            'site': {
                'title': site.name,
                'href': url_for("facilities.site_show", site_id=site.id)
            },
            'buildings': [{
                    'href': url_for("facilities.building_levels", building_shortname=building.short_name),
                    'title': building.street+" "+building.number
                } for building in facilities.sort_buildings(site.buildings)]
        } for site in Site.q.order_by(Site.name).all()])


@bp.route('/site/<int:site_id>')
def site_show(site_id):
    site = Site.get(site_id)
    buildings_list = facilities.sort_buildings(site.buildings)
    return render_template('facilities/site_show.html',
        buildings=buildings_list,
        page_title=site.name)


@bp.route('/building/<int:building_id>/')
@bp.route('/building/<building_shortname>/')
def building_show(building_id=None, building_shortname=None):
    building = facilities.determine_building(id=building_id, shortname=building_shortname)

    if building is None:
        flash(u"Gebäude existiert nicht!", 'error')
        abort(404)

    rooms_list = building.rooms
    return render_template('facilities/building_show.html',<commit_msg>Make `SiteTable` endpoint use `.value` methods<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2015 The Pycroft Authors. See the AUTHORS file.
# This file is part of the Pycroft project and licensed under the terms of
# the Apache License, Version 2.0. See the LICENSE file for details.
"""
    web.blueprints.facilities
    ~~~~~~~~~~~~~~

    This module defines view functions for /facilities
    :copyright: (c) 2012 by AG DSN.
"""
from operator import and_

from collections import defaultdict
from flask import (Blueprint, flash, jsonify, render_template, url_for,
                   redirect, request, abort)
from flask_login import current_user
from flask_wtf import FlaskForm as Form
from sqlalchemy.sql import and_, select, exists
from sqlalchemy.orm import joinedload, aliased

from pycroft import lib, config
from pycroft.helpers import facilities
from pycroft.helpers.i18n import gettext
from pycroft.helpers.net import sort_ports
from pycroft.lib.address import get_or_create_address
from pycroft.lib.facilities import get_overcrowded_rooms, create_room, edit_room, \
    RoomAlreadyExistsException, suggest_room_address_data
from pycroft.lib.infrastructure import create_patch_port, edit_patch_port, delete_patch_port, \
    PatchPortAlreadyExistsException
from pycroft.model import session
from pycroft.model.facilities import Room, Site, Building
from pycroft.model.port import PatchPort
from pycroft.model.property import CurrentProperty
from pycroft.model.user import User
from web.blueprints.access import BlueprintAccess
from web.blueprints.facilities.forms import (
    RoomLogEntry, PatchPortForm, CreateRoomForm, EditRoomForm)
from web.blueprints.helpers.log import format_room_log_entry
from web.blueprints.helpers.user import user_button
from web.blueprints.navigation import BlueprintNavigation
from .address import get_address_entity, address_entity_search_query
from .tables import (BuildingLevelRoomTable, RoomLogTable, SiteTable,
                     RoomOvercrowdedTable, PatchPortTable)

bp = Blueprint('facilities', __name__)
access = BlueprintAccess(bp, required_properties=['facilities_show'])
nav = BlueprintNavigation(bp, "Wohnheime", blueprint_access=access)

@bp.route('/')
def root():
    return redirect(url_for(".overview"))

@nav.navigate(u"Wohnheime")
@bp.route('/sites/')
def overview():
    return render_template(
        'facilities/site_overview.html',
        site_table=SiteTable(data_url=url_for('.overview_json')),
    )

@bp.route('/sites/json')
def overview_json():
    T = SiteTable
    return jsonify(items=[{
            'site': T.site.value(
                title=site.name,
                href=url_for("facilities.site_show", site_id=site.id)
            ),
            'buildings': [T.buildings.single_value(
                href=url_for("facilities.building_levels",
                             building_shortname=building.short_name),
                title=building.street_and_number
            ) for building in facilities.sort_buildings(site.buildings)]
        } for site in Site.q.order_by(Site.name).all()])


@bp.route('/site/<int:site_id>')
def site_show(site_id):
    site = Site.get(site_id)
    buildings_list = facilities.sort_buildings(site.buildings)
    return render_template('facilities/site_show.html',
        buildings=buildings_list,
        page_title=site.name)


@bp.route('/building/<int:building_id>/')
@bp.route('/building/<building_shortname>/')
def building_show(building_id=None, building_shortname=None):
    building = facilities.determine_building(id=building_id, shortname=building_shortname)

    if building is None:
        flash(u"Gebäude existiert nicht!", 'error')
        abort(404)

    rooms_list = building.rooms
    return render_template('facilities/building_show.html',
{'fraction_non_alphanumeric': 0.09758376718729142, 'fraction_numerical': 0.004271792818048324, 'mean_word_length': 4.279774489076814, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6329375', 'n_tokens_mistral': 2251, 'n_tokens_neox': 2151, 'n_words': 537}