##// END OF EJS Templates
Added type hints to some models and managers
neko259 -
r1083:ad1beed4 default
parent child Browse files
Show More
@@ -124,7 +124,7 b' class PostManager(models.Manager):'
124 post.delete()
124 post.delete()
125
125
126 @cached_result
126 @cached_result
127 def get_posts_per_day(self):
127 def get_posts_per_day(self) -> float:
128 """
128 """
129 Gets average count of posts per day for the last 7 days
129 Gets average count of posts per day for the last 7 days
130 """
130 """
@@ -200,6 +200,7 b' class Post(models.Model, Viewable):'
200
200
201 self.refmap = ', '.join(post_urls)
201 self.refmap = ', '.join(post_urls)
202
202
203 # TODO Is this still needed?
203 def get_sorted_referenced_posts(self):
204 def get_sorted_referenced_posts(self):
204 return self.refmap
205 return self.refmap
205
206
@@ -232,18 +233,18 b' class Post(models.Model, Viewable):'
232 def get_thread(self):
233 def get_thread(self):
233 return self.thread
234 return self.thread
234
235
235 def get_threads(self):
236 def get_threads(self) -> list:
236 """
237 """
237 Gets post's thread.
238 Gets post's thread.
238 """
239 """
239
240
240 return self.threads
241 return self.threads
241
242
242 def get_referenced_posts(self):
243 def get_referenced_posts(self) -> list:
243 return self.referenced_posts.only('id', 'threads')
244 return self.referenced_posts.only('id', 'threads')
244
245
245 def get_view(self, moderator=False, need_open_link=False,
246 def get_view(self, moderator=False, need_open_link=False,
246 truncated=False, *args, **kwargs):
247 truncated=False, *args, **kwargs) -> str:
247 """
248 """
248 Renders post's HTML view. Some of the post params can be passed over
249 Renders post's HTML view. Some of the post params can be passed over
249 kwargs for the means of caching (if we view the thread, some params
250 kwargs for the means of caching (if we view the thread, some params
@@ -296,7 +297,7 b' class Post(models.Model, Viewable):'
296 'Deleted post {}'.format(self))
297 'Deleted post {}'.format(self))
297
298
298 def get_post_data(self, format_type=DIFF_TYPE_JSON, request=None,
299 def get_post_data(self, format_type=DIFF_TYPE_JSON, request=None,
299 include_last_update=False):
300 include_last_update=False) -> str:
300 """
301 """
301 Gets post HTML or JSON data that can be rendered on a page or used by
302 Gets post HTML or JSON data that can be rendered on a page or used by
302 API.
303 API.
@@ -8,6 +8,7 b' from boards import settings'
8 import boards
8 import boards
9 from boards.utils import cached_result
9 from boards.utils import cached_result
10 from boards.models.post import Post
10 from boards.models.post import Post
11 from boards.models.tag import Tag
11
12
12
13
13 __author__ = 'neko259'
14 __author__ = 'neko259'
@@ -59,7 +60,7 b' class Thread(models.Model):'
59 bumpable = models.BooleanField(default=True)
60 bumpable = models.BooleanField(default=True)
60 max_posts = models.IntegerField(default=settings.MAX_POSTS_PER_THREAD)
61 max_posts = models.IntegerField(default=settings.MAX_POSTS_PER_THREAD)
61
62
62 def get_tags(self):
63 def get_tags(self) -> list:
63 """
64 """
64 Gets a sorted tag list.
65 Gets a sorted tag list.
65 """
66 """
@@ -78,7 +79,7 b' class Thread(models.Model):'
78
79
79 logger.info('Bumped thread %d' % self.id)
80 logger.info('Bumped thread %d' % self.id)
80
81
81 def has_post_limit(self):
82 def has_post_limit(self) -> bool:
82 return self.max_posts > 0
83 return self.max_posts > 0
83
84
84 def update_bump_status(self):
85 def update_bump_status(self):
@@ -86,21 +87,21 b' class Thread(models.Model):'
86 self.bumpable = False
87 self.bumpable = False
87 self.update_posts_time()
88 self.update_posts_time()
88
89
89 def get_reply_count(self):
90 def get_reply_count(self) -> int:
90 return self.get_replies().count()
91 return self.get_replies().count()
91
92
92 def get_images_count(self):
93 def get_images_count(self) -> int:
93 return self.get_replies().annotate(images_count=Count(
94 return self.get_replies().annotate(images_count=Count(
94 'images')).aggregate(Sum('images_count'))['images_count__sum']
95 'images')).aggregate(Sum('images_count'))['images_count__sum']
95
96
96 def can_bump(self):
97 def can_bump(self) -> bool:
97 """
98 """
98 Checks if the thread can be bumped by replying to it.
99 Checks if the thread can be bumped by replying to it.
99 """
100 """
100
101
101 return self.bumpable and not self.archived
102 return self.bumpable and not self.archived
102
103
103 def get_last_replies(self):
104 def get_last_replies(self) -> list:
104 """
105 """
105 Gets several last replies, not including opening post
106 Gets several last replies, not including opening post
106 """
107 """
@@ -116,7 +117,7 b' class Thread(models.Model):'
116
117
117 return last_replies
118 return last_replies
118
119
119 def get_skipped_replies_count(self):
120 def get_skipped_replies_count(self) -> int:
120 """
121 """
121 Gets number of posts between opening post and last replies.
122 Gets number of posts between opening post and last replies.
122 """
123 """
@@ -125,7 +126,7 b' class Thread(models.Model):'
125 reply_count - 1)
126 reply_count - 1)
126 return reply_count - last_replies_count - 1
127 return reply_count - last_replies_count - 1
127
128
128 def get_replies(self, view_fields_only=False):
129 def get_replies(self, view_fields_only=False) -> list:
129 """
130 """
130 Gets sorted thread posts
131 Gets sorted thread posts
131 """
132 """
@@ -136,7 +137,7 b' class Thread(models.Model):'
136 query = query.defer('poster_ip')
137 query = query.defer('poster_ip')
137 return query.all()
138 return query.all()
138
139
139 def get_replies_with_images(self, view_fields_only=False):
140 def get_replies_with_images(self, view_fields_only=False) -> list:
140 """
141 """
141 Gets replies that have at least one image attached
142 Gets replies that have at least one image attached
142 """
143 """
@@ -144,14 +145,14 b' class Thread(models.Model):'
144 return self.get_replies(view_fields_only).annotate(images_count=Count(
145 return self.get_replies(view_fields_only).annotate(images_count=Count(
145 'images')).filter(images_count__gt=0)
146 'images')).filter(images_count__gt=0)
146
147
147 def add_tag(self, tag):
148 def add_tag(self, tag: Tag):
148 """
149 """
149 Connects thread to a tag and tag to a thread
150 Connects thread to a tag and tag to a thread
150 """
151 """
151
152
152 self.tags.add(tag)
153 self.tags.add(tag)
153
154
154 def get_opening_post(self, only_id=False):
155 def get_opening_post(self, only_id=False) -> Post:
155 """
156 """
156 Gets the first post of the thread
157 Gets the first post of the thread
157 """
158 """
@@ -164,7 +165,7 b' class Thread(models.Model):'
164 return opening_post
165 return opening_post
165
166
166 @cached_result
167 @cached_result
167 def get_opening_post_id(self):
168 def get_opening_post_id(self) -> int:
168 """
169 """
169 Gets ID of the first thread post.
170 Gets ID of the first thread post.
170 """
171 """
@@ -191,7 +192,7 b' class Thread(models.Model):'
191 def __str__(self):
192 def __str__(self):
192 return 'T#{}/{}'.format(self.id, self.get_opening_post_id())
193 return 'T#{}/{}'.format(self.id, self.get_opening_post_id())
193
194
194 def get_tag_url_list(self):
195 def get_tag_url_list(self) -> list:
195 return boards.models.Tag.objects.get_tag_url_list(self.get_tags())
196 return boards.models.Tag.objects.get_tag_url_list(self.get_tags())
196
197
197 def update_posts_time(self):
198 def update_posts_time(self):
General Comments 0
You need to be logged in to leave comments. Login now