This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from io import BytesIO | |
from django.db.models.fields.files import ImageField | |
from PIL import Image | |
""" | |
This field will automatically convert your jpg/png files to webp | |
Usage: | |
Replace your ImageField with this one. | |
Parameters: | |
webp_quality: Integer, 0-100, Defaults to 80. For lossy, 0 gives the smallest size and 100 the largest. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_hash(body): | |
secret_key = sha256(os.environ.get("TG_TOKEN", "").encode()).digest() | |
hash_string = body.pop("hash") | |
data_string = [f"{key}={value}" for key, value in body.items()] | |
data_string = "\n".join(sorted(data_string)) | |
return hmac.new(secret_key, data_string.encode(), sha256).hexdigest() == hash_string |