summaryrefslogtreecommitdiffstats
path: root/todolists/views.py
blob: 3a96dc8795b6f468bba5569c3fa0b672e7fd0849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth.models import User
from archweb_dev.main.utils import render_response
from archweb_dev.main.models import Todolist, TodolistPkg, Package
from archweb_dev.main.models import Arch, Repo

# FIXME: ugly hackery. http://code.djangoproject.com/ticket/3450
import django.db
IntegrityError = django.db.backend.Database.IntegrityError

def flag(request, listid, pkgid):
    list = get_object_or_404(Todolist, id=listid)
    pkg  = get_object_or_404(TodolistPkg, id=pkgid)
    pkg.complete = not pkg.complete
    pkg.save()
    return HttpResponseRedirect('/todo/%s/' % (listid))

def view(request, listid):
    list = get_object_or_404(Todolist, id=listid)
    pkgs = TodolistPkg.objects.filter(list=list.id).order_by('pkg')
    return render_response(
        request, 
        'todolists/view.html', 
        {'list':list,'pkgs':pkgs})

def list(request):
    lists = Todolist.objects.order_by('-date_added')
    for l in lists:
        l.complete = TodolistPkg.objects.filter(
            list=l.id,complete=False).count() == 0
    return render_response(request, 'todolists/list.html', {'lists':lists})

@user_passes_test(lambda u: u.has_perm('todolists.add_todolist'))
def add(request):
    if request.POST:
        try:
            m = User.objects.get(username=request.user.username)
        except User.DoesNotExist:
            return render_response(request, 'error_page.html',
                {'errmsg': 'Cannot find a maintainer record for you!'})
        # create the list
        todo = Todolist(
            creator     = m,
            name        = request.POST.get('name'),
            description = request.POST.get('description'))
        todo.save()
        # now link in packages
        for p in request.POST.get('packages').split("\n"):
            for pkg in Package.objects.filter(
                    pkgname=p.strip()).order_by('arch'):
                todopkg = TodolistPkg(
                    list = todo,
                    pkg  = pkg)
                try:
                    todopkg.save()
                except IntegrityError, (num, desc):
                    if num == 1062: # duplicate entry aka dupe package on list
                        pass
        return HttpResponseRedirect('/todo/')
    return render_response(request, 'todolists/add.html')

# vim: set ts=4 sw=4 et: