-
Notifications
You must be signed in to change notification settings - Fork 731
/
entry.py
578 lines (479 loc) · 16.2 KB
/
entry.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
"""Base entry models for Zinnia"""
import os
from django.contrib.sites.models import Site
from django.db import models
from django.db.models import Q
from django.template.defaultfilters import slugify
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible
from django.utils.html import strip_tags
from django.utils.text import Truncator
from django.utils.translation import ugettext_lazy as _
import django_comments as comments
from django_comments.models import CommentFlag
from tagging.fields import TagField
from tagging.utils import parse_tag_input
from zinnia.flags import PINGBACK
from zinnia.flags import TRACKBACK
from zinnia.managers import DRAFT, HIDDEN, PUBLISHED
from zinnia.managers import EntryPublishedManager
from zinnia.managers import entries_published
from zinnia.markups import html_format
from zinnia.preview import HTMLPreview
from zinnia.settings import AUTO_CLOSE_COMMENTS_AFTER
from zinnia.settings import AUTO_CLOSE_PINGBACKS_AFTER
from zinnia.settings import AUTO_CLOSE_TRACKBACKS_AFTER
from zinnia.settings import ENTRY_CONTENT_TEMPLATES
from zinnia.settings import ENTRY_DETAIL_TEMPLATES
from zinnia.settings import UPLOAD_TO
from zinnia.url_shortener import get_url_shortener
@python_2_unicode_compatible
class CoreEntry(models.Model):
"""
Abstract core entry model class providing
the fields and methods required for publishing
content over time.
"""
STATUS_CHOICES = ((DRAFT, _('draft')),
(HIDDEN, _('hidden')),
(PUBLISHED, _('published')))
title = models.CharField(
_('title'), max_length=255)
slug = models.SlugField(
_('slug'), max_length=255,
unique_for_date='publication_date',
help_text=_("Used to build the entry's URL."))
status = models.IntegerField(
_('status'), db_index=True,
choices=STATUS_CHOICES, default=DRAFT)
publication_date = models.DateTimeField(
_('publication date'),
db_index=True, default=timezone.now,
help_text=_("Used to build the entry's URL."))
start_publication = models.DateTimeField(
_('start publication'),
db_index=True, blank=True, null=True,
help_text=_('Start date of publication.'))
end_publication = models.DateTimeField(
_('end publication'),
db_index=True, blank=True, null=True,
help_text=_('End date of publication.'))
sites = models.ManyToManyField(
Site,
related_name='entries',
verbose_name=_('sites'),
help_text=_('Sites where the entry will be published.'))
creation_date = models.DateTimeField(
_('creation date'),
default=timezone.now)
last_update = models.DateTimeField(
_('last update'), default=timezone.now)
objects = models.Manager()
published = EntryPublishedManager()
@property
def is_actual(self):
"""
Checks if an entry is within his publication period.
"""
now = timezone.now()
if self.start_publication and now < self.start_publication:
return False
if self.end_publication and now >= self.end_publication:
return False
return True
@property
def is_visible(self):
"""
Checks if an entry is visible and published.
"""
return self.is_actual and self.status == PUBLISHED
@property
def previous_entry(self):
"""
Returns the previous published entry if exists.
"""
return self.previous_next_entries[0]
@property
def next_entry(self):
"""
Returns the next published entry if exists.
"""
return self.previous_next_entries[1]
@property
def previous_next_entries(self):
"""
Returns and caches a tuple containing the next
and previous published entries.
Only available if the entry instance is published.
"""
previous_next = getattr(self, 'previous_next', None)
if previous_next is None:
if not self.is_visible:
previous_next = (None, None)
setattr(self, 'previous_next', previous_next)
return previous_next
entries = list(self.__class__.published.all())
index = entries.index(self)
try:
previous = entries[index + 1]
except IndexError:
previous = None
if index:
_next = entries[index - 1]
else:
_next = None
previous_next = (previous, _next)
setattr(self, 'previous_next', previous_next)
return previous_next
@property
def short_url(self):
"""
Returns the entry's short url.
"""
return get_url_shortener()(self)
def save(self, *args, **kwargs):
"""
Overrides the save method to update the
the last_update field.
"""
self.last_update = timezone.now()
super(CoreEntry, self).save(*args, **kwargs)
@models.permalink
def get_absolute_url(self):
"""
Builds and returns the entry's URL based on
the slug and the creation date.
"""
publication_date = self.publication_date
if timezone.is_aware(publication_date):
publication_date = timezone.localtime(publication_date)
return ('zinnia:entry_detail', (), {
'year': publication_date.strftime('%Y'),
'month': publication_date.strftime('%m'),
'day': publication_date.strftime('%d'),
'slug': self.slug})
def __str__(self):
return '%s: %s' % (self.title, self.get_status_display())
class Meta:
"""
CoreEntry's meta informations.
"""
abstract = True
ordering = ['-publication_date']
get_latest_by = 'publication_date'
verbose_name = _('entry')
verbose_name_plural = _('entries')
index_together = [['slug', 'publication_date'],
['status', 'publication_date',
'start_publication', 'end_publication']]
permissions = (('can_view_all', 'Can view all entries'),
('can_change_status', 'Can change status'),
('can_change_author', 'Can change author(s)'), )
class ContentEntry(models.Model):
"""
Abstract content model class providing field
and methods to write content inside an entry.
"""
content = models.TextField(_('content'), blank=True)
@property
def html_content(self):
"""
Returns the "content" field formatted in HTML.
"""
return html_format(self.content)
@property
def html_preview(self):
"""
Returns a preview of the "content" field or
the "lead" field if defined, formatted in HTML.
"""
return HTMLPreview(self.html_content,
getattr(self, 'html_lead', ''))
@property
def word_count(self):
"""
Counts the number of words used in the content.
"""
return len(strip_tags(self.html_content).split())
class Meta:
abstract = True
class DiscussionsEntry(models.Model):
"""
Abstract discussion model class providing
the fields and methods to manage the discussions
(comments, pingbacks, trackbacks).
"""
comment_enabled = models.BooleanField(
_('comments enabled'), default=True,
help_text=_('Allows comments if checked.'))
pingback_enabled = models.BooleanField(
_('pingbacks enabled'), default=True,
help_text=_('Allows pingbacks if checked.'))
trackback_enabled = models.BooleanField(
_('trackbacks enabled'), default=True,
help_text=_('Allows trackbacks if checked.'))
comment_count = models.IntegerField(
_('comment count'), default=0)
pingback_count = models.IntegerField(
_('pingback count'), default=0)
trackback_count = models.IntegerField(
_('trackback count'), default=0)
@property
def discussions(self):
"""
Returns a queryset of the published discussions.
"""
return comments.get_model().objects.for_model(
self).filter(is_public=True, is_removed=False)
@property
def comments(self):
"""
Returns a queryset of the published comments.
"""
return self.discussions.filter(Q(flags=None) | Q(
flags__flag=CommentFlag.MODERATOR_APPROVAL))
@property
def pingbacks(self):
"""
Returns a queryset of the published pingbacks.
"""
return self.discussions.filter(flags__flag=PINGBACK)
@property
def trackbacks(self):
"""
Return a queryset of the published trackbacks.
"""
return self.discussions.filter(flags__flag=TRACKBACK)
def discussion_is_still_open(self, discussion_type, auto_close_after):
"""
Checks if a type of discussion is still open
are a certain number of days.
"""
discussion_enabled = getattr(self, discussion_type)
if (discussion_enabled and isinstance(auto_close_after, int) and
auto_close_after >= 0):
return (timezone.now() - (
self.start_publication or self.publication_date)).days < \
auto_close_after
return discussion_enabled
@property
def comments_are_open(self):
"""
Checks if the comments are open with the
AUTO_CLOSE_COMMENTS_AFTER setting.
"""
return self.discussion_is_still_open(
'comment_enabled', AUTO_CLOSE_COMMENTS_AFTER)
@property
def pingbacks_are_open(self):
"""
Checks if the pingbacks are open with the
AUTO_CLOSE_PINGBACKS_AFTER setting.
"""
return self.discussion_is_still_open(
'pingback_enabled', AUTO_CLOSE_PINGBACKS_AFTER)
@property
def trackbacks_are_open(self):
"""
Checks if the trackbacks are open with the
AUTO_CLOSE_TRACKBACKS_AFTER setting.
"""
return self.discussion_is_still_open(
'trackback_enabled', AUTO_CLOSE_TRACKBACKS_AFTER)
class Meta:
abstract = True
class RelatedEntry(models.Model):
"""
Abstract model class for making manual relations
between the differents entries.
"""
related = models.ManyToManyField(
'self',
blank=True,
verbose_name=_('related entries'))
@property
def related_published(self):
"""
Returns only related entries published.
"""
return entries_published(self.related)
class Meta:
abstract = True
class LeadEntry(models.Model):
"""
Abstract model class providing a lead content to the entries.
"""
lead = models.TextField(
_('lead'), blank=True,
help_text=_('Lead paragraph'))
@property
def html_lead(self):
"""
Returns the "lead" field formatted in HTML.
"""
return html_format(self.lead)
class Meta:
abstract = True
class ExcerptEntry(models.Model):
"""
Abstract model class to add an excerpt to the entries.
"""
excerpt = models.TextField(
_('excerpt'), blank=True,
help_text=_('Used for SEO purposes.'))
def save(self, *args, **kwargs):
"""
Overrides the save method to create an excerpt
from the content field if void.
"""
if not self.excerpt and self.status == PUBLISHED:
self.excerpt = Truncator(strip_tags(
getattr(self, 'content', ''))).words(50)
super(ExcerptEntry, self).save(*args, **kwargs)
class Meta:
abstract = True
def image_upload_to_dispatcher(entry, filename):
"""
Dispatch function to allow overriding of ``image_upload_to`` method.
Outside the model for fixing an issue with Django's migrations on Python 2.
"""
return entry.image_upload_to(filename)
class ImageEntry(models.Model):
"""
Abstract model class to add an image for illustrating the entries.
"""
def image_upload_to(self, filename):
"""
Compute the upload path for the image field.
"""
now = timezone.now()
filename, extension = os.path.splitext(filename)
return os.path.join(
UPLOAD_TO,
now.strftime('%Y'),
now.strftime('%m'),
now.strftime('%d'),
'%s%s' % (slugify(filename), extension))
image = models.ImageField(
_('image'), blank=True,
upload_to=image_upload_to_dispatcher,
help_text=_('Used for illustration.'))
image_caption = models.TextField(
_('caption'), blank=True,
help_text=_("Image's caption."))
class Meta:
abstract = True
class FeaturedEntry(models.Model):
"""
Abstract model class to mark entries as featured.
"""
featured = models.BooleanField(
_('featured'), default=False)
class Meta:
abstract = True
class AuthorsEntry(models.Model):
"""
Abstract model class to add relationship
between the entries and their authors.
"""
authors = models.ManyToManyField(
'zinnia.Author',
blank=True,
related_name='entries',
verbose_name=_('authors'))
class Meta:
abstract = True
class CategoriesEntry(models.Model):
"""
Abstract model class to categorize the entries.
"""
categories = models.ManyToManyField(
'zinnia.Category',
blank=True,
related_name='entries',
verbose_name=_('categories'))
class Meta:
abstract = True
class TagsEntry(models.Model):
"""
Abstract model class to add tags to the entries.
"""
tags = TagField(_('tags'))
@property
def tags_list(self):
"""
Return iterable list of tags.
"""
return parse_tag_input(self.tags)
class Meta:
abstract = True
class LoginRequiredEntry(models.Model):
"""
Abstract model class to restrcit the display
of the entry on authenticated users.
"""
login_required = models.BooleanField(
_('login required'), default=False,
help_text=_('Only authenticated users can view the entry.'))
class Meta:
abstract = True
class PasswordRequiredEntry(models.Model):
"""
Abstract model class to restrict the display
of the entry to users knowing the password.
"""
password = models.CharField(
_('password'), max_length=50, blank=True,
help_text=_('Protects the entry with a password.'))
class Meta:
abstract = True
class ContentTemplateEntry(models.Model):
"""
Abstract model class to display entry's content
with a custom template.
"""
content_template = models.CharField(
_('content template'), max_length=250,
default='zinnia/_entry_detail.html',
choices=[('zinnia/_entry_detail.html', _('Default template'))] +
ENTRY_CONTENT_TEMPLATES,
help_text=_("Template used to display the entry's content."))
class Meta:
abstract = True
class DetailTemplateEntry(models.Model):
"""
Abstract model class to display entries with a
custom template if needed on the detail page.
"""
detail_template = models.CharField(
_('detail template'), max_length=250,
default='entry_detail.html',
choices=[('entry_detail.html', _('Default template'))] +
ENTRY_DETAIL_TEMPLATES,
help_text=_("Template used to display the entry's detail page."))
class Meta:
abstract = True
class AbstractEntry(
CoreEntry,
ContentEntry,
DiscussionsEntry,
RelatedEntry,
LeadEntry,
ExcerptEntry,
ImageEntry,
FeaturedEntry,
AuthorsEntry,
CategoriesEntry,
TagsEntry,
LoginRequiredEntry,
PasswordRequiredEntry,
ContentTemplateEntry,
DetailTemplateEntry):
"""
Final abstract entry model class assembling
all the abstract entry model classes into a single one.
In this manner we can override some fields without
reimplemting all the AbstractEntry.
"""
class Meta(CoreEntry.Meta):
abstract = True