Show More
@@ -1,8 +1,3 b'' | |||||
1 | import uuid |
|
|||
2 |
|
||||
3 | import boards |
|
|||
4 | from boards.abstracts.tripcode import Tripcode |
|
|||
5 |
|
||||
6 |
|
|
1 | from boards.models import Tag | |
7 |
|
2 | |||
8 | MAX_TRIPCODE_COLLISIONS = 50 |
|
3 | MAX_TRIPCODE_COLLISIONS = 50 | |
@@ -123,27 +118,6 b' class SettingsManager:' | |||||
123 | tags.remove(tag.name) |
|
118 | tags.remove(tag.name) | |
124 | self.set_setting(SETTING_HIDDEN_TAGS, tags) |
|
119 | self.set_setting(SETTING_HIDDEN_TAGS, tags) | |
125 |
|
120 | |||
126 | def get_tripcode(self): |
|
|||
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 |
|
|||
132 |
|
||||
133 | def reset_tripcode(self): |
|
|||
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()) |
|
|||
146 |
|
||||
147 |
|
121 | |||
148 | class SessionSettingsManager(SettingsManager): |
|
122 | class SessionSettingsManager(SettingsManager): | |
149 | """ |
|
123 | """ |
@@ -1,3 +1,4 b'' | |||||
|
1 | import hashlib | |||
1 | import re |
|
2 | import re | |
2 | import time |
|
3 | import time | |
3 |
|
4 | |||
@@ -9,12 +10,12 b' from django.forms.util import ErrorList' | |||||
9 | from django.utils.translation import ugettext_lazy as _ |
|
10 | from django.utils.translation import ugettext_lazy as _ | |
10 | import requests |
|
11 | import requests | |
11 |
|
12 | |||
12 | from boards.abstracts.settingsmanager import SessionSettingsManager |
|
|||
13 | from boards.mdx_neboard import formatters |
|
13 | from boards.mdx_neboard import formatters | |
14 | from boards.models.post import TITLE_MAX_LENGTH |
|
14 | from boards.models.post import TITLE_MAX_LENGTH | |
15 | from boards.models import Tag, Post |
|
15 | from boards.models import Tag, Post | |
16 | from neboard import settings |
|
16 | from neboard import settings | |
17 | import boards.settings as board_settings |
|
17 | import boards.settings as board_settings | |
|
18 | import neboard | |||
18 |
|
19 | |||
19 | HEADER_CONTENT_LENGTH = 'content-length' |
|
20 | HEADER_CONTENT_LENGTH = 'content-length' | |
20 | HEADER_CONTENT_TYPE = 'content-type' |
|
21 | HEADER_CONTENT_TYPE = 'content-type' | |
@@ -130,7 +131,10 b' class NeboardForm(forms.Form):' | |||||
130 | class PostForm(NeboardForm): |
|
131 | class PostForm(NeboardForm): | |
131 |
|
132 | |||
132 | title = forms.CharField(max_length=TITLE_MAX_LENGTH, required=False, |
|
133 | title = forms.CharField(max_length=TITLE_MAX_LENGTH, required=False, | |
133 |
label=LABEL_TITLE |
|
134 | label=LABEL_TITLE, | |
|
135 | widget=forms.TextInput( | |||
|
136 | attrs={ATTRIBUTE_PLACEHOLDER: | |||
|
137 | 'test#tripcode'})) | |||
134 | text = forms.CharField( |
|
138 | text = forms.CharField( | |
135 | widget=FormatPanel(attrs={ |
|
139 | widget=FormatPanel(attrs={ | |
136 | ATTRIBUTE_PLACEHOLDER: TEXT_PLACEHOLDER, |
|
140 | ATTRIBUTE_PLACEHOLDER: TEXT_PLACEHOLDER, | |
@@ -152,7 +156,6 b' class PostForm(NeboardForm):' | |||||
152 | threads = forms.CharField(required=False, label=_('Additional threads'), |
|
156 | threads = forms.CharField(required=False, label=_('Additional threads'), | |
153 | widget=forms.TextInput(attrs={ATTRIBUTE_PLACEHOLDER: |
|
157 | widget=forms.TextInput(attrs={ATTRIBUTE_PLACEHOLDER: | |
154 | '123 456 789'})) |
|
158 | '123 456 789'})) | |
155 | tripcode = forms.BooleanField(label=_('Tripcode'), required=False) |
|
|||
156 |
|
159 | |||
157 | session = None |
|
160 | session = None | |
158 | need_to_ban = False |
|
161 | need_to_ban = False | |
@@ -240,8 +243,17 b' class PostForm(NeboardForm):' | |||||
240 | return file or self.cleaned_data['file_url'] |
|
243 | return file or self.cleaned_data['file_url'] | |
241 |
|
244 | |||
242 | def get_tripcode(self): |
|
245 | def get_tripcode(self): | |
243 |
|
|
246 | title = self.cleaned_data['title'] | |
244 | return SessionSettingsManager(self.session).get_tripcode() |
|
247 | if title is not None and '#' in title: | |
|
248 | code = title.split('#', maxsplit=1)[1] + neboard.settings.SECRET_KEY | |||
|
249 | return hashlib.md5(code.encode()).hexdigest() | |||
|
250 | ||||
|
251 | def get_title(self): | |||
|
252 | title = self.cleaned_data['title'] | |||
|
253 | if title is not None and '#' in title: | |||
|
254 | return title.split('#', maxsplit=1)[0] | |||
|
255 | else: | |||
|
256 | return title | |||
245 |
|
257 | |||
246 | def _clean_text_file(self): |
|
258 | def _clean_text_file(self): | |
247 | text = self.cleaned_data.get('text') |
|
259 | text = self.cleaned_data.get('text') |
@@ -26,11 +26,6 b'' | |||||
26 | {% else %} |
|
26 | {% else %} | |
27 | <p>{% trans 'No hidden tags.' %}</p> |
|
27 | <p>{% trans 'No hidden tags.' %}</p> | |
28 | {% endif %} |
|
28 | {% endif %} | |
29 | <div>{% trans "Tripcode:" %} {% autoescape off %}{{ tripcode.get_view }}{% endautoescape %} |
|
|||
30 | <form method="post" class="post-button-form"> |
|
|||
31 | (<button name="reset_tripcode" value="0">{% trans "reset" %}</button>) |
|
|||
32 | </form> |
|
|||
33 | </div> |
|
|||
34 | </div> |
|
29 | </div> | |
35 |
|
30 | |||
36 | <div class="post-form-w"> |
|
31 | <div class="post-form-w"> |
@@ -139,7 +139,7 b' class AllThreadsView(PostMixin, BaseBoar' | |||||
139 |
|
139 | |||
140 | data = form.cleaned_data |
|
140 | data = form.cleaned_data | |
141 |
|
141 | |||
142 | title = data[FORM_TITLE] |
|
142 | title = form.get_title() | |
143 | text = data[FORM_TEXT] |
|
143 | text = data[FORM_TEXT] | |
144 | file = form.get_file() |
|
144 | file = form.get_file() | |
145 | threads = data[FORM_THREADS] |
|
145 | threads = data[FORM_THREADS] |
@@ -4,21 +4,17 b' from django.utils import timezone' | |||||
4 |
|
4 | |||
5 | from boards.abstracts.settingsmanager import get_settings_manager, \ |
|
5 | from boards.abstracts.settingsmanager import get_settings_manager, \ | |
6 | SETTING_USERNAME, SETTING_LAST_NOTIFICATION_ID, SETTING_IMAGE_VIEWER |
|
6 | SETTING_USERNAME, SETTING_LAST_NOTIFICATION_ID, SETTING_IMAGE_VIEWER | |
7 | from boards.abstracts.tripcode import Tripcode |
|
|||
8 | from boards.middlewares import SESSION_TIMEZONE |
|
7 | from boards.middlewares import SESSION_TIMEZONE | |
9 | from boards.views.base import BaseBoardView, CONTEXT_FORM |
|
8 | from boards.views.base import BaseBoardView, CONTEXT_FORM | |
10 | from boards.forms import SettingsForm, PlainErrorList |
|
9 | from boards.forms import SettingsForm, PlainErrorList | |
11 | from boards import settings |
|
10 | from boards import settings | |
12 |
|
11 | |||
13 | PARAM_RESET_TRIPCODE = 'reset_tripcode' |
|
|||
14 |
|
||||
15 | FORM_THEME = 'theme' |
|
12 | FORM_THEME = 'theme' | |
16 | FORM_USERNAME = 'username' |
|
13 | FORM_USERNAME = 'username' | |
17 | FORM_TIMEZONE = 'timezone' |
|
14 | FORM_TIMEZONE = 'timezone' | |
18 | FORM_IMAGE_VIEWER = 'image_viewer' |
|
15 | FORM_IMAGE_VIEWER = 'image_viewer' | |
19 |
|
16 | |||
20 | CONTEXT_HIDDEN_TAGS = 'hidden_tags' |
|
17 | CONTEXT_HIDDEN_TAGS = 'hidden_tags' | |
21 | CONTEXT_TRIPCODE = 'tripcode' |
|
|||
22 |
|
18 | |||
23 | TEMPLATE = 'boards/settings.html' |
|
19 | TEMPLATE = 'boards/settings.html' | |
24 |
|
20 | |||
@@ -45,17 +41,12 b' class SettingsView(BaseBoardView):' | |||||
45 |
|
41 | |||
46 | params[CONTEXT_FORM] = form |
|
42 | params[CONTEXT_FORM] = form | |
47 | params[CONTEXT_HIDDEN_TAGS] = settings_manager.get_hidden_tags() |
|
43 | params[CONTEXT_HIDDEN_TAGS] = settings_manager.get_hidden_tags() | |
48 | params[CONTEXT_TRIPCODE] = Tripcode(settings_manager.get_tripcode()) |
|
|||
49 |
|
44 | |||
50 | return render(request, TEMPLATE, params) |
|
45 | return render(request, TEMPLATE, params) | |
51 |
|
46 | |||
52 | def post(self, request): |
|
47 | def post(self, request): | |
53 | settings_manager = get_settings_manager(request) |
|
48 | settings_manager = get_settings_manager(request) | |
54 |
|
49 | |||
55 | if PARAM_RESET_TRIPCODE in request.POST: |
|
|||
56 | settings_manager.reset_tripcode() |
|
|||
57 | return self.get(request) |
|
|||
58 |
|
||||
59 | with transaction.atomic(): |
|
50 | with transaction.atomic(): | |
60 | form = SettingsForm(request.POST, error_class=PlainErrorList) |
|
51 | form = SettingsForm(request.POST, error_class=PlainErrorList) | |
61 |
|
52 |
@@ -101,7 +101,7 b' class ThreadView(BaseBoardView, PostMixi' | |||||
101 |
|
101 | |||
102 | data = form.cleaned_data |
|
102 | data = form.cleaned_data | |
103 |
|
103 | |||
104 | title = data[FORM_TITLE] |
|
104 | title = form.get_title() | |
105 | text = data[FORM_TEXT] |
|
105 | text = data[FORM_TEXT] | |
106 | file = form.get_file() |
|
106 | file = form.get_file() | |
107 | threads = data[FORM_THREADS] |
|
107 | threads = data[FORM_THREADS] |
General Comments 0
You need to be logged in to leave comments.
Login now