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,})
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.