2018-06-28 06:51:43 -04:00
|
|
|
from computed_property import ComputedCharField
|
2017-10-24 21:01:52 -04:00
|
|
|
from django.contrib.auth import get_user_model
|
2018-06-28 06:51:43 -04:00
|
|
|
from django.contrib.sites.models import Site as DjangoSite
|
2017-10-24 21:01:52 -04:00
|
|
|
from django.db import models
|
2017-10-24 21:31:08 -04:00
|
|
|
from django.urls import reverse
|
2018-06-28 06:51:43 -04:00
|
|
|
from django.utils.functional import cached_property
|
2017-10-27 04:41:25 -04:00
|
|
|
from itertools import groupby
|
2017-11-16 05:52:42 -05:00
|
|
|
from mf2util import interpret
|
2017-10-24 21:31:08 -04:00
|
|
|
from slugify import slugify
|
2017-10-27 01:42:49 -04:00
|
|
|
from textwrap import shorten
|
2018-06-28 06:51:43 -04:00
|
|
|
from urllib.parse import urljoin, urlparse
|
2017-10-24 21:01:52 -04:00
|
|
|
|
2018-03-21 06:50:40 -04:00
|
|
|
from lemonshort.short_url import short_url
|
2017-10-24 23:04:22 -04:00
|
|
|
from meta.models import ModelMeta
|
2017-11-01 21:59:23 -04:00
|
|
|
from model_utils.models import TimeStampedModel
|
2018-06-28 06:51:43 -04:00
|
|
|
from users.models import Site
|
2017-10-24 23:04:22 -04:00
|
|
|
|
2017-10-24 21:01:52 -04:00
|
|
|
from . import kinds
|
2017-12-10 21:30:46 -05:00
|
|
|
from lemoncurry import requests, utils
|
2023-08-10 02:52:37 -04:00
|
|
|
|
2017-10-25 18:01:36 -04:00
|
|
|
ENTRY_KINDS = [(k.id, k.id) for k in kinds.all]
|
2017-10-24 21:01:52 -04:00
|
|
|
|
|
|
|
|
2017-11-19 19:09:30 -05:00
|
|
|
class CatManager(models.Manager):
|
2017-11-19 18:59:03 -05:00
|
|
|
def from_name(self, name):
|
2017-11-19 19:09:30 -05:00
|
|
|
cat, created = self.get_or_create(name=name, slug=slugify(name))
|
|
|
|
return cat
|
2017-11-19 18:59:03 -05:00
|
|
|
|
|
|
|
|
2017-11-19 19:09:30 -05:00
|
|
|
class Cat(models.Model):
|
|
|
|
objects = CatManager()
|
2017-11-19 18:59:03 -05:00
|
|
|
name = models.CharField(max_length=255, unique=True)
|
|
|
|
slug = models.CharField(max_length=255, unique=True)
|
|
|
|
|
2017-11-19 19:37:36 -05:00
|
|
|
def __str__(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
return "#" + self.name
|
2017-11-19 19:37:36 -05:00
|
|
|
|
2017-11-19 18:59:03 -05:00
|
|
|
@property
|
|
|
|
def url(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
return reverse("entries:cat", args=(self.slug,))
|
2017-11-19 18:59:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
2023-08-10 02:52:37 -04:00
|
|
|
ordering = ("name",)
|
2017-11-19 18:59:03 -05:00
|
|
|
|
|
|
|
|
2017-10-26 17:52:26 -04:00
|
|
|
class EntryManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super(EntryManager, self).get_queryset()
|
2023-08-10 02:52:37 -04:00
|
|
|
return qs.select_related("author").prefetch_related("cats", "syndications")
|
2017-10-26 17:52:26 -04:00
|
|
|
|
|
|
|
|
2017-11-01 21:59:23 -04:00
|
|
|
class Entry(ModelMeta, TimeStampedModel):
|
2017-10-26 17:52:26 -04:00
|
|
|
objects = EntryManager()
|
2017-10-24 21:01:52 -04:00
|
|
|
kind = models.CharField(
|
2023-08-10 02:52:37 -04:00
|
|
|
max_length=30, choices=ENTRY_KINDS, db_index=True, default=ENTRY_KINDS[0][0]
|
2017-10-24 21:01:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
name = models.CharField(max_length=100, blank=True)
|
2017-10-27 01:04:05 -04:00
|
|
|
photo = models.ImageField(blank=True)
|
2017-10-24 21:01:52 -04:00
|
|
|
content = models.TextField()
|
|
|
|
|
2023-08-10 02:52:37 -04:00
|
|
|
cats = models.ManyToManyField(Cat, related_name="entries")
|
2017-11-19 18:59:03 -05:00
|
|
|
|
2017-11-16 05:52:42 -05:00
|
|
|
in_reply_to = models.CharField(max_length=255, blank=True)
|
|
|
|
like_of = models.CharField(max_length=255, blank=True)
|
|
|
|
repost_of = models.CharField(max_length=255, blank=True)
|
2017-11-12 16:42:31 -05:00
|
|
|
|
2017-10-26 17:52:26 -04:00
|
|
|
author = models.ForeignKey(
|
|
|
|
get_user_model(),
|
2023-08-10 02:52:37 -04:00
|
|
|
related_name="entries",
|
2017-10-26 17:52:26 -04:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2017-10-24 21:01:52 -04:00
|
|
|
|
2017-11-16 05:52:42 -05:00
|
|
|
@property
|
|
|
|
def reply_context(self):
|
|
|
|
if not self.in_reply_to:
|
|
|
|
return None
|
2023-08-10 02:52:37 -04:00
|
|
|
return interpret(requests.mf2(self.in_reply_to).to_dict(), self.in_reply_to)
|
2017-11-16 05:52:42 -05:00
|
|
|
|
2017-11-01 21:59:23 -04:00
|
|
|
@property
|
|
|
|
def published(self):
|
|
|
|
return self.created
|
|
|
|
|
|
|
|
@property
|
|
|
|
def updated(self):
|
|
|
|
return self.modified
|
2017-10-24 21:01:52 -04:00
|
|
|
|
2017-10-24 23:04:22 -04:00
|
|
|
_metadata = {
|
2023-08-10 02:52:37 -04:00
|
|
|
"description": "excerpt",
|
|
|
|
"image": "image_url",
|
|
|
|
"twitter_creator": "twitter_creator",
|
|
|
|
"og_profile_id": "og_profile_id",
|
2017-10-24 23:04:22 -04:00
|
|
|
}
|
|
|
|
|
2017-10-27 01:42:49 -04:00
|
|
|
@property
|
|
|
|
def title(self):
|
2017-10-27 04:41:25 -04:00
|
|
|
if self.name:
|
|
|
|
return self.name
|
2023-08-10 02:52:37 -04:00
|
|
|
return shorten(utils.to_plain(self.paragraphs[0]), width=100, placeholder="…")
|
2017-10-27 01:42:49 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def excerpt(self):
|
2017-10-27 04:41:25 -04:00
|
|
|
try:
|
2017-12-10 21:30:46 -05:00
|
|
|
return utils.to_plain(self.paragraphs[0 if self.name else 1])
|
2017-10-27 04:41:25 -04:00
|
|
|
except IndexError:
|
2023-08-10 02:52:37 -04:00
|
|
|
return " "
|
2017-10-27 01:42:49 -04:00
|
|
|
|
2017-10-27 04:07:28 -04:00
|
|
|
@property
|
2017-10-27 04:41:25 -04:00
|
|
|
def paragraphs(self):
|
|
|
|
lines = self.content.splitlines()
|
2023-08-10 02:52:37 -04:00
|
|
|
return ["\n".join(para) for k, para in groupby(lines, key=bool) if k]
|
2017-10-27 04:07:28 -04:00
|
|
|
|
2017-10-24 23:04:22 -04:00
|
|
|
@property
|
|
|
|
def twitter_creator(self):
|
|
|
|
return self.author.twitter_username
|
|
|
|
|
|
|
|
@property
|
|
|
|
def og_profile_id(self):
|
|
|
|
return self.author.facebook_id
|
|
|
|
|
2017-10-24 23:06:12 -04:00
|
|
|
@property
|
|
|
|
def image_url(self):
|
2017-10-27 01:04:05 -04:00
|
|
|
return self.photo.url if self.photo else self.author.avatar_url
|
2017-10-24 23:06:12 -04:00
|
|
|
|
2017-10-24 21:01:52 -04:00
|
|
|
def __str__(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
return "{0} {1}: {2}".format(self.kind, self.id, self.title)
|
2017-10-24 21:01:52 -04:00
|
|
|
|
2017-10-27 01:51:46 -04:00
|
|
|
def get_absolute_url(self):
|
2018-05-10 23:45:26 -04:00
|
|
|
return self.absolute_url
|
|
|
|
|
|
|
|
@property
|
|
|
|
def absolute_url(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
base = "https://" + DjangoSite.objects.get_current().domain
|
2018-03-21 07:56:33 -04:00
|
|
|
return urljoin(base, self.url)
|
2017-10-27 01:51:46 -04:00
|
|
|
|
2018-07-02 01:08:13 -04:00
|
|
|
@property
|
|
|
|
def affected_urls(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
base = "https://" + DjangoSite.objects.get_current().domain
|
2018-07-02 01:08:13 -04:00
|
|
|
kind = kinds.from_id[self.kind]
|
|
|
|
urls = {
|
|
|
|
self.url,
|
2023-08-10 02:52:37 -04:00
|
|
|
reverse("entries:index", kwargs={"kind": kind}),
|
|
|
|
reverse("entries:atom_by_kind", kwargs={"kind": kind}),
|
|
|
|
reverse("entries:rss_by_kind", kwargs={"kind": kind}),
|
2018-07-02 01:08:13 -04:00
|
|
|
} | {cat.url for cat in self.cats.all()}
|
|
|
|
if kind.on_home:
|
|
|
|
urls |= {
|
2023-08-10 02:52:37 -04:00
|
|
|
reverse("home:index"),
|
|
|
|
reverse("entries:atom"),
|
|
|
|
reverse("entries:rss"),
|
2018-07-02 01:08:13 -04:00
|
|
|
}
|
|
|
|
return {urljoin(base, u) for u in urls}
|
|
|
|
|
2017-10-24 21:31:08 -04:00
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
kind = kinds.from_id[self.kind]
|
2018-05-03 22:13:52 -04:00
|
|
|
args = [kind, self.id]
|
2017-10-27 01:51:46 -04:00
|
|
|
if kind.slug:
|
2017-10-24 21:31:08 -04:00
|
|
|
args.append(self.slug)
|
2023-08-10 02:52:37 -04:00
|
|
|
return reverse("entries:entry", args=args)
|
2017-10-24 21:31:08 -04:00
|
|
|
|
2018-03-21 06:50:40 -04:00
|
|
|
@property
|
|
|
|
def short_url(self):
|
|
|
|
return short_url(self)
|
|
|
|
|
2017-10-24 21:31:08 -04:00
|
|
|
@property
|
|
|
|
def slug(self):
|
|
|
|
return slugify(self.name)
|
|
|
|
|
2017-10-30 23:51:50 -04:00
|
|
|
@property
|
|
|
|
def json_ld(self):
|
2023-08-10 02:52:37 -04:00
|
|
|
base = "https://" + DjangoSite.objects.get_current().domain
|
2017-10-30 23:51:50 -04:00
|
|
|
url = urljoin(base, self.url)
|
|
|
|
|
|
|
|
posting = {
|
2023-08-10 02:52:37 -04:00
|
|
|
"@context": "http://schema.org",
|
|
|
|
"@type": "BlogPosting",
|
|
|
|
"@id": url,
|
|
|
|
"url": url,
|
|
|
|
"mainEntityOfPage": url,
|
|
|
|
"author": {
|
|
|
|
"@type": "Person",
|
|
|
|
"url": urljoin(base, self.author.url),
|
|
|
|
"name": self.author.name,
|
2017-10-30 23:51:50 -04:00
|
|
|
},
|
2023-08-10 02:52:37 -04:00
|
|
|
"headline": self.title,
|
|
|
|
"description": self.excerpt,
|
|
|
|
"datePublished": self.created.isoformat(),
|
|
|
|
"dateModified": self.modified.isoformat(),
|
2017-10-30 23:51:50 -04:00
|
|
|
}
|
|
|
|
if self.photo:
|
2023-08-10 02:52:37 -04:00
|
|
|
posting["image"] = (urljoin(base, self.photo.url),)
|
2017-10-30 23:51:50 -04:00
|
|
|
return posting
|
|
|
|
|
2017-10-24 21:01:52 -04:00
|
|
|
class Meta:
|
2023-08-10 02:52:37 -04:00
|
|
|
verbose_name_plural = "entries"
|
|
|
|
ordering = ["-created"]
|
2017-10-24 22:45:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Syndication(models.Model):
|
|
|
|
entry = models.ForeignKey(
|
2023-08-10 02:52:37 -04:00
|
|
|
Entry, related_name="syndications", on_delete=models.CASCADE
|
2017-10-24 22:45:33 -04:00
|
|
|
)
|
|
|
|
url = models.CharField(max_length=255)
|
|
|
|
|
2018-06-28 06:51:43 -04:00
|
|
|
domain = ComputedCharField(
|
2023-08-10 02:52:37 -04:00
|
|
|
compute_from="calc_domain",
|
|
|
|
max_length=255,
|
2018-06-28 06:51:43 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
def calc_domain(self):
|
|
|
|
domain = urlparse(self.url).netloc
|
2023-08-10 02:52:37 -04:00
|
|
|
if domain.startswith("www."):
|
2018-06-28 06:51:43 -04:00
|
|
|
domain = domain[4:]
|
|
|
|
return domain
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def site(self):
|
|
|
|
d = self.domain
|
|
|
|
try:
|
|
|
|
return Site.objects.get(domain=d)
|
|
|
|
except Site.DoesNotExist:
|
2023-08-10 02:52:37 -04:00
|
|
|
return Site(name=d, domain=d, icon="fas fa-newspaper")
|
2018-06-28 06:51:43 -04:00
|
|
|
|
2017-10-24 22:45:33 -04:00
|
|
|
class Meta:
|
2023-08-10 02:52:37 -04:00
|
|
|
ordering = ["domain"]
|