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