예제 #1
0
    def create(self, request, *args, **kwargs):
        owner = _get_owner(request)
        survey = utils.publish_xlsform(request, owner)

        if isinstance(survey, XForm):
            xform = XForm.objects.get(pk=survey.pk)
            # The XForm has been created, but `publish_xlsform` relies on
            # `onadata.apps.main.forms.QuickConverter`, which uses standard
            # Django forms and only recognizes the `xls_file`, `xls_url`,
            # `dropbox_xls_url`, and `text_xls_form` fields.
            # Use the DRF serializer to update the XForm with values for other
            # fields.
            serializer = XFormSerializer(xform,
                                         data=request.data,
                                         context={'request': request},
                                         partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()
            # noti = xform.logs.create(source=request.user, type=7, title="new Kobo form",
            #                          organization=request.organization,
            #                         description="new kobo form {0} created by {1}".
            #                         format(xform.title, request.user.username))
            # result = {}
            # result['description'] = noti.description
            # result['url'] = noti.get_absolute_url()
            # ChannelGroup("notify-0").send({"text":json.dumps(result)})
            # if noti.organization:
            #     ChannelGroup("notify-{}".format(noti.organization.id)).send({"text":json.dumps(result)})
            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)
예제 #2
0
    def update(self, request, pk, *args, **kwargs):
        if 'xls_file' in request.FILES or 'text_xls_form' in request.data:
            # A new XLSForm has been uploaded and will replace the existing
            # form
            existing_xform = get_object_or_404(XForm, pk=pk)
            # Behave like `onadata.apps.main.views.update_xform`: only allow
            # the update to proceed if the user is the owner
            owner = existing_xform.user
            if request.user.pk != owner.pk:
                raise exceptions.PermissionDenied(
                    detail=_("Only a form's owner can overwrite its contents"))
            survey = utils.publish_xlsform(request, owner, existing_xform)
            if not isinstance(survey, XForm):
                if isinstance(survey, dict) and 'text' in survey:
                    # Typical error text; pass it along
                    raise exceptions.ParseError(detail=survey['text'])
                else:
                    # Something odd; hopefully it can be coerced into a string
                    raise exceptions.ParseError(detail=survey)
            post_update_xform.apply_async((), {
                'xform_id': existing_xform.id,
                'user': request.user.id
            },
                                          countdown=2)

        return super(XFormViewSet, self).update(request, pk, *args, **kwargs)
예제 #3
0
    def create(self, request, *args, **kwargs):
        owner = _get_owner(request)
        survey = utils.publish_xlsform(request, owner)

        if isinstance(survey, XForm):
            xform = XForm.objects.get(pk=survey.pk)
            # The XForm has been created, but `publish_xlsform` relies on
            # `onadata.apps.main.forms.QuickConverter`, which uses standard
            # Django forms and only recognizes the `xls_file`, `xls_url`,
            # `dropbox_xls_url`, and `text_xls_form` fields.
            # Use the DRF serializer to update the XForm with values for other
            # fields.
            serializer = XFormSerializer(
                xform,
                data=request.data,
                context={'request': request},
                partial=True
            )
            serializer.is_valid(raise_exception=True)
            serializer.save()
            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)
예제 #4
0
    def create(self, request, *args, **kwargs):
        owner = _get_owner(request)
        survey = utils.publish_xlsform(request, owner)

        if isinstance(survey, XForm):
            xform = XForm.objects.get(pk=survey.pk)
            # The XForm has been created, but `publish_xlsform` relies on
            # `onadata.apps.main.forms.QuickConverter`, which uses standard
            # Django forms and only recognizes the `xls_file`, `xls_url`,
            # `dropbox_xls_url`, and `text_xls_form` fields.
            # Use the DRF serializer to update the XForm with values for other
            # fields.
            serializer = XFormSerializer(xform,
                                         data=request.data,
                                         context={'request': request},
                                         partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()
            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)
