コード例 #1
0
def update_meta(instance):
    if instance.fieldsight_instance.project_fxf and \
            instance.fieldsight_instance.site:
        task_obj = CeleryTaskProgress.objects.create(
            user=instance.user,
            description='Change site info',
            task_type=25,
            content_object=instance.fieldsight_instance)
        if task_obj:
            from onadata.apps.fieldsight.tasks import update_meta_details
            update_meta_details.apply_async(
                (instance.fieldsight_instance.project_fxf.id, instance.id,
                 task_obj.id, instance.fieldsight_instance.site.id),
                countdown=1)
コード例 #2
0
    def create_new_submission(self, request, site, form):
        fs_xf = FieldSightXF.objects.get(pk=form)
        xform = fs_xf.xf
        xml_file_list = self.request.FILES.pop('xml_submission_file', [])
        xml_file = xml_file_list[0] if len(xml_file_list) else None
        xml = xml_file.read()
        username = self.kwargs.get('username')
        user = get_object_or_404(User, username=username)
        media_files = request.FILES.values()
        new_uuid = get_uuid_from_xml(xml)
        site_id = site

        xml_hash = Instance.get_hash(xml)

        if xform.has_start_time:
            # XML matches are identified by identical content hash OR, when a
            # content hash is not present, by string comparison of the full
            # content, which is slow! Use the management command
            # `populate_xml_hashes_for_instances` to hash existing submissions
            existing_instance = Instance.objects.filter(
                Q(xml_hash=xml_hash)
                | Q(xml_hash=Instance.DEFAULT_XML_HASH, xml=xml),
                xform__user=xform.user,
            ).first()
        else:
            existing_instance = None

        if existing_instance:
            # ensure we have saved the extra attachments
            any_new_attachment = save_attachments(existing_instance,
                                                  media_files)

            if not any_new_attachment:
                raise DuplicateInstance()
            else:
                context = self.get_serializer_context()
                serializer = SubmissionSerializer(existing_instance,
                                                  context=context)
                return Response(serializer.data,
                                headers=self.get_openrosa_headers(request),
                                status=status.HTTP_201_CREATED,
                                template_name=self.template_name)
        with transaction.atomic():
            if fs_xf.is_survey:
                instance = save_submission(
                    xform=xform,
                    xml=xml,
                    media_files=media_files,
                    new_uuid=new_uuid,
                    submitted_by=user,
                    status='submitted_via_web',
                    date_created_override=None,
                    fxid=None,
                    site=None,
                    fs_poj_id=fs_xf.id,
                    project=fs_xf.project.id,
                )
            else:
                if fs_xf.site:
                    instance = save_submission(
                        xform=xform,
                        xml=xml,
                        media_files=media_files,
                        new_uuid=new_uuid,
                        submitted_by=user,
                        status='submitted_via_web',
                        date_created_override=None,
                        fxid=fs_xf.id,
                        site=site_id,
                    )
                else:
                    instance = save_submission(
                        xform=xform,
                        xml=xml,
                        media_files=media_files,
                        new_uuid=new_uuid,
                        submitted_by=user,
                        status='submitted_via_web',
                        date_created_override=None,
                        fxid=None,
                        site=site_id,
                        fs_poj_id=fs_xf.id,
                        project=fs_xf.project.id,
                    )
                    task_obj = CeleryTaskProgress.objects.create(
                        user=user,
                        description='Change site info',
                        task_type=25,
                        content_object=instance.fieldsight_instance)
                    if task_obj:
                        from onadata.apps.fieldsight.tasks import \
                            update_meta_details
                        update_meta_details.apply_async(
                            (fs_xf.id, instance.id, task_obj.id, site_id),
                            countdown=1)
                    else:
                        from onadata.apps.fieldsight.tasks import \
                            update_meta_details
                        update_meta_details.apply_async(
                            (fs_xf.id, instance.id, 0, site_id), countdown=1)

            noti_type = 16
            title = "new submission"

            if instance.fieldsight_instance.site:
                extra_object = instance.fieldsight_instance.site
                extra_message = ""
                project = extra_object.project
                site = extra_object
                organization = extra_object.project.organization

            else:
                extra_object = instance.fieldsight_instance.project
                extra_message = "project"
                project = extra_object
                site = None
                organization = extra_object.organization

            instance.fieldsight_instance.logs.create(
                source=user,
                type=noti_type,
                title=title,
                organization=organization,
                project=project,
                site=site,
                extra_object=extra_object,
                extra_message=extra_message,
                content_object=instance.fieldsight_instance)

        context = self.get_serializer_context()
        serializer = SubmissionSerializer(instance, context=context)
        return Response(serializer.data,
                        headers=self.get_openrosa_headers(request),
                        status=status.HTTP_201_CREATED,
                        template_name=self.template_name)
