Пример #1
0
class UserSchema(BaseSchema):
    username = EmqString(required=True)
    email = EmqEmail(required=True)
    password = EmqString(required=True, len_min=6, len_max=100, load_only=True)
    enable = EmqInteger(allow_none=True)
    nickname = EmqString(allow_none=True)
    phone = EmqString(allow_none=True, len_max=15)
    department = EmqString(allow_none=True)
    lastRequestTime = EmqDateTime(allow_none=True)
    loginTime = EmqDateTime(allow_none=True)
    expiresAt = EmqDateTime(allow_none=True)
    roleIntID = EmqInteger(allow_none=True)
    userAuthType = EmqInteger(allow_none=True, validate=OneOf([1, 2]))
    groups = EmqList(allow_none=True, list_type=str, load_only=True)
    tenantID = EmqString(dump_only=True)

    @validates('username')
    def validate_username(self, value):
        if value in current_app.config.get('RESERVED'):
            raise FormInvalid(field='username')

    @validates('email')
    def email_is_exist(self, value):
        try:
            split_email = value.split('@')[0]
            if split_email in current_app.config.get('RESERVED'):
                raise FormInvalid(field='email')
        except Exception:
            raise FormInvalid(field='email')
        email = User.query.filter(User.email == value).first()
        if email:
            raise DataExisted(field='email')

    @post_load
    def handle_user_auth_type(self, data):
        if 'user_id' not in g:
            data['userAuthType'] = 1
        groups_uid = self.get_request_data(key='groups')
        auth_type = data.get('userAuthType')
        if auth_type not in (1, 2):
            raise FormInvalid(field='userAuthType')
        if auth_type == 2 and groups_uid:
            groups = Group.query \
                .filter_tenant(tenant_uid=g.tenant_uid) \
                .filter(Group.groupID.in_(set(groups_uid))).all()
            if len(groups_uid) != len(groups):
                raise DataNotFound(field='groups')
            data['groups'] = groups
        else:
            data.pop('groups', None)
        return data
Пример #2
0
class UpdateUserSchema(BaseSchema):
    roleIntID = EmqInteger(required=True)
    enable = EmqInteger(required=True)
    expiresAt = EmqDateTime(allow_none=True)
    phone = EmqString(allow_none=True, len_max=15)
    userAuthType = EmqInteger(allow_none=True)
    groups = EmqList(allow_none=True, list_type=str, load_only=True)

    @post_load
    def handle_user_auth_type(self, data):
        if 'user_id' not in g:
            data['userAuthType'] = 1
        groups_uid = self.get_request_data(key='groups')
        auth_type = data.get('userAuthType')
        if auth_type not in (1, 2):
            raise FormInvalid(field='userAuthType')
        if auth_type == 2 and groups_uid:
            groups = Group.query \
                .filter_tenant(tenant_uid=g.tenant_uid) \
                .filter(Group.groupID.in_(set(groups_uid))).all()
            if len(groups_uid) != len(groups):
                raise DataNotFound(field='groups')
            data['groups'] = groups
        else:
            data.pop('groups', None)
        return data
Пример #3
0
class ApplicationSchema(BaseSchema):
    """ application management """

    appID = EmqString(dump_only=True)
    appName = EmqString(required=True)
    appToken = EmqString(dump_only=True)
    expiredAt = EmqDateTime(allow_none=True)  # expired time
    description = EmqString(allow_none=True, len_max=300)
    appStatus = EmqInteger(required=True, validate=OneOf([0, 1]))
    userIntID = EmqInteger(allow_none=True)
    roleIntID = EmqInteger(required=True)  # app role id
    groups = EmqList(allow_none=True, list_type=str, load_only=True)  # product uid

    @validates('appName')
    def validate_app_name(self, value):
        if self._validate_obj('appName', value):
            return
        app = Application.query \
            .filter_tenant(tenant_uid=g.tenant_uid) \
            .filter(Application.appName == value).first()
        if app:
            raise DataExisted(field='appName')

    @post_load
    def handle_app_groups(self, in_data):
        groups_uid = in_data.get('groups')
        if not groups_uid:
            return in_data
        groups = Group.query \
            .filter_tenant(tenant_uid=g.tenant_uid) \
            .filter(Group.groupID.in_(set(groups_uid))).all()
        if len(groups_uid) != len(groups):
            raise DataNotFound(field='groups')
        in_data['groups'] = groups
        return in_data