예제 #5
0
 def update(self, request, pk, *args, **kwargs):
     if 'xls_file' in request.FILES or 'text_xls_form' in request.data:
         # A new XLSForm has been uploaded and will replace the existing
         # form
         existing_xform = get_object_or_404(XForm, pk=pk)
         # Behave like `onadata.apps.main.views.update_xform`: only allow
         # the update to proceed if the user is the owner
         owner = existing_xform.user
         if request.user.pk != owner.pk:
             raise exceptions.PermissionDenied(
                 detail=_("Only a form's owner can overwrite its contents"))
         survey = utils.publish_xlsform(request, owner, existing_xform)
         if not isinstance(survey, XForm):
             if isinstance(survey, dict) and 'text' in survey:
                 # Typical error text; pass it along
                 raise exceptions.ParseError(detail=survey['text'])
             else:
                 # Something odd; hopefully it can be coerced into a string
                 raise exceptions.ParseError(detail=survey)
     # Let the superclass handle updates to the other fields
     # noti = existing_xform.logs.create(source=request.user, type=7, title="Kobo form Updated",
     #                              organization=request.organization,
     #                             description="new kobo form {0} Updated by {1}".
     #                             format(existing_xform.title, request.user.username))
     # result = {}
     # result['description'] = noti.description
     # result['url'] = noti.get_absolute_url()
     # ChannelGroup("notify-0").send({"text":json.dumps(result)})
     # if noti.organization:
     #     ChannelGroup("notify-{}".format(noti.organization.id)).send({"text":json.dumps(result)})
     return super(XFormViewSet, self).update(request, pk, *args, **kwargs)
예제 #6
0
def _try_update_xlsform(request, xform, owner):
    survey = \
        utils.publish_xlsform(request, owner, xform.id_string, xform.project)

    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)
예제 #7
0
 def create(self, request, *args, **kwargs):
     survey = utils.publish_xlsform(request, request.user)
     if isinstance(survey, XForm):
         xform = XForm.objects.get(pk=survey.pk)
         serializer = serializers.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)
예제 #8
0
def _try_update_xlsform(request, xform, owner):
    survey = \
        utils.publish_xlsform(request, owner, xform.id_string, xform.project)

    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)
예제 #9
0
 def create(self, request, *args, **kwargs):
     survey = utils.publish_xlsform(request, request.user)
     if isinstance(survey, XForm):
         xform = XForm.objects.get(pk=survey.pk)
         serializer = serializers.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)
예제 #10
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)
예제 #11
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)
예제 #12
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):
            serializer = XFormCreateSerializer(
                survey, 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)
예제 #13
0
def _try_update_xlsform(request, xform, owner):
    survey = \
        utils.publish_xlsform(request, owner, xform.id_string, xform.project)

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

        # send form update notification
        send_message(
            instance_id=xform.id, target_id=xform.id,
            target_type=XFORM, user=request.user or owner,
            message_verb=FORM_UPDATED)

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

    return Response(survey, status=status.HTTP_400_BAD_REQUEST)
예제 #14
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)
예제 #15
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):
            # survey is a DataDictionary we need an XForm to return the correct
            # role for the user after form publishing.
            serializer = XFormCreateSerializer(
                survey, 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)
예제 #16
0
 def update(self, request, pk, *args, **kwargs):
     if 'xls_file' in request.FILES or 'text_xls_form' in request.data:
         # A new XLSForm has been uploaded and will replace the existing
         # form
         existing_xform = get_object_or_404(XForm, pk=pk)
         # Behave like `onadata.apps.main.views.update_xform`: only allow
         # the update to proceed if the user is the owner
         owner = existing_xform.user
         if request.user.pk != owner.pk:
             raise exceptions.PermissionDenied(
                 detail=_("Only a form's owner can overwrite its contents"))
         survey = utils.publish_xlsform(request, owner, existing_xform)
         if not isinstance(survey, XForm):
             if isinstance(survey, dict) and 'text' in survey:
                 # Typical error text; pass it along
                 raise exceptions.ParseError(detail=survey['text'])
             else:
                 # Something odd; hopefully it can be coerced into a string
                 raise exceptions.ParseError(detail=survey)
     # Let the superclass handle updates to the other fields
     return super(XFormViewSet, self).update(request, pk, *args, **kwargs)