views.py 3.42 KB
Newer Older
1
from __future__ import annotations
2
from django.utils.translation import gettext as _
Enrico Zini's avatar
Enrico Zini committed
3
from django import http, forms
4
5
6
7
8
9
from django.views.generic.edit import FormView
from django.core.exceptions import PermissionDenied
import backend.models as bmodels
import minechangelogs.models as mmodels
from backend.mixins import VisitorMixin
import datetime
Enrico Zini's avatar
Enrico Zini committed
10
from nm2.lib.forms import BootstrapAttrsMixin
11
12


Enrico Zini's avatar
Enrico Zini committed
13
class MinechangelogsForm(BootstrapAttrsMixin, forms.Form):
14
15
16
    query = forms.CharField(
        required=True,
        label=_("Query"),
Enrico Zini's avatar
Enrico Zini committed
17
18
19
        help_text=_("Enter one keyword per line. Changelog entries to be shown must match at least one keyword. "
                    "You often need to tweak the keywords to improve the quality of results. "
                    "Note that keyword matching is case-sensitive."),
20
21
22
23
24
        widget=forms.Textarea(attrs=dict(rows=5, cols=40))
    )
    download = forms.BooleanField(
        required=False,
        label=_("Download"),
Carsten Schoenert's avatar
Carsten Schoenert committed
25
        help_text=_("Activate this field to download the changelog instead of displaying it."),
26
27
28
29
30
31
32
33
    )


class MineChangelogs(VisitorMixin, FormView):
    template_name = "minechangelogs/minechangelogs.html"
    form_class = MinechangelogsForm

    def check_permissions(self):
34
35
        super().check_permissions()
        if not self.request.user.is_authenticated:
36
37
38
            raise PermissionDenied

    def load_objects(self):
39
        super().load_objects()
40
41
42
43
44
45
46
        self.key = self.kwargs.get("key", None)
        if self.key:
            self.person = bmodels.Person.lookup_or_404(self.key)
        else:
            self.person = None

    def get_initial(self):
47
        res = super().get_initial()
48
49
50
51
52
53
54
        if not self.person:
            return res

        query = [
            self.person.fullname,
            self.person.email,
        ]
55
        if self.person.ldap_fields.cn and self.person.ldap_fields.mn and self.person.ldap_fields.sn:
56
            # some people don't use their middle names in changelogs
57
58
59
            query.append("{} {}".format(self.person.ldap_fields.cn, self.person.ldap_fields.sn))
        if self.person.ldap_fields.uid:
            query.append(self.person.ldap_fields.uid)
60
61
62
        return {"query": "\n".join(query)}

    def get_context_data(self, **kw):
63
        ctx = super().get_context_data(**kw)
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
        info = mmodels.info()
        info["max_ts"] = datetime.datetime.fromtimestamp(info["max_ts"])
        info["last_indexed"] = datetime.datetime.fromtimestamp(info["last_indexed"])
        ctx.update(
            info=info,
            person=self.person,
        )
        return ctx

    def form_valid(self, form):
        query = form.cleaned_data["query"]
        keywords = [x.strip() for x in query.split("\n")]
        entries = mmodels.query(keywords)
        if form.cleaned_data["download"]:
            def send_entries():
                for e in entries:
                    yield e
                    yield "\n\n"
            res = http.HttpResponse(send_entries(), content_type="text/plain")
            if self.person:
                res["Content-Disposition"] = 'attachment; filename=changelogs-%s.txt' % self.person.lookup_key
            else:
                res["Content-Disposition"] = 'attachment; filename=changelogs.txt'
            return res

89
        entries = [e for e in entries if not e.startswith("debian-keyring ")]
90
91
92
93
        return self.render_to_response(self.get_context_data(
            form=form,
            entries=entries,
            keywords=keywords))