from django.shortcuts import get_object_or_404, render from django.http import HttpResponse from .models import Tool, Task, Event from .forms import EventForm def index(request): context = { 'tools': Tool.objects.all(), 'tasks': Task.objects.all(), } return render(request, 'tasks/index.djhtml', context) def toolDetail(request, tool_slug): tool = get_object_or_404(Tool, slug=tool_slug) tasks = tool.task_set.all() context = { 'tool': tool, 'tasks': tasks, } return render(request, 'tasks/toolDetail.djhtml', context) def taskDetail(request, tool_slug, task_slug): tool = get_object_or_404(Tool, slug=tool_slug) task = get_object_or_404(tool.task_set, slug=task_slug) events = task.event_set.all() if request.method == 'POST': if request.user.is_authenticated: event = Event(task=task) form = EventForm(request.POST, user=request.user, instance=event) if form.is_valid(): form.save() form = EventForm(user=request.user) else: form = EventForm(request.POST) form.add_error(None, "Not authenticated: please login") else: form = EventForm(user=request.user) context = { 'tool': tool, 'task': task, 'events': events, 'form': form, } if request.user.is_authenticated: context.update({ 'task_subs': task.grouptasksubscription_set.filter(group__user=request.user), 'tool_subs': tool.grouptoolsubscription_set.filter(group__user=request.user), }) return render(request, 'tasks/taskDetail.djhtml', context)