Show More
@@ -1,181 +1,180 b'' | |||||
1 | import logging |
|
1 | import logging | |
2 |
|
2 | |||
3 | from datetime import datetime, timedelta, date |
|
3 | from datetime import datetime, timedelta, date | |
4 | from datetime import time as dtime |
|
4 | from datetime import time as dtime | |
5 |
|
5 | |||
6 | from boards.abstracts.exceptions import BannedException, ArchiveException |
|
6 | from boards.abstracts.exceptions import BannedException, ArchiveException | |
7 | from django.db import models, transaction |
|
7 | from django.db import models, transaction | |
8 | from django.utils import timezone |
|
8 | from django.utils import timezone | |
9 | from django.dispatch import Signal |
|
9 | from django.dispatch import Signal | |
10 |
|
10 | |||
11 | import boards |
|
11 | import boards | |
12 |
|
12 | |||
13 | from boards.models.user import Ban |
|
13 | from boards.models.user import Ban | |
14 | from boards.mdx_neboard import Parser |
|
14 | from boards.mdx_neboard import Parser | |
15 | from boards.models import Attachment |
|
15 | from boards.models import Attachment | |
16 | from boards import utils |
|
16 | from boards import utils | |
17 |
|
17 | |||
18 | __author__ = 'neko259' |
|
18 | __author__ = 'neko259' | |
19 |
|
19 | |||
20 | POSTS_PER_DAY_RANGE = 7 |
|
20 | POSTS_PER_DAY_RANGE = 7 | |
21 | NO_IP = '0.0.0.0' |
|
21 | NO_IP = '0.0.0.0' | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | post_import_deps = Signal() |
|
24 | post_import_deps = Signal() | |
25 |
|
25 | |||
26 |
|
26 | |||
27 | class PostManager(models.Manager): |
|
27 | class PostManager(models.Manager): | |
28 | @transaction.atomic |
|
28 | @transaction.atomic | |
29 | def create_post(self, title: str, text: str, file=None, thread=None, |
|
29 | def create_post(self, title: str, text: str, file=None, thread=None, | |
30 | ip=NO_IP, tags: list=None, opening_posts: list=None, |
|
30 | ip=NO_IP, tags: list=None, opening_posts: list=None, | |
31 | tripcode='', monochrome=False, images=[]): |
|
31 | tripcode='', monochrome=False, images=[]): | |
32 | """ |
|
32 | """ | |
33 | Creates new post |
|
33 | Creates new post | |
34 | """ |
|
34 | """ | |
35 |
|
35 | |||
36 | if thread is not None and thread.is_archived(): |
|
36 | if thread is not None and thread.is_archived(): | |
37 | raise ArchiveException('Cannot post into an archived thread') |
|
37 | raise ArchiveException('Cannot post into an archived thread') | |
38 |
|
38 | |||
39 | if not utils.is_anonymous_mode(): |
|
39 | if not utils.is_anonymous_mode(): | |
40 | is_banned = Ban.objects.filter(ip=ip).exists() |
|
40 | is_banned = Ban.objects.filter(ip=ip).exists() | |
41 | else: |
|
41 | else: | |
42 | is_banned = False |
|
42 | is_banned = False | |
43 |
|
43 | |||
44 | if is_banned: |
|
44 | if is_banned: | |
45 | raise BannedException("This user is banned") |
|
45 | raise BannedException("This user is banned") | |
46 |
|
46 | |||
47 | if not tags: |
|
47 | if not tags: | |
48 | tags = [] |
|
48 | tags = [] | |
49 | if not opening_posts: |
|
49 | if not opening_posts: | |
50 | opening_posts = [] |
|
50 | opening_posts = [] | |
51 |
|
51 | |||
52 | posting_time = timezone.now() |
|
52 | posting_time = timezone.now() | |
53 | new_thread = False |
|
53 | new_thread = False | |
54 | if not thread: |
|
54 | if not thread: | |
55 | thread = boards.models.thread.Thread.objects.create( |
|
55 | thread = boards.models.thread.Thread.objects.create( | |
56 | bump_time=posting_time, last_edit_time=posting_time, |
|
56 | bump_time=posting_time, last_edit_time=posting_time, | |
57 | monochrome=monochrome) |
|
57 | monochrome=monochrome) | |
58 | list(map(thread.tags.add, tags)) |
|
58 | list(map(thread.tags.add, tags)) | |
59 | boards.models.thread.Thread.objects.process_oldest_threads() |
|
59 | boards.models.thread.Thread.objects.process_oldest_threads() | |
60 | new_thread = True |
|
60 | new_thread = True | |
61 |
|
61 | |||
62 | pre_text = Parser().preparse(text) |
|
62 | pre_text = Parser().preparse(text) | |
63 |
|
63 | |||
64 | post = self.create(title=title, |
|
64 | post = self.create(title=title, | |
65 | text=pre_text, |
|
65 | text=pre_text, | |
66 | pub_time=posting_time, |
|
66 | pub_time=posting_time, | |
67 | poster_ip=ip, |
|
67 | poster_ip=ip, | |
68 | thread=thread, |
|
68 | thread=thread, | |
69 | last_edit_time=posting_time, |
|
69 | last_edit_time=posting_time, | |
70 | tripcode=tripcode, |
|
70 | tripcode=tripcode, | |
71 | opening=new_thread) |
|
71 | opening=new_thread) | |
72 | post.threads.add(thread) |
|
72 | post.threads.add(thread) | |
73 |
|
73 | |||
74 | logger = logging.getLogger('boards.post.create') |
|
74 | logger = logging.getLogger('boards.post.create') | |
75 |
|
75 | |||
76 | logger.info('Created post [{}] with text [{}] by {}'.format(post, |
|
76 | logger.info('Created post [{}] with text [{}] by {}'.format(post, | |
77 | post.get_text(),post.poster_ip)) |
|
77 | post.get_text(),post.poster_ip)) | |
78 |
|
78 | |||
79 | if file: |
|
79 | if file: | |
80 | self._add_file_to_post(file, post) |
|
80 | self._add_file_to_post(file, post) | |
81 | for image in images: |
|
81 | for image in images: | |
82 |
post. |
|
82 | post.attachments.add(image) | |
83 |
|
83 | |||
84 | post.connect_threads(opening_posts) |
|
84 | post.connect_threads(opening_posts) | |
85 | post.set_global_id() |
|
85 | post.set_global_id() | |
86 |
|
86 | |||
87 | # Thread needs to be bumped only when the post is already created |
|
87 | # Thread needs to be bumped only when the post is already created | |
88 | if not new_thread: |
|
88 | if not new_thread: | |
89 | thread.last_edit_time = posting_time |
|
89 | thread.last_edit_time = posting_time | |
90 | thread.bump() |
|
90 | thread.bump() | |
91 | thread.save() |
|
91 | thread.save() | |
92 |
|
92 | |||
93 | return post |
|
93 | return post | |
94 |
|
94 | |||
95 | def delete_posts_by_ip(self, ip): |
|
95 | def delete_posts_by_ip(self, ip): | |
96 | """ |
|
96 | """ | |
97 | Deletes all posts of the author with same IP |
|
97 | Deletes all posts of the author with same IP | |
98 | """ |
|
98 | """ | |
99 |
|
99 | |||
100 | posts = self.filter(poster_ip=ip) |
|
100 | posts = self.filter(poster_ip=ip) | |
101 | for post in posts: |
|
101 | for post in posts: | |
102 | post.delete() |
|
102 | post.delete() | |
103 |
|
103 | |||
104 | @utils.cached_result() |
|
104 | @utils.cached_result() | |
105 | def get_posts_per_day(self) -> float: |
|
105 | def get_posts_per_day(self) -> float: | |
106 | """ |
|
106 | """ | |
107 | Gets average count of posts per day for the last 7 days |
|
107 | Gets average count of posts per day for the last 7 days | |
108 | """ |
|
108 | """ | |
109 |
|
109 | |||
110 | day_end = date.today() |
|
110 | day_end = date.today() | |
111 | day_start = day_end - timedelta(POSTS_PER_DAY_RANGE) |
|
111 | day_start = day_end - timedelta(POSTS_PER_DAY_RANGE) | |
112 |
|
112 | |||
113 | day_time_start = timezone.make_aware(datetime.combine( |
|
113 | day_time_start = timezone.make_aware(datetime.combine( | |
114 | day_start, dtime()), timezone.get_current_timezone()) |
|
114 | day_start, dtime()), timezone.get_current_timezone()) | |
115 | day_time_end = timezone.make_aware(datetime.combine( |
|
115 | day_time_end = timezone.make_aware(datetime.combine( | |
116 | day_end, dtime()), timezone.get_current_timezone()) |
|
116 | day_end, dtime()), timezone.get_current_timezone()) | |
117 |
|
117 | |||
118 | posts_per_period = float(self.filter( |
|
118 | posts_per_period = float(self.filter( | |
119 | pub_time__lte=day_time_end, |
|
119 | pub_time__lte=day_time_end, | |
120 | pub_time__gte=day_time_start).count()) |
|
120 | pub_time__gte=day_time_start).count()) | |
121 |
|
121 | |||
122 | ppd = posts_per_period / POSTS_PER_DAY_RANGE |
|
122 | ppd = posts_per_period / POSTS_PER_DAY_RANGE | |
123 |
|
123 | |||
124 | return ppd |
|
124 | return ppd | |
125 |
|
125 | |||
126 | @transaction.atomic |
|
126 | @transaction.atomic | |
127 | def import_post(self, title: str, text: str, pub_time: str, global_id, |
|
127 | def import_post(self, title: str, text: str, pub_time: str, global_id, | |
128 | opening_post=None, tags=list(), files=list(), |
|
128 | opening_post=None, tags=list(), files=list(), | |
129 | tripcode=None, version=1): |
|
129 | tripcode=None, version=1): | |
130 | is_opening = opening_post is None |
|
130 | is_opening = opening_post is None | |
131 | if is_opening: |
|
131 | if is_opening: | |
132 | thread = boards.models.thread.Thread.objects.create( |
|
132 | thread = boards.models.thread.Thread.objects.create( | |
133 | bump_time=pub_time, last_edit_time=pub_time) |
|
133 | bump_time=pub_time, last_edit_time=pub_time) | |
134 | list(map(thread.tags.add, tags)) |
|
134 | list(map(thread.tags.add, tags)) | |
135 | else: |
|
135 | else: | |
136 | thread = opening_post.get_thread() |
|
136 | thread = opening_post.get_thread() | |
137 |
|
137 | |||
138 | post = self.create(title=title, |
|
138 | post = self.create(title=title, | |
139 | text=text, |
|
139 | text=text, | |
140 | pub_time=pub_time, |
|
140 | pub_time=pub_time, | |
141 | poster_ip=NO_IP, |
|
141 | poster_ip=NO_IP, | |
142 | last_edit_time=pub_time, |
|
142 | last_edit_time=pub_time, | |
143 | global_id=global_id, |
|
143 | global_id=global_id, | |
144 | opening=is_opening, |
|
144 | opening=is_opening, | |
145 | thread=thread, |
|
145 | thread=thread, | |
146 | tripcode=tripcode, |
|
146 | tripcode=tripcode, | |
147 | version=version) |
|
147 | version=version) | |
148 |
|
148 | |||
149 | for file in files: |
|
149 | for file in files: | |
150 | self._add_file_to_post(file, post) |
|
150 | self._add_file_to_post(file, post) | |
151 |
|
151 | |||
152 | post.threads.add(thread) |
|
152 | post.threads.add(thread) | |
153 |
|
153 | |||
154 | url_to_post = '[post]{}[/post]'.format(str(global_id)) |
|
154 | url_to_post = '[post]{}[/post]'.format(str(global_id)) | |
155 | replies = self.filter(text__contains=url_to_post) |
|
155 | replies = self.filter(text__contains=url_to_post) | |
156 | for reply in replies: |
|
156 | for reply in replies: | |
157 | post_import_deps.send(reply.__class__) |
|
157 | post_import_deps.send(reply.__class__) | |
158 |
|
158 | |||
159 | @transaction.atomic |
|
159 | @transaction.atomic | |
160 | def update_post(self, post, title: str, text: str, pub_time: str, |
|
160 | def update_post(self, post, title: str, text: str, pub_time: str, | |
161 | tags=list(), files=list(), tripcode=None, version=1): |
|
161 | tags=list(), files=list(), tripcode=None, version=1): | |
162 | post.title = title |
|
162 | post.title = title | |
163 | post.text = text |
|
163 | post.text = text | |
164 | post.pub_time = pub_time |
|
164 | post.pub_time = pub_time | |
165 | post.tripcode = tripcode |
|
165 | post.tripcode = tripcode | |
166 | post.version = version |
|
166 | post.version = version | |
167 | post.save() |
|
167 | post.save() | |
168 |
|
168 | |||
169 | post.clear_cache() |
|
169 | post.clear_cache() | |
170 |
|
170 | |||
171 | post.images.clear() |
|
|||
172 | post.attachments.clear() |
|
171 | post.attachments.clear() | |
173 | for file in files: |
|
172 | for file in files: | |
174 | self._add_file_to_post(file, post) |
|
173 | self._add_file_to_post(file, post) | |
175 |
|
174 | |||
176 | thread = post.get_thread() |
|
175 | thread = post.get_thread() | |
177 | thread.tags.clear() |
|
176 | thread.tags.clear() | |
178 | list(map(thread.tags.add, tags)) |
|
177 | list(map(thread.tags.add, tags)) | |
179 |
|
178 | |||
180 | def _add_file_to_post(self, file, post): |
|
179 | def _add_file_to_post(self, file, post): | |
181 | post.attachments.add(Attachment.objects.create_with_hash(file)) |
|
180 | post.attachments.add(Attachment.objects.create_with_hash(file)) |
General Comments 0
You need to be logged in to leave comments.
Login now