File size: 593 Bytes
d42510a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from hashids import Hashids
from django.conf import settings

class HashIdService:
    _hasher = Hashids(salt=getattr(settings, 'HASHIDS_SALT', settings.SECRET_KEY), min_length=8)

    @classmethod
    def encode(cls, id_val):
        if id_val is None:
            return None
        return cls._hasher.encode(id_val)

    @classmethod
    def decode(cls, hash_val):
        if not hash_val:
            return None
        try:
            decoded = cls._hasher.decode(hash_val)
            if decoded:
                return decoded[0]
        except:
            pass
        return None