##// END OF EJS Templates
Tripcode collision preventer
neko259 -
r1297:dd1093e1 default
parent child Browse files
Show More
@@ -1,7 +1,12 b''
1 import uuid
1 import uuid
2 from django.shortcuts import get_object_or_404
2
3 import boards
4 from boards.abstracts.tripcode import Tripcode
5
3 from boards.models import Tag
6 from boards.models import Tag
4
7
8 MAX_TRIPCODE_COLLISIONS = 50
9
5 __author__ = 'neko259'
10 __author__ = 'neko259'
6
11
7 SESSION_SETTING = 'setting'
12 SESSION_SETTING = 'setting'
@@ -119,10 +124,25 b' class SettingsManager:'
119 self.set_setting(SETTING_HIDDEN_TAGS, tags)
124 self.set_setting(SETTING_HIDDEN_TAGS, tags)
120
125
121 def get_tripcode(self):
126 def get_tripcode(self):
122 return self.get_setting(SETTING_TRIPCODE, str(uuid.uuid4()))
127 tripcode = self.get_setting(SETTING_TRIPCODE)
128 if tripcode is None:
129 self.reset_tripcode()
130 tripcode = self.get_setting(SETTING_TRIPCODE)
131 return tripcode
123
132
124 def reset_tripcode(self):
133 def reset_tripcode(self):
125 self.set_setting(SETTING_TRIPCODE, str(uuid.uuid4()))
134 tripcode = Tripcode(str(uuid.uuid4()))
135
136 # If we cannot find a collision-free tripcode, then let the collision
137 # be destiny
138 collision_counter = 0
139 while boards.models.Post.objects.filter(
140 tripcode__startswith=tripcode.get_short_text()).exists()\
141 and collision_counter < MAX_TRIPCODE_COLLISIONS:
142 tripcode = Tripcode(str(uuid.uuid4()))
143 collision_counter += 1
144 print('Tripcode collision detected') # FIXME Use proper logging
145 self.set_setting(SETTING_TRIPCODE, tripcode.get_full_text())
126
146
127
147
128 class SessionSettingsManager(SettingsManager):
148 class SessionSettingsManager(SettingsManager):
@@ -21,7 +21,10 b' class Tripcode:'
21 def get_short_text(self):
21 def get_short_text(self):
22 return self.tripcode[:8]
22 return self.tripcode[:8]
23
23
24 def get_full_text(self):
25 return self.tripcode
26
24 def get_view(self):
27 def get_view(self):
25 return '<span style="color: #{}; background: #{}">{}</span>'\
28 return '<span title="{}" style="color: #{}; background: #{}">{}</span>'\
26 .format(self.get_color(), self.get_background(),
29 .format(self.get_full_text(), self.get_color(), self.get_background(),
27 self.get_short_text()) No newline at end of file
30 self.get_short_text())
General Comments 0
You need to be logged in to leave comments. Login now