article.py 2.02 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

cynddl's avatar
cynddl committed
8

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


luxcem's avatar
luxcem committed
16
class Article(VoteMixin):
cynddl's avatar
cynddl committed
17
    url = models.URLField("URL")
cynddl's avatar
cynddl committed
18 19
    lang = models.CharField(_("Language"), max_length=50, null=True)
    metadata = models.TextField(_("Opengraph metadata"), blank=True, null=True)
luxcem's avatar
luxcem committed
20 21
    screenshot = models.ImageField(_("Article screenshot"),
                                   blank=True, null=True)
cynddl's avatar
cynddl committed
22 23 24
    title = models.CharField(_("Article title"), max_length=255, default="")
    website = models.CharField(_("Website"), max_length=255, default="")
    extracts = models.TextField(_("Content extracts"), blank=True, null=True)
cynddl's avatar
cynddl committed
25 26 27

    created_at = models.DateTimeField(_("Creation date"), auto_now_add=True)
    updated_at = models.DateTimeField(_("Last update"), auto_now=True)
luxcem's avatar
luxcem committed
28 29
    published_at = models.DateTimeField(_("Publication date"),
                                        blank=True, null=True)
cynddl's avatar
cynddl committed
30
    status = models.CharField(
cynddl's avatar
cynddl committed
31 32
        _("Status"), choices=STATUS_CHOICES,
        default="PENDING", max_length=20)
cynddl's avatar
cynddl committed
33

luxcem's avatar
luxcem committed
34 35
    #: priority: True if article have priority
    priority = models.BooleanField(default=False)
36

luxcem's avatar
luxcem committed
37
    tags = TaggableManager()
38

39 40 41
    class Meta:
        verbose_name = _("Article")
        verbose_name_plural = _("Articles")
luxcem's avatar
luxcem committed
42 43 44 45 46
        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
47 48 49

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

    def parse(self):
luxcem's avatar
luxcem committed
52
        article = ArticleParser(url=self.url, language=self.lang)
dave's avatar
dave committed
53 54 55 56 57 58
        article.download()
        article.parse()
        self.title = article.title
        self.extracts = article.text
        # self.screenshot = getScreenshot(self.url)
        # TODO: save metadata