Exemplo n.º 1
0
class SettingsSchema(mm.Schema):
    admin_principals = PrincipalList(allow_groups=True)
    authorized_principals = PrincipalList(allow_groups=True)
    tileserver_url = fields.String(validate=[
        validate.URL(schemes={'http', 'https'}),
        lambda value: all(x in value for x in ('{x}', '{y}', '{z}'))
    ],
                                   allow_none=True)
    booking_limit = fields.Int(validate=[validate.Range(min=1)])
    notifications_enabled = fields.Bool()
    notification_before_days = fields.Int(
        validate=[validate.Range(min=1, max=30)])
    notification_before_days_weekly = fields.Int(
        validate=[validate.Range(min=1, max=30)])
    notification_before_days_monthly = fields.Int(
        validate=[validate.Range(min=1, max=30)])
    end_notifications_enabled = fields.Bool()
    end_notification_daily = fields.Int(
        validate=[validate.Range(min=1, max=30)])
    end_notification_weekly = fields.Int(
        validate=[validate.Range(min=1, max=30)])
    end_notification_monthly = fields.Int(
        validate=[validate.Range(min=1, max=30)])
    excluded_categories = ModelList(Category)
    grace_period = fields.Int(validate=[validate.Range(min=0, max=24)],
                              allow_none=True)
Exemplo n.º 2
0
class SettingsSchema(mm.Schema):
    admin_principals = PrincipalList(allow_groups=True)
    authorized_principals = PrincipalList(allow_groups=True)
    managers_edit_rooms = fields.Bool()
    tileserver_url = fields.String(validate=validate.URL(schemes={'http', 'https'}), allow_none=True)
    booking_limit = fields.Int(validate=not_empty)
    notifications_enabled = fields.Bool()
    notification_before_days = fields.Int(validate=validate.Range(min=1, max=30))
    notification_before_days_weekly = fields.Int(validate=validate.Range(min=1, max=30))
    notification_before_days_monthly = fields.Int(validate=validate.Range(min=1, max=30))
    end_notifications_enabled = fields.Bool()
    end_notification_daily = fields.Int(validate=validate.Range(min=1, max=30))
    end_notification_weekly = fields.Int(validate=validate.Range(min=1, max=30))
    end_notification_monthly = fields.Int(validate=validate.Range(min=1, max=30))
    excluded_categories = ModelList(Category)
    grace_period = fields.Int(validate=validate.Range(min=0, max=24), allow_none=True)

    @validates('tileserver_url')
    def _check_tileserver_url_placeholders(self, tileserver_url, **kwargs):
        if tileserver_url is None:
            return
        missing = {x for x in ('{x}', '{y}', '{z}') if x not in tileserver_url}
        if missing:
            # validated client-side, no i18n needed
            raise ValidationError('Missing placeholders: {}'.format(', '.join(missing)))
Exemplo n.º 3
0
class EquipmentTypeArgs(mm.Schema):
    class Meta:
        rh_context = ('equipment_type',)

    name = fields.String(validate=validate.Length(min=2), required=True)
    features = ModelList(RoomFeature, load_default=[])

    @validates('name')
    def _check_name_unique(self, name, **kwargs):
        equipment_type = self.context['equipment_type']
        query = EquipmentType.query.filter(func.lower(EquipmentType.name) == name.lower())
        if equipment_type:
            query = query.filter(EquipmentType.id != equipment_type.id)
        if query.has_rows():
            raise ValidationError(_('Name must be unique'))
Exemplo n.º 4
0
class SettingsSchema(mm.Schema):
    tileserver_url = fields.String(validate=[
        validate.URL(schemes={'http', 'https'}),
        lambda value: all(x in value for x in ('{x}', '{y}', '{z}'))
    ])
    booking_limit = fields.Int(validate=[validate.Range(min=1)])
    notifications_enabled = fields.Bool()
    notification_before_days = fields.Int(
        validate=[validate.Range(min=1, max=30)])
    notification_before_days_weekly = fields.Int(
        validate=[validate.Range(min=1, max=30)])
    notification_before_days_monthly = fields.Int(
        validate=[validate.Range(min=1, max=30)])
    end_notifications_enabled = fields.Bool()
    end_notification_daily = fields.Int(
        validate=[validate.Range(min=1, max=30)])
    end_notification_weekly = fields.Int(
        validate=[validate.Range(min=1, max=30)])
    end_notification_monthly = fields.Int(
        validate=[validate.Range(min=1, max=30)])
    excluded_categories = ModelList(Category)

    class Meta:
        strict = True  # TODO: remove with marshmallow 3
Exemplo n.º 5
0
class RHEquipmentTypes(RHRoomBookingAdminBase):
    def _process_args(self):
        id_ = request.view_args.get('equipment_type_id')
        self.equipment_type = EquipmentType.get_one(
            id_) if id_ is not None else None

    def _dump_equipment_types(self):
        query = EquipmentType.query.options(joinedload('features')).order_by(
            EquipmentType.name)
        return admin_equipment_type_schema.dump(query, many=True)

    def _get_room_counts(self):
        query = (db.session.query(RoomEquipmentAssociation.c.equipment_id,
                                  db.func.count()).group_by(
                                      RoomEquipmentAssociation.c.equipment_id))
        return dict(query)

    def _jsonify_one(self, equipment_type):
        counts = self._get_room_counts()
        eq = admin_equipment_type_schema.dump(equipment_type)
        eq['num_rooms'] = counts.get(eq['id'], 0)
        return jsonify(eq)

    def _jsonify_many(self):
        counts = self._get_room_counts()
        equipment_types = self._dump_equipment_types()
        for eq in equipment_types:
            eq['num_rooms'] = counts.get(eq['id'], 0)
        return jsonify(equipment_types)

    def _process_GET(self):
        if self.equipment_type:
            return self._jsonify_one(self.equipment_type)
        else:
            return self._jsonify_many()

    def _process_DELETE(self):
        db.session.delete(self.equipment_type)
        db.session.flush()
        return '', 204

    @use_kwargs({
        'name':
        fields.String(validate=validate.Length(min=2), required=True),
        'features':
        ModelList(RoomFeature, missing=[])
    })
    def _process_POST(self, name, features):
        self._check_conflict(name)
        equipment_type = EquipmentType(name=name, features=features)
        db.session.add(equipment_type)
        db.session.flush()
        return self._jsonify_one(equipment_type), 201

    @use_kwargs({
        'name': fields.String(validate=validate.Length(min=2)),
        'features': ModelList(RoomFeature)
    })
    def _process_PATCH(self, name=None, features=None):
        if name is not None:
            self._check_conflict(name)
            self.equipment_type.name = name
        if features is not None:
            self.equipment_type.features = features
        db.session.flush()
        return self._jsonify_one(self.equipment_type)

    def _check_conflict(self, name):
        query = EquipmentType.query.filter(
            db.func.lower(EquipmentType.name) == name.lower())
        if self.equipment_type:
            query = query.filter(EquipmentType.id != self.equipment_type.id)
        if query.has_rows():
            abort(422, messages={'name': [_('Name must be unique')]})