tripcode.py
49 lines
| 1.3 KiB
| text/x-python
|
PythonLexer
neko259
|
r2024 | from django.urls import reverse | ||
neko259
|
r2026 | from boards.utils import cached_result | ||
neko259
|
r2024 | TRIPCODE_VIEW = '<a href="{url}?tripcode={full_text}"' \ | ||
'class="tripcode" title="{full_text}"' \ | ||||
'style="border: solid 2px #{color}; border-left: solid 1ex #{color};">' \ | ||||
'{short_text}</a>' | ||||
neko259
|
r2026 | SHORT_TEXT_CHARS = 8 | ||
COLOR_CHARS = 6 | ||||
neko259
|
r2024 | |||
neko259
|
r1296 | class Tripcode: | ||
def __init__(self, code_str): | ||||
self.tripcode = code_str | ||||
def get_color(self): | ||||
neko259
|
r2026 | return self.tripcode[:COLOR_CHARS] | ||
neko259
|
r1296 | |||
def get_background(self): | ||||
code = self.get_color() | ||||
result = '' | ||||
for i in range(0, len(code), 2): | ||||
p = code[i:i+2] | ||||
background = hex(255 - int(p, 16))[2:] | ||||
if len(background) < 2: | ||||
background = '0' + background | ||||
result += background | ||||
return result | ||||
def get_short_text(self): | ||||
neko259
|
r2026 | return self.tripcode[:SHORT_TEXT_CHARS] | ||
neko259
|
r1296 | |||
neko259
|
r1297 | def get_full_text(self): | ||
return self.tripcode | ||||
neko259
|
r2026 | def _get_cache_key(self): | ||
return [self.tripcode] | ||||
@cached_result(key_method=_get_cache_key) | ||||
neko259
|
r2024 | def get_view(self): | ||
return TRIPCODE_VIEW.format(url=reverse('feed'), | ||||
short_text=self.get_short_text(), | ||||
full_text=self.get_full_text(), | ||||
color=self.get_color()) | ||||
neko259
|
r2026 | |||