Beispiel #1
0
    def forms(self, request, **kwargs):
        """Add a form to a project or list forms for the project.

        The request key `xls_file` holds the XLSForm file object.
        """
        project = get_object_or_404(Project, pk=kwargs.get('pk'))
        if request.method.upper() == 'POST':
            survey = utils.publish_project_xform(request, project)

            if isinstance(survey, XForm):
                xform = XForm.objects.get(pk=survey.pk)
                serializer = XFormSerializer(xform,
                                             context={'request': request})

                return Response(serializer.data,
                                status=status.HTTP_201_CREATED)

            return Response(survey, status=status.HTTP_400_BAD_REQUEST)

        xforms = XForm.objects.filter(project=project)
        serializer = XFormSerializer(xforms,
                                     context={'request': request},
                                     many=True)

        return Response(serializer.data)
    def test_manager_can_update_xform(self):
        self._publish_xls_form_to_project()
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)
        view = XFormViewSet.as_view({'put': 'update'})
        description = 'DESCRIPTION'
        request = self.factory.get('/', **self.extra)
        xfs = XFormSerializer(instance=self.xform,
                              context={'request': request})
        data = json.loads(JSONRenderer().render(xfs.data))
        data.update({'public': True, 'description': description})

        self.assertFalse(self.xform.shared)

        request = self.factory.put('/', data=data, **self.extra)
        response = view(request, pk=self.xform.id)
        self.assertEqual(response.status_code, 400)
        self.assertFalse(self.xform.shared)

        role.ManagerRole.add(self.user, self.xform)
        request = self.factory.put('/', data=data, **self.extra)
        response = view(request, pk=self.xform.id)

        self.xform.reload()
        self.assertTrue(self.xform.shared)
        self.assertEqual(self.xform.description, description)
        self.assertEqual(response.data['public'], True)
        self.assertEqual(response.data['description'], description)
Beispiel #3
0
def _try_update_xlsform(request, xform, owner):
    if xform.instances.count() > 0:
        data = _(u"Cannot update the xls file in a form that has"
                 u" submissions")
        return Response(data, status=status.HTTP_400_BAD_REQUEST)

    survey = \
        utils.publish_xlsform(request, owner, xform.id_string)

    if isinstance(survey, XForm):
        serializer = XFormSerializer(xform, context={'request': request})

        return Response(serializer.data, status=status.HTTP_200_OK)

    return Response(survey, status=status.HTTP_400_BAD_REQUEST)
Beispiel #4
0
    def test_form_tags(self):
        self._publish_xls_form_to_project()
        view = XFormViewSet.as_view({
            'get': 'labels',
            'post': 'labels',
            'delete': 'labels'
        })
        list_view = XFormViewSet.as_view({
            'get': 'list',
        })
        formid = self.xform.pk

        # no tags
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.data, [])

        # add tag "hello"
        request = self.factory.post('/', data={"tags": "hello"}, **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 201)
        self.assertEqual(response.data, [u'hello'])

        # check filter by tag
        request = self.factory.get('/', data={"tags": "hello"}, **self.extra)
        self.form_data = XFormSerializer(self.xform,
                                         context={
                                             'request': request
                                         }).data
        response = list_view(request, pk=formid)
        self.assertNotEqual(response.get('Last-Modified'), None)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [self.form_data])

        request = self.factory.get('/', data={"tags": "goodbye"}, **self.extra)
        response = list_view(request, pk=formid)
        self.assertEqual(response.status_code, 200)
        self.assertNotEqual(response.get('Last-Modified'), None)
        self.assertEqual(response.data, [])

        # remove tag "hello"
        request = self.factory.delete('/',
                                      data={"tags": "hello"},
                                      **self.extra)
        response = view(request, pk=formid, label='hello')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.get('Last-Modified'), None)
        self.assertEqual(response.data, [])
Beispiel #5
0
    def create(self, request, *args, **kwargs):
        try:
            owner = _get_owner(request)
        except ValidationError as e:
            return Response({'message': e.messages[0]},
                            status=status.HTTP_400_BAD_REQUEST)

        survey = utils.publish_xlsform(request, owner)
        if isinstance(survey, XForm):
            xform = XForm.objects.get(pk=survey.pk)
            serializer = XFormSerializer(xform, context={'request': request})
            headers = self.get_success_headers(serializer.data)

            return Response(serializer.data,
                            status=status.HTTP_201_CREATED,
                            headers=headers)

        return Response(survey, status=status.HTTP_400_BAD_REQUEST)
Beispiel #6
0
 def test_xform_serializer_none(self):
     data = {
         'title': u'',
         'owner': None,
         'public': False,
         'public_data': False,
         'require_auth': False,
         'description': u'',
         'downloadable': False,
         'allows_sms': False,
         'uuid': u'',
         'instances_with_geopoints': False,
         'num_of_submissions': 0,
         'version': u'',
         'project': None,
         'created_by': None
     }
     self.assertEqual(data, XFormSerializer(None).data)
    def test_forms_endpoint_with_metadata(self):
        for data_type in ['supporting_doc', 'media', 'source']:
            self._add_form_metadata(self.xform, data_type, self.data_value,
                                    self.path)
        # /forms
        view = XFormViewSet.as_view({'get': 'retrieve'})
        formid = self.xform.pk
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 200)
        data = XFormSerializer(self.xform, context={'request': request}).data
        self.assertEqual(response.data, data)

        # /projects/[pk]/forms
        view = ProjectViewSet.as_view({'get': 'forms'})
        request = self.factory.get('/', **self.extra)
        response = view(request, pk=self.project.pk)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data, [data])
    def test_owner_role(self):
        self._publish_xls_form_to_project()
        self._make_submissions()
        view = XFormViewSet.as_view({
            'get': 'retrieve',
            'put': 'update',
            'delete': 'destroy'
        })
        data_view = DataViewSet.as_view({'get': 'list'})
        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(extra_post_data=alice_data)
        formid = self.xform.pk

        request = self.factory.get('/', **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 404)

        response = data_view(request, pk=formid)
        self.assertEqual(response.status_code, 404)

        role.OwnerRole.add(self.user, self.xform)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 200)

        response = data_view(request, pk=formid)
        self.assertEqual(response.status_code, 200)

        xfs = XFormSerializer(instance=self.xform,
                              context={'request': request})
        data = json.loads(JSONRenderer().render(xfs.data))
        data.update({'public': True, 'description': "Some description"})
        request = self.factory.put('/', data=data, **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 200)

        request = self.factory.delete('/', **self.extra)
        response = view(request, pk=formid)
        self.assertEqual(response.status_code, 204)
Beispiel #9
0
    def clone(self, request, *args, **kwargs):
        self.object = self.get_object()
        data = {'xform': self.object.pk, 'username': request.DATA['username']}
        serializer = CloneXFormSerializer(data=data)
        if serializer.is_valid():
            clone_to_user = User.objects.get(username=data['username'])
            if not request.user.has_perm('can_add_xform',
                                         clone_to_user.profile):
                raise exceptions.PermissionDenied(
                    detail=_(u"User %(user)s has no permission to add "
                             "xforms to account %(account)s" % {
                                 'user': request.user.username,
                                 'account': data['username']
                             }))
            xform = serializer.save()
            serializer = XFormSerializer(xform.cloned_form,
                                         context={'request': request})

            return Response(data=serializer.data,
                            status=status.HTTP_201_CREATED)

        return Response(data=serializer.errors,
                        status=status.HTTP_400_BAD_REQUEST)