-
Notifications
You must be signed in to change notification settings - Fork 42
/
article_models.py
68 lines (55 loc) · 2.39 KB
/
article_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Core Django imports.
from django.contrib.auth.models import User
from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.utils.text import slugify
# Third party app imports
from taggit.managers import TaggableManager
from ckeditor_uploader.fields import RichTextUploadingField
# Blog application imports.
from blog.utils.blog_utils import count_words, read_time
from blog.models.category_models import Category
class Article(models.Model):
# Article status constants
DRAFTED = "DRAFTED"
PUBLISHED = "PUBLISHED"
# CHOICES
STATUS_CHOICES = (
(DRAFTED, 'Draft'),
(PUBLISHED, 'Publish'),
)
# BLOG MODEL FIELDS
category = models.ForeignKey(Category, on_delete=models.CASCADE,
related_name='articles')
title = models.CharField(max_length=250, null=False, blank=False)
slug = models.SlugField()
author = models.ForeignKey(User, on_delete=models.CASCADE,
related_name='articles')
image = models.ImageField(default='article-default.jpg',
upload_to='article_pics')
image_credit = models.CharField(max_length=250, null=True, blank=True)
body = RichTextUploadingField(blank=True)
tags = TaggableManager(blank=True)
date_published = models.DateTimeField(null=True, blank=True,
default=timezone.now)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES,
default='DRAFT')
views = models.PositiveIntegerField(default=0)
count_words = models.CharField(max_length=50, default=0)
read_time = models.CharField(max_length=50, default=0)
deleted = models.BooleanField(default=False)
class Meta:
unique_together = ("title",)
ordering = ('-date_published',)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
self.slug = slugify(self.title, allow_unicode=True)
self.count_words = count_words(self.body)
self.read_time = read_time(self.body)
super(Article, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('blog:article_detail', kwargs={'username': self.author.username.lower(), 'slug': self.slug})