コード例 #3
0
    def create(self, request, *args, **kwargs):
        if self.request.user.is_anonymous():
            self.permission_denied(self.request)

        fsxfid = kwargs.get('pk', None)
        siteid = kwargs.get('site_id', None)
        if fsxfid is None or siteid is None:
            return self.error_response("Site Id Or Form ID Not Given", False,
                                       request)
        try:
            fsxfid = int(fsxfid)
            fxf = get_object_or_404(FieldSightXF, pk=kwargs.get('pk'))
            if fxf.project:
                # A form assigned from project
                if siteid == '0':
                    siteid = None
                elif Site.objects.filter(pk=siteid).exists() == False:
                    return self.error_response("siteid Invalid", False,
                                               request)
                if fsxfid is None:
                    return self.error_response("Fieldsight Form ID Not Given",
                                               False, request)
                try:
                    fs_proj_xf = fxf
                    xform = fs_proj_xf.xf
                    proj_id = fs_proj_xf.project.id
                except Exception as e:
                    return self.error_response(str(e), False, request)
                if request.method.upper() == 'HEAD':
                    return Response(status=status.HTTP_204_NO_CONTENT,
                                    headers=self.get_openrosa_headers(request),
                                    template_name=self.template_name)
                params = self.request.query_params
                flagged_instance = params.get("instance")

                error, instance = create_instance_from_xml(
                    request, None, siteid, fs_proj_xf.id, proj_id, xform,
                    flagged_instance)

                if error or not instance:
                    return self.error_response(error, False, request)

                fi = instance.fieldsight_instance
                fi_id = fi.id
                last_edited_date = EditedSubmission.objects.filter(
                    old__id=fi_id).last(
                    ).date if EditedSubmission.objects.filter(
                        old__id=fi_id).last() else None
                last_instance_log = FieldSightLog.objects.filter(
                    object_id=fi_id,
                    type=16).first().date if FieldSightLog.objects.filter(
                        object_id=fi_id, type=16).first() else None
                delta = 101  # make sure the submission is new not installment of attachment of previous submission
                if last_instance_log and last_edited_date:
                    delta = (EditedSubmission.objects.filter(
                        old__id=fi_id).last().date -
                             FieldSightLog.objects.filter(
                                 object_id=fi_id,
                                 type=16).first().date).total_seconds()
                if (not FieldSightLog.objects.filter(
                        object_id=fi_id, type=16).exists()) or (
                            flagged_instance and delta > 100):
                    if flagged_instance:
                        fi.form_status = None
                        fi.save()
                    if fs_proj_xf.is_survey:
                        instance.fieldsight_instance.logs.create(
                            source=self.request.user,
                            type=16,
                            title="new Project level Submission",
                            organization=fs_proj_xf.project.organization,
                            project=fs_proj_xf.project,
                            extra_object=fs_proj_xf.project,
                            extra_message="project",
                            content_object=instance.fieldsight_instance)
                    else:
                        task_obj = CeleryTaskProgress.objects.create(
                            user=self.request.user,
                            description='Change site info',
                            task_type=25,
                            content_object=instance.fieldsight_instance)
                        if task_obj:
                            from onadata.apps.fieldsight.tasks import update_meta_details
                            update_meta_details.apply_async(
                                (fs_proj_xf.id, instance.id, task_obj.id,
                                 siteid),
                                countdown=1)
                        site = Site.objects.get(pk=siteid)
                        instance.fieldsight_instance.logs.create(
                            source=self.request.user,
                            type=16,
                            title="new Site level Submission",
                            organization=fs_proj_xf.project.organization,
                            project=fs_proj_xf.project,
                            site=site,
                            extra_object=site,
                            content_object=instance.fieldsight_instance)

                context = self.get_serializer_context()
                serializer = FieldSightSubmissionSerializer(instance,
                                                            context=context)
                return Response(serializer.data,
                                headers=self.get_openrosa_headers(request),
                                status=status.HTTP_201_CREATED,
                                template_name=self.template_name)

            #  Site level forms assigned in particular site.
            fs_proj_xf = fxf.fsform.pk if fxf.fsform else None
            proj_id = fxf.fsform.project.pk if fxf.fsform else fxf.site.project.pk
            xform = fxf.xf
        except:
            return self.error_response("Site Id Or Form ID Not Vaild", False,
                                       request)
        if request.method.upper() == 'HEAD':
            return Response(status=status.HTTP_204_NO_CONTENT,
                            headers=self.get_openrosa_headers(request),
                            template_name=self.template_name)

        params = self.request.query_params
        flagged_instance = params.get("instance")
        error, instance = create_instance_from_xml(request, fsxfid, siteid,
                                                   fs_proj_xf, proj_id, xform,
                                                   flagged_instance)
        extra_message = ""

        if error or not instance:
            return self.error_response(error, False, request)

        if fxf.is_survey:
            extra_message = "project"
        fi = instance.fieldsight_instance
        fi_id = fi.id
        last_edited_date = EditedSubmission.objects.filter(
            old__id=fi_id).last().date if EditedSubmission.objects.filter(
                old__id=fi_id).last() else None
        last_instance_log = FieldSightLog.objects.filter(
            object_id=fi_id,
            type=16).first().date if FieldSightLog.objects.filter(
                object_id=fi_id, type=16).first() else None
        delta = 101
        if last_instance_log and last_edited_date:
            delta = (
                EditedSubmission.objects.filter(old__id=fi_id).last().date -
                FieldSightLog.objects.filter(
                    object_id=fi_id, type=16).first().date).total_seconds()
        if (not FieldSightLog.objects.filter(
                object_id=fi_id, type=16).exists()) or (flagged_instance
                                                        and delta > 100):
            instance.fieldsight_instance.logs.create(
                source=self.request.user,
                type=16,
                title="new Submission",
                organization=instance.fieldsight_instance.site.project.
                organization,
                project=instance.fieldsight_instance.site.project,
                site=instance.fieldsight_instance.site,
                extra_message=extra_message,
                extra_object=instance.fieldsight_instance.site,
                content_object=instance.fieldsight_instance)

            if flagged_instance:
                fi.form_status = None
                fi.save()
        context = self.get_serializer_context()
        serializer = FieldSightSubmissionSerializer(instance, context=context)
        return Response(serializer.data,
                        headers=self.get_openrosa_headers(request),
                        status=status.HTTP_201_CREATED,
                        template_name=self.template_name)