Source

django-fitlog / fitlog / views.py

from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.forms.formsets import formset_factory
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext

from fitlog.models import FitLog, BodyWeightLog
from fitlog.forms import (
    FitLogForm, CardioLogForm, WeightLogForm, WeightLogSetForm,
    WeightLogSetFormSet, BodyWeightForm)


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


def home(request):
    return render_response(request, 'fitlog/home.html', {})


@login_required
def mylogs(request):
    return render_response(request, 'fitlog/my_logs.html', {})


@login_required
def add_log(request):

    user = request.user

    if request.method == 'POST':
        form = FitLogForm(request.POST)
        if form.is_valid():
            fit_log = form.save(user)
            return HttpResponseRedirect(reverse('view_fitlog',
                                                args=(fit_log.id, )))
    else:
        form = FitLogForm()
    return render_response(request, 'fitlog/add_log.html', {'form': form})


@login_required
def add_cardio(request, fitlog_id):
    user = request.user
    fit_log = get_object_or_404(FitLog, id=fitlog_id, member=user)

    if request.method == 'POST':
        form = CardioLogForm(request.POST)
        if form.is_valid():
            form.save(fit_log)
            return HttpResponseRedirect(reverse('view_fitlog',
                                                args=(fit_log.id, )))
    else:
        form = CardioLogForm()
    return render_response(request, 'fitlog/add_cardio.html',
                           {'form': form, 'fitlog': fit_log})


@login_required
def add_weight_training_log(request, fitlog_id):
    user = request.user
    fit_log = get_object_or_404(FitLog, id=fitlog_id, member=user)
    form_set = formset_factory(
        WeightLogSetForm, extra=5, formset=WeightLogSetFormSet)

    if request.method == 'POST':
        form = WeightLogForm(request.POST)
        weight_form_set = form_set(request.POST)
        if form.is_valid() and weight_form_set.is_valid():
            weight_log = form.save(fit_log)
            weight_form_set.save(weight_log)
            return HttpResponseRedirect(reverse(
                'view_fitlog', args=(fit_log.id, )))
    else:
        form = WeightLogForm()
        weight_form_set = form_set()
    return render_response(request, 'fitlog/add_weight_training.html',
                           {'form': form, 'fitlog': fit_log,
                            'weight_form_set': weight_form_set})


@login_required
def view_fitlog(request, fitlog_id):
    fit_log = get_object_or_404(FitLog, id=fitlog_id)
    return render_response(request, 'fitlog/fitlog.html', {'fitlog': fit_log})


@login_required
def edit_fitlog(request, fitlog_id):
    user = request.user
    fit_log = get_object_or_404(FitLog, id=fitlog_id, member=user)

    if request.method == 'POST':
        form = FitLogForm(request.POST, instance=fit_log)
        if form.is_valid():
            form.save(user)
            return HttpResponseRedirect(reverse('view_fitlog',
                                                args=(fit_log.id, )))
    else:
        form = FitLogForm(instance=fit_log)
    return render_response(request, 'fitlog/edit_fitlog.html',
                           {'form': form, 'fitlog': fit_log})


@login_required
def bodyweight_log(request):
    bodyweight_log = BodyWeightLog.objects.by_member(request.user)
    return render_response(request, 'fitlog/bodyweight_log.html',
                           {'bodyweight_log': bodyweight_log, })


@login_required
def add_bodyweight_log(request):

    user = request.user

    if request.method == 'POST':
        form = BodyWeightForm(request.POST)
        if form.is_valid():
            fit_log = form.save(user)
            return HttpResponseRedirect(reverse('bodyweight_log'))
    else:
        form = BodyWeightForm()
    return render_response(request, 'fitlog/add_bodyweight_log.html',
                           {'form': form, })