mediacms/users/forms.py

61 lines
1.7 KiB
Python
Raw Normal View History

2020-12-15 22:33:43 +01:00
from django import forms
2021-05-26 17:35:21 +02:00
from .models import Channel, User
2020-12-15 22:33:43 +01:00
class SignupForm(forms.Form):
name = forms.CharField(max_length=100, label="Name")
def signup(self, request, user):
user.name = self.cleaned_data["name"]
user.save()
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = (
"name",
"description",
"email",
"logo",
"notification_on_comments",
"is_featured",
"advancedUser",
"is_manager",
"is_editor",
2021-05-26 17:35:21 +02:00
# "allow_contact",
2020-12-15 22:33:43 +01:00
)
def clean_logo(self):
image = self.cleaned_data.get("logo", False)
if image:
if image.size > 2 * 1024 * 1024:
raise forms.ValidationError("Image file too large ( > 2mb )")
return image
else:
raise forms.ValidationError("Please provide a logo")
def __init__(self, user, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
self.fields.pop("is_featured")
if not user.is_superuser:
self.fields.pop("advancedUser")
self.fields.pop("is_manager")
self.fields.pop("is_editor")
class ChannelForm(forms.ModelForm):
class Meta:
model = Channel
fields = ("banner_logo",)
def clean_banner_logo(self):
image = self.cleaned_data.get("banner_logo", False)
if image:
if image.size > 2 * 1024 * 1024:
raise forms.ValidationError("Image file too large ( > 2mb )")
return image
else:
raise forms.ValidationError("Please provide a banner")