コード例 #1
0
ファイル: models.py プロジェクト: cereigido/django-parsesync
    def _get_payload(self):
        self.payload = {}

        for field in self._meta.fields:
            field_class = field.__class__.__name__
            prepare_method = '_prepare_%s' % to_snake_case(field_class)
            parse_field_name = to_camel_case(field.column)

            # TODO: when a field is null, it should be removed from parse
            # ignoring "system" fields
            if field.name not in self.SYSTEM_FIELDS and hasattr(self, field.name) and getattr(self, field.name) is not None:
                if hasattr(self, prepare_method):
                    self.payload[parse_field_name] = getattr(self, prepare_method)(field)
                else:
                    self.payload[parse_field_name] = self._prepare_field(field)
コード例 #2
0
    def save(self, model, model_name, results):
        for item in results:
            object_id = item.get('objectId')
            updated_at = item.get('updatedAt')
            files = []

            try:
                instance = model.objects.get(object_id=object_id)
                print '\tUpdating Django %s.%s, last updated at %s...' % (model_name, object_id, updated_at)
            except model.DoesNotExist:
                print '\tCreating Django %s.%s, last updated at %s...' % (model_name, object_id, updated_at)
                instance = model()

            for key, value in item.items():
                snake_key = to_snake_case(key)

                if type(value) != dict:
                    setattr(instance, snake_key, value)
                elif value['__type'] == 'Date':
                    conv_value = datetime.strptime(value['iso'], "%Y-%m-%dT%H:%M:%S.000Z")
                    setattr(instance, snake_key, conv_value)
                elif value['__type'] == 'File':
                    if 'url' in value:
                        dl_file = urlretrieve(value['url'])
                        files.append([getattr(instance, snake_key), value['name'], File(open(dl_file[0]))])
                elif value['__type'] == 'Pointer':
                    setattr(instance, '%s_id' % value['className'].lower(), value['objectId'])
                else:
                    print 'Unhandled: %s' % value

            # avoiding Parse update, saving only locally
            instance.save_to_parse = False
            try:
                instance.save()
            except Exception, e:
                print 'Error [%s] ocurred while saving your content' % e

            # saving and associating files
            for f in files:
                f[0].save(f[1], f[2])

            self.psc.set_last_updated_item_from_parse(model, updated_at)
            self.psc.save()
コード例 #3
0
ファイル: tests.py プロジェクト: cereigido/django-parsesync
 def test_django_system_fields_are_snake_case(self):
     """ parsesync.models :: Django system fields are in snake_case """
     self.assertEquals(ParseModel.CREATED_AT_DJANGO_FIELD, to_snake_case(ParseModel.CREATED_AT_DJANGO_FIELD))
     self.assertEquals(ParseModel.UPDATED_AT_DJANGO_FIELD, to_snake_case(ParseModel.UPDATED_AT_DJANGO_FIELD))
     self.assertEquals(ParseModel.OBJECT_ID_DJANGO_FIELD, to_snake_case(ParseModel.OBJECT_ID_DJANGO_FIELD))
コード例 #4
0
ファイル: tests.py プロジェクト: cereigido/django-parsesync
 def test_string_is_converted_to_snake_case(self):
     """ parsesync :: Strings are properly converted to snake_case """
     self.assertEqual(to_snake_case("fooBar"), "foo_bar")