def create(self, request, *args, **kwargs): params = self.request.query_params username = self.kwargs.get('username') if self.request.user.is_anonymous(): if username is None: # raises a permission denied exception, forces authentication self.permission_denied(self.request) else: user = get_object_or_404(User, username=username.lower()) profile, created = UserProfile.objects.get_or_create(user=user) if profile.require_auth: # raises a permission denied exception, # forces authentication self.permission_denied(self.request) elif not username: # get the username from the user if not set username = (request.user and request.user.username) if request.method.upper() == 'HEAD': return Response(status=status.HTTP_204_NO_CONTENT, headers=self.get_openrosa_headers(request), template_name=self.template_name) is_json_request = is_json(request) error, instance = (create_instance_from_json if is_json_request else create_instance_from_xml)(username, request) if error or not instance: return self.error_response(error, is_json_request, request) if 'activity' in params: cluster_activity = int(params['activity']) cluster_activity = ClusterA.objects.get(pk=cluster_activity) s = Submission(cluster_activity=cluster_activity, instance=instance) s.save() beneficiaries = params.get('beneficiary', '') if not beneficiaries == '': beneficiary = params['beneficiary'] try: beneficiary = int(beneficiary) s.beneficiary = Beneficiary.objects.get(pk=beneficiary) s.save() except: pass 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)
def create(self, request, *args, **kwargs): print("TEST CREATE") username = self.kwargs.get('username') if self.request.user.is_anonymous(): print("1") if username is None: print("2") # raises a permission denied exception, forces authentication self.permission_denied(self.request) else: print("3") user = get_object_or_404(User, username=username.lower()) profile, created = UserProfile.objects.get_or_create(user=user) if profile.require_auth: print("4") # raises a permission denied exception, # forces authentication self.permission_denied(self.request) elif not username: print("5") # get the username from the user if not set username = (request.user and request.user.username) if request.method.upper() == 'HEAD': print("6") return Response(status=status.HTTP_204_NO_CONTENT, headers=self.get_openrosa_headers(request), template_name=self.template_name) is_json_request = is_json(request) try: create_fn = create_instance_from_json if is_json_request else\ create_instance_from_xml error, instance = create_fn(username, request) except AttributeError: error = _(u'Incorrect format, see format details here, ' u'https://api.ona.io/static/docs/submissions.html') if error or not instance: print("7") return self.error_response(error, is_json_request, request) 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)
def old_create(self, request, *args, **kwargs): logging.basicConfig(filename='ex.log', level=logging.DEBUG) username = self.kwargs.get('username') password = request.GET.get('password', '') logging.info('mpower:password = '******'HEAD': return Response(status=status.HTTP_204_NO_CONTENT, headers=self.get_openrosa_headers(request), template_name=self.template_name) is_json_request = is_json(request) error, instance = (create_instance_from_json if is_json_request else create_instance_from_xml)(username, request) if error or not instance: return self.error_response(error, is_json_request, request) 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)
def create(self, request, *args, **kwargs): username = self.kwargs.get('username') if self.request.user.is_anonymous: if username is None: # Authentication is mandatory when username is omitted from the # submission URL raise NotAuthenticated else: user = get_object_or_404(User, username=username.lower()) profile, created = UserProfile.objects.get_or_create(user=user) if profile.require_auth: raise NotAuthenticated elif not username: # get the username from the user if not set username = (request.user and request.user.username) if request.method.upper() == 'HEAD': return Response(status=status.HTTP_204_NO_CONTENT, headers=self.get_openrosa_headers(request), template_name=self.template_name) is_json_request = is_json(request) create_instance_func = (create_instance_from_json if is_json_request else create_instance_from_xml) try: error, instance = create_instance_func(username, request) except UnauthenticatedEditAttempt: # It's important to respond with a 401 instead of a 403 so that # digest authentication can work properly raise NotAuthenticated if error or not instance: return self.error_response(error, is_json_request, request) 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)
def create(self, request, *args, **kwargs): username = self.kwargs.get('username') site = self.request.query_params.get('site') form = self.request.query_params.get('form') dep = self.request.query_params.get('dep', False) if request.method.upper() == 'HEAD': return Response(status=status.HTTP_204_NO_CONTENT, headers=self.get_openrosa_headers(request), template_name=self.template_name) if form and form != "undefined" and not dep: return self.create_new_submission(request, site, form) elif dep: uuid_value = dep.replace("uuid:", "") if not Instance.objects.filter(uuid=uuid_value).exists(): return Response({'error': "Cannot edit this submission"}, headers=self.get_openrosa_headers(request), status=status.HTTP_400_BAD_REQUEST) is_json_request = is_json(request) # error, instance = (create_instance_from_json if is_json_request else create_instance_from_xml)(username, request) if error or not instance: return self.error_response(error, is_json_request, request) update_mongo(instance) update_meta(instance) update_default_status(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) else: return Response({'error': "submission not implemented"}, headers=self.get_openrosa_headers(request), status=status.HTTP_400_BAD_REQUEST)
def create(self, request, *args, **kwargs): username = self.kwargs.get('username') # if self.request.user.is_anonymous(): # if username is None: # # raises a permission denied exception, forces authentication # self.permission_denied(self.request) # else: # user = get_object_or_404(User, username=username.lower()) # # profile, created = UserProfile.objects.get_or_create(user=user) # # if profile.require_auth: # # raises a permission denied exception, # # forces authentication # self.permission_denied(self.request) # elif not username: # # get the username from the user if not set # username = (request.user and request.user.username) if request.method.upper() == 'HEAD': return Response(status=status.HTTP_204_NO_CONTENT, headers=self.get_openrosa_headers(request), template_name=self.template_name) is_json_request = is_json(request) error, instance = (create_instance_from_json if is_json_request else create_instance_from_xml)(username, request) if error or not instance: return self.error_response(error, is_json_request, request) update_mongo(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)
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)
def create(self, request, *args, **kwargs): username = self.kwargs.get('username') password = self.request.GET.get('password') if self.request.user.is_anonymous(): if username is None and password is None: # raises a permission denied exception, forces authentication self.permission_denied(self.request) else: user = get_object_or_404(User, username=username.lower()) profile, created = UserProfile.objects.get_or_create(user=user) if profile.require_auth: # raises a permission denied exception, # forces authentication self.permission_denied(self.request) elif not username: # get the username from the user if not set username = (request.user and request.user.username) if request.method.upper() == 'HEAD': return Response(status=status.HTTP_204_NO_CONTENT, headers=self.get_openrosa_headers(request), template_name=self.template_name) is_json_request = is_json(request) error, instance = (create_instance_from_json if is_json_request else create_instance_from_xml)(username, request) if error or not instance: return self.error_response(error, is_json_request, request) print('instance.xform.id_string') print(instance.xform.id_string) print('\n\n\n\n\n') ret_msg = '' # creating schedule for current submission if instance.xform.id_string == "household_information": ret_msg = insert_beneficiary_info(username, instance) # create_user_schedule(username, instance) else: dict_form = request.DATA schedule_id = dict_form.get("scheduleId") print('\n\n\n\n\n\n Schedule id from mobile = ') print(schedule_id) print('\n\n\n\n\n') if schedule_id is not None: update_schedule_status(schedule_id) if ret_msg is not None and len(ret_msg) > 0: return self.mobile_user_response(instance, ret_msg, request) 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)
def create(self, request, *args, **kwargs): username = self.kwargs.get('username') password = self.request.GET.get('password') if self.request.user.is_anonymous(): if username is None and password is None: # raises a permission denied exception, forces authentication self.permission_denied(self.request) else: user = get_object_or_404(User, username=username.lower()) profile, created = UserProfile.objects.get_or_create(user=user) if profile.require_auth: # raises a permission denied exception, # forces authentication self.permission_denied(self.request) elif not username: # get the username from the user if not set username = (request.user and request.user.username) if request.method.upper() == 'HEAD': return Response(status=status.HTTP_204_NO_CONTENT, headers=self.get_openrosa_headers(request), template_name=self.template_name) is_json_request = is_json(request) xml_file_list = request.FILES tf = request.FILES['xml_submission_file'] print "_________________________________________________________________ss_" print tf #print xml #call_parave_ai_reg_api(xml,request,username) #xml_file = xml_file_list[0] if len(xml_file_list) else None #print xml_file error, instance = (create_instance_from_json if is_json_request else create_instance_from_xml)(username, request) if error or not instance: return self.error_response(error, is_json_request, request) #print('instance.xform.id_string') #print(instance.xform.id_string) #print('\n\n\n\n\n') ret_msg = '' # creating schedule for current submission #if instance.xform.id_string == "household_information": # ret_msg = insert_beneficiary_info(username, instance) # create_user_schedule(username, instance) #else: # dict_form = request.DATA # schedule_id = dict_form.get("scheduleId"); # if schedule_id is not None: # update_schedule_status(schedule_id) if ret_msg is not None and len(ret_msg) > 0: return self.mobile_user_response(instance, ret_msg, request) context = self.get_serializer_context() serializer = SubmissionSerializer(instance, context=context) instance_parse() #After new db parse implement stop it query_dict = {'_id': str(instance.id)} options = { 'group_delimiter': '/', 'split_select_multiples': True, 'binary_select_multiples': False, # 'meta': meta.replace(",", "") if meta else None, 'exp_data_typ': 'lbl' } if instance.xform.db_export: #print 'Entered' create_db_async_export(instance.xform, 'dbtable', json.dumps(query_dict), False, options) update_instance_approval_status(instance.id, 'Submitted') xmlf = tf.read() print xmlf return Response(serializer.data, headers=self.get_openrosa_headers(request), status=status.HTTP_201_CREATED, template_name=self.template_name)