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-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
|
|
|
|
ENTRY_KINDS = [(k.id, k.__name__) for k in kinds.all]
|
|
|
|
|
|
|
|
|
2017-10-24 23:04:22 -04:00
|
|
|
class Entry(ModelMeta, models.Model):
|
2017-10-24 21:01:52 -04:00
|
|
|
kind = models.CharField(
|
|
|
|
max_length=30,
|
|
|
|
choices=ENTRY_KINDS,
|
|
|
|
default=ENTRY_KINDS[0][0]
|
|
|
|
)
|
|
|
|
|
|
|
|
name = models.CharField(max_length=100, blank=True)
|
|
|
|
content = models.TextField()
|
|
|
|
|
|
|
|
author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
published = models.DateTimeField()
|
|
|
|
updated = models.DateTimeField()
|
|
|
|
|
2017-10-24 23:04:22 -04:00
|
|
|
_metadata = {
|
|
|
|
'description': 'content',
|
|
|
|
'twitter_creator': 'twitter_creator',
|
|
|
|
'og_profile_id': 'og_profile_id',
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def twitter_creator(self):
|
|
|
|
return self.author.twitter_username
|
|
|
|
|
|
|
|
@property
|
|
|
|
def og_profile_id(self):
|
|
|
|
return self.author.facebook_id
|
|
|
|
|
2017-10-24 21:01:52 -04:00
|
|
|
def __str__(self):
|
|
|
|
return '{kind} {id}: {content}'.format(
|
|
|
|
kind=self.kind,
|
|
|
|
id=self.id,
|
|
|
|
content=self.content
|
|
|
|
)
|
|
|
|
|
2017-10-24 21:31:08 -04:00
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
kind = kinds.from_id[self.kind]
|
|
|
|
route = 'entries:{kind}_entry'.format(kind=kind.plural)
|
|
|
|
args = [self.id]
|
|
|
|
if kind.has('slug'):
|
|
|
|
route += '_slug'
|
|
|
|
args.append(self.slug)
|
|
|
|
return reverse(route, args=args)
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
|
|
|
class Syndication(models.Model):
|
|
|
|
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']
|