コード例 #1
0
class Tag(StoredObject):
    _id = StringField(primary=True)
    date_created = DateTimeField(validate=True, auto_now_add=True)
    date_modified = DateTimeField(validate=True, auto_now=True)
    value = StringField(default='default', validate=MinLengthValidator(5))
    keywords = StringField(default=['keywd1', 'keywd2'],
                           validate=MinLengthValidator(5),
                           list=True)
    _meta = {'optimistic': True}
コード例 #2
0
 class Foo(StoredObject):
     _id = IntegerField()
     test_field_max = StringField(list=False,
                                  validate=[
                                      MaxLengthValidator(5),
                                  ])
     test_field_min = StringField(list=False,
                                  validate=[
                                      MinLengthValidator(5),
                                  ])
コード例 #3
0
 class Foo(StoredObject):
     _meta = {
         'optimistic': True
     }
     _id = StringField()
     my_bar = ForeignField('Bar', backref='my_foos')
     my_other_bar = ForeignField('Bar', backref='my_foos')
コード例 #4
0
    def test_validate_string_list(self):
        '''
        Assert that the field cannot be saved with lists containing any type other than string
        '''
        test_lists = [
            [
                1,
                2,
                3,
            ],
            [
                4.0,
                4.0,
            ],
            [
                datetime.datetime.now(),
                datetime.datetime.now(),
            ],
        ]

        Schema = create_schema('string_list_schema',
                               field=StringField(
                                   list=True,
                                   validate=True,
                               ))

        test_row = Schema()
        for test_list in test_lists:
            test_row.field = test_list
            self.assertRaises(Exception, test_row.save)
コード例 #5
0
class TwoFactorUserSettings(AddonUserSettingsBase):
    totp_secret = StringField()  # hexadecimal
    totp_drift = IntegerField()
    is_confirmed = BooleanField(default=False)

    @property
    def totp_secret_b32(self):
        return b32encode(unhexlify(self.totp_secret))

    @property
    def otpauth_url(self):
        return 'otpauth://totp/OSF:{}?secret={}'.format(
            self.owner.username, self.totp_secret_b32)

    def to_json(self, user):
        rv = super(TwoFactorUserSettings, self).to_json(user)
        rv.update({
            'is_confirmed': self.is_confirmed,
            'secret': self.totp_secret_b32,
            'drift': self.totp_drift,
            'otpauth_url': self.otpauth_url,
        })
        return rv

    ###################
    # Utility methods #
    ###################

    def verify_code(self, code):
        accepted, drift = accept_totp(key=self.totp_secret,
                                      response=code,
                                      drift=self.totp_drift)
        if accepted:
            self.totp_drift = drift
            return True
        return False

    #############
    # Callbacks #
    #############

    def on_add(self):
        # TODO(hrybacki, sloria): push status message shouldn't need a session
        push_status_message('Please <a href="#TfaVerify">activate your'
                            ' device</a> before continuing.')
        super(TwoFactorUserSettings, self).on_add()
        self.totp_secret = _generate_seed()
        self.totp_drift = 0
        self.is_confirmed = False

    def on_delete(self):
        if self.is_confirmed:
            push_status_message('Successfully deauthorized two-factor'
                                ' authentication. Please delete the'
                                ' verification code on your device.')
        super(TwoFactorUserSettings, self).on_delete()
        self.totp_secret = None
        self.totp_drift = 0
        self.is_confirmed = False
コード例 #6
0
def create_schema(schema_name, **fields):

    fields['_id'] = StringField(primary=True)
    fields['_meta'] = {'optimistic': True}

    schema = type(schema_name, (StoredObject, ), fields)

    schema.set_storage(PickleStorage(schema_name))

    return schema
コード例 #7
0
class TestSchema(StoredObject):

    _id = StringField(primary=True)

    # Simple fields

    intfield = IntegerField(list=False, validate=True)
    floatfield = FloatField(list=False, validate=True)
    boolfield = BooleanField(list=False, validate=True)
    datetimefield = DateTimeField(list=False, validate=True)
    stringfield = StringField(list=False, validate=True)
    regexfield = StringField(list=False, validate=RegexValidator('^foo$'))
    urlfield = StringField(list=False, validate=URLValidator())

    int_min_field = IntegerField(validate=MinValueValidator(3))
    int_max_field = IntegerField(validate=MaxValueValidator(15))
    string_min_field = StringField(validate=MinLengthValidator(3))
    string_max_field = StringField(validate=MaxLengthValidator(15))

    # List fields

    # int_list = IntegerField(list=True, validate=MinValueValidator(3))
    # float_list = FloatField(list=True, validate=True)
    # bool_list = BooleanField(list=True, validate=True)
    # datetime_list = DateTimeField(list=True, default=[])
    # string_list = StringField(list=True, validate=True)

    _meta = {'optimistic': True}
