def restore_object(self, attrs, instance=None): if instance: metadata = JsonField.to_json(attrs.get('metadata')) if self.partial and metadata: if not isinstance(instance.metadata, dict): instance.metadata = {} instance.metadata.update(metadata) attrs['metadata'] = instance.metadata return super(ProjectSerializer, self).restore_object(attrs, instance) if 'request' in self.context: created_by = self.context['request'].user return Project( name=attrs.get('name'), organization=attrs.get('organization'), created_by=created_by, metadata=attrs.get('metadata'), ) return attrs
def test_publish_to_public_project_public_form(self): public_project = Project(name='demo', shared=True, metadata=json.dumps({'description': ''}), created_by=self.user, organization=self.user) public_project.save() self.project = public_project data = { 'owner': 'http://testserver/api/v1/users/%s' % self.project.organization.username, 'public': True, 'public_data': True, 'description': u'transportation_2011_07_25', 'downloadable': True, 'allows_sms': False, 'encrypted': False, 'sms_id_string': u'transportation_2011_07_25', 'id_string': u'transportation_2011_07_25', 'title': u'transportation_2011_07_25', 'bamboo_dataset': u'' } self._publish_xls_form_to_project(publish_data=data, merge=False) self.assertEquals(self.xform.shared, True) self.assertEquals(self.xform.shared_data, True)
def restore_object(self, attrs, instance=None): if instance: return super(ProjectSerializer, self)\ .restore_object(attrs, instance) if 'request' in self.context: created_by = self.context['request'].user return Project( name=attrs.get('name'), organization=attrs.get('organization'), created_by=created_by,) return attrs
def test_publish_to_public_project(self): public_project = Project(name='demo', shared=True, metadata=json.dumps({'description': ''}), created_by=self.user, organization=self.user) public_project.save() self.project = public_project self._publish_xls_form_to_project(public=True) self.assertEquals(self.xform.shared, True) self.assertEquals(self.xform.shared_data, True)
def test_user_can_view_public_projects(self): public_project = Project(name='demo', shared=True, metadata=json.dumps({'description': ''}), created_by=self.user, organization=self.user) public_project.save() alice_data = {'username': '******', 'email': '*****@*****.**'} self._login_user_and_profile(alice_data) view = ProjectViewSet.as_view({'get': 'retrieve'}) request = self.factory.get('/', **self.extra) response = view(request, pk=public_project.pk) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['public'], True) self.assertEqual(response.data['projectid'], public_project.pk) self.assertEqual(response.data['name'], 'demo')