def _load_post_and_files(self): # Populates self._post and self._files if self.method == 'POST': if self.environ.get('CONTENT_TYPE', '').startswith('multipart'): self._raw_post_data = '' try: self._post, self._files = self.parse_file_upload( self.META, self.environ['wsgi.input']) except: # An error occured while parsing POST data. Since when # formatting the error the request handler might access # self.POST, set self._post and self._file to prevent # attempts to parse POST data again. self._post = http.QueryDict('') self._files = datastructures.MultiValueDict() # Mark that an error occured. This allows self.__repr__ to # be explicit about it instead of simply representing an # empty POST self._post_parse_error = True raise else: self._post, self._files = http.QueryDict( self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict() else: self._post, self._files = http.QueryDict( '', encoding=self._encoding), datastructures.MultiValueDict()
def _load_post_and_files(self): # Populates self._post and self._files if self.method == 'POST': if self.environ.get('CONTENT_TYPE', '').startswith('multipart'): self._raw_post_data = '' self._post, self._files = self.parse_file_upload( self.META, self.environ['wsgi.input']) else: self._post, self._files = http.QueryDict( self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict() else: self._post, self._files = http.QueryDict( '', encoding=self._encoding), datastructures.MultiValueDict()
def entry(self): with connection.cursor() as cursor: cursor.execute( f''' select * from "{self.table_name}" where "{self.table_name}"."{self.column_name}"=%(entity_code)s ''', { 'entity_code': self.entity_code, }, ) columns = [col[0] for col in cursor.description] row = cursor.fetchone() if row is None: raise self.DoesNotExist if cursor.fetchone() is not None: raise self.MultipleObjectsReturned cursor.execute( '''select field_id, field_title from "{table_name}"''' .format(table_name=re.sub(r'(_\d{4})$', r'_fields\1', self.table_name)) ) fields = dict(cursor) entry = datastructures.MultiValueDict() for (column, value) in zip(columns, row): key = fields.get(column, column) entry.appendlist(key, value) return entry
def _load_post_and_files(self): # Populates self._post and self._files if self.method == 'POST': if self.environ.get('CONTENT_TYPE', '').startswith('multipart'): header_dict = dict([(k, v) for k, v in self.environ.items() if k.startswith('HTTP_')]) header_dict['Content-Type'] = self.environ.get( 'CONTENT_TYPE', '') self._post, self._files = http.parse_file_upload( header_dict, self.raw_post_data) else: self._post, self._files = http.QueryDict( self.raw_post_data), datastructures.MultiValueDict() else: self._post, self._files = http.QueryDict( ''), datastructures.MultiValueDict()
def change_answer_set(request): """ Switch to review/editting of current answers """ post_data = datastructures.MultiValueDict(request.POST) sset = post_data.pop('submission', '') try: submission_set = SubmissionSet.objects.get(pk__in=sset) except: return HttpResponse("NOK", status=400) if post_data.get('action', None) == 'delete': # Delete given set submission_set.delete() else: # Create submission set of current answers _create_submission_set(request, submission_set.tag) # Unbind our submission set (puts submissions back as 'editable') submission_set.submissions.update(submission_set=None) # Delete this submission set submission_set.delete() return HttpResponse("OK")
def __init__(self, request): assert self._permitted_methods # this request should be used for global environment info, like # constructing absolute URIs. it should not be used for query # parameters, because the request may not have been for this particular # resource. self._request = request # this dict will contain the applicable query parameters self._query_params = datastructures.MultiValueDict()
def _load_post_and_files(This): "Populates This._post and This._files" Ctype = Env.HTTP_CONTENT_TYPE or '' if Ctype.startswith('multipart'): This._post, This._files = http.parse_file_upload( This._headers_in, This.raw_post_data) else: This._post, This._files = http.QueryDict( This.raw_post_data), datastructures.MultiValueDict()
def _load_post_and_files(self): "Populates self._post and self._files" if 'content-type' in self._req.headers_in and self._req.headers_in[ 'content-type'].startswith('multipart'): self._post, self._files = http.parse_file_upload( self._req.headers_in, self.raw_post_data) else: self._post, self._files = http.QueryDict( self.raw_post_data), datastructures.MultiValueDict()
def _load_post_and_files(self): "Populates self._post and self._files" if self.method != 'POST': self._post, self._files = http.QueryDict('', encoding=self._encoding), datastructures.MultiValueDict() return if 'content-type' in self._req.headers_in and self._req.headers_in['content-type'].startswith('multipart'): self._raw_post_data = '' try: self._post, self._files = self.parse_file_upload(self.META, self._req) except: # See django.core.handlers.wsgi.WSGIHandler for an explanation # of what's going on here. self._post = http.QueryDict('') self._files = datastructures.MultiValueDict() self._post_parse_error = True raise else: self._post, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict()
def href(self, query_params=None): """Return URI to this resource.""" kwargs = self._uri_args() path = urlresolvers.reverse(self.dispatch_request, kwargs=kwargs) full_query_params = datastructures.MultiValueDict(self._query_params) if query_params: full_query_params.update(query_params) if full_query_params: path += '?' + urllib.urlencode(full_query_params.lists(), doseq=True) return self._request.build_absolute_uri(path)
def _submit(request): post_data = datastructures.MultiValueDict(request.POST) submission_set_tag = post_data.pop('submission_set_tag', '') for question_slug, answers in post_data.iteritems(): # validate the question try: question = Question.objects.get( slug=question_slug, placeholder__page__publisher_is_draft=False, ) except Question.DoesNotExist: return HttpResponseBadRequest( "Invalid question '%s'" % question_slug, ) # check answers is a list of slugs if question.question_type != 'F' and not ANSWER_RE.match(answers): return HttpResponseBadRequest("Invalid answers: %s" % answers) # validate and score the answer try: score = question.score(answers) except Answer.DoesNotExist: return HttpResponseBadRequest("Invalid answer '%s:%s'" % (question_slug, answers)) # save, but don't update submissions belonging to an existing set filter_attrs = { 'user': request.user, 'question': question_slug, 'submission_set': None, } attrs = {'answer': answers, 'score': score} rows = Submission.objects.filter(**filter_attrs).update(**attrs) if not rows: attrs.update(filter_attrs) Submission.objects.create(**attrs) # Create submission set if requested if submission_set_tag: submission_set_tag = submission_set_tag[0] if submission_set_tag: _create_submission_set(request, submission_set_tag) return HttpResponse("OK")
def get(self, query_parameters=None, **kwarg_query_parameters): """ @param query_parameters: a dict or MultiValueDict """ query_parameters = copy.copy(query_parameters) # avoid mutating original if query_parameters is None: query_parameters = {} query_parameters.update(kwarg_query_parameters) string_parameters = datastructures.MultiValueDict() for key, values in self._iterlists(query_parameters): string_parameters.setlist( key, [self._stringify_query_parameter(value) for value in values]) response = self._request('GET', query_parameters=string_parameters.lists()) assert response.status == 200 return self._read_representation(response.decoded_body())
def adaptRequest(self, request): request._post, request._files = QueryDict( request.raw_post_data, encoding=request._encoding), datastructures.MultiValueDict() return request