113 lines
3.5 KiB
Python
113 lines
3.5 KiB
Python
|
|
from django import forms
|
||
|
|
from django.core import validators
|
||
|
|
from .models import File, Message, PublicMessage, PublicFile, Friendship
|
||
|
|
from django.contrib.auth import get_user_model
|
||
|
|
from django.contrib.auth.forms import UserCreationForm
|
||
|
|
|
||
|
|
class CustomUserCreationForm(UserCreationForm):
|
||
|
|
class Meta(UserCreationForm.Meta):
|
||
|
|
labels = {
|
||
|
|
'username': '用户名',
|
||
|
|
'password1': '密码',
|
||
|
|
'password2': '确认密码'
|
||
|
|
}
|
||
|
|
help_texts = {
|
||
|
|
'username': '150个字符以内。可以包含中文、字母、数字和 @/./+/-/_。',
|
||
|
|
'password1': '您的密码不能与其他个人信息太相似。\n您的密码必须包含至少 8 个字符。\n您的密码不能是常用密码。\n您的密码不能全部是数字。',
|
||
|
|
'password2': '再次输入密码以确认。'
|
||
|
|
}
|
||
|
|
|
||
|
|
def __init__(self, *args, **kwargs):
|
||
|
|
super().__init__(*args, **kwargs)
|
||
|
|
# 修改用户名字段的正则验证
|
||
|
|
self.fields['username'].validators = [
|
||
|
|
validators.RegexValidator(
|
||
|
|
r'^[\w\u4e00-\u9fa5@.+-_]+$',
|
||
|
|
'用户名只能包含中文、字母、数字和 @/./+/-/_。'
|
||
|
|
)
|
||
|
|
]
|
||
|
|
|
||
|
|
class FileUploadForm(forms.ModelForm):
|
||
|
|
is_public = forms.BooleanField(
|
||
|
|
required=False,
|
||
|
|
label='设为公开文件',
|
||
|
|
help_text='如果勾选,此文件将对所有用户可见'
|
||
|
|
)
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
model = File
|
||
|
|
fields = ['file', 'description', 'shared_with', 'is_public']
|
||
|
|
labels = {
|
||
|
|
'file': '文件',
|
||
|
|
'description': '描述',
|
||
|
|
'shared_with': '共享用户'
|
||
|
|
}
|
||
|
|
|
||
|
|
def __init__(self, *args, **kwargs):
|
||
|
|
super().__init__(*args, **kwargs)
|
||
|
|
self.fields['shared_with'].required = False
|
||
|
|
|
||
|
|
class MessageForm(forms.ModelForm):
|
||
|
|
class Meta:
|
||
|
|
model = Message
|
||
|
|
fields = ['recipients', 'content', 'file']
|
||
|
|
labels = {
|
||
|
|
'recipients': '收件人',
|
||
|
|
'content': '消息内容',
|
||
|
|
'file': '附件'
|
||
|
|
}
|
||
|
|
|
||
|
|
class PublicMessageForm(forms.ModelForm):
|
||
|
|
class Meta:
|
||
|
|
model = PublicMessage
|
||
|
|
fields = ['content']
|
||
|
|
labels = {
|
||
|
|
'content': '留言内容'
|
||
|
|
}
|
||
|
|
widgets = {
|
||
|
|
'content': forms.Textarea(attrs={'rows': 3, 'placeholder': '您的留言...'}),
|
||
|
|
}
|
||
|
|
|
||
|
|
class FriendRequestForm(forms.Form):
|
||
|
|
username = forms.CharField(
|
||
|
|
label='用户名',
|
||
|
|
max_length=150,
|
||
|
|
widget=forms.TextInput(attrs={
|
||
|
|
'placeholder': '输入要添加的好友用户名',
|
||
|
|
'class': 'w-full px-3 py-2 border rounded-md'
|
||
|
|
})
|
||
|
|
)
|
||
|
|
|
||
|
|
def clean_username(self):
|
||
|
|
username = self.cleaned_data['username']
|
||
|
|
User = get_user_model()
|
||
|
|
try:
|
||
|
|
user = User.objects.get(username=username)
|
||
|
|
except User.DoesNotExist:
|
||
|
|
raise forms.ValidationError('用户不存在')
|
||
|
|
return user
|
||
|
|
|
||
|
|
class FriendActionForm(forms.Form):
|
||
|
|
ACTION_CHOICES = [
|
||
|
|
('accept', '接受'),
|
||
|
|
('reject', '拒绝'),
|
||
|
|
]
|
||
|
|
|
||
|
|
action = forms.ChoiceField(
|
||
|
|
choices=ACTION_CHOICES,
|
||
|
|
widget=forms.RadioSelect,
|
||
|
|
label='操作'
|
||
|
|
)
|
||
|
|
|
||
|
|
class PublicFileForm(forms.ModelForm):
|
||
|
|
class Meta:
|
||
|
|
model = PublicFile
|
||
|
|
fields = ['file', 'description']
|
||
|
|
labels = {
|
||
|
|
'file': '文件',
|
||
|
|
'description': '描述'
|
||
|
|
}
|
||
|
|
widgets = {
|
||
|
|
'description': forms.Textarea(attrs={'rows': 3, 'placeholder': '文件描述...'}),
|
||
|
|
}
|