diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-05-01 17:37:18 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-06-17 13:21:28 +0200 | 
| commit | aa3850870c5671178c4648f91c62cdd3fc8affa4 (patch) | |
| tree | 2a399c7bbb37e7792f9ab75e46a0cb821426c646 /ishtar_common/models.py | |
| parent | de16fbff8bb115a4e03e739673b7d70e7f164237 (diff) | |
| download | Ishtar-aa3850870c5671178c4648f91c62cdd3fc8affa4.tar.bz2 Ishtar-aa3850870c5671178c4648f91c62cdd3fc8affa4.zip | |
Manage tiny urls for QR codes
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") | 
