Source

django-pim / projects / views.py

from projects.forms import ProjectForm, GoalForm, NoteForm
from django.template.defaultfilters import slugify
from django.shortcuts import redirect, get_object_or_404, render
from projects.models import Project, Goal
import datetime
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from icalendar.cal import Calendar, Todo
from django.http import HttpResponse
from django.contrib.sites.models import Site

@login_required
def index(req):
    goal_list = Goal.objects.filter(complete=False).order_by('due_on')
    return render(req, 'projects/list_goals.html', {'goal_list':goal_list})

@login_required
def new_project(req):
    if req.method == 'POST':
        f = ProjectForm(req.POST)
        if f.is_valid():
            prj = f.save(commit=False)
            prj.slug = slugify(prj.title)
            prj.save()
            messages.success(req, 'New project <b>%s</b> has been created.' % prj)
            return redirect(prj)
    else:
        f = ProjectForm()
    return render(req, 'projects/new_project.html', {'form':f})

@login_required
def view_project(req, prj_slug):
    prj = get_object_or_404(Project, slug=prj_slug)
    if req.GET.get('complete', 'false') == 'true':
        prj.complete = True
        prj.completed_on = datetime.date.today()
        prj.save()
        messages.success(req, '<b>%s</b> has been marked as completed.' % prj)
    goals = prj.goal_set.all().order_by('complete', 'due_on')
    notes = prj.notes.all().order_by('created_on')
    return render(req, 'projects/view_project.html', {'project':prj, 'goals':goals, 'notes':notes})

@login_required
def new_goal(req, prj_slug):
    prj = get_object_or_404(Project, slug=prj_slug)
    if req.method == 'POST':
        f = GoalForm(req.POST)
        if f.is_valid():
            goal = f.save(commit=False)
            goal.slug = slugify(goal.title)
            goal.project = prj
            goal.save()
            messages.success(req, 'Goal <b>%s</b> has been created for project <b>%s</b>.' % (goal, prj))
            return redirect(prj)
    else:
        f = GoalForm()
    return render(req, 'projects/new_goal.html', {'project':prj, 'form':f})

@login_required
def view_goal(req, prj_slug, goal_slug):
    prj = get_object_or_404(Project, slug=prj_slug)
    goal = get_object_or_404(Goal, project=prj, slug=goal_slug)
    if req.GET.get('complete', 'false') == 'true':
        goal.complete = True
        goal.completed_on = datetime.date.today()
        goal.save()
        messages.success(req, 'Goal <b>%s</b> has been marked as completed.' % goal)
        return redirect(prj)
    notes = goal.notes.all().order_by('created_on')
    return render(req, 'projects/view_goal.html', {'goal':goal, 'notes':notes})

@login_required
def new_note(req, prj_slug, goal_slug=None):
    prj = get_object_or_404(Project, slug=prj_slug)
    goal = None
    action_url = reverse('projects-create-note', args=(prj_slug,))
    if goal_slug:
        goal = get_object_or_404(Goal, project=prj, slug=goal_slug)
        action_url = reverse('projects-create-goal-note', args=(prj_slug, goal_slug,))
    if req.method == 'POST':
        f = NoteForm(req.POST)
        if f.is_valid():
            note = f.save(commit=False)
            note.slug = slugify(note.title)
            if goal:
                note.content_object = goal
                note.save()
                messages.success(req, 'A note has been created on goal <b>%s</b>.' % goal)
                return redirect(goal)
            else:
                note.content_object = prj
                note.save()
                messages.success(req, 'A note has been created on project <b>%s</b>.' % prj)
                return redirect(prj)
    else:
        f = NoteForm()
    return render(req, 'projects/new_note.html', {'project':prj, 'goal':goal, 'form':f, 'action_url':action_url})

@login_required
def view_note(req, prj_slug, note_slug, goal_slug=None):
    prj = get_object_or_404(Project, slug=prj_slug)
    goal = None
    action_url = reverse('projects-view-note', args=(prj_slug, note_slug,))
    if goal_slug:
        goal = get_object_or_404(Goal, project=prj, slug=goal_slug)
        action_url = reverse('projects-view-goal-note', args=(prj_slug, goal_slug, note_slug,))
        note = goal.notes.get(slug=note_slug)
    else:
        note = prj.notes.get(slug=note_slug)
    if req.method == 'POST':
        f = NoteForm(req.POST, instance=note)
        if f.is_valid():
            f.save()
            messages.success(req, 'Note saved successfully.')
    else:
        f = NoteForm(instance=note)
    return render(req, 'projects/view_note.html', {'project':prj, 'goal':goal, 'form':f, 'action_url':action_url})

@login_required
def complete_projects(req):
    return render(req, 'projects/list_projects.html', {'project_list':Project.objects.filter(complete=True)})

def ical(req):
    if req.method == 'PUT':
        cal = Calendar.from_ical(req.raw_post_data)
        for todo in cal.walk('VTODO'):
            if 'completed' in todo:
                try:
                    goal = Goal.objects.get(pk=int(todo['uid']))
                    goal.complete = True
                    goal.completed_on = datetime.date.today()
                    goal.save()
                except Goal.DoesNotExist:
                    pass
    cal = Calendar()
    cal.add('prodid', '-//Kevin Veroneau//projects//')
    cal.add('version', '2.0')
    goal_list = Goal.objects.filter(project__complete=False)
    for goal in goal_list:
        todo = Todo()
        todo.add('summary', goal.title)
        todo.add('description', goal.description.replace("\r\n", '\\n'))
        todo['uid'] = str(goal.pk)
        todo.add('created', goal.created_on)
        todo.add('last-modified', goal.created_on)
        todo.add('url', "http://%s%s" % (Site.objects.get_current(), goal.get_absolute_url()))
        todo.add('due', goal.due_on)
        if goal.complete:
            todo.add('status', 'COMPLETED')
            todo.add('completed', goal.completed_on)
        todo.add('categories', goal.project.title)
        cal.add_component(todo)
    return HttpResponse(cal.to_ical(), mimetype='text/plain')
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.