コード例 #8
0
ファイル: models.py プロジェクト: wearpants/osf.io
class TwoFactorUserSettings(AddonUserSettingsBase):
    totp_secret = StringField()  # hexadecimal
    totp_drift = IntegerField()
    is_confirmed = BooleanField(default=False)

    @property
    def totp_secret_b32(self):
        return b32encode(unhexlify(self.totp_secret))

    @property
    def otpauth_url(self):
        return 'otpauth://totp/OSF:{}?secret={}'.format(
            self.owner.username, self.totp_secret_b32)

    def to_json(self, user):
        rv = super(TwoFactorUserSettings, self).to_json(user)
        rv.update({
            'is_enabled': True,
            'is_confirmed': self.is_confirmed,
            'secret': self.totp_secret_b32,
            'drift': self.totp_drift,
        })
        return rv

    ###################
    # Utility methods #
    ###################

    def verify_code(self, code):
        accepted, drift = accept_totp(key=self.totp_secret,
                                      response=code,
                                      drift=self.totp_drift)
        if accepted:
            self.totp_drift = drift
            return True
        return False

    #############
    # Callbacks #
    #############

    def on_add(self):
        super(TwoFactorUserSettings, self).on_add()
        self.totp_secret = _generate_seed()
        self.totp_drift = 0
        self.is_confirmed = False

    def on_delete(self):
        super(TwoFactorUserSettings, self).on_delete()
        self.totp_secret = None
        self.totp_drift = 0
        self.is_confirmed = False
コード例 #9
0
    def test_validate_min_length_list(self):
        '''
        Assert that a list with MinLengthValidator(n) cannot
        be saved with a length less than n.
        '''
        list_of_items_for_min = [
            [
                1,
                2,
                3,
                4,
                5,
            ],
            [
                'whoa',
                'whoa',
            ],
            [
                'awesome',
                'awesome',
                'awesome',
                'awesome',
            ],
            [
                'awesome',
                'awesome',
                'awesome',
                'awesome',
                'awesome',
                'awesome',
                'awesome',
                'awesome',
            ],
        ]

        Schema = create_schema(
            'min_length_schema',
            field=StringField(
                list=True,
                validate=MinLengthValidator(5),
                list_validate=[MinLengthValidator(5),
                               MaxLengthValidator(7)]))
        test_row = Schema()
        for test_list in list_of_items_for_min:
            test_row.field = test_list
            self.assertRaises(Exception, test_row.save)
コード例 #10
0
    def test_validate_max_length_list(self):
        '''
        Assert that a list with MaxLengthValidator(n) cannot
        be saved with a length greater than n.
        '''
        list_of_items_for_max = [
            [
                1,
                2,
                3,
                4,
                5,
            ],
            [
                'whoaaaaa',
                'whoaaaaa',
            ],
            [
                'awesome',
                'aweseome',
                'awesome',
                'awesome',
            ],
            [
                'awesome',
                'awesome',
                'awesome',
                'awesome',
                'awesome',
                'awesome',
                'awesome',
                'awesome',
            ],
        ]

        Schema = create_schema(
            'max_length_schema',
            field=StringField(
                list=True,
                validate=MaxLengthValidator(7),
                list_validate=[MinLengthValidator(5),
                               MaxLengthValidator(7)]))
        # import pdb; pdb.set_trace()
        test_row = Schema()
コード例 #11
0
 class Foo(StoredObject):
     _id = IntegerField()
     url_field = StringField(list=False, validate=[URLValidator()])
コード例 #12
0
 class Bar(StoredObject):
     _meta = {'optimistic': True}
     _id = StringField()
コード例 #13
0
 class Foo(StoredObject):
     _id = IntegerField()
     field = StringField(list=True, validate=True)
コード例 #14
0
 class Foo(StoredObject):
     _id = IntegerField()
     test_field = StringField(list=True,
                              validate=MaxLengthValidator(3),
                              list_validate=MinLengthValidator(3))