article.py 1.76 KB
Newer Older
cynddl's avatar
cynddl committed
1
from django.db import models
cynddl's avatar
cynddl committed
2 3
from django.utils.translation import ugettext_lazy as _
from taggit.managers import TaggableManager
4
from .vote import UnDVotedMixin
luxcem's avatar
luxcem committed
5 6
from newspaper import Article as ArticleParser

cynddl's avatar
cynddl committed
7

8
class Article(UnDVotedMixin):
cynddl's avatar
cynddl committed
9
    url = models.URLField("URL")
cynddl's avatar
cynddl committed
10 11
    lang = models.CharField(_("Language"), max_length=50, null=True)
    metadata = models.TextField(_("Opengraph metadata"), blank=True, null=True)
luxcem's avatar
luxcem committed
12 13
    screenshot = models.ImageField(_("Article screenshot"),
                                   blank=True, null=True)
cynddl's avatar
cynddl committed
14 15 16
    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
17 18 19

    created_at = models.DateTimeField(_("Creation date"), auto_now_add=True)
    updated_at = models.DateTimeField(_("Last update"), auto_now=True)
luxcem's avatar
luxcem committed
20 21
    published_at = models.DateTimeField(_("Publication date"),
                                        blank=True, null=True)
cynddl's avatar
cynddl committed
22 23 24 25 26 27

    STATUS_CHOICES = (
        ("PENDING", _("Pending")),
        ("PUBLISHED", _("Published")),
        ("REJECTED", _("Rejected"))
    )
luxcem's avatar
luxcem committed
28

cynddl's avatar
cynddl committed
29
    status = models.CharField(
cynddl's avatar
cynddl committed
30 31
        _("Status"), choices=STATUS_CHOICES,
        default="PENDING", max_length=20)
cynddl's avatar
cynddl committed
32 33

    tags = TaggableManager()
34

35 36
    # TODO: adds links to user

37 38 39
    class Meta:
        verbose_name = _("Article")
        verbose_name_plural = _("Articles")
luxcem's avatar
luxcem committed
40 41 42

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

    def parse(self):
luxcem's avatar
luxcem committed
45
        article = ArticleParser(url=self.url, language=self.lang)
dave's avatar
dave committed
46 47 48 49 50 51
        article.download()
        article.parse()
        self.title = article.title
        self.extracts = article.text
        # self.screenshot = getScreenshot(self.url)
        # TODO: save metadata