##// END OF EJS Templates
Added local stickers feature
neko259 -
r1940:1f7b0788 default
parent child Browse files
Show More
@@ -13,7 +13,7 b' class SessionAttachmentAlias(AttachmentA'
13
13
14 def get_image(self, alias):
14 def get_image(self, alias):
15 settings_manager = SessionSettingsManager(self.session)
15 settings_manager = SessionSettingsManager(self.session)
16 return settings_manager.get_image_by_alias(alias)
16 return settings_manager.get_attachment_by_alias(alias)
17
17
18
18
19 class ModelAttachmentAlias(AttachmentAlias):
19 class ModelAttachmentAlias(AttachmentAlias):
@@ -1,5 +1,6 b''
1 from boards import settings
1 from boards import settings
2 from boards.models import Tag, TagAlias
2 from boards.models import Tag, TagAlias, Attachment
3 from boards.models.attachment import AttachmentSticker
3 from boards.models.thread import FAV_THREAD_NO_UPDATES
4 from boards.models.thread import FAV_THREAD_NO_UPDATES
4 from boards.models.tag import DEFAULT_LOCALE
5 from boards.models.tag import DEFAULT_LOCALE
5
6
@@ -164,16 +165,29 b' class SettingsManager:'
164 names = set(name.strip() for name in names)
165 names = set(name.strip() for name in names)
165 return names
166 return names
166
167
167 def get_image_by_alias(self, alias):
168 def get_attachment_by_alias(self, alias):
168 images = self.get_setting(SETTING_IMAGES)
169 images = self.get_setting(SETTING_IMAGES)
169 if images is not None and len(images) > 0:
170 if images and alias in images:
170 return images.get(alias)
171 return Attachment.objects.get(id=images.get(alias))
171
172
172 def add_image_alias(self, alias, image):
173 def add_attachment_alias(self, alias, attachment):
173 images = self.get_setting(SETTING_IMAGES)
174 images = self.get_setting(SETTING_IMAGES)
174 if images is None:
175 if images is None:
175 images = dict()
176 images = dict()
176 images.put(alias, image)
177 images[alias] = attachment.id
178 self.set_setting(SETTING_IMAGES, images)
179
180 def remove_attachment_alias(self, alias):
181 images = self.get_setting(SETTING_IMAGES)
182 del images[alias]
183 self.set_setting(SETTING_IMAGES, images)
184
185 def get_stickers(self):
186 images = self.get_setting(SETTING_IMAGES)
187 if images:
188 return [AttachmentSticker(name=key,
189 attachment=Attachment.objects.get(id=value))
190 for key, value in images.items()]
177
191
178
192
179 class SessionSettingsManager(SettingsManager):
193 class SessionSettingsManager(SettingsManager):
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -622,3 +622,12 b' msgstr "\xd0\xa1\xd1\x82\xd0\xb8\xd0\xba\xd0\xb5\xd1\x80\xd1\x8b"'
622
622
623 msgid "Available by addresses:"
623 msgid "Available by addresses:"
624 msgstr "Доступно ΠΏΠΎ адрСсам:"
624 msgstr "Доступно ΠΏΠΎ адрСсам:"
625
626 msgid "Local stickers"
627 msgstr "Π›ΠΎΠΊΠ°Π»ΡŒΠ½Ρ‹Π΅ стикСры"
628
629 msgid "Global stickers"
630 msgstr "Π“Π»ΠΎΠ±Π°Π»ΡŒΠ½Ρ‹Π΅ стикСры"
631
632 msgid "Remove sticker"
633 msgstr "Π£Π΄Π°Π»ΠΈΡ‚ΡŒ стикСр"
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -59,3 +59,5 b' msgstr "\xd0\xa0\xd0\xb0\xd1\x81\xd1\x87\xd1\x91\xd1\x82 PoW..."'
59 msgid "Duplicates search"
59 msgid "Duplicates search"
60 msgstr "Поиск Π΄ΡƒΠ±Π»ΠΈΠΊΠ°Ρ‚ΠΎΠ²"
60 msgstr "Поиск Π΄ΡƒΠ±Π»ΠΈΠΊΠ°Ρ‚ΠΎΠ²"
61
61
62 msgid "Add local sticker"
63 msgstr "Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ Π»ΠΎΠΊΠ°Π»ΡŒΠ½Ρ‹ΠΉ стикСр"
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -622,3 +622,12 b' msgstr "\xd0\xa1\xd1\x82\xd1\x96\xd0\xba\xd0\xb5\xd1\x80\xd0\xb8"'
622
622
623 msgid "Available by addresses:"
623 msgid "Available by addresses:"
624 msgstr "Доступно Π·Π° адрСсами:"
624 msgstr "Доступно Π·Π° адрСсами:"
625
626 msgid "Local stickers"
627 msgstr "Π›ΠΎΠΊΠ°Π»ΡŒΠ½Ρ– стікСри"
628
629 msgid "Global stickers"
630 msgstr "Π“Π»ΠΎΠ±Π°Π»ΡŒΠ½Ρ– стікСри"
631
632 msgid "Remove sticker"
633 msgstr "Π’ΠΈΠ΄Π°Π»ΠΈΡ‚ΠΈ стікСр"
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -59,3 +59,5 b' msgstr "\xd0\xa0\xd0\xbe\xd0\xb7\xd1\x80\xd0\xb0\xd1\x85\xd0\xbe\xd0\xb2\xd1\x83\xd1\x94\xd1\x82\xd1\x8c\xd1\x81\xd1\x8f PoW..."'
59 msgid "Duplicates search"
59 msgid "Duplicates search"
60 msgstr "ΠŸΠΎΡˆΡƒΠΊ Π΄ΡƒΠ±Π»Ρ–ΠΊΠ°Ρ‚Ρ–Π²"
60 msgstr "ΠŸΠΎΡˆΡƒΠΊ Π΄ΡƒΠ±Π»Ρ–ΠΊΠ°Ρ‚Ρ–Π²"
61
61
62 msgid "Add local sticker"
63 msgstr "Π”ΠΎΠ΄Π°Ρ‚ΠΈ локальний стікСр"
@@ -125,7 +125,7 b' class Attachment(models.Model):'
125 if file_viewer is None:
125 if file_viewer is None:
126 file_viewer = AbstractViewer
126 file_viewer = AbstractViewer
127
127
128 return file_viewer(self.file, self.mimetype, self.hash, self.url).get_view()
128 return file_viewer(self.file, self.mimetype, self.id, self.url).get_view()
129
129
130 def __str__(self):
130 def __str__(self):
131 return self.url or self.file.url
131 return self.url or self.file.url
@@ -57,7 +57,7 b" CSS_CLASS_THUMB = 'thumb'"
57 ABSTRACT_VIEW = '<div class="image">'\
57 ABSTRACT_VIEW = '<div class="image">'\
58 '{}'\
58 '{}'\
59 '<div class="image-metadata"><a href="{}" download >{}, {}</a>'\
59 '<div class="image-metadata"><a href="{}" download >{}, {}</a>'\
60 ' <a class="file-menu" href="#" data-type="{}" data-search-url="{}" data-filename="{}">πŸ” </a></div>'\
60 ' <a class="file-menu" href="#" data-type="{}" data-search-url="{}" data-filename="{}" data-id="{}">πŸ” </a></div>'\
61 '</div>'
61 '</div>'
62 URL_VIEW = '<div class="image">' \
62 URL_VIEW = '<div class="image">' \
63 '{}' \
63 '{}' \
@@ -100,10 +100,10 b' def file_exists(filename):'
100
100
101
101
102 class AbstractViewer:
102 class AbstractViewer:
103 def __init__(self, file, file_type, hash, url):
103 def __init__(self, file, file_type, id, url):
104 self.file = file
104 self.file = file
105 self.file_type = file_type
105 self.file_type = file_type
106 self.hash = hash
106 self.id = id
107 self.url = url
107 self.url = url
108 self.extension = get_extension(self.file.name)
108 self.extension = get_extension(self.file.name)
109
109
@@ -122,7 +122,7 b' class AbstractViewer:'
122
122
123 return ABSTRACT_VIEW.format(self.get_format_view(), self.file.url,
123 return ABSTRACT_VIEW.format(self.get_format_view(), self.file.url,
124 self.file_type, filesizeformat(self.file.size),
124 self.file_type, filesizeformat(self.file.size),
125 self.file_type, search_url, self.file.name)
125 self.file_type, search_url, self.file.name, self.id)
126
126
127 def get_format_view(self):
127 def get_format_view(self):
128 image_name = PLAIN_FILE_FORMATS.get(self.extension, self.extension)
128 image_name = PLAIN_FILE_FORMATS.get(self.extension, self.extension)
@@ -193,7 +193,7 b' class ImageViewer(AbstractViewer):'
193
193
194 return IMAGE_FORMAT_VIEW.format(CSS_CLASS_THUMB,
194 return IMAGE_FORMAT_VIEW.format(CSS_CLASS_THUMB,
195 thumb_url,
195 thumb_url,
196 self.hash,
196 self.id,
197 str(pre_width),
197 str(pre_width),
198 str(pre_height), str(width), str(height),
198 str(pre_height), str(width), str(height),
199 full=self.file.url, image_meta=metadata)
199 full=self.file.url, image_meta=metadata)
@@ -160,6 +160,7 b' function addContextMenu() {'
160 var fileSearchUrl = $trigger.data('search-url');
160 var fileSearchUrl = $trigger.data('search-url');
161 var isImage = IMAGE_TYPES.indexOf($trigger.data('type')) > -1;
161 var isImage = IMAGE_TYPES.indexOf($trigger.data('type')) > -1;
162 var hasUrl = fileSearchUrl.length > 0;
162 var hasUrl = fileSearchUrl.length > 0;
163 var id = $trigger.data('id');
163 return {
164 return {
164 items: {
165 items: {
165 duplicates: {
166 duplicates: {
@@ -188,8 +189,17 b' function addContextMenu() {'
188 callback: function(key, opts) {
189 callback: function(key, opts) {
189 window.location = 'http://tineye.com/search?url=' + fileSearchUrl;
190 window.location = 'http://tineye.com/search?url=' + fileSearchUrl;
190 }
191 }
192 },
193 addAlias: {
194 name: gettext('Add local sticker'),
195 callback: function(key, opts) {
196 var alias = prompt(gettext('Input sticker name'));
197 if (alias) {
198 window.location = '/stickers/?action=add&name=' + alias + '&id=' + id;
199 }
200 }
191 }
201 }
192 },
202 }
193 };
203 };
194 }
204 }
195 });
205 });
@@ -10,11 +10,24 b''
10
10
11 {% block content %}
11 {% block content %}
12 <div id="posts-table">
12 <div id="posts-table">
13 {% for sticker in stickers %}
13 {% if local_stickers %}
14 <div class="gallery_image">
14 <h1>{% trans "Local stickers" %}</h1>
15 {{ sticker.attachment.get_view|safe }}
15 {% for sticker in local_stickers %}
16 <div>{{ sticker.name }}</div>
16 <div class="gallery_image">
17 </div>
17 {{ sticker.attachment.get_view|safe }}
18 {% endfor %}
18 <div>{{ sticker.name }}</div>
19 <div><a href="?action=remove&name={{ sticker.name }}">{% trans "Remove sticker" %}</a></div>
20 </div>
21 {% endfor %}
22 {% endif %}
23 {% if global_stickers %}
24 <h1>{% trans "Global stickers" %}</h1>
25 {% for sticker in global_stickers %}
26 <div class="gallery_image">
27 {{ sticker.attachment.get_view|safe }}
28 <div>{{ sticker.name }}</div>
29 </div>
30 {% endfor %}
31 {% endif %}
19 </div>
32 </div>
20 {% endblock %}
33 {% endblock %}
@@ -24,6 +24,7 b''
24 {% else %}
24 {% else %}
25 <p>{% trans 'No hidden tags.' %}</p>
25 <p>{% trans 'No hidden tags.' %}</p>
26 {% endif %}
26 {% endif %}
27 <p><a href="{% url 'stickers' %}">{% trans 'Stickers' %}</a></p>
27 </div>
28 </div>
28
29
29 <div class="post-form-w">
30 <div class="post-form-w">
@@ -195,7 +195,9 b' def api_get_stickers(request):'
195 if not term:
195 if not term:
196 return HttpResponseBadRequest()
196 return HttpResponseBadRequest()
197
197
198 stickers = AttachmentSticker.objects.filter(name__contains=term)
198 global_stickers = AttachmentSticker.objects.filter(name__contains=term)
199 local_stickers = [sticker for sticker in get_settings_manager(request).get_stickers() if term in sticker.name]
200 stickers = list(global_stickers) + local_stickers
199
201
200 image_dict = [{'thumb': sticker.attachment.get_thumb_url(),
202 image_dict = [{'thumb': sticker.attachment.get_thumb_url(),
201 'alias': sticker.name}
203 'alias': sticker.name}
@@ -1,11 +1,13 b''
1 from django.shortcuts import render
1 from django.shortcuts import render, redirect
2 from django.utils.decorators import method_decorator
2 from django.utils.decorators import method_decorator
3 from django.views.decorators.csrf import csrf_protect
3 from django.views.decorators.csrf import csrf_protect
4
4
5 from boards.models.attachment import AttachmentSticker
5 from boards.abstracts.settingsmanager import get_settings_manager
6 from boards.models.attachment import AttachmentSticker, Attachment
6 from boards.views.base import BaseBoardView
7 from boards.views.base import BaseBoardView
7
8
8 CONTEXT_STICKERS = 'stickers'
9 CONTEXT_GLOBAL_STICKERS = 'global_stickers'
10 CONTEXT_LOCAL_STICKERS = 'local_stickers'
9
11
10 TEMPLATE = 'boards/aliases.html'
12 TEMPLATE = 'boards/aliases.html'
11
13
@@ -13,13 +15,31 b" TEMPLATE = 'boards/aliases.html'"
13 class AliasesView(BaseBoardView):
15 class AliasesView(BaseBoardView):
14 @method_decorator(csrf_protect)
16 @method_decorator(csrf_protect)
15 def get(self, request, category=None):
17 def get(self, request, category=None):
18 result = self._process_creation(request)
19 if result:
20 return result
21
16 params = dict()
22 params = dict()
17
23
18 if category:
24 if category:
19 params[CONTEXT_STICKERS] = AttachmentSticker.objects.filter(
25 params[CONTEXT_GLOBAL_STICKERS] = AttachmentSticker.objects.filter(
20 name__startswith=(category + '/'))
26 name__startswith=(category + '/'))
21 else:
27 else:
22 params[CONTEXT_STICKERS] = AttachmentSticker.objects.all()
28 params[CONTEXT_GLOBAL_STICKERS] = AttachmentSticker.objects.all()
29 params[CONTEXT_LOCAL_STICKERS] = get_settings_manager(request).get_stickers()
23
30
24 return render(request, TEMPLATE, params)
31 return render(request, TEMPLATE, params)
25
32
33 def _process_creation(self, request):
34 action = request.GET.get('action')
35 if action == 'add' and 'name' in request.GET and 'id' in request.GET:
36 name = request.GET['name']
37 id = request.GET['id']
38 attachment = Attachment.objects.get(id=id)
39 get_settings_manager(request).add_attachment_alias(name, attachment)
40
41 return redirect('stickers')
42 if action == 'remove' and 'name' in request.GET:
43 name = request.GET['name']
44 get_settings_manager(request).remove_attachment_alias(name)
45 return redirect('stickers')
General Comments 0
You need to be logged in to leave comments. Login now