##// END OF EJS Templates
Unified getting post title or text part if there is no title
neko259 -
r1358:88eb479d default
parent child Browse files
Show More
@@ -8,6 +8,7 b' from django.core.exceptions import Objec'
8 from django.core.urlresolvers import reverse
8 from django.core.urlresolvers import reverse
9 from django.db import models, transaction
9 from django.db import models, transaction
10 from django.db.models import TextField, QuerySet
10 from django.db.models import TextField, QuerySet
11 from django.template.defaultfilters import striptags, truncatewords
11 from django.template.loader import render_to_string
12 from django.template.loader import render_to_string
12 from django.utils import timezone
13 from django.utils import timezone
13
14
@@ -21,6 +22,8 b' from boards.models.post.export import ge'
21 from boards.models.user import Notification, Ban
22 from boards.models.user import Notification, Ban
22 import boards.models.thread
23 import boards.models.thread
23
24
25 TITLE_MAX_WORDS = 10
26
24 APP_LABEL_BOARDS = 'boards'
27 APP_LABEL_BOARDS = 'boards'
25
28
26 POSTS_PER_DAY_RANGE = 7
29 POSTS_PER_DAY_RANGE = 7
@@ -165,6 +168,7 b' class PostManager(models.Manager):'
165
168
166 return ppd
169 return ppd
167
170
171
168 class Post(models.Model, Viewable):
172 class Post(models.Model, Viewable):
169 """A post is a message."""
173 """A post is a message."""
170
174
@@ -182,7 +186,7 b' class Post(models.Model, Viewable):'
182 images = models.ManyToManyField(PostImage, null=True, blank=True,
186 images = models.ManyToManyField(PostImage, null=True, blank=True,
183 related_name='post_images', db_index=True)
187 related_name='post_images', db_index=True)
184 attachments = models.ManyToManyField(Attachment, null=True, blank=True,
188 attachments = models.ManyToManyField(Attachment, null=True, blank=True,
185 related_name='attachment_posts')
189 related_name='attachment_posts')
186
190
187 poster_ip = models.GenericIPAddressField()
191 poster_ip = models.GenericIPAddressField()
188
192
@@ -205,21 +209,20 b' class Post(models.Model, Viewable):'
205 opening = models.BooleanField()
209 opening = models.BooleanField()
206
210
207 def __str__(self):
211 def __str__(self):
208 return 'P#{}/{}'.format(self.id, self.title)
212 return 'P#{}/{}'.format(self.id, self.get_title())
209
213
210 def get_referenced_posts(self):
214 def get_referenced_posts(self):
211 threads = self.get_threads().all()
215 threads = self.get_threads().all()
212 return self.referenced_posts.filter(threads__in=threads)\
216 return self.referenced_posts.filter(threads__in=threads)\
213 .order_by('pub_time').distinct().all()
217 .order_by('pub_time').distinct().all()
214
218
215 def get_title(self) -> str:
219 def get_title(self) -> str:
216 """
220 return self.title
217 Gets original post title or part of its text.
218 """
219
221
220 title = self.title
222 def get_title_or_text(self):
223 title = self.get_title()
221 if not title:
224 if not title:
222 title = self.get_text()
225 title = truncatewords(striptags(self.get_text()), TITLE_MAX_WORDS)
223
226
224 return title
227 return title
225
228
@@ -36,7 +36,7 b''
36 {% else %}
36 {% else %}
37 {% if need_op_data %}
37 {% if need_op_data %}
38 {% with thread.get_opening_post as op %}
38 {% with thread.get_opening_post as op %}
39 {% trans " in " %}{{ op.get_link_view|safe }} <span class="title">{{ op.get_title|striptags|truncatewords:5 }}</span>
39 {% trans " in " %}{{ op.get_link_view|safe }} <span class="title">{{ op.get_title_or_text }}</span>
40 {% endwith %}
40 {% endwith %}
41 {% endif %}
41 {% endif %}
42 {% endif %}
42 {% endif %}
@@ -6,8 +6,7 b''
6 {% load tz %}
6 {% load tz %}
7
7
8 {% block head %}
8 {% block head %}
9 <title>{{ opening_post.get_title|striptags|truncatewords:10 }}
9 <title>{{ opening_post.get_title_or_text }} - {{ site_name }}</title>
10 - {{ site_name }}</title>
11 {% endblock %}
10 {% endblock %}
12
11
13 {% block content %}
12 {% block content %}
@@ -18,7 +18,7 b''
18 <button name="method" value="subscribe" class="not_fav"></button>
18 <button name="method" value="subscribe" class="not_fav"></button>
19 {% endif %}
19 {% endif %}
20 </form>
20 </form>
21 {{ opening_post.get_title|striptags|truncatewords:10 }}
21 {{ opening_post.get_title_or_text }}
22 </h2>
22 </h2>
23 </div>
23 </div>
24
24
General Comments 0
You need to be logged in to leave comments. Login now