Пример #4
0
class DeviceEventSchema(BaseSchema):
    class Meta:
        additional = ('deviceID', )

    msgTime = EmqDateTime(allow_none=True)
    streamID = EmqString(required=True)
    topic = EmqString(required=True)
    dataType = EmqInteger(required=True)
    data = EmqString(required=True)
    responseResult = EmqString(required=True)
Пример #5
0
class PublishLogSchema(PublishSchema):
    msgTime = EmqDateTime(dump_only=True)
    payload = EmqDict()
    publishStatus = EmqInteger(dump_only=True)

    @post_dump
    def dump_payload(self, data):
        """ payload type dict to json"""

        payload = data.get('payload')
        if payload:
            data['payload'] = json.dumps(payload)
        return data
Пример #6
0
class TimerPublishSchema(BaseSchema):
    taskName = EmqString(required=True)
    deviceID = EmqString(required=True)
    topic = EmqString(required=True, len_max=500)  # publish topic
    payload = EmqString(required=True, len_max=10000)  # publish payload
    timerType = EmqInteger(required=True, validate=OneOf([1, 2]))
    intervalTime = EmqDict(allow_none=True)
    crontabTime = EmqDateTime(allow_none=True)
    deviceIntID = EmqInteger(allow_none=True)  # client index id
    taskStatus = EmqInteger(dump_only=True)

    @post_load
    def handle_data(self, data):
        data['taskStatus'] = 2
        data = self.validate_timer_format(data)
        result = PublishSchema().load({**data}).data
        data['deviceIntID'] = result['deviceIntID']
        data['payload'] = result['payload']
        data['topic'] = result['topic'] if result.get('topic') else None
        data['payload'] = json.loads(data['payload'])
        return data

    @staticmethod
    def validate_timer_format(data):
        timer_type = data.get('timerType')
        interval_time = data.get('intervalTime')
        crontab_time = data.get('crontabTime')
        if timer_type == 1 and crontab_time and not interval_time:
            date_now = arrow.now(tz=current_app.config['TIMEZONE']).shift(
                minutes=+2)
            try:
                crontab_time = arrow.get(crontab_time)
            except ParserError:
                raise FormInvalid(field='crontabTime')
            if crontab_time < date_now:
                FormInvalid(field='crontabTime')
        elif timer_type == 2 and interval_time and not crontab_time:
            check_status = check_interval_time(interval_time)
            if not check_status:
                raise FormInvalid(field='intervalTime')
        else:
            raise FormInvalid(field='timerType')
        return data
Пример #7
0
class BaseDeviceSchema:
    deviceName = EmqString(required=True)
    deviceType = EmqInteger(required=True,
                            validate=OneOf([1, 2]))  # 1:endDevice. 2:gateway
    productID = EmqString(required=True, len_max=6)
    authType = EmqInteger(required=True, validate=OneOf([1,
                                                         2]))  # 1:token 2:cert
    carrier = EmqInteger()
    upLinkNetwork = EmqInteger(allow_none=True, validate=OneOf(range(1, 8)))
    deviceID = EmqString(allow_none=True, len_min=8, len_max=36)
    deviceUsername = EmqString(allow_none=True, len_min=8, len_max=36)
    token = EmqString(allow_none=True, len_min=8, len_max=36)
    location = EmqString(allow_none=True)
    latitude = EmqFloat(allow_none=True)
    longitude = EmqFloat(allow_none=True)
    blocked = EmqInteger(allow_none=True)
    manufacturer = EmqString(allow_none=True)
    serialNumber = EmqString(allow_none=True)
    softVersion = EmqString(allow_none=True)
    hardwareVersion = EmqString(allow_none=True)
    deviceConsoleIP = EmqString(allow_none=True)
    deviceConsoleUsername = EmqString(allow_none=True)
    deviceConsolePort = EmqInteger(allow_none=True)
    mac = EmqString(allow_none=True)
    metaData = EmqJson(allow_none=True)
    description = EmqString(allow_none=True, len_max=300)
    deviceStatus = EmqInteger(dump_only=True)
    lastConnection = EmqDateTime(dump_only=True)
    groups = EmqList(allow_none=True, list_type=str, load_only=True)
    certs = EmqList(allow_none=True, list_type=int, load_only=True)
    productType = EmqInteger(
        load_only=True)  # 1:endDevice product 2:gateway product
    scopes = fields.Nested(DeviceScopeSchema,
                           only='scope',
                           many=True,
                           dump_only=True)

    def __init__(self, *args, **kwargs):
        pass