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

8
9
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin

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

cynddl's avatar
cynddl committed
14
15
from taggit.models import Tag
from rp.forms import TagMultipleChoiceField
16

luxcem's avatar
luxcem committed
17
18
19
20
from rp.models import Article
from .votes import UDList


cynddl's avatar
cynddl committed
21
class ArticleListFlux(UDList):
luxcem's avatar
luxcem committed
22
    model = Article
cynddl's avatar
cynddl committed
23
24
25
26
27
    paginate_by = 10

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

28
29
        if filter_view in ["published", "draft", "rejected"]:
            qs = Article.objects.filter(status=filter_view.upper())
cynddl's avatar
cynddl committed
30
        else:
31
            qs = Article.objects.filter(status="NEW")
cynddl's avatar
cynddl committed
32
33
34
35
36
37

        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")
38
        context["nb_draft"] = Article.objects.filter(status="DRAFT").count()
cynddl's avatar
cynddl committed
39
        return context
40
41
42
43
44
45
46
47
48
49
50
51


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


52
class ArticleEdit(PermissionRequiredMixin, UpdateView):
53
    model = Article
54
55
    permission_required = 'can_edit'

56
    fields = ['screenshot', 'url', 'lang', 'title', 'tags', 'extracts']
cynddl's avatar
cynddl committed
57
    success_url = reverse_lazy("rp:article-list")
58
59
60
61
62
63

    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
64
65
        elif "view" in self.request.POST:
            self.success_url = reverse("rp:article-view", args=[self.object.id])
66
        elif "publish" in self.request.POST:
67
            self.object.publish()
68
            self.object.save()
69
70
71
72
73
74
75
76
77
78

        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
79
80
        # Change the layout of the tags field
        form.fields["tags"] = TagMultipleChoiceField(
81
            queryset=Tag.objects.all(), required=False)
cynddl's avatar
cynddl committed
82

83
84
85
86
87
        left_layout = Div(
            Div(
                Field('title', wrapper_class='col-sm-10'),
                Field('lang', wrapper_class='col-sm-2'),
                css_class="row"),
88
89
90
            AppendedText(
                'url',
                _('<a href="%s">Go to link</a>') % form.initial['url']),
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
            '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