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