Beispiel #1
0
    def get(self, facility_id):
        user = repo.get_user(request.cookies)
        anonymous = user is None

        facility = self.get_or_abort(facility_id)

        if anonymous or not repo.is_administrator(user):
            return marshal(facility, facility_fields)
        else:
            return marshal(facility, facility_fields_admin)
Beispiel #2
0
    def get(self):

        if 'name' in request.args and request.args['name'] == '':
            return marshal([], facility_fields)

        facilities = current_app.db_session.query(Facility).order_by(Facility.name.asc())

        max_capacity = None
        min_capacity = None
        if 'name' in request.args:
            name_filter = Facility.name.ilike('%%%s%%' % request.args['name'])
            short_code_filter = Facility.short_code.ilike(
                '%s' % request.args['name'])
            facilities = facilities.filter(or_(name_filter, short_code_filter))
        if 'bbox' in request.args:
            facilities = facilities.filter(
                Facility.pos.contained(bbox_to_polygon(request.args['bbox'])))
        if 'district' in request.args:
            facilities = facilities.filter(
                Facility.district_id == request.args['district'])
        if 'min_capacity' in request.args:
            try:
                min_capacity = int(request.args['min_capacity'])
                facilities = facilities.filter(
                    Facility.capacity >= min_capacity)
            except ValueError:
                abort(400, min_capacity='must be a valid integer')
        if 'max_capacity' in request.args:
            try:
                max_capacity = int(request.args['max_capacity'])
                facilities = facilities.filter(
                    Facility.capacity <= max_capacity)
            except ValueError:
                abort(400, max_capacity='must be a valid integer')
        if max_capacity and min_capacity and min_capacity > max_capacity:
            abort(400, max_capacity=('max_capacity must be greater than '
                                     'min_capacity'))

        if 'short_code' in request.args:
            facilities = facilities.filter(
                Facility.short_code == request.args['short_code'])

        if 'type' in request.args:
            try:
                type_id = int(request.args['type'])
                facilities = facilities.filter(
                    Facility.facility_type_id == type_id)
            except ValueError:
                abort(400, type='type must integer')

        if 'accessibility' in request.args:
            accessibility = request.args["accessibility"]
            keys = accessibility.split(",")
            facilities = facilities.filter(Facility.accessibility.has_all(keys))

        anonymous = False
        user = {}
        superuser = repo.is_super_admin(request.cookies)
        if not superuser:
            user = repo.get_user(request.cookies)
            anonymous = user is None

        # Do not show deleted facilities by default
        show_deleted = request.args.get('show_deleted', False)
        if not show_deleted:
            facilities = facilities.filter(Facility.is_deleted == False)

        # Do not show not published facilities by default
        show_not_published = request.args.get('show_not_published', False)
        if not show_not_published:
            facilities = facilities.filter(Facility.is_published == True)

        # NOTE: Admin should see all facilities, regular AD users only to see their own
        if not anonymous and not superuser and repo.is_administrator(user):
            if 'show_only_my_facilities' in request.args and request.args['show_only_my_facilities'] == 'True':
                my_facilities = repo.get_facilities_edit_credentials(user['id'], request.cookies)
                facilities = facilities.filter(Facility.id.in_(my_facilities))

        facilities = facilities.all()
        if anonymous or not repo.is_administrator(user):
            return marshal(facilities, facility_fields)

        return marshal(facilities, facility_fields_admin)