2017-10-24 21:01:52 -04:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.db import models
|
2017-10-24 21:31:08 -04:00
|
|
|
from django.urls import reverse
|
|
|
|
from slugify import slugify
|
2017-10-27 01:42:49 -04:00
|
|
|
from textwrap import shorten
|
2017-10-24 21:01:52 -04:00
|
|
|
|
2017-10-24 23:04:22 -04:00
|
|
|
from meta.models import ModelMeta
|
2017-10-24 22:45:33 -04:00
|
|
|
from users.models import Profile
|
2017-10-24 23:04:22 -04:00
|
|
|
|
2017-10-24 21:01:52 -04:00
|
|
|
from . import kinds
|
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-10-26 17:52:26 -04:00
|
|
|
class EntryManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super(EntryManager, self).get_queryset()
|
|
|
|
return qs.select_related('author').prefetch_related('syndications')
|
|
|
|
|
|
|
|
|
2017-10-24 23:04:22 -04:00
|
|
|
class Entry(ModelMeta, models.Model):
|
2017-10-26 17:52:26 -04:00
|
|
|
objects = EntryManager()
|
2017-10-24 21:01:52 -04:00
|
|
|
kind = models.CharField(
|
|
|
|
max_length=30,
|
|
|
|
choices=ENTRY_KINDS,
|
2017-10-26 17:52:26 -04:00
|
|
|
db_index=True,
|
2017-10-24 21:01:52 -04:00
|
|
|
default=ENTRY_KINDS[0][0]
|
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2017-10-26 17:52:26 -04:00
|
|
|
author = models.ForeignKey(
|
|
|
|
get_user_model(),
|
|
|
|
related_name='entries',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2017-10-24 21:01:52 -04:00
|
|
|
|
|
|
|
published = models.DateTimeField()
|
|
|
|
updated = models.DateTimeField()
|
|
|
|
|
2017-10-24 23:04:22 -04:00
|
|
|
_metadata = {
|
2017-10-27 01:42:49 -04:00
|
|
|
'description': 'excerpt',
|
2017-10-24 23:06:12 -04:00
|
|
|
'image': 'image_url',
|
2017-10-24 23:04:22 -04:00
|
|
|
'twitter_creator': 'twitter_creator',
|
|
|
|
'og_profile_id': 'og_profile_id',
|
|
|
|
}
|
|
|
|
|
2017-10-27 01:42:49 -04:00
|
|
|
@property
|
|
|
|
def title(self):
|
|
|
|
return self.name if self.name else self.excerpt
|
|
|
|
|
|
|
|
@property
|
|
|
|
def excerpt(self):
|
|
|
|
first_line = self.content.split('\n')[0]
|
|
|
|
return shorten(first_line, width=100, placeholder='…')
|
|
|
|
|
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):
|
2017-10-27 01:42:49 -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):
|
|
|
|
return self.url
|
|
|
|
|
2017-10-24 21:31:08 -04:00
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
kind = kinds.from_id[self.kind]
|
2017-10-27 01:51:46 -04:00
|
|
|
route = kind.entry
|
2017-10-24 21:31:08 -04:00
|
|
|
args = [self.id]
|
2017-10-27 01:51:46 -04:00
|
|
|
if kind.slug:
|
|
|
|
route = kind.entry_slug
|
2017-10-24 21:31:08 -04:00
|
|
|
args.append(self.slug)
|
2017-10-27 01:51:46 -04:00
|
|
|
return reverse('entries:' + route, args=args)
|
2017-10-24 21:31:08 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def slug(self):
|
|
|
|
return slugify(self.name)
|
|
|
|
|
2017-10-24 21:01:52 -04:00
|
|
|
class Meta:
|
|
|
|
verbose_name_plural = 'entries'
|
|
|
|
ordering = ['-published']
|
2017-10-24 22:45:33 -04:00
|
|
|
|
|
|
|
|
2017-10-26 17:52:26 -04:00
|
|
|
class SyndicationManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super(SyndicationManager, self).get_queryset()
|
|
|
|
return qs.select_related('profile__site')
|
|
|
|
|
|
|
|
|
2017-10-24 22:45:33 -04:00
|
|
|
class Syndication(models.Model):
|
2017-10-26 17:52:26 -04:00
|
|
|
objects = SyndicationManager()
|
2017-10-24 22:45:33 -04:00
|
|
|
entry = models.ForeignKey(
|
|
|
|
Entry,
|
|
|
|
related_name='syndications',
|
|
|
|
on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
|
|
|
|
url = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['profile']
|