summaryrefslogtreecommitdiffstats
path: root/todolists/views.py
blob: 519fae9d4f3c9871f8cabd99d601ff7b6caca7f1 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from django import forms

from django.http import HttpResponse
from django.core.mail import send_mail
from django.shortcuts import get_object_or_404, redirect
from django.contrib.auth.decorators import login_required, permission_required
from django.db.models import Count
from django.views.decorators.cache import never_cache
from django.views.generic.create_update import delete_object
from django.views.generic.simple import direct_to_template
from django.template import Context, loader
from django.utils import simplejson

from main.models import Todolist, TodolistPkg, Package

class TodoListForm(forms.Form):
    name = forms.CharField(max_length=255,
            widget=forms.TextInput(attrs={'size': '30'}))
    description = forms.CharField(required=False,
            widget=forms.Textarea(attrs={'rows': '4', 'cols': '60'}))
    packages = forms.CharField(required=False,
            help_text='(one per line)',
            widget=forms.Textarea(attrs={'rows': '20', 'cols': '60'}))

    def clean_packages(self):
        package_names = [s.strip() for s in
                self.cleaned_data['packages'].split("\n")]
        package_names = set(package_names)
        packages = Package.objects.filter(
                pkgname__in=package_names).exclude(
                repo__testing=True).order_by('arch')
        return packages


@login_required
@never_cache
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()
    if request.is_ajax():
        return HttpResponse(
            simplejson.dumps({'complete': pkg.complete}),
            mimetype='application/json')
    return redirect(list)

@login_required
@never_cache
def view(request, listid):
    list = get_object_or_404(Todolist, id=listid)
    return direct_to_template(request, 'todolists/view.html', {'list': list})

@login_required
@never_cache
def list(request):
    lists = Todolist.objects.select_related('creator').annotate(
            pkg_count=Count('todolistpkg')).order_by('-date_added')
    incomplete = Todolist.objects.filter(todolistpkg__complete=False).annotate(
            Count('todolistpkg')).values_list('id', 'todolistpkg__count')

    # tag each list with an incomplete package count
    lookup = {}
    for k, v in incomplete:
        lookup[k] = v
    for l in lists:
        l.incomplete_count = lookup.get(l.id, 0)

    return direct_to_template(request, 'todolists/list.html', {'lists': lists})

# TODO: this calls for transaction management and async emailing
@permission_required('main.add_todolist')
@never_cache
def add(request):
    if request.POST:
        form = TodoListForm(request.POST)
        if form.is_valid():
            todo = Todolist.objects.create(
                creator     = request.user,
                name        = form.cleaned_data['name'],
                description = form.cleaned_data['description'])

            for pkg in form.cleaned_data['packages']:
                tpkg = TodolistPkg.objects.create(list=todo, pkg=pkg)
                send_todolist_email(tpkg)

            return redirect('/todo/')
    else:
        form = TodoListForm()

    page_dict = {
            'title': 'Add Todo List',
            'form': form,
            'submit_text': 'Create List'
    }
    return direct_to_template(request, 'general_form.html', page_dict)

# TODO: this calls for transaction management and async emailing
@permission_required('main.change_todolist')
@never_cache
def edit(request, list_id):
    todo_list = get_object_or_404(Todolist, id=list_id)
    if request.POST:
        form = TodoListForm(request.POST)
        if form.is_valid():
            todo_list.name = form.cleaned_data['name']
            todo_list.description = form.cleaned_data['description']
            todo_list.save()

            packages = [p.pkg for p in todo_list.packages]

            # first delete any packages not in the new list
            for p in todo_list.packages:
                if p.pkg not in form.cleaned_data['packages']:
                    p.delete()

            # now add any packages not in the old list
            for pkg in form.cleaned_data['packages']:
                if pkg not in packages:
                    tpkg = TodolistPkg.objects.create(
                            list=todo_list, pkg=pkg)
                    send_todolist_email(tpkg)

            return redirect(todo_list)
    else:
        form = TodoListForm(initial={
            'name': todo_list.name,
            'description': todo_list.description,
            'packages': '\n'.join(todo_list.package_names),
        })
    page_dict = {
            'title': 'Edit Todo List: %s' % todo_list.name,
            'form': form,
            'submit_text': 'Save List'
    }
    return direct_to_template(request, 'general_form.html', page_dict)

@permission_required('main.delete_todolist')
@never_cache
def delete_todolist(request, object_id):
    return delete_object(request, object_id=object_id, model=Todolist,
            template_name="todolists/todolist_confirm_delete.html",
            post_delete_redirect='/todo/')

def send_todolist_email(todo):
    '''Sends an e-mail to the maintainer of a package notifying them that the
    package has been added to a todo list'''
    maints = todo.pkg.maintainers
    if not maints:
        return

    page_dict = {
            'pkg': todo.pkg,
            'todolist': todo.list,
            'weburl': todo.pkg.get_full_url()
    }
    t = loader.get_template('todolists/email_notification.txt')
    c = Context(page_dict)
    send_mail('arch: Package [%s] added to Todolist' % todo.pkg.pkgname,
            t.render(c),
            'Arch Website Notification <nobody@archlinux.org>',
            [m.email for m in maints],
            fail_silently=True)

def public_list(request):
    todo_lists = Todolist.objects.incomplete()
    return direct_to_template(request, "todolists/public_list.html",
            {"todo_lists": todo_lists})


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