Show More
@@ -1,7 +1,12 b'' | |||
|
1 | 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 | 6 | from boards.models import Tag |
|
4 | 7 | |
|
8 | MAX_TRIPCODE_COLLISIONS = 50 | |
|
9 | ||
|
5 | 10 | __author__ = 'neko259' |
|
6 | 11 | |
|
7 | 12 | SESSION_SETTING = 'setting' |
@@ -119,10 +124,25 b' class SettingsManager:' | |||
|
119 | 124 | self.set_setting(SETTING_HIDDEN_TAGS, tags) |
|
120 | 125 | |
|
121 | 126 | def get_tripcode(self): |
|
122 |
|
|
|
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 | 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 | 148 | class SessionSettingsManager(SettingsManager): |
@@ -21,7 +21,10 b' class Tripcode:' | |||
|
21 | 21 | def get_short_text(self): |
|
22 | 22 | return self.tripcode[:8] |
|
23 | 23 | |
|
24 | def get_full_text(self): | |
|
25 | return self.tripcode | |
|
26 | ||
|
24 | 27 | def get_view(self): |
|
25 | return '<span style="color: #{}; background: #{}">{}</span>'\ | |
|
26 | .format(self.get_color(), self.get_background(), | |
|
27 | self.get_short_text()) No newline at end of file | |
|
28 | return '<span title="{}" style="color: #{}; background: #{}">{}</span>'\ | |
|
29 | .format(self.get_full_text(), self.get_color(), self.get_background(), | |
|
30 | self.get_short_text()) |
General Comments 0
You need to be logged in to leave comments.
Login now