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