views.py 4.73 KB
Newer Older
1
2
3
4
5
#   views.py - views for comments
#
#   This file is part of debexpo
#   https://salsa.debian.org/mentors.debian.net-team/debexpo
#
6
#   Copyright © 2019 Baptiste Beauplat <lyknode@cilg.org>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#
#   Permission is hereby granted, free of charge, to any person
#   obtaining a copy of this software and associated documentation
#   files (the "Software"), to deal in the Software without
#   restriction, including without limitation the rights to use,
#   copy, modify, merge, publish, distribute, sublicense, and/or sell
#   copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following
#   conditions:
#
#   The above copyright notice and this permission notice shall be
#   included in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.

29
from logging import getLogger
30

31
32
33
34
35
36
from django.conf import settings
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponseNotAllowed
from django.urls import reverse

Baptiste Beauplat's avatar
Baptiste Beauplat committed
37
from .forms import SubscriptionForm, CommentForm
38
from .models import PackageSubscription
Baptiste Beauplat's avatar
Baptiste Beauplat committed
39
from debexpo.packages.models import Package, PackageUpload
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

log = getLogger(__name__)


@login_required
def subscriptions(request):
    if request.method == 'POST':
        return HttpResponseRedirect(reverse('subscribe_package',
                                    args=[request.POST.get('package')]))

    return render(request, 'subscriptions.html', {
        'settings': settings,
        'subscriptions': PackageSubscription.objects.filter(user=request.user)
        .all()
    })


@login_required
def unsubscribe(request, name):
    if request.method != 'POST':
        return HttpResponseNotAllowed(['POST'])

    sub = get_object_or_404(PackageSubscription, user=request.user,
                            package=name)
    sub.delete()

    return HttpResponseRedirect(reverse('subscriptions'))


@login_required
def subscribe(request, name):
    sub = PackageSubscription.objects.filter(user=request.user,
                                             package=name)
    instance = None

    if sub.exists():
        instance = sub.get()

    if request.method == 'POST':
        form = SubscriptionForm(request.POST, instance=instance)
        package = request.POST.get('next')

82
83
84
85
86
87
        # The condition here is just for show. The form is composed of two
        # checkbox which are either absent for False or present with any value
        # for True. It's not possible for it to be not valid.
        #
        # The test is kept here for future needs.
        if form.is_valid():  # pragma: no branch
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
            subscription = form.save(commit=False)
            subscription.user = request.user
            subscription.package = name
            subscription.save()

            if subscription.can_delete():
                log.info(f'Unsubscribe {request.user.email} for package {name}')
                subscription.delete()
            else:
                log.info(f'Updating subscription for {request.user.email} on '
                         f'{name}: '
                         f'{", ".join(subscription.get_subscriptions())}')

            if package:
                return HttpResponseRedirect(reverse('package', args=[package]))
            else:
                return HttpResponseRedirect(reverse('subscriptions'))
    else:
        form = SubscriptionForm(instance=instance)
        package = request.GET.get('next')

    return render(request, 'subscribe.html', {
        'settings': settings,
        'package': name,
        'next': package,
        'form': form,
    })
Baptiste Beauplat's avatar
Baptiste Beauplat committed
115
116
117
118
119
120
121
122
123
124


@login_required
def comment(request, name):
    if request.method != 'POST':
        return HttpResponseNotAllowed(['POST'])

    upload_id = request.POST.get('upload_id')
    package = get_object_or_404(Package, name=name)
    upload = get_object_or_404(PackageUpload, package=package, pk=upload_id)
125
126
    redirect_url = reverse('package', args=[name])
    index = upload.get_index()
Baptiste Beauplat's avatar
Baptiste Beauplat committed
127
128
129
130
131
132
133
134
135

    form = CommentForm(request.POST)
    if form.is_valid():
        comment = form.save(commit=False)
        comment.user = request.user
        comment.upload = upload
        comment.save()
        comment.notify(request)

136
    return HttpResponseRedirect(f"{redirect_url}#upload-{index}")