# Path: app/validators.py
# def validate_image(value):
#     """
#     Validates if the image which is uploading is exactly image, because file can be renamed.
#     Raises an error if validation not passed.
# 
#     :param str value: The file object.
#     """
#     if not imghdr.what(value):
#         raise ValidationError('Tried to upload not an image!')
# 
# def validate_pdf(value):
#     """
#     Validates the uploading file if it is a PDF.
#     Raises an error if validation not passed.
# 
#     :param value: The file object.
#     """
#     try:
#         PyPDF2.PdfFileReader(io.BytesIO(value.read()))
#     except PyPDF2.utils.PdfReadError:
#         raise ValidationError('Tried to upload not PDF as a book!')


from django import forms
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator, MaxValueValidator
from django.core.validators import MinLengthValidator, MaxLengthValidator
from django.core.validators import RegexValidator, EmailValidator
from .validators import validate_image, validate_pdf
class LogInUsernameForm(forms.Form):
    username = forms.CharField(max_length=30,
                               validators=[RegexValidator(regex='^[a-zA-Z0-9_]{2,30}')])
    passw = forms.CharField(max_length=16)


# ----------------------------------------------------------------------------------------------------------------------
class LogInEmailForm(forms.Form):
    username = forms.CharField(max_length=320,
                               validators=[EmailValidator()])
    passw = forms.CharField(max_length=16)


# ----------------------------------------------------------------------------------------------------------------------
class GenerateAuthorsForm(forms.Form):
    part = forms.CharField(max_length=70)


# ----------------------------------------------------------------------------------------------------------------------
class GenerateBooksForm(forms.Form):
    part = forms.CharField(max_length=150)


# ----------------------------------------------------------------------------------------------------------------------
class AddBookForm(forms.Form):
    bookname = forms.CharField(max_length=150)
    author = forms.CharField(max_length=100)
    category = forms.CharField(max_length=30)
    language = forms.CharField(max_length=30)
    about = forms.CharField(widget=forms.Textarea)

# Path: app/validators.py
# def validate_image(value):
#     """
#     Validates if the image which is uploading is exactly image, because file can be renamed.
#     Raises an error if validation not passed.
# 
#     :param str value: The file object.
#     """
#     if not imghdr.what(value):
#         raise ValidationError('Tried to upload not an image!')
# 
# def validate_pdf(value):
#     """
#     Validates the uploading file if it is a PDF.
#     Raises an error if validation not passed.
# 
#     :param value: The file object.
#     """
#     try:
#         PyPDF2.PdfFileReader(io.BytesIO(value.read()))
#     except PyPDF2.utils.PdfReadError:
#         raise ValidationError('Tried to upload not PDF as a book!')


# Path: app/forms.py
from django import forms
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator, MaxValueValidator
from django.core.validators import MinLengthValidator, MaxLengthValidator
from django.core.validators import RegexValidator, EmailValidator
from .validators import validate_image, validate_pdf

class LogInUsernameForm(forms.Form):
    username = forms.CharField(max_length=30,
                               validators=[RegexValidator(regex='^[a-zA-Z0-9_]{2,30}')])
    passw = forms.CharField(max_length=16)


# ----------------------------------------------------------------------------------------------------------------------
class LogInEmailForm(forms.Form):
    username = forms.CharField(max_length=320,
                               validators=[EmailValidator()])
    passw = forms.CharField(max_length=16)


# ----------------------------------------------------------------------------------------------------------------------
class GenerateAuthorsForm(forms.Form):
    part = forms.CharField(max_length=70)


# ----------------------------------------------------------------------------------------------------------------------
class GenerateBooksForm(forms.Form):
    part = forms.CharField(max_length=150)


# ----------------------------------------------------------------------------------------------------------------------
class AddBookForm(forms.Form):
    bookname = forms.CharField(max_length=150)
    author = forms.CharField(max_length=100)
    category = forms.CharField(max_length=30)
    language = forms.CharField(max_length=30)
    about = forms.CharField(widget=forms.Textarea)

    bookfile = forms.FileField(validators=[validate_pdf])