def create(self, request, *args, **kwargs): rest_info = request.rest_info if not rest_info: # This should never happen. return HttpResponseServerError() if not getattr(rest_info, 'model') or not rest_info.model: # This is a misconfiguration. return HttpResponseServerError() else: model = rest_info.model ModelForm = modelform_factory(model, fields="__all__") if not request.method == 'POST': # This is actually a misconfiguration. If this method is # called without POST someone played around with the # BaseWrapper.handler_methods dictionary. return HttpResponseNotAllowed(rest_info.allowed_methods) form = ModelForm(request.POST, request.FILES) if form.is_valid(): obj = form.save() return render_to_rest(obj) else: return render_form_error_to_rest(form)
def vote(request): if request.method == 'POST': form = VoteForm(request.POST) if form.is_valid(): choice = form.cleaned_data.get('choice') choice.votes += 1 choice.save() return render_to_rest({'poll': choice.poll.id, 'choice': choice.id}) else: return render_form_error_to_rest(form) raise Http404
def poll_create_multiple(request): PollFormSet = modelformset_factory(Poll, fields="__all__") if request.method == 'POST': formset = PollFormSet(request.POST) if formset.is_valid(): objects = formset.save() return render_to_rest(objects) else: if request.is_rest(): return render_form_error_to_rest(formset) else: formset = PollFormSet() return render_to_response('update.html', { 'formset': formset, }, context_instance=RequestContext(request) )
def poll_create_or_update(request, id=None): if id: p = get_object_or_404(Poll, pk=id) if request.method == 'POST': if id: form = PollForm(request.POST, instance=p) else: form = PollForm(request.POST) if form.is_valid(): obj = form.save() return render_to_rest(obj) else: if request.is_rest(): return render_form_error_to_rest(form) else: if id: form = PollForm(instance=p) else: form = PollForm() return render_to_response('update.html', { 'form': form, }, context_instance=RequestContext(request) )
# changed the request type to POST for views. return HttpResponseNotAllowed(rest_info.allowed_methods) # We enforce an update. For the creation of new objects # "POST" (create) should be used. entity = get_object_or_404(model, pk=entity_id) form = ModelForm(request.POST, instance=entity) if form.is_valid(): e = form.save() # Always return the object. The Resource decides if it # will be included in the response or not. return render_to_rest(e) else: return render_form_error_to_rest(form) def update_multiple(self, request, *args, **kwargs): # TODO This has to be implemented. ModelFormSets now if they # have to create or update an entity by the hidden submission # of the id: <input type="hidden" name="form-0-id" id="id_form-0-id" /> # Thus, we have to add the ids from the URI as POST values or # as the sender to include the id in each entity (which makes more # sense. But is against the HTTP-PUT specification. # # TODO: Review (incomplete) code below. # rest_info = request.rest_info if not rest_info: # This should never happen. return HttpResponseServerError()