Source

django-feature-request / feature_request / views.py

Full commit
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.forms.models import model_to_dict

from feature_request.forms import FeatureRequestForm
from feature_request.models import FeatureRequest

from voting.views import vote_on_object as _vote_on_object

# ------------------------------------------------------------------------------
def render_response(request, template_name, template_params={}):
    return render_to_response(template_name,
        context_instance=RequestContext(request, template_params))

# ------------------------------------------------------------------------------
@login_required
def delete(request, feature_id, template_name='feature_request/delete.html'):

    user = request.user
    feature_request=FeatureRequest.objects.get_users_feature_request(feature_id, user)

    if not feature_request:
        # If the feature request doesn't exist or the user didn't create it
        # ignore the delete request
        return HttpResponseRedirect(reverse('feature_request_home'))

    if request.method == 'POST':
        FeatureRequest.objects.delete_request(request, feature_request)
        return HttpResponseRedirect(reverse('feature_request_home'))

    return render_response(request, template_name, {'feature_request':feature_request})

# ------------------------------------------------------------------------------
@login_required
def submit(request, feature_id, template_name='feature_request/submit.html'):

    user = request.user
    feature_request=FeatureRequest.objects.get_users_feature_request(feature_id, user)

    if not feature_request:
        # If the feature request doesn't exist or the user didn't create it
        # ignore the delete request
        return HttpResponseRedirect(reverse('feature_request_home'))

    if request.method == 'POST':
        FeatureRequest.objects.submit_request(request, feature_request)
        return HttpResponseRedirect(reverse('feature_request_home'))

    return render_response(request, template_name, {'feature_request':feature_request})

# ------------------------------------------------------------------------------
@login_required
def edit(request, feature_id, template_name='feature_request/edit.html'):

    user = request.user
    feature_request=FeatureRequest.objects.get_users_feature_request(feature_id, user)

    if not feature_request:
        # If the feature request doesn't exist or the user didn't create it
        # ignore the delete request
        return HttpResponseRedirect(reverse('feature_request_home'))

    if request.method == 'POST':
        form = FeatureRequestForm(request.POST)
        if form.is_valid():
            form.save(request, user)
        return HttpResponseRedirect(reverse('feature_request_home'))

    form = FeatureRequestForm(model_to_dict(feature_request))
    return render_response(request, template_name, {'form':form})

# ------------------------------------------------------------------------------
@login_required
def add(request, template_name='feature_request/add.html'):
    
    user = request.user
        
    if request.method == 'POST':
        form = FeatureRequestForm(request.POST)
        if form.is_valid():

            form.save(request, user)
            return HttpResponseRedirect(reverse('feature_request_home'))
    else :
        form = FeatureRequestForm()

    return render_response(request, template_name, {'form':form,})

# ------------------------------------------------------------------------------
@login_required
def view_all(request, template_name='feature_request/view_all.html'):
    user=request.user
    #print "***** getting feature_requests *****"
    feature_requests = FeatureRequest.objects.get_features()
    #print "***** getting my feature requests *****"
    my_requests = FeatureRequest.objects.get_my_features(user)
    #print my_requests
    #print "***** rendering *****"
    return render_response(
            request,
            template_name,
            {
                'feature_requests':feature_requests,
                'my_requests':my_requests,
            })

# ------------------------------------------------------------------------------
@login_required
def vote_on_object(request, object_id, direction, post_vote_redirect=None):
    return _vote_on_object(
            request,
            FeatureRequest,
            direction,
            post_vote_redirect=post_vote_redirect,
            object_id=object_id,
            template_name=None)