summaryrefslogtreecommitdiff
path: root/packages/views.py
blob: 3318d21083cefedce8670a32414117aa3b961cdb (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import urllib
from django import forms
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.contrib.auth.models import User
from django.contrib.admin.widgets import AdminDateWidget
from django.views.generic import list_detail
from django.db.models import Q
import datetime
from archweb_dev.main.utils import render_response
from archweb_dev.main.models import Package, PackageFile
from archweb_dev.main.models import Arch, Repo, Signoff
from archweb_dev.main.utils import make_choice

def update(request):
    if request.POST.has_key('adopt'):
        mode = 'adopt'
        message = 'Adoption was successful'
    if request.POST.has_key('disown'):
        mode = 'disown'
        message = 'Disown was successful'

    ids = request.POST.getlist('pkgid')
    for id in ids:
        pkg = Package.objects.get(id=id)
        if mode == 'adopt':
            pkg.maintainer = request.user
        elif mode == 'disown':
            pkg.maintainer_id = 0
        pkg.save()
    return render_response(request, 'status_page.html', {'message':message})

def details(request, pkgid=0, name='', repo='', arch=''):
    if pkgid != 0:
        pkg = get_object_or_404(Package, id=pkgid)
    elif all([name, repo, arch]):
        pkg= get_object_or_404(Package,
                pkgname=name, repo__name__iexact=repo, arch__name=arch)
    else:
        p = Package.objects.filter(pkgname=name)
        if repo: p = p.filter(repo__name__iexact=repo)
        lenp = p.count()
        # if more then one result, send to the search view
        if lenp > 1: return search(request, name)
        if lenp < 1: return render_response(request, 'error_page.html',
            {'errmsg': 'No matching packages.'})
        pkg = p[0]

    return render_response(request, 'packages/details.html', {'pkg': pkg})


class PackageSearchForm(forms.Form):
    repo = forms.ChoiceField(required=False)
    arch = forms.ChoiceField(required=False)
    q = forms.CharField(required=False)
    maintainer = forms.ChoiceField(required=False)
    last_update = forms.DateField(required=False, widget=AdminDateWidget())
    flagged = forms.ChoiceField(
            choices=[('', 'All')] + make_choice(['Flagged', 'Not Flagged']),
            required=False)
    limit = forms.ChoiceField(
            choices=make_choice([50, 100, 250]) + [('all', 'All')],
            required=False,
            initial=50)

    def clean_limit(self):
        limit = self.cleaned_data['limit']
        if limit == 'all':
            limit = None
        elif limit:
            try:
                limit = int(limit)
            except:
                raise forms.ValidationError("Should be an integer")
        else:
            limit = 50
        return limit


    def __init__(self, *args, **kwargs):
        super(PackageSearchForm, self).__init__(*args, **kwargs)
        self.fields['repo'].choices = self.fields[
                'repo'].widget.choices = [('', 'All')] + make_choice(
                        [repo.name for repo in Repo.objects.all()])
        self.fields['arch'].choices = self.fields[
                'arch'].widget.choices = [('', 'All')] + make_choice(
                        [arch.name for arch in Arch.objects.all()])
        self.fields['maintainer'].choices = self.fields[
                'maintainer'].widget.choices = [
                        ('', 'All'), ('orphan', 'Orphan')] + make_choice(
                        [m.username for m in User.objects.all()])

def search(request, page=None):
    current_query = '?'
    limit=50
    packages = Package.objects.all()

    if request.GET:
        current_query += urllib.urlencode(request.GET)
        form = PackageSearchForm(data=request.GET)
        if form.is_valid():
            if form.cleaned_data['repo']:
                packages = packages.filter(
                        repo__name=form.cleaned_data['repo'])
            if form.cleaned_data['arch']:
                packages = packages.filter(
                        arch__name=form.cleaned_data['arch'])
            if form.cleaned_data['maintainer'] == 'orphan':
                packages=packages.filter(maintainer__id = 0)
            if form.cleaned_data['flagged'] == 'Flagged':
                packages=packages.filter(needupdate=True)
            elif form.cleaned_data['flagged'] == 'Not Flagged':
                packages = packages.filter(needupdate=False)
            elif form.cleaned_data['maintainer']:
                packages = packages.filter(
                    maintainer__username=form.cleaned_data['maintainer'])
            limit = form.cleaned_data['limit']
            if form.cleaned_data['q']:
                query = form.cleaned_data['q']
                q = Q(pkgname__icontains=query) | Q(pkgdesc__icontains=query)
                packages = packages.filter(q)
            if form.cleaned_data['last_update']:
                lu = form.cleaned_data['last_update']
                packages = packages.filter(last_update__gte=
                        datetime.datetime(lu.year, lu.month, lu.day, 0, 0))
    else:
        form = PackageSearchForm()

    page_dict = {'search_form': form,
            'current_query': current_query
            }
    if len(packages) == 1:
        return HttpResponseRedirect(packages[0].get_absolute_url())

    if 'sort' in request.GET:
        print 'sorting'
        packages = packages.order_by(request.GET['sort'], 'repo', 'arch', 'pkgname')
    else:
        print 'not sorting'
        packages = packages.order_by('repo', 'arch', '-last_update', 'pkgname')

    print packages

    return list_detail.object_list(request, packages,
            template_name="packages/search.html",
            page=page,
            paginate_by=limit,
            template_object_name="package",
            extra_context=page_dict)
    
    # OLD IMPLEMENTATION BELOW HERE
    if request.GET.has_key('q'):
        # take the q GET var over the one passed on the URL
        query = request.GET['q'].strip()

    # fetch the form vars
    repo       = request.GET.get('repo', 'all')
    arch       = request.GET.get('arch', 'all')
    lastupdate = request.GET.get('lastupdate', '')
    limit      = int(request.GET.get('limit', '50'))
    skip       = int(request.GET.get('skip', '0'))
    sort       = request.GET.get('sort', '')
    maint      = request.GET.get('maint', 'all')
    flagged_only = request.GET.get('flagged_only', 'n')

    # build the form lists
    repos = Repo.objects.all()
    arches  = Arch.objects.all()
    users = User.objects.all()
    # copy GET data over and add the lists
    c = request.GET.copy()
    c['repos'], c['arches']  = repos, arches
    c['users'] = users
    c['limit'], c['skip'] = limit, skip
    c['lastupdate'] = lastupdate
    c['sort'] = sort
    # 'q' gets renamed to 'query', so it's not in GET
    c['query'] = query

    # validate
    errors = {}
    validate(errors, 'Last Update', lastupdate, validators.isValidANSIDate, True, request)
    validate(errors, 'Page Limit', str(limit), validators.isOnlyDigits, True, request)
    validate(errors, 'Page Skip', str(skip), validators.isOnlyDigits, True, request)
    if errors:
        c['errors'] = errors
        return render_response(request, 'packages/search.html', c)

    if query:
        res1 = Package.objects.filter(pkgname__icontains=query)
        res2 = Package.objects.filter(pkgdesc__icontains=query)
        results = res1 | res2
    else:
        results = Package.objects.all()
    if repo != 'all' and repo in [x.name for x in repos]:
        results = results.filter(repo__name__iexact=repo)
    if arch != 'all' and arch in [x.name for x in arches]:
        results = results.filter(arch__name__iexact=arch)
    if maint != 'all':
        results = results.filter(maintainer=maint)
    if flagged_only != 'n':
        results = results.filter(needupdate=1)
    if lastupdate:
        results = results.filter(
            last_update__gte=datetime.datetime(
                int(lastupdate[0:4]),
                int(lastupdate[5:7]),
                int(lastupdate[8:10])))

    # sort results
    if sort == '':
        results = results.order_by('repo', 'arch', '-last_update', 'pkgname')
    else:
        # duplicate sort fields shouldn't hurt anything
        results = results.order_by(sort, 'repo', 'arch', 'pkgname')

    qs = request.GET.copy()
    # build pagination urls
    if results.count() > (skip + limit) and limit > 0:
        qs['skip'] = skip + limit
        c['nextpage'] = '?' + qs.urlencode()
    if skip > 0:
        qs['skip'] = max(0, skip - limit)
        c['prevpage'] = '?' + qs.urlencode()
    # pass the querystring to the template so we can build sort queries
    c['querystring'] = request.GET

    # if only there's only one result, pass right to the package details view
    if results.count() == 1: return details(request, results[0].id)
    # limit result set
    if limit > 0: results = results[skip:(skip+limit)]

    c['results'] = results
    return render_response(request, 'packages/search.html', c)

def files(request, pkgid):
    pkg = get_object_or_404(Package, id=pkgid)
    files = PackageFile.objects.filter(pkg=pkgid)
    return render_response(request, 'packages/files.html', {'pkg':pkg,'files':files})

def unflag(request, pkgid):
    pkg = get_object_or_404(Package, id=pkgid)
    pkg.needupdate = 0
    pkg.save()
    return HttpResponseRedirect('/packages/%d/' % (pkg.id))

def signoffs(request, message=None):
    packages = Package.objects.filter(repo__name="Testing").order_by("pkgname")
    return render_response(request, 'packages/signoffs.html',
            {'packages': packages, 'message': message})

def signoff_package(request, arch, pkgname):
    pkg = get_object_or_404(Package,
            arch__name=arch,
            pkgname=pkgname,
            repo__name="Testing")

    signoff, created = Signoff.objects.get_or_create(
            pkg=pkg,
            pkgver=pkg.pkgver,
            pkgrel=pkg.pkgrel,
            packager=request.user)

    if created:
        message = "You have successfully signed off for %s on %s" % (
                pkg.pkgname, pkg.arch)
    else:
        message = "You have already signed off for %s on %s" % (
                pkg.pkgname, pkg.arch)

    return signoffs(request, message)



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