Show More
@@ -1,128 +1,128 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 django.db import models, transaction |
|
6 | from django.db import models, transaction | |
7 | from django.utils import timezone |
|
7 | from django.utils import timezone | |
8 |
|
8 | |||
9 | import boards |
|
9 | import boards | |
10 |
|
10 | |||
11 | from boards.models.user import Ban |
|
11 | from boards.models.user import Ban | |
12 | from boards.mdx_neboard import Parser |
|
12 | from boards.mdx_neboard import Parser | |
13 | from boards.models import PostImage, Attachment |
|
13 | from boards.models import PostImage, Attachment | |
14 | from boards import utils |
|
14 | from boards import utils | |
15 |
|
15 | |||
16 | __author__ = 'neko259' |
|
16 | __author__ = 'neko259' | |
17 |
|
17 | |||
18 | IMAGE_TYPES = ( |
|
18 | IMAGE_TYPES = ( | |
19 | 'jpeg', |
|
19 | 'jpeg', | |
20 | 'jpg', |
|
20 | 'jpg', | |
21 | 'png', |
|
21 | 'png', | |
22 | 'bmp', |
|
22 | 'bmp', | |
23 | 'gif', |
|
23 | 'gif', | |
24 | ) |
|
24 | ) | |
25 |
|
25 | |||
26 | POSTS_PER_DAY_RANGE = 7 |
|
26 | POSTS_PER_DAY_RANGE = 7 | |
27 | NO_IP = '0.0.0.0' |
|
27 | NO_IP = '0.0.0.0' | |
28 |
|
28 | |||
29 |
|
29 | |||
30 | class PostManager(models.Manager): |
|
30 | class PostManager(models.Manager): | |
31 | @transaction.atomic |
|
31 | @transaction.atomic | |
32 | def create_post(self, title: str, text: str, file=None, thread=None, |
|
32 | def create_post(self, title: str, text: str, file=None, thread=None, | |
33 | ip=NO_IP, tags: list=None, opening_posts: list=None, |
|
33 | ip=NO_IP, tags: list=None, opening_posts: list=None, | |
34 | tripcode=''): |
|
34 | tripcode=''): | |
35 | """ |
|
35 | """ | |
36 | Creates new post |
|
36 | Creates new post | |
37 | """ |
|
37 | """ | |
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 |
|
41 | |||
42 | # TODO Raise specific exception and catch it in the views |
|
42 | # TODO Raise specific exception and catch it in the views | |
43 | if is_banned: |
|
43 | if is_banned: | |
44 | raise Exception("This user is banned") |
|
44 | raise Exception("This user is banned") | |
45 |
|
45 | |||
46 | if not tags: |
|
46 | if not tags: | |
47 | tags = [] |
|
47 | tags = [] | |
48 | if not opening_posts: |
|
48 | if not opening_posts: | |
49 | opening_posts = [] |
|
49 | opening_posts = [] | |
50 |
|
50 | |||
51 | posting_time = timezone.now() |
|
51 | posting_time = timezone.now() | |
52 | new_thread = False |
|
52 | new_thread = False | |
53 | if not thread: |
|
53 | if not thread: | |
54 | thread = boards.models.thread.Thread.objects.create( |
|
54 | thread = boards.models.thread.Thread.objects.create( | |
55 | bump_time=posting_time, last_edit_time=posting_time) |
|
55 | bump_time=posting_time, last_edit_time=posting_time) | |
56 | list(map(thread.tags.add, tags)) |
|
56 | list(map(thread.tags.add, tags)) | |
57 | boards.models.thread.Thread.objects.process_oldest_threads() |
|
57 | boards.models.thread.Thread.objects.process_oldest_threads() | |
58 | new_thread = True |
|
58 | new_thread = True | |
59 |
|
59 | |||
60 | pre_text = Parser().preparse(text) |
|
60 | pre_text = Parser().preparse(text) | |
61 |
|
61 | |||
62 | post = self.create(title=title, |
|
62 | post = self.create(title=title, | |
63 | text=pre_text, |
|
63 | text=pre_text, | |
64 | pub_time=posting_time, |
|
64 | pub_time=posting_time, | |
65 | poster_ip=ip, |
|
65 | poster_ip=ip, | |
66 | thread=thread, |
|
66 | thread=thread, | |
67 | last_edit_time=posting_time, |
|
67 | last_edit_time=posting_time, | |
68 | tripcode=tripcode, |
|
68 | tripcode=tripcode, | |
69 | opening=new_thread) |
|
69 | opening=new_thread) | |
70 | post.threads.add(thread) |
|
70 | post.threads.add(thread) | |
71 |
|
71 | |||
72 | logger = logging.getLogger('boards.post.create') |
|
72 | logger = logging.getLogger('boards.post.create') | |
73 |
|
73 | |||
74 | logger.info('Created post {} with text {} by {}'.format(post, |
|
74 | logger.info('Created post [{}] with text [{}] by {}'.format(post, | |
75 | post.get_text(),post.poster_ip)) |
|
75 | post.get_text(),post.poster_ip)) | |
76 |
|
76 | |||
77 | # TODO Move this to other place |
|
77 | # TODO Move this to other place | |
78 | if file: |
|
78 | if file: | |
79 | file_type = file.name.split('.')[-1].lower() |
|
79 | file_type = file.name.split('.')[-1].lower() | |
80 | if file_type in IMAGE_TYPES: |
|
80 | if file_type in IMAGE_TYPES: | |
81 | post.images.add(PostImage.objects.create_with_hash(file)) |
|
81 | post.images.add(PostImage.objects.create_with_hash(file)) | |
82 | else: |
|
82 | else: | |
83 | post.attachments.add(Attachment.objects.create_with_hash(file)) |
|
83 | post.attachments.add(Attachment.objects.create_with_hash(file)) | |
84 |
|
84 | |||
85 | post.build_url() |
|
85 | post.build_url() | |
86 | post.connect_replies() |
|
86 | post.connect_replies() | |
87 | post.connect_threads(opening_posts) |
|
87 | post.connect_threads(opening_posts) | |
88 | post.connect_notifications() |
|
88 | post.connect_notifications() | |
89 |
|
89 | |||
90 | # Thread needs to be bumped only when the post is already created |
|
90 | # Thread needs to be bumped only when the post is already created | |
91 | if not new_thread: |
|
91 | if not new_thread: | |
92 | thread.last_edit_time = posting_time |
|
92 | thread.last_edit_time = posting_time | |
93 | thread.bump() |
|
93 | thread.bump() | |
94 | thread.save() |
|
94 | thread.save() | |
95 |
|
95 | |||
96 | return post |
|
96 | return post | |
97 |
|
97 | |||
98 | def delete_posts_by_ip(self, ip): |
|
98 | def delete_posts_by_ip(self, ip): | |
99 | """ |
|
99 | """ | |
100 | Deletes all posts of the author with same IP |
|
100 | Deletes all posts of the author with same IP | |
101 | """ |
|
101 | """ | |
102 |
|
102 | |||
103 | posts = self.filter(poster_ip=ip) |
|
103 | posts = self.filter(poster_ip=ip) | |
104 | for post in posts: |
|
104 | for post in posts: | |
105 | post.delete() |
|
105 | post.delete() | |
106 |
|
106 | |||
107 | @utils.cached_result() |
|
107 | @utils.cached_result() | |
108 | def get_posts_per_day(self) -> float: |
|
108 | def get_posts_per_day(self) -> float: | |
109 | """ |
|
109 | """ | |
110 | Gets average count of posts per day for the last 7 days |
|
110 | Gets average count of posts per day for the last 7 days | |
111 | """ |
|
111 | """ | |
112 |
|
112 | |||
113 | day_end = date.today() |
|
113 | day_end = date.today() | |
114 | day_start = day_end - timedelta(POSTS_PER_DAY_RANGE) |
|
114 | day_start = day_end - timedelta(POSTS_PER_DAY_RANGE) | |
115 |
|
115 | |||
116 | day_time_start = timezone.make_aware(datetime.combine( |
|
116 | day_time_start = timezone.make_aware(datetime.combine( | |
117 | day_start, dtime()), timezone.get_current_timezone()) |
|
117 | day_start, dtime()), timezone.get_current_timezone()) | |
118 | day_time_end = timezone.make_aware(datetime.combine( |
|
118 | day_time_end = timezone.make_aware(datetime.combine( | |
119 | day_end, dtime()), timezone.get_current_timezone()) |
|
119 | day_end, dtime()), timezone.get_current_timezone()) | |
120 |
|
120 | |||
121 | posts_per_period = float(self.filter( |
|
121 | posts_per_period = float(self.filter( | |
122 | pub_time__lte=day_time_end, |
|
122 | pub_time__lte=day_time_end, | |
123 | pub_time__gte=day_time_start).count()) |
|
123 | pub_time__gte=day_time_start).count()) | |
124 |
|
124 | |||
125 | ppd = posts_per_period / POSTS_PER_DAY_RANGE |
|
125 | ppd = posts_per_period / POSTS_PER_DAY_RANGE | |
126 |
|
126 | |||
127 | return ppd |
|
127 | return ppd | |
128 |
|
128 |
General Comments 0
You need to be logged in to leave comments.
Login now