Show More
@@ -1,154 +1,174 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' | |
8 |
|
13 | |||
9 | # Remove this, it is not used any more cause there is a user's permission |
|
14 | # Remove this, it is not used any more cause there is a user's permission | |
10 | PERMISSION_MODERATE = 'moderator' |
|
15 | PERMISSION_MODERATE = 'moderator' | |
11 |
|
16 | |||
12 | SETTING_THEME = 'theme' |
|
17 | SETTING_THEME = 'theme' | |
13 | SETTING_FAVORITE_TAGS = 'favorite_tags' |
|
18 | SETTING_FAVORITE_TAGS = 'favorite_tags' | |
14 | SETTING_HIDDEN_TAGS = 'hidden_tags' |
|
19 | SETTING_HIDDEN_TAGS = 'hidden_tags' | |
15 | SETTING_PERMISSIONS = 'permissions' |
|
20 | SETTING_PERMISSIONS = 'permissions' | |
16 | SETTING_USERNAME = 'username' |
|
21 | SETTING_USERNAME = 'username' | |
17 | SETTING_LAST_NOTIFICATION_ID = 'last_notification' |
|
22 | SETTING_LAST_NOTIFICATION_ID = 'last_notification' | |
18 | SETTING_IMAGE_VIEWER = 'image_viewer' |
|
23 | SETTING_IMAGE_VIEWER = 'image_viewer' | |
19 | SETTING_TRIPCODE = 'tripcode' |
|
24 | SETTING_TRIPCODE = 'tripcode' | |
20 |
|
25 | |||
21 | DEFAULT_THEME = 'md' |
|
26 | DEFAULT_THEME = 'md' | |
22 |
|
27 | |||
23 |
|
28 | |||
24 | class SettingsManager: |
|
29 | class SettingsManager: | |
25 | """ |
|
30 | """ | |
26 | Base settings manager class. get_setting and set_setting methods should |
|
31 | Base settings manager class. get_setting and set_setting methods should | |
27 | be overriden. |
|
32 | be overriden. | |
28 | """ |
|
33 | """ | |
29 | def __init__(self): |
|
34 | def __init__(self): | |
30 | pass |
|
35 | pass | |
31 |
|
36 | |||
32 | def get_theme(self) -> str: |
|
37 | def get_theme(self) -> str: | |
33 | theme = self.get_setting(SETTING_THEME) |
|
38 | theme = self.get_setting(SETTING_THEME) | |
34 | if not theme: |
|
39 | if not theme: | |
35 | theme = DEFAULT_THEME |
|
40 | theme = DEFAULT_THEME | |
36 | self.set_setting(SETTING_THEME, theme) |
|
41 | self.set_setting(SETTING_THEME, theme) | |
37 |
|
42 | |||
38 | return theme |
|
43 | return theme | |
39 |
|
44 | |||
40 | def set_theme(self, theme): |
|
45 | def set_theme(self, theme): | |
41 | self.set_setting(SETTING_THEME, theme) |
|
46 | self.set_setting(SETTING_THEME, theme) | |
42 |
|
47 | |||
43 | def has_permission(self, permission): |
|
48 | def has_permission(self, permission): | |
44 | permissions = self.get_setting(SETTING_PERMISSIONS) |
|
49 | permissions = self.get_setting(SETTING_PERMISSIONS) | |
45 | if permissions: |
|
50 | if permissions: | |
46 | return permission in permissions |
|
51 | return permission in permissions | |
47 | else: |
|
52 | else: | |
48 | return False |
|
53 | return False | |
49 |
|
54 | |||
50 | def get_setting(self, setting, default=None): |
|
55 | def get_setting(self, setting, default=None): | |
51 | pass |
|
56 | pass | |
52 |
|
57 | |||
53 | def set_setting(self, setting, value): |
|
58 | def set_setting(self, setting, value): | |
54 | pass |
|
59 | pass | |
55 |
|
60 | |||
56 | def add_permission(self, permission): |
|
61 | def add_permission(self, permission): | |
57 | permissions = self.get_setting(SETTING_PERMISSIONS) |
|
62 | permissions = self.get_setting(SETTING_PERMISSIONS) | |
58 | if not permissions: |
|
63 | if not permissions: | |
59 | permissions = [permission] |
|
64 | permissions = [permission] | |
60 | else: |
|
65 | else: | |
61 | permissions.append(permission) |
|
66 | permissions.append(permission) | |
62 | self.set_setting(SETTING_PERMISSIONS, permissions) |
|
67 | self.set_setting(SETTING_PERMISSIONS, permissions) | |
63 |
|
68 | |||
64 | def del_permission(self, permission): |
|
69 | def del_permission(self, permission): | |
65 | permissions = self.get_setting(SETTING_PERMISSIONS) |
|
70 | permissions = self.get_setting(SETTING_PERMISSIONS) | |
66 | if not permissions: |
|
71 | if not permissions: | |
67 | permissions = [] |
|
72 | permissions = [] | |
68 | else: |
|
73 | else: | |
69 | permissions.remove(permission) |
|
74 | permissions.remove(permission) | |
70 | self.set_setting(SETTING_PERMISSIONS, permissions) |
|
75 | self.set_setting(SETTING_PERMISSIONS, permissions) | |
71 |
|
76 | |||
72 | def get_fav_tags(self) -> list: |
|
77 | def get_fav_tags(self) -> list: | |
73 | tag_names = self.get_setting(SETTING_FAVORITE_TAGS) |
|
78 | tag_names = self.get_setting(SETTING_FAVORITE_TAGS) | |
74 | tags = [] |
|
79 | tags = [] | |
75 | if tag_names: |
|
80 | if tag_names: | |
76 | tags = list(Tag.objects.filter(name__in=tag_names)) |
|
81 | tags = list(Tag.objects.filter(name__in=tag_names)) | |
77 | return tags |
|
82 | return tags | |
78 |
|
83 | |||
79 | def add_fav_tag(self, tag): |
|
84 | def add_fav_tag(self, tag): | |
80 | tags = self.get_setting(SETTING_FAVORITE_TAGS) |
|
85 | tags = self.get_setting(SETTING_FAVORITE_TAGS) | |
81 | if not tags: |
|
86 | if not tags: | |
82 | tags = [tag.name] |
|
87 | tags = [tag.name] | |
83 | else: |
|
88 | else: | |
84 | if not tag.name in tags: |
|
89 | if not tag.name in tags: | |
85 | tags.append(tag.name) |
|
90 | tags.append(tag.name) | |
86 |
|
91 | |||
87 | tags.sort() |
|
92 | tags.sort() | |
88 | self.set_setting(SETTING_FAVORITE_TAGS, tags) |
|
93 | self.set_setting(SETTING_FAVORITE_TAGS, tags) | |
89 |
|
94 | |||
90 | def del_fav_tag(self, tag): |
|
95 | def del_fav_tag(self, tag): | |
91 | tags = self.get_setting(SETTING_FAVORITE_TAGS) |
|
96 | tags = self.get_setting(SETTING_FAVORITE_TAGS) | |
92 | if tag.name in tags: |
|
97 | if tag.name in tags: | |
93 | tags.remove(tag.name) |
|
98 | tags.remove(tag.name) | |
94 | self.set_setting(SETTING_FAVORITE_TAGS, tags) |
|
99 | self.set_setting(SETTING_FAVORITE_TAGS, tags) | |
95 |
|
100 | |||
96 | def get_hidden_tags(self) -> list: |
|
101 | def get_hidden_tags(self) -> list: | |
97 | tag_names = self.get_setting(SETTING_HIDDEN_TAGS) |
|
102 | tag_names = self.get_setting(SETTING_HIDDEN_TAGS) | |
98 | tags = [] |
|
103 | tags = [] | |
99 | if tag_names: |
|
104 | if tag_names: | |
100 | tags = list(Tag.objects.filter(name__in=tag_names)) |
|
105 | tags = list(Tag.objects.filter(name__in=tag_names)) | |
101 |
|
106 | |||
102 | return tags |
|
107 | return tags | |
103 |
|
108 | |||
104 | def add_hidden_tag(self, tag): |
|
109 | def add_hidden_tag(self, tag): | |
105 | tags = self.get_setting(SETTING_HIDDEN_TAGS) |
|
110 | tags = self.get_setting(SETTING_HIDDEN_TAGS) | |
106 | if not tags: |
|
111 | if not tags: | |
107 | tags = [tag.name] |
|
112 | tags = [tag.name] | |
108 | else: |
|
113 | else: | |
109 | if not tag.name in tags: |
|
114 | if not tag.name in tags: | |
110 | tags.append(tag.name) |
|
115 | tags.append(tag.name) | |
111 |
|
116 | |||
112 | tags.sort() |
|
117 | tags.sort() | |
113 | self.set_setting(SETTING_HIDDEN_TAGS, tags) |
|
118 | self.set_setting(SETTING_HIDDEN_TAGS, tags) | |
114 |
|
119 | |||
115 | def del_hidden_tag(self, tag): |
|
120 | def del_hidden_tag(self, tag): | |
116 | tags = self.get_setting(SETTING_HIDDEN_TAGS) |
|
121 | tags = self.get_setting(SETTING_HIDDEN_TAGS) | |
117 | if tag.name in tags: |
|
122 | if tag.name in tags: | |
118 | tags.remove(tag.name) |
|
123 | tags.remove(tag.name) | |
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 |
|
|
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): | |
129 | """ |
|
149 | """ | |
130 | Session-based settings manager. All settings are saved to the user's |
|
150 | Session-based settings manager. All settings are saved to the user's | |
131 | session. |
|
151 | session. | |
132 | """ |
|
152 | """ | |
133 | def __init__(self, session): |
|
153 | def __init__(self, session): | |
134 | SettingsManager.__init__(self) |
|
154 | SettingsManager.__init__(self) | |
135 | self.session = session |
|
155 | self.session = session | |
136 |
|
156 | |||
137 | def get_setting(self, setting, default=None): |
|
157 | def get_setting(self, setting, default=None): | |
138 | if setting in self.session: |
|
158 | if setting in self.session: | |
139 | return self.session[setting] |
|
159 | return self.session[setting] | |
140 | else: |
|
160 | else: | |
141 | self.set_setting(setting, default) |
|
161 | self.set_setting(setting, default) | |
142 | return default |
|
162 | return default | |
143 |
|
163 | |||
144 | def set_setting(self, setting, value): |
|
164 | def set_setting(self, setting, value): | |
145 | self.session[setting] = value |
|
165 | self.session[setting] = value | |
146 |
|
166 | |||
147 |
|
167 | |||
148 | def get_settings_manager(request) -> SettingsManager: |
|
168 | def get_settings_manager(request) -> SettingsManager: | |
149 | """ |
|
169 | """ | |
150 | Get settings manager based on the request object. Currently only |
|
170 | Get settings manager based on the request object. Currently only | |
151 | session-based manager is supported. In the future, cookie-based or |
|
171 | session-based manager is supported. In the future, cookie-based or | |
152 | database-based managers could be implemented. |
|
172 | database-based managers could be implemented. | |
153 | """ |
|
173 | """ | |
154 | return SessionSettingsManager(request.session) |
|
174 | return SessionSettingsManager(request.session) |
@@ -1,27 +1,30 b'' | |||||
1 | class Tripcode: |
|
1 | class Tripcode: | |
2 | def __init__(self, code_str): |
|
2 | def __init__(self, code_str): | |
3 | self.tripcode = code_str |
|
3 | self.tripcode = code_str | |
4 |
|
4 | |||
5 | def get_color(self): |
|
5 | def get_color(self): | |
6 | return self.tripcode[:6] |
|
6 | return self.tripcode[:6] | |
7 |
|
7 | |||
8 | def get_background(self): |
|
8 | def get_background(self): | |
9 | code = self.get_color() |
|
9 | code = self.get_color() | |
10 | result = '' |
|
10 | result = '' | |
11 |
|
11 | |||
12 | for i in range(0, len(code), 2): |
|
12 | for i in range(0, len(code), 2): | |
13 | p = code[i:i+2] |
|
13 | p = code[i:i+2] | |
14 | background = hex(255 - int(p, 16))[2:] |
|
14 | background = hex(255 - int(p, 16))[2:] | |
15 | if len(background) < 2: |
|
15 | if len(background) < 2: | |
16 | background = '0' + background |
|
16 | background = '0' + background | |
17 | result += background |
|
17 | result += background | |
18 |
|
18 | |||
19 | return result |
|
19 | return result | |
20 |
|
20 | |||
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