Esempio n. 1
0
    def setUp(self):
        super().setUp()
        set_current_instance(self.instance)
        schema = [
            {
                'name': 'name',
                'type': 'string'
            },
            {
                'name': 'ref',
                'type': 'reference',
                'target': 'self'
            },
            {
                'name': 'user',
                'type': 'reference',
                'target': 'user'
            },
        ]
        self.klass = G(Klass, schema=schema, name='test', description='test')

        DataObject.load_klass(self.klass)
        self.object = G(DataObject, _klass=self.klass, name='a')

        self.user = G(User)
        DataObject.load_klass(self.klass)
        self.object_2 = G(DataObject,
                          _klass=self.klass,
                          name='b',
                          user=self.user.pk,
                          ref=self.object.pk)

        self.hla = G(DataObjectHighLevelApi, klass=self.klass)
        self.endpoint_url = reverse('v2:hla-objects-endpoint',
                                    args=[self.instance.name, self.hla.name])
Esempio n. 2
0
 def __init__(self, *args, **kwargs):
     # Skip loading cache during schema generation
     if 'context' in kwargs and kwargs['context'].get(
             'include_dynamic_fields', True):
         user_profile_klass = Klass.get_user_profile()
         DataObject.load_klass(user_profile_klass)
     super().__init__(*args, **kwargs)
def loaded_klass(klass):
    old_klass = getattr(DataObject, 'loaded_klass', None)
    DataObject.load_klass(klass)
    try:
        yield
    finally:
        if old_klass:
            DataObject.load_klass(old_klass)
Esempio n. 4
0
    def setUp(self):
        super().setUp()
        set_current_instance(self.instance)
        schema = [
            {
                'name': 'name',
                'type': 'string',
                'order_index': True,
                'filter_index': True
            },
            {
                'name': 'field2',
                'type': 'string',
                'order_index': True,
                'filter_index': True
            },
            {
                'name': 'field3',
                'type': 'string',
                'order_index': True,
                'filter_index': True
            },
            {
                'name': 'ref',
                'type': 'reference',
                'target': 'self'
            },
            {
                'name': 'user',
                'type': 'reference',
                'target': 'user'
            },
        ]
        self.klass = G(Klass, schema=schema, name='test', description='test')

        DataObject.load_klass(self.klass)
        self.object = G(DataObject,
                        _klass=self.klass,
                        name='a',
                        field2='b',
                        field3='c')

        self.user = G(User)
        DataObject.load_klass(self.klass)
        self.object = G(DataObject,
                        _klass=self.klass,
                        name='b',
                        field2='a',
                        field3='d',
                        ref=self.object.pk,
                        user=self.user.pk)

        self.hla = G(DataObjectHighLevelApi, klass=self.klass)
        self.get_url = reverse('v1:hla-objects-get',
                               args=[self.instance.name, self.hla.name])
        self.list_url = reverse('v1:hla-objects-list',
                                args=[self.instance.name])
Esempio n. 5
0
    def create(self, validated_data):
        user = validated_data['owner']
        user.save()

        # Add default ACL
        if 'acl' not in validated_data:
            validated_data['acl'] = {}
        if 'users' not in validated_data['acl']:
            validated_data['acl']['users'] = {}
        validated_data['acl']['users'].update(
            {str(user.id): DataObject.get_acl_permission_values()[:]})

        return super().create(validated_data)
Esempio n. 6
0
    def setUp(self):
        super().setUp()
        self.url = reverse(self.url_name, args=(self.instance.name, ))

        set_current_instance(self.instance)
        user_profile = Klass.get_user_profile()
        user_profile.schema = [{
            'name': 'int',
            'type': 'integer',
            'filter_index': True
        }]
        user_profile.save()

        # Add one random dataobject to make ids not in line with users
        G(DataObject)

        DataObject.load_klass(user_profile)
        self.user1 = User.objects.create(username='******',
                                         password='******',
                                         profile_data={'int': 100})
        self.user2 = User.objects.create(username='******',
                                         password='******',
                                         profile_data={'int': 150})
Esempio n. 7
0
    def test_updating_with_profile(self):
        profile_date = '2015-01-01T12:00:00.000000Z'
        data = {
            'username': '******',
            'password': '******',
            'profile': {
                'datetime': profile_date
            }
        }

        response = self.client.put(self.url, data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        user = User.objects.first()
        self.assertEqual(user.username, data['username'])
        self.assertTrue(user.check_password(data['password']))

        # Load bogus class to make sure correct one is always reloaded (test for regression)
        DataObject.load_klass(G(Klass))
        response = self.client.get(self.url)
        self.assertEqual(response.data['profile']['datetime'], {
            'type': 'datetime',
            'value': profile_date
        })
Esempio n. 8
0
def create_data_objects(klass):
    with loaded_klass(klass):
        for idx in range(10):
            kwargs = {'_klass': klass}
            with tempfile.NamedTemporaryFile(suffix='.ext') as tmp_file:
                tmp_file.write(b'File content %d' % idx)
                tmp_file.seek(0)
                for field in klass.schema:
                    if field['type'] == 'integer':
                        value = idx
                    elif field['type'] == 'string':
                        value = 'string_%d' % idx
                    elif field['type'] == 'file':
                        value = File(tmp_file)
                    elif field['type'] == 'geopoint':
                        value = Point(12.1, 54.2)
                    else:
                        continue
                    kwargs[field['name']] = value
                DataObject(**kwargs).save()
Esempio n. 9
0
 def initial(self, request, *args, **kwargs):
     initial = super().initial(request, *args, **kwargs)
     self.klass = Klass.get_user_profile()
     DataObject.load_klass(self.klass)
     return initial