flask-security icon indicating copy to clipboard operation
flask-security copied to clipboard

Unable to Add custom fields to Register form

Open satyajeetnim opened this issue 7 years ago • 5 comments

I have followed the steps listed here: https://pythonhosted.org/Flask-Security/customizing.html#forms Also I have looked at a lot of similar issues and tried the solutions mentioned there, but all in vain. Basically, I simply want to add a Name field in the registration form, but it isn't working. These are my changes:

forms.py

from wtforms import StringField
from flask_security.forms import RegisterForm, Required

class ExtendedRegisterForm(RegisterForm):
        name = StringField('First Name', [Required()])

models.py

from flask_mongoengine import MongoEngine
from flask_security import UserMixin, RoleMixin

mongodb = MongoEngine()

class Role(mongodb.Document, RoleMixin):
        name = mongodb.StringField(max_length=80, unique=True)
        description = mongodb.StringField(max_length=255)


class User(mongodb.Document, UserMixin):
        email = mongodb.StringField(max_length=255, unique=True)
        name = mongodb.StringField(max_length=255)
        password = mongodb.StringField(max_length=255)
        active = mongodb.BooleanField(default=True)
        created_at = mongodb.DateTimeField()
        confirmed_at = mongodb.DateTimeField()
        roles = mongodb.ListField(mongodb.ReferenceField(Role), default=[]) 

init.py

from flask import Flask
from flask_security import Security, MongoEngineUserDatastore
from test.models import User, Role
from test.forms import ExtendedRegisterForm

app = Flask(__name__)

app.config.from_object('test.config.test_config')

from test.models import mongodb
app.db = mongodb
app.db.init_app(app)

test = MongoEngineUserDatastore(app.db, User, Role)
security = Security(app, test, register_form=ExtendedRegisterForm)

from flask.ext.mail import Mail
app.mail = Mail(app)

In addition to this I wanted to achieve 2 things:

  1. Every time someone registers add role to the user and add created date/time. I already have added these fields to the models.py

Can someone tell me what it is that I am doing wrong and how should I add user role and creating date/time during registration.

satyajeetnim avatar Apr 02 '18 20:04 satyajeetnim

If you'd like help for something like this, please at least use code formatting and proper code indentation to make it easier for others to read.

jeffoneill avatar Apr 06 '18 11:04 jeffoneill

@jeffoneill: Sorry about that, I have updated the original description to add code formatting and indentation to it. Thank !!!

satyajeetnim avatar Apr 07 '18 20:04 satyajeetnim

@satyajeetnim Any news on this? It seems I am facing the same issue here.

greole avatar Jun 14 '18 07:06 greole

Same problem here! Any news?

matiaskochlowski avatar Apr 05 '19 19:04 matiaskochlowski

Use signals as described here: https://flask-security.readthedocs.io/en/latest/api.html?highlight=signals

where you initialize your app register a handler: user_registered.connect(myuser.user_registered_sighandler, self)

That handler might look something like:

def user_registered_sighandler(sender, **extra):
    # Add additional fields to new user - basic role
    user = extra.get('user')
    sender.db.add_role_to_user(user, 'basic')
    sender.db.db.session.commit()

As for a create date - you can do that in your model __init__:

    def __init__(self, **kwargs):
        """
        :param kwargs: other init keys
        """
        self.create_datetime = datetime.datetime.utcnow()
        self.update_datetime = datetime.datetime.utcnow()
        # overwrite with passed kwargs
        super().__init__(**kwargs)

jwag956 avatar May 06 '19 17:05 jwag956