Files
jeweller-extend/extend/utils.py
2022-06-20 11:53:11 +03:00

26 lines
841 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
from django.core.exceptions import ValidationError
from django.utils.crypto import get_random_string
from django.utils.translation import gettext_lazy as _
def validate_url(value):
"""
Валидация ссылок на видео с видеохостинга YouTube
"""
video_url = value
regex = re.compile(
r"(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/(watch\?v=|embed/|v/|.+\?v=)?(?P<id>[A-Za-z0-9\-=_]{11})"
)
match = regex.match(video_url)
if not match:
raise ValidationError(
_("%(value)s не является корректной YouTube ссылкой"),
params={"value": value},
)
def generate_secret_key():
chars = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)"
return get_random_string(50, chars)