| 1 | #!/usr/bin/python |
|---|
| 2 | # -*- encoding: utf-8 -*- |
|---|
| 3 | |
|---|
| 4 | ## |
|---|
| 5 | ## views.py |
|---|
| 6 | ## Created on Sat Dec 23 00:08:09 2006 |
|---|
| 7 | ## by Antti Kaihola <antti.kaihola@linux-aktivaattori.org> |
|---|
| 8 | ## |
|---|
| 9 | ## Copyright (C) 2006 Antti Kaihola |
|---|
| 10 | ## This program is free software; you can redistribute it and/or modify |
|---|
| 11 | ## it under the terms of the GNU General Public License as published by |
|---|
| 12 | ## the Free Software Foundation; either version 2 of the License, or |
|---|
| 13 | ## (at your option) any later version. |
|---|
| 14 | ## |
|---|
| 15 | ## This program is distributed in the hope that it will be useful, |
|---|
| 16 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | ## GNU General Public License for more details. |
|---|
| 19 | ## |
|---|
| 20 | ## You should have received a copy of the GNU General Public License |
|---|
| 21 | ## along with this program; if not, write to the Free Software |
|---|
| 22 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 23 | ## |
|---|
| 24 | |
|---|
| 25 | import re |
|---|
| 26 | |
|---|
| 27 | from django.shortcuts import render_to_response |
|---|
| 28 | from django.template import RequestContext |
|---|
| 29 | from django import newforms as forms |
|---|
| 30 | from django.http import HttpResponseRedirect |
|---|
| 31 | |
|---|
| 32 | from models import TracSite |
|---|
| 33 | |
|---|
| 34 | from tracaccess import import_tickets_csv |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | def rrr(template, request, **kw): |
|---|
| 38 | return render_to_response(template, kw, RequestContext(request)) |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | class SelectSitesForm(forms.Form): |
|---|
| 42 | sites = forms.MultipleChoiceField(required=False, label=_('Trac sites')) |
|---|
| 43 | owners = forms.CharField(required=False, label=_('Ticket owners')) |
|---|
| 44 | |
|---|
| 45 | def index(request): |
|---|
| 46 | sites_form = SelectSitesForm() |
|---|
| 47 | sites_form.fields['sites'].widget.choices = ((site.id, site.name) |
|---|
| 48 | for site in TracSite.objects.all()) |
|---|
| 49 | return rrr('tracregator/index.html', request, form=sites_form) |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | def filter_owners(owners, tickets): |
|---|
| 53 | for ticket in tickets: |
|---|
| 54 | if not owners or 'owner' not in ticket or ticket['owner'] in owners: |
|---|
| 55 | yield ticket |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | def tickets(request, sites_str=None, owners_str=None): |
|---|
| 59 | if request.GET: |
|---|
| 60 | if request.GET.has_key('show_tickets_for_sites'): |
|---|
| 61 | sites = '+'.join(site.name |
|---|
| 62 | for site in TracSite.objects.filter(pk__in=request.GET.getlist('sites')).order_by('name')) |
|---|
| 63 | else: |
|---|
| 64 | sites = 'all_sites' |
|---|
| 65 | owners = filter(None, re.split(r'\W+', request.GET['owners'])) \ |
|---|
| 66 | or ['all_owners'] |
|---|
| 67 | owners.sort() |
|---|
| 68 | return HttpResponseRedirect('./%s/%s/' % ( |
|---|
| 69 | sites, '+'.join(owners))) |
|---|
| 70 | |
|---|
| 71 | sites = TracSite.objects.all() |
|---|
| 72 | if sites_str is not None and sites_str != 'all_sites': |
|---|
| 73 | sites = sites.filter(name__in=sites_str.split('+')) |
|---|
| 74 | |
|---|
| 75 | if owners_str is None or owners_str == 'all_owners': |
|---|
| 76 | owners = None |
|---|
| 77 | else: |
|---|
| 78 | owners = owners_str.split('+') |
|---|
| 79 | |
|---|
| 80 | for site in sites: |
|---|
| 81 | site.tickets = filter_owners( |
|---|
| 82 | owners, |
|---|
| 83 | import_tickets_csv(site.name, site.baseurl, site.report_number, site.username, site.password)) |
|---|
| 84 | |
|---|
| 85 | return rrr('tracregator/tickets.html', request, |
|---|
| 86 | sites=sites, |
|---|
| 87 | owners=owners) |
|---|
| 88 | |
|---|