<s> """<STR_LIT>""" <EOL> from flask_wtf import Form <EOL> from wtforms import ( <EOL> BooleanField , HiddenField , IntegerField , PasswordField , <EOL> SelectField , StringField , SubmitField , TextAreaField , <EOL> ) <EOL> from wtforms . validators import ( <EOL> DataRequired , Optional , Email , regexp , Length , URL , ValidationError <EOL> ) <EOL> from wtforms . ext . sqlalchemy . fields import ( <EOL> QuerySelectField , QuerySelectMultipleField <EOL> ) <EOL> from sqlalchemy . orm . session import make_transient , make_transient_to_detached <EOL> from flask_babelplus import lazy_gettext as _ <EOL> from flaskbb . utils . fields import BirthdayField <EOL> from flaskbb . utils . widgets import SelectBirthdayWidget <EOL> from flaskbb . extensions import db <EOL> from flaskbb . forum . models import Forum , Category <EOL> from flaskbb . user . models import User , Group <EOL> from flaskbb . utils . requirements import IsAtleastModerator <EOL> from flask_allows import Permission <EOL> USERNAME_RE = r'<STR_LIT>' <EOL> is_username = regexp ( USERNAME_RE , <EOL> message = _ ( "<STR_LIT>" ) ) <EOL> def selectable_forums ( ) : <EOL> return Forum . query . order_by ( Forum . position ) <EOL> def selectable_categories ( ) : <EOL> return Category . query . order_by ( Category . position ) <EOL> def selectable_groups ( ) : <EOL> return Group . query . order_by ( Group . id . asc ( ) ) . all ( ) <EOL> def select_primary_group ( ) : <EOL> return Group . query . filter ( Group . guest != True ) . order_by ( Group . id ) <EOL> class UserForm ( Form ) : <EOL> username = StringField ( _ ( "<STR_LIT>" ) , validators = [ <EOL> DataRequired ( message = _ ( "<STR_LIT>" ) ) , <EOL> is_username ] ) <EOL> email = StringField ( _ ( "<STR_LIT>" ) , validators = [ <EOL> DataRequired ( message = _ ( "<STR_LIT>" ) ) , <EOL> Email ( message = _ ( "<STR_LIT>" ) ) ] ) <EOL> password = PasswordField ( "<STR_LIT>" , validators = [ <EOL> Optional ( ) ] ) <EOL> birthday = BirthdayField ( _ ( "<STR_LIT>" ) , format = "<STR_LIT>" , <EOL> widget = SelectBirthdayWidget ( ) , <EOL> validators = [ Optional ( ) ] ) <EOL> gender = SelectField ( _ ( "<STR_LIT>" ) , default = "<STR_LIT:None>" , choices = [ <EOL> ( "<STR_LIT:None>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) ] ) <EOL> location = StringField ( _ ( "<STR_LIT>" ) , validators = [ <EOL> Optional ( ) ] ) <EOL> website = StringField ( _ ( "<STR_LIT>" ) , validators = [ <EOL> Optional ( ) , URL ( ) ] ) <EOL> avatar = StringField ( _ ( "<STR_LIT>" ) , validators = [ <EOL> Optional ( ) , URL ( ) ] ) <EOL> signature = TextAreaField ( _ ( "<STR_LIT>" ) , validators = [ <EOL> Optional ( ) , Length ( min = <NUM_LIT:0> , max = <NUM_LIT> ) ] ) <EOL> notes = TextAreaField ( _ ( "<STR_LIT>" ) , validators = [ <EOL> Optional ( ) , Length ( min = <NUM_LIT:0> , max = <NUM_LIT> ) ] ) <EOL> primary_group = QuerySelectField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> query_factory = select_primary_group , <EOL> get_label = "<STR_LIT:name>" ) <EOL> secondary_groups = QuerySelectMultipleField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> query_factory = select_primary_group , <EOL> get_label = "<STR_LIT:name>" ) <EOL> submit = SubmitField ( _ ( "<STR_LIT>" ) ) <EOL> def validate_username ( self , field ) : <EOL> if hasattr ( self , "<STR_LIT:user>" ) : <EOL> user = User . query . filter ( <EOL> db . and_ ( <EOL> User . username . like ( field . data ) , <EOL> db . not_ ( User . id == self . user . id ) <EOL> ) <EOL> ) . first ( ) <EOL> else : <EOL> user = User . query . filter ( User . username . like ( field . data ) ) . first ( ) <EOL> if user : <EOL> raise ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> def validate_email ( self , field ) : <EOL> if hasattr ( self , "<STR_LIT:user>" ) : <EOL> user = User . query . filter ( <EOL> db . and_ ( <EOL> User . email . like ( field . data ) , <EOL> db . not_ ( User . id == self . user . id ) <EOL> ) <EOL> ) . first ( ) <EOL> else : <EOL> user = User . query . filter ( User . email . like ( field . data ) ) . first ( ) <EOL> if user : <EOL> raise ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> def save ( self ) : <EOL> data = self . data <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> user = User ( ** data ) <EOL> return user . save ( ) <EOL> class AddUserForm ( UserForm ) : <EOL> pass <EOL> class EditUserForm ( UserForm ) : <EOL> def __init__ ( self , user , * args , ** kwargs ) : <EOL> self . user = user <EOL> kwargs [ '<STR_LIT>' ] = self . user <EOL> UserForm . __init__ ( self , * args , ** kwargs ) <EOL> class GroupForm ( Form ) : <EOL> name = StringField ( _ ( "<STR_LIT>" ) , validators = [ <EOL> DataRequired ( message = _ ( "<STR_LIT>" ) ) ] ) <EOL> description = TextAreaField ( _ ( "<STR_LIT>" ) , validators = [ <EOL> Optional ( ) ] ) <EOL> admin = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> super_mod = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> mod = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> banned = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" ) <EOL> ) <EOL> guest = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" ) <EOL> ) <EOL> editpost = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" ) <EOL> ) <EOL> deletepost = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" ) <EOL> ) <EOL> deletetopic = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> posttopic = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> postreply = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" ) <EOL> ) <EOL> mod_edituser = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> mod_banuser = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" ) <EOL> ) <EOL> submit = SubmitField ( _ ( "<STR_LIT>" ) ) <EOL> def validate_name ( self , field ) : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> group = Group . query . filter ( <EOL> db . and_ ( <EOL> Group . name . like ( field . data ) , <EOL> db . not_ ( Group . id == self . group . id ) <EOL> ) <EOL> ) . first ( ) <EOL> else : <EOL> group = Group . query . filter ( Group . name . like ( field . data ) ) . first ( ) <EOL> if group : <EOL> raise ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> def validate_banned ( self , field ) : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> group = Group . query . filter ( <EOL> db . and_ ( <EOL> Group . banned , <EOL> db . not_ ( Group . id == self . group . id ) <EOL> ) <EOL> ) . count ( ) <EOL> else : <EOL> group = Group . query . filter_by ( banned = True ) . count ( ) <EOL> if field . data and group > <NUM_LIT:0> : <EOL> raise ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> def validate_guest ( self , field ) : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> group = Group . query . filter ( <EOL> db . and_ ( <EOL> Group . guest , <EOL> db . not_ ( Group . id == self . group . id ) <EOL> ) <EOL> ) . count ( ) <EOL> else : <EOL> group = Group . query . filter_by ( guest = True ) . count ( ) <EOL> if field . data and group > <NUM_LIT:0> : <EOL> raise ValidationError ( _ ( "<STR_LIT>" ) ) <EOL> def save ( self ) : <EOL> data = self . data <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> group = Group ( ** data ) <EOL> return group . save ( ) <EOL> class EditGroupForm ( GroupForm ) : <EOL> def __init__ ( self , group , * args , ** kwargs ) : <EOL> self . group = group <EOL> kwargs [ '<STR_LIT>' ] = self . group <EOL> GroupForm . __init__ ( self , * args , ** kwargs ) <EOL> class AddGroupForm ( GroupForm ) : <EOL> pass <EOL> class ForumForm ( Form ) : <EOL> title = StringField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> validators = [ DataRequired ( message = _ ( "<STR_LIT>" ) ) ] <EOL> ) <EOL> description = TextAreaField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> validators = [ Optional ( ) ] , <EOL> description = _ ( "<STR_LIT>" ) <EOL> ) <EOL> position = IntegerField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> default = <NUM_LIT:1> , <EOL> validators = [ DataRequired ( message = _ ( "<STR_LIT>" ) ) ] <EOL> ) <EOL> category = QuerySelectField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> query_factory = selectable_categories , <EOL> allow_blank = False , <EOL> get_label = "<STR_LIT:title>" , <EOL> description = _ ( "<STR_LIT>" ) <EOL> ) <EOL> external = StringField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> validators = [ Optional ( ) , URL ( ) ] , <EOL> description = _ ( "<STR_LIT>" ) <EOL> ) <EOL> moderators = StringField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> show_moderators = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" ) <EOL> ) <EOL> locked = BooleanField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> description = _ ( "<STR_LIT>" ) <EOL> ) <EOL> groups = QuerySelectMultipleField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> query_factory = selectable_groups , <EOL> get_label = "<STR_LIT:name>" , <EOL> description = _ ( "<STR_LIT>" ) <EOL> ) <EOL> submit = SubmitField ( _ ( "<STR_LIT>" ) ) <EOL> def validate_external ( self , field ) : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> if self . forum . topics . count ( ) > <NUM_LIT:0> : <EOL> raise ValidationError ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> def validate_show_moderators ( self , field ) : <EOL> if field . data and not self . moderators . data : <EOL> raise ValidationError ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> def validate_moderators ( self , field ) : <EOL> approved_moderators = [ ] <EOL> if field . data : <EOL> moderators = [ mod . strip ( ) for mod in field . data . split ( '<STR_LIT:U+002C>' ) ] <EOL> users = User . query . filter ( User . username . in_ ( moderators ) ) <EOL> for user in users : <EOL> if not Permission ( IsAtleastModerator , identity = user ) : <EOL> raise ValidationError ( <EOL> _ ( "<STR_LIT>" , <EOL> user = user . username ) <EOL> ) <EOL> else : <EOL> approved_moderators . append ( user ) <EOL> field . data = approved_moderators <EOL> def save ( self ) : <EOL> data = self . data <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> forum = Forum ( ** data ) <EOL> return forum . save ( ) <EOL> class EditForumForm ( ForumForm ) : <EOL> id = HiddenField ( ) <EOL> def __init__ ( self , forum , * args , ** kwargs ) : <EOL> self . forum = forum <EOL> kwargs [ '<STR_LIT>' ] = self . forum <EOL> ForumForm . __init__ ( self , * args , ** kwargs ) <EOL> def save ( self ) : <EOL> data = self . data <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> forum = Forum ( ** data ) <EOL> make_transient ( forum ) <EOL> make_transient_to_detached ( forum ) <EOL> return forum . save ( ) <EOL> class AddForumForm ( ForumForm ) : <EOL> pass <EOL> class CategoryForm ( Form ) : <EOL> title = StringField ( _ ( "<STR_LIT>" ) , validators = [ <EOL> DataRequired ( message = _ ( "<STR_LIT>" ) ) ] ) <EOL> description = TextAreaField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> validators = [ Optional ( ) ] , <EOL> description = _ ( "<STR_LIT>" ) <EOL> ) <EOL> position = IntegerField ( <EOL> _ ( "<STR_LIT>" ) , <EOL> default = <NUM_LIT:1> , <EOL> validators = [ DataRequired ( message = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) ] <EOL> ) <EOL> submit = SubmitField ( _ ( "<STR_LIT>" ) ) <EOL> def save ( self ) : <EOL> data = self . data <EOL> data . pop ( '<STR_LIT>' , None ) <EOL> category = Category ( ** data ) <EOL> return category . save ( ) </s>
