Exemple #1
0
    def read(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

        if 'id' in kwargs:
            try:
                q = model.objects.get(pk=kwargs.get('id'))
            except ObjectDoesNotExist:
                return HttpResponseNotFound('An object with id %s does not exist.' % (kwargs.get('id')))
            except MultipleObjectsReturned:
                # Should never happen, as we are looking for the primary key.
                return HttpResponseServerError()
        elif 'id_list' in kwargs:
            q = model.objects.filter(id__in=kwargs.get('id_list').split(';'))
        else:
            q = model.objects.all()

        # Note an empty queryset is still a valid response and should return 200. Thus, no special
        # case to handle.
        return render_to_rest(q)
Exemple #2
0
    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)
Exemple #3
0
def poll_index(request):
    poll_list = Poll.objects.all().order_by('-pub_date')
    if request.is_rest():
        return render_to_rest(poll_list)
    return render_to_response('index.html',  {
        'poll_list': poll_list,
    })
Exemple #4
0
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
Exemple #5
0
def results(request, id):
    poll = get_object_or_404(Poll, pk=id)
    if request.is_rest():
        result = {'choice': []}
        for choice in poll.choice_set.all():
            result['choice'].append({'name': choice.choice, 'votes': choice.votes})
        return render_to_rest(result)
    return render_to_response('results.html', {
        'poll': poll,
        }, context_instance=RequestContext(request)
    )
Exemple #6
0
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)
    )
Exemple #7
0
    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()
        if not getattr(rest_info, 'model') or not rest_info.model:
            # This is a misconfiguration.
            return HttpResponseServerError()
        else:
            model = rest_info.model

        # Put requests have to point to a resource entity and not to
        # a handler resource. Thus, the indication of an id is
        # mandatory.
        if not kwargs.get('id') or not kwargs.get('id_list'):
            return HttpResponseNotAllowed(rest_info.allowed_methods)

        ModelFormSet = modelformset_factory(model)

        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.
            #
            # * Actually this handler is called via "PUT" but Django
            # is not capable of handling PUT requests. Thus, we 
            # changed the request type to POST for views.
            return HttpResponseNotAllowed(rest_info.allowed_methods)

        formset = ModelFormSet(request.POST, request.FILES)
        if formset.is_valid():
            objects = formset.save()
            return render_to_rest(objects)
        else:
            rest_info.error_by_form(formset)
            return HttpResponse()
Exemple #8
0
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)
    )
Exemple #9
0
            # (*) Actually this handler is called via "PUT" but Django
            # is not capable of handling PUT requests. Thus, we 
            # 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:
Exemple #10
0
def poll_detail(request, id):
    p = get_object_or_404(Poll, pk=id)
    if request.is_rest():
        return render_to_rest(p)
    return render_to_response('detail.html', {'poll': p})