mirror of
https://github.com/aljazceru/CTFd.git
synced 2025-12-18 14:34:21 +01:00
Extract custom user field attachment into seperate function
This commit is contained in:
@@ -3,6 +3,7 @@ from wtforms.fields.html5 import EmailField
|
|||||||
from wtforms.validators import InputRequired
|
from wtforms.validators import InputRequired
|
||||||
|
|
||||||
from CTFd.forms import BaseForm
|
from CTFd.forms import BaseForm
|
||||||
|
from CTFd.forms.users import attach_custom_user_fields
|
||||||
from CTFd.forms.fields import SubmitField
|
from CTFd.forms.fields import SubmitField
|
||||||
from CTFd.models import UserFields
|
from CTFd.models import UserFields
|
||||||
|
|
||||||
@@ -23,18 +24,7 @@ def RegistrationForm(*args, **kwargs):
|
|||||||
fields.append(entry)
|
fields.append(entry)
|
||||||
return fields
|
return fields
|
||||||
|
|
||||||
new_fields = UserFields.query.all()
|
attach_custom_user_fields(_RegistrationForm)
|
||||||
for field in new_fields:
|
|
||||||
validators = []
|
|
||||||
if field.required:
|
|
||||||
validators.append(InputRequired())
|
|
||||||
setattr(
|
|
||||||
_RegistrationForm,
|
|
||||||
f"fields[{field.id}]",
|
|
||||||
StringField(
|
|
||||||
field.name, description=field.description, validators=validators
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
return _RegistrationForm(*args, **kwargs)
|
return _RegistrationForm(*args, **kwargs)
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from wtforms import PasswordField, SelectField, StringField
|
|||||||
from wtforms.fields.html5 import DateField, URLField
|
from wtforms.fields.html5 import DateField, URLField
|
||||||
|
|
||||||
from CTFd.forms import BaseForm
|
from CTFd.forms import BaseForm
|
||||||
|
from CTFd.forms.users import attach_custom_user_fields
|
||||||
from CTFd.forms.fields import SubmitField
|
from CTFd.forms.fields import SubmitField
|
||||||
from CTFd.models import FieldEntries, UserFields
|
from CTFd.models import FieldEntries, UserFields
|
||||||
from CTFd.utils.countries import SELECT_COUNTRIES_LIST
|
from CTFd.utils.countries import SELECT_COUNTRIES_LIST
|
||||||
@@ -33,26 +34,14 @@ def SettingsForm(*args, **kwargs):
|
|||||||
initial = user_fields.get(field.id, "")
|
initial = user_fields.get(field.id, "")
|
||||||
form_field.data = initial
|
form_field.data = initial
|
||||||
if form_field.render_kw:
|
if form_field.render_kw:
|
||||||
form_field.render_kw["initial"] = initial
|
form_field.render_kw["data-initial"] = initial
|
||||||
else:
|
else:
|
||||||
form_field.render_kw = {"data-initial": initial}
|
form_field.render_kw = {"data-initial": initial}
|
||||||
entry = (field.name, form_field)
|
entry = (field.name, form_field)
|
||||||
fields.append(entry)
|
fields.append(entry)
|
||||||
return fields
|
return fields
|
||||||
|
|
||||||
new_fields = UserFields.query.filter_by(editable=True).all()
|
attach_custom_user_fields(_SettingsForm, editable=True)
|
||||||
for field in new_fields:
|
|
||||||
validators = []
|
|
||||||
if field.required:
|
|
||||||
validators.append(InputRequired())
|
|
||||||
|
|
||||||
setattr(
|
|
||||||
_SettingsForm,
|
|
||||||
f"fields[{field.id}]",
|
|
||||||
StringField(
|
|
||||||
field.name, description=field.description, validators=validators
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
return _SettingsForm(*args, **kwargs)
|
return _SettingsForm(*args, **kwargs)
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,25 @@ from CTFd.forms.fields import SubmitField
|
|||||||
from CTFd.models import FieldEntries, UserFields
|
from CTFd.models import FieldEntries, UserFields
|
||||||
from CTFd.utils.countries import SELECT_COUNTRIES_LIST
|
from CTFd.utils.countries import SELECT_COUNTRIES_LIST
|
||||||
|
|
||||||
|
def build_custom_user_fields():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def attach_custom_user_fields(form_cls, **kwargs):
|
||||||
|
new_fields = UserFields.filter_by(**kwargs).query.all()
|
||||||
|
for field in new_fields:
|
||||||
|
validators = []
|
||||||
|
if field.required:
|
||||||
|
validators.append(InputRequired())
|
||||||
|
|
||||||
|
setattr(
|
||||||
|
form_cls,
|
||||||
|
f"fields[{field.id}]",
|
||||||
|
StringField(
|
||||||
|
field.name, description=field.description, validators=validators
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class UserSearchForm(BaseForm):
|
class UserSearchForm(BaseForm):
|
||||||
field = SelectField(
|
field = SelectField(
|
||||||
@@ -89,19 +108,7 @@ def UserEditForm(*args, **kwargs):
|
|||||||
if obj:
|
if obj:
|
||||||
self.obj = obj
|
self.obj = obj
|
||||||
|
|
||||||
new_fields = UserFields.query.all()
|
attach_custom_user_fields(_UserEditForm)
|
||||||
for field in new_fields:
|
|
||||||
validators = []
|
|
||||||
if field.required:
|
|
||||||
validators.append(InputRequired())
|
|
||||||
|
|
||||||
setattr(
|
|
||||||
_UserEditForm,
|
|
||||||
f"fields[{field.id}]",
|
|
||||||
StringField(
|
|
||||||
field.name, description=field.description, validators=validators
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
return _UserEditForm(*args, **kwargs)
|
return _UserEditForm(*args, **kwargs)
|
||||||
|
|
||||||
@@ -121,18 +128,6 @@ def UserCreateForm(*args, **kwargs):
|
|||||||
fields.append(entry)
|
fields.append(entry)
|
||||||
return fields
|
return fields
|
||||||
|
|
||||||
new_fields = UserFields.query.all()
|
attach_custom_user_fields(_UserCreateForm)
|
||||||
for field in new_fields:
|
|
||||||
validators = []
|
|
||||||
if field.required:
|
|
||||||
validators.append(InputRequired())
|
|
||||||
|
|
||||||
setattr(
|
|
||||||
_UserCreateForm,
|
|
||||||
f"fields[{field.id}]",
|
|
||||||
StringField(
|
|
||||||
field.name, description=field.description, validators=validators
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
return _UserCreateForm(*args, **kwargs)
|
return _UserCreateForm(*args, **kwargs)
|
||||||
|
|||||||
Reference in New Issue
Block a user