articles.py 3.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
from django.http import HttpResponseRedirect
from django.views.generic.detail import DetailView
from django.views.generic.edit import UpdateView
from django.urls import reverse
from django import forms

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Field, Div, HTML
from crispy_forms.bootstrap import AppendedText


luxcem's avatar
luxcem committed
12
13
14
15
from rp.models import Article
from .votes import UDList


cynddl's avatar
cynddl committed
16
class ArticleListFlux(UDList):
luxcem's avatar
luxcem committed
17
    model = Article
cynddl's avatar
cynddl committed
18
19
20
21
22
23
24
25
    paginate_by = 10

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

        if filter_view == "published":
            qs = Article.objects.filter(status="PUBLISHED")
        elif filter_view == "draft":
cynddl's avatar
cynddl committed
26
27
28
29
            qs = Article.objects.extra(where=[
                "und_score_up + und_score_down >= 3",
                "status='PENDING'"
            ])
luxcem's avatar
luxcem committed
30
31
        elif filter_view == "rejected":
            qs = Article.objects.filter(status="REJECTED")
cynddl's avatar
cynddl committed
32
33
34
35
36
37
38
39
        else:
            qs = Article.objects.filter(status="PENDING")

        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")
cynddl's avatar
cynddl committed
40
41
42
43
        context["nb_draft"] = Article.objects.extra(where=[
            "und_score_up + und_score_down >= 3",
            "status='PENDING'"
        ]).count()
cynddl's avatar
cynddl committed
44
        return context
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


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


class ArticleEdit(UpdateView):
    model = Article
    fields = ['screenshot', 'url', 'lang', 'title', 'tags', 'extracts']
    success_url = 'article-list'

    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])
        elif "publish" in self.request.POST:
            self.object.status = "PUBLISHED"
            self.object.save()
            self.success_url = reverse("rp:article-list")

        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

        left_layout = Div(
            Div(
                Field('title', wrapper_class='col-sm-10'),
                Field('lang', wrapper_class='col-sm-2'),
                css_class="row"),
            AppendedText('url',
                '<a href="%s">Go to link</a>' % form.initial['url']),
            '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"))

        form.helper.add_input(
            Submit('submit', 'Update the article', css_class='btn-primary'))

        return form