pyfedi/app/community/forms.py

76 lines
3.3 KiB
Python
Raw Normal View History

from flask_wtf import FlaskForm
2023-09-17 21:19:51 +12:00
from wtforms import StringField, SubmitField, TextAreaField, BooleanField, HiddenField, SelectField, FileField
from wtforms.validators import ValidationError, DataRequired, Email, EqualTo, Length, Optional
from flask_babel import _, lazy_gettext as _l
2023-09-17 21:19:51 +12:00
from app.utils import domain_from_url
2023-09-03 16:30:20 +12:00
class AddLocalCommunity(FlaskForm):
community_name = StringField(_l('Name'), validators=[DataRequired()])
url = StringField(_l('Url'), render_kw={'placeholder': '/c/'})
description = TextAreaField(_l('Description'))
rules = TextAreaField(_l('Rules'))
nsfw = BooleanField('18+ NSFW')
submit = SubmitField(_l('Create'))
class SearchRemoteCommunity(FlaskForm):
address = StringField(_l('Server address'), validators=[DataRequired()])
2023-09-17 21:19:51 +12:00
submit = SubmitField(_l('Search'))
class CreatePost(FlaskForm):
communities = SelectField(_l('Community'), validators=[DataRequired()], coerce=int)
type = HiddenField() # https://getbootstrap.com/docs/4.6/components/navs/#tabs
discussion_title = StringField(_l('Title'), validators={Optional(), Length(min=3, max=255)})
discussion_body = TextAreaField(_l('Body'), render_kw={'placeholder': 'Text (optional)'})
link_title = StringField(_l('Title'), validators={Optional(), Length(min=3, max=255)})
link_url = StringField(_l('URL'), render_kw={'placeholder': 'https://...'})
image_title = StringField(_l('Title'), validators={Optional(), Length(min=3, max=255)})
image_file = FileField(_('Image'))
# flair = SelectField(_l('Flair'), coerce=int)
nsfw = BooleanField(_l('NSFW'))
nsfl = BooleanField(_l('NSFL'))
notify = BooleanField(_l('Send me post reply notifications'))
submit = SubmitField(_l('Post'))
def validate(self, extra_validators=None) -> bool:
if not super().validate():
return False
if self.type.data is None or self.type.data == '':
self.type.data = 'discussion'
if self.type.data == 'discussion':
if self.discussion_title.data == '':
self.discussion_title.errors.append(_('Title is required.'))
return False
elif self.type.data == 'link':
if self.link_title.data == '':
self.link_title.errors.append(_('Title is required.'))
return False
if self.link_url.data == '':
self.link_url.errors.append(_('URL is required.'))
return False
domain = domain_from_url(self.link_url.data)
if domain.banned:
self.link_url.errors.append(_(f"Links to %s are not allowed.".format(domain.name)))
return False
elif self.type.data == 'image':
if self.image_title.data == '':
self.image_title.errors.append(_('Title is required.'))
return False
if self.image_file.data == '':
self.image_file.errors.append(_('File is required.'))
return False
elif self.type.data == 'poll':
self.discussion_title.errors.append(_('Poll not implemented yet.'))
return False
return True
class NewReplyForm(FlaskForm):
body = TextAreaField(_l('Body'), render_kw={'placeholder': 'What are your thoughts?'})
submit = SubmitField(_l('Comment'))