articles.py 4.9 KB
Newer Older
1
2
from django.http import HttpResponseRedirect
from django.views.generic.detail import DetailView
3
from django.views.generic.list import ListView
4
from django.views.generic.edit import UpdateView
5
from django.utils.translation import ugettext_lazy as _
cynddl's avatar
cynddl committed
6
from django.urls import reverse, reverse_lazy
cynddl's avatar
cynddl committed
7
from django.db.models import Count
8
9
from django import forms

10
11
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin

12
from crispy_forms.helper import FormHelper
cynddl's avatar
cynddl committed
13
from crispy_forms.layout import Layout, Field, Div, HTML
14
15
from crispy_forms.bootstrap import AppendedText

cynddl's avatar
cynddl committed
16
17
from taggit.models import Tag
from rp.forms import TagMultipleChoiceField
18

luxcem's avatar
luxcem committed
19
20
21
22
from rp.models import Article
from .votes import UDList


23
24
25
26
class ArticleList(ListView):
    model = Article
    paginate_by = 10
    template_name = "rp/article_list_public.html"
27
28
    filter_lang = None

cynddl's avatar
cynddl committed
29
30
    filter_tag = None

31
32
33
34
35
36
    def get_queryset(self):
        if self.filter_lang in ["EN", "FR"]:
            qs = Article.objects.filter(lang=self.filter_lang)
        else:
            qs = Article.objects.filter()

Thibaut Broggi's avatar
Thibaut Broggi committed
37
38
39
        if self.request.GET.get('q', '') != '':
            qs = qs.filter(title__contains=self.request.GET.get('q', ''))

cynddl's avatar
cynddl committed
40
41
42
43
        filter_tag = self.kwargs.get("filter_tag", self.filter_tag)
        if filter_tag is not None:
            qs = qs.filter(tags__name__in=[filter_tag])

44
        return qs.order_by('-created_at')
cynddl's avatar
cynddl committed
45
46
47
48
49
50
51

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        qs = Tag.objects.annotate(num_times=Count('taggit_taggeditem_items')).all()
        qs = qs.order_by('-num_times')
        context["tags"] = qs
Thibaut Broggi's avatar
Thibaut Broggi committed
52
        context["search"] = self.request.GET.get("q", '')
cynddl's avatar
cynddl committed
53
54

        return context
55
56


cynddl's avatar
cynddl committed
57
class ArticleListFlux(UDList):
luxcem's avatar
luxcem committed
58
    model = Article
cynddl's avatar
cynddl committed
59
60
61
62
63
    paginate_by = 10

    def get_queryset(self):
        filter_view = self.kwargs.get("filter_view", "draft")

64
65
        if filter_view in ["published", "draft", "rejected"]:
            qs = Article.objects.filter(status=filter_view.upper())
cynddl's avatar
cynddl committed
66
        else:
67
            qs = Article.objects.filter(status="NEW")
cynddl's avatar
cynddl committed
68
69
70
71
72
73

        return qs.order_by('-created_at')

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["filter_view"] = self.kwargs.get("filter_view", "draft")
74
        context["nb_draft"] = Article.objects.filter(status="DRAFT").count()
cynddl's avatar
cynddl committed
75
        return context
76
77
78
79
80
81
82
83
84
85
86
87


class ArticleDetailView(DetailView):
    model = Article
    preview = False

    def get_context_data(self, **kwargs):
        context = super(ArticleDetailView, self).get_context_data(**kwargs)
        context['is_preview'] = self.preview
        return context


88
class ArticleEdit(PermissionRequiredMixin, UpdateView):
89
    model = Article
90
91
    permission_required = 'can_edit'

92
    fields = ['screenshot', 'url', 'lang', 'title', 'tags', 'extracts']
cynddl's avatar
cynddl committed
93
    success_url = reverse_lazy("rp:article-list")
94

95
96
97
98
99
100
101
102
103
104
105
    def get(self, request, **kwargs):
        self.object = self.get_object()

        if 'fetch_content' in self.request.GET:
            self.object.fetch_content()
        elif 'fetch_image' in self.request.GET:
            self.object.fetch_image()

        context = self.get_context_data(object=self.object)
        return self.render_to_response(context)

106
107
108
109
110
    def form_valid(self, form):
        self.object = form.save()

        if "preview" in self.request.POST:
            self.success_url = reverse("rp:article-preview", args=[self.object.id])
cynddl's avatar
cynddl committed
111
112
        elif "view" in self.request.POST:
            self.success_url = reverse("rp:article-view", args=[self.object.id])
113
        elif "publish" in self.request.POST:
114
            self.object.publish()
115
            self.object.save()
116
117
118
119
120
121
122
123
124
125

        return HttpResponseRedirect(self.get_success_url())

    def get_form(self, form_class=None):
        form = super().get_form(form_class)
        form.fields["screenshot"].widget = forms.widgets.FileInput()

        form.helper = FormHelper()
        form.helper.form_tag = False

cynddl's avatar
cynddl committed
126
127
        # Change the layout of the tags field
        form.fields["tags"] = TagMultipleChoiceField(
128
            queryset=Tag.objects.all(), required=False)
cynddl's avatar
cynddl committed
129

130
131
132
133
134
        left_layout = Div(
            Div(
                Field('title', wrapper_class='col-sm-10'),
                Field('lang', wrapper_class='col-sm-2'),
                css_class="row"),
135
136
137
            AppendedText(
                'url',
                _('<a href="%s">Go to link</a>') % form.initial['url']),
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
            'tags',
            'extracts',
            css_class="col-sm-8")

        right_layout = Div(
            'screenshot',
            HTML(
                """
                {% if form.screenshot.value %}
                <img class="img-responsive mb-4"
                     src="/media/{{ form.screenshot.value }}">
                {% endif %}
                """
            ),
            css_class="col-sm-4")

        form.helper.layout = Layout(
            Div(left_layout, right_layout, css_class="row"))

        return form