Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 153 lines (118 sloc) 4.813 kb
dd35852 @peterbe initial commit
authored
1 import uuid
2 import datetime
3 from django.db import models
62c0d0e @peterbe related search works
authored
4 from django.core.cache import cache
d208cb1 @peterbe akismet and celery stuff
authored
5 from django.db.models.signals import post_save
6 from django.dispatch import receiver
4d11c8b @peterbe adding feed and upgrading bootstrap css
authored
7 from .utils import render_comment_text, stx_to_html
dd35852 @peterbe initial commit
authored
8
9
10 class ArrayField(models.CharField):
11
12 __metaclass__ = models.SubfieldBase
13 description = "basic field for storing string arrays"
14
15 def __init__(self, *args, **kwargs):
16 kwargs['max_length'] = kwargs.get('max_length', 200)
17 super(ArrayField, self).__init__(*args, **kwargs)
18
19 def to_python(self, value):
20 if isinstance(value, list):
21 return value
22
23 return value.split('|')
24
25 def get_prep_value(self, value):
26 return '|'.join(value)
27
28
29 class Category(models.Model):
30 name = models.CharField(max_length=100)
31
32 def __repr__(self):
33 return '<%s: %r>' % (self.__class__.__name__, self.name)
34
35
36 class BlogItem(models.Model):
15d474e @peterbe adding basic search
authored
37 """
38 Indexes executed for this:
39
40 CREATE INDEX plog_blogitem_text_eng_idx ON plog_blogitem
41 USING gin(to_tsvector('english', text));
42
43 CREATE INDEX plog_blogitem_title_eng_idx ON plog_blogitem
44 USING gin(to_tsvector('english', title));
45
46 REINDEX TABLE plog_blogitem;
47
48
49 """
dd35852 @peterbe initial commit
authored
50 oid = models.CharField(max_length=100, db_index=True)
51 title = models.CharField(max_length=200)
52 alias = models.CharField(max_length=200, null=True)
53 bookmark = models.BooleanField(default=False)
54 text = models.TextField()
55 text_rendered = models.TextField(blank=True)
56 summary = models.TextField()
57 url = models.URLField(null=True)
58 pub_date = models.DateTimeField()
59 display_format = models.CharField(max_length=20)
60 categories = models.ManyToManyField(Category)
61 keywords = ArrayField(max_length=500)
62 plogrank = models.FloatField(null=True)
63 codesyntax = models.CharField(max_length=20, blank=True)
64
65 def __repr__(self):
66 return '<%s: %r>' % (self.__class__.__name__, self.oid)
67
4d11c8b @peterbe adding feed and upgrading bootstrap css
authored
68 @models.permalink
69 def get_absolute_url(self):
70 return ('blog_post', [self.oid])
71
72 @property
73 def rendered(self):
74 if not self.text_rendered:
75 if self.display_format == 'structuredtext':
76 self.text_rendered = stx_to_html(self.text, self.codesyntax)
77 else:
78 raise NotImplementedError(self.display_format)
79 self.save()
80 return self.text_rendered
81
62c0d0e @peterbe related search works
authored
82 def count_comments(self):
83 cache_key = 'nocomments:%s' % self.pk
84 count = cache.get(cache_key)
85 if count is None:
86 count = self._count_comments()
87 cache.set(cache_key, count, 60 * 60 * 24)
88 return count
89
90 def _count_comments(self):
91 return BlogComment.objects.filter(blogitem=self).count()
92
dd35852 @peterbe initial commit
authored
93
94 class BlogComment(models.Model):
15d474e @peterbe adding basic search
authored
95 """
96 Indexes executed for this:
97
98 CREATE INDEX plog_blogcomment_comment_eng_idx ON plog_blogcomment
99 USING gin(to_tsvector('english', comment));
100
101 REINDEX TABLE plog_blogcomment;
102
103 """
104
dd35852 @peterbe initial commit
authored
105 oid = models.CharField(max_length=100, db_index=True)
106 blogitem = models.ForeignKey(BlogItem, null=True)
107 parent = models.ForeignKey('BlogComment', null=True)
108 approved = models.BooleanField(default=False)
109 comment = models.TextField()
110 comment_rendered = models.TextField(blank=True, null=True)
111 add_date = models.DateTimeField(default=datetime.datetime.utcnow)
112 name = models.CharField(max_length=100, blank=True)
113 email = models.CharField(max_length=100, blank=True)
d208cb1 @peterbe akismet and celery stuff
authored
114 user_agent = models.CharField(max_length=300, blank=True, null=True)
115 ip_address = models.IPAddressField(blank=True, null=True)
116 akismet_pass = models.NullBooleanField(null=True)
dd35852 @peterbe initial commit
authored
117
118 @property
119 def rendered(self):
120 if not self.comment_rendered:
121 self.comment_rendered = render_comment_text(self.comment)
122 self.save()
123 return self.comment_rendered
124
125 @classmethod
126 def next_oid(cls):
127 return 'c' + uuid.uuid4().hex[:6]
15d474e @peterbe adding basic search
authored
128
129 def get_absolute_url(self):
130 return self.blogitem.get_absolute_url() + '#%s' % self.oid
131
132 def correct_blogitem_parent(self):
133 assert self.blogitem is None
134 if self.parent.blogitem is None:
135 self.parent.correct_blogitem_parent()
136 self.blogitem = self.parent.blogitem
137 self.save()
62c0d0e @peterbe related search works
authored
138
139
140 @receiver(post_save, sender=BlogComment)
141 @receiver(post_save, sender=BlogItem)
142 def invalidate_blogitem_comment_count(sender, instance, **kwargs):
143 if sender is BlogItem:
144 pk = instance.pk
145 elif sender is BlogComment:
146 if instance.blogitem is None:
147 instance.correct_blogitem_parent() # legacy
148 pk = instance.blogitem.pk
149 else:
150 raise NotImplementedError(sender)
151 cache_key = 'nocomments:%s' % pk
152 cache.delete(cache_key)
Something went wrong with that request. Please try again.