diff options
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 4165128f6..e5721d922 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -31,6 +31,7 @@ import os import pyqrcode import re import shutil +import string import tempfile import time from io import BytesIO @@ -1010,6 +1011,31 @@ class HierarchicalType(GeneralType): return " > ".join(reversed(lbls)) +class TinyUrl(models.Model): + CHAR_MAP = string.ascii_letters + string.digits + link = models.URLField() + + @classmethod + def index_to_char(cls, seq): + return "".join([cls.CHAR_MAP[x] for x in seq]) + + def get_short_id(self): + c_id = self.id + digits = [] + while c_id > 0: + digits.append(c_id % 62) + c_id //= 62 + digits.reverse() + return self.index_to_char(digits) + + @classmethod + def decode_id(cls, value): + i = 0 + for c in value: + i = i * 64 + cls.CHAR_MAP.index(c) + return i + + class ItemKey(models.Model): key = models.TextField(_("Key")) content_type = models.ForeignKey(ContentType) @@ -1653,7 +1679,12 @@ class QRCodeItem(models.Model, ImageContainerModel): else: scheme = "http" url = scheme + "://" + site.domain + url - qr = pyqrcode.create(url, version=settings.ISHTAR_QRCODE_VERSION) + tiny_url = TinyUrl() + tiny_url.link = url + tiny_url.save() + short_url = scheme + "://" + site.domain + reverse( + 'tiny-redirect', args=[tiny_url.get_short_id()]) + qr = pyqrcode.create(short_url, version=settings.ISHTAR_QRCODE_VERSION) tmpdir_created = False if not tmpdir: tmpdir = tempfile.mkdtemp("-qrcode") |