article.py 2.91 KB
Newer Older
cynddl's avatar
cynddl committed
1
from django.db import models
cynddl's avatar
cynddl committed
2
from django.utils.translation import ugettext_lazy as _
luxcem's avatar
luxcem committed
3

cynddl's avatar
cynddl committed
4
from taggit.managers import TaggableManager
luxcem's avatar
luxcem committed
5
from newspaper import Article as ArticleParser
luxcem's avatar
luxcem committed
6
from django_und.models import VoteMixin
luxcem's avatar
luxcem committed
7

8 9
from datetime import datetime

cynddl's avatar
cynddl committed
10

luxcem's avatar
luxcem committed
11 12 13 14 15 16
STATUS_CHOICES = (
    ("PENDING", _("Pending")),
    ("PUBLISHED", _("Published")),
    ("REJECTED", _("Rejected"))
)

17 18 19 20 21 22
LANG_CHOICES = (
    ("FR", _("French")),
    ("EN", _("English")),
    ("NA", _("Other"))
)

23 24 25 26 27 28 29 30 31 32
URL_HELP_TEXT = """The URL should not contain any marketing tags. We
automatically strip the most known tags."""

TITLE_HELP_TEXT = """Please remove non-necessary parts such as newspapers'
names and leave only the article title."""

EXTRACTS_HELP_TEXT = """Please select short and helpful extracts from the
article content. You should aim at around 500 characters. Use bracket ellipsis
[…] to cut parts not required to understand the context."""

luxcem's avatar
luxcem committed
33

luxcem's avatar
luxcem committed
34
class Article(VoteMixin):
35
    url = models.URLField("URL", help_text=URL_HELP_TEXT)
36 37
    lang = models.CharField(
        _("Language"), choices=LANG_CHOICES, default="NA", max_length=50)
38 39 40 41 42 43 44
    metadata = models.TextField(
        _("Opengraph metadata"), blank=True, null=True)
    screenshot = models.ImageField(
        _("Article screenshot"), blank=True, null=True)
    title = models.CharField(
        _("Article title"), max_length=255, default="",
        help_text=TITLE_HELP_TEXT)
cynddl's avatar
cynddl committed
45
    website = models.CharField(_("Website"), max_length=255, default="")
46 47 48
    extracts = models.TextField(
        _("Content extracts"), blank=True, null=True,
        help_text=EXTRACTS_HELP_TEXT)
cynddl's avatar
cynddl committed
49 50 51

    created_at = models.DateTimeField(_("Creation date"), auto_now_add=True)
    updated_at = models.DateTimeField(_("Last update"), auto_now=True)
52 53
    published_at = models.DateTimeField(
        _("Publication date"), blank=True, null=True)
cynddl's avatar
cynddl committed
54
    status = models.CharField(
55
        _("Status"), choices=STATUS_CHOICES, default="PENDING", max_length=20)
cynddl's avatar
cynddl committed
56

luxcem's avatar
luxcem committed
57 58
    #: priority: True if article have priority
    priority = models.BooleanField(default=False)
59

60
    tags = TaggableManager(blank=True)
61

62 63 64
    class Meta:
        verbose_name = _("Article")
        verbose_name_plural = _("Articles")
luxcem's avatar
luxcem committed
65 66 67 68 69
        permissions = (
            ("can_change_status", "Can change article status"),
            ("can_change_priority", "Can change article priority"),
            ("can_vote", "Can vote articles"),
        )
luxcem's avatar
luxcem committed
70 71 72

    def __str__(self):
        return self.title
dave's avatar
dave committed
73

74 75 76 77 78 79 80
    def publish(self):
        if not self.published_at:
            self.published_at = datetime.now()

        self.status = "PUBLISHED"
        self.save()

dave's avatar
dave committed
81
    def parse(self):
82 83
        lang_lower = self.lang.lower() if self.lang != "NA" else None
        article = ArticleParser(url=self.url, language=lang_lower)
dave's avatar
dave committed
84 85 86 87 88 89
        article.download()
        article.parse()
        self.title = article.title
        self.extracts = article.text
        # self.screenshot = getScreenshot(self.url)
        # TODO: save metadata