class UserGroupManager(AlchemyManager): model = UserGroup fields = ('user_id', 'group_id') create_fields = ( 'user_id', 'group_id', ) update_fields = tuple() list_fields = ('user_id', 'group_id') _field_validators = { 'user_id': StringField('user_id', required=True), 'group_id': StringField('group_id', required=True) }
class UserManager(AlchemyManager): model = User fields = ('id', 'username', 'first_name') create_fields = ('username', 'first_name') update_fields = ('first_name', ) _field_validators = { # username must be between 3 and 20 alphanumeric characters 'username': StringField('username', minimum=3, regex=re.compile(r'^[a-zA-Z0-9]{3,20}'), required=True), 'first_name': StringField('first_name', required=True) }
def test_translate_keeps_undefined_fields(self): """ Tests that any fields not in the defined dictionary are still included. """ field_dict = [StringField('field1'), IntegerField('field2')] field = DictField('f', required=True, field_list=field_dict) input_vals = dict(field1='hey', field2='5', field3='Who Cares?') resp = field.translate(input_vals, validate=True) self.assertIn('field3', resp)
def test_translate_success(self): """ Tests the expected conditions for the translate method. """ field_dict = [StringField('field1'), IntegerField('field2')] field = DictField('f', required=True, field_list=field_dict) input_vals = dict(field1='hey', field2='5') resp = field.translate(input_vals, validate=True) self.assertDictEqual(dict(field1='hey', field2=5), resp)
class GroupManager(AlchemyManager): model = Group fields = ('id', 'name', 'users.id', 'users.username', 'users.first_name') create_fields = ('name', ) update_fields = tuple() list_fields = ( 'id', 'name', ) _field_validators = { 'name': StringField('name', minimum=3, maximum=20), 'id': IntegerField('id') }
class HelloWorldViewset(ResourceBase): namespace = name_space manager = MM1() resource_name = 'myresource' _relationships = [ Relationship('related', property_map={'related': 'id'}, relation='ComplimentaryViewset') ] @apimethod(methods=['GET']) @translate(fields=[StringField('content')], validate=True) def hello(cls, request, *args, **kwargs): return cls(properties=request.query_args)
class PostManager(AlchemyManager): model = Post fields = ( 'id', 'username', 'post_text', 'comments.id', ) create_fields = ( 'username', 'post_text', ) _field_validators = { 'username': StringField('username', minimum=3, regex=re.compile(r'^[a-zA-Z0-9]+$')) }
class ItemManager(BaseManager): """Manager that interops between ripozo and grpc. Provides convience functions primarily for basic CRUD. """ fields = ( 'id', 'name', ) field_validators = [ IntegerField('id', required=True), StringField('name', required=True), ] def create(self, values, *args, **kwargs): raise NotImplementedError() def delete(self, lookup_keys, *args, **kwargs): raise NotImplementedError() def retrieve(self, lookup_keys, *args, **kwargs): """Retrieve a single item and nothing more as a dictionary. Args: lookup_keys (dict): Taken from url_params on flask request. Used to lookup a item and its associated values. Returns: dict or None: Properties of the retrieved item or None if no such item found. """ resp = items_conn.stub.GetItem( items_pb2.GetItemRequest(id=lookup_keys['id'])) item = resp.item if item: return dict(id=item.id, name=item.name) def retrieve_list(self, filters, *args, **kwargs): raise NotImplementedError() def update(self, lookup_keys, updates, *args, **kwargs): raise NotImplementedError()
def test_translate_subfield_failure(self): """ Tests that when one of the child fields fails then this field as a whole will fail. """ field_dict = [ StringField('field1'), IntegerField('field2', maximum=10) ] field = DictField('f', required=True, field_list=field_dict) input_vals = dict(field1='hey', field2='notanumber') self.assertRaises(TranslationException, field.translate, input_vals, validate=True) input_vals = dict(field1='hey', field2='11') self.assertRaises(ValidationException, field.translate, input_vals, validate=True)
def test_validate_regex(self): f = StringField('field', regex=re.compile(r'^something$')) self.assertRaises(ValidationException, f._validate, 'notsomething') original = 'something' new = f._validate(original) self.assertEqual(original, new)