pallets-eco / flask-security

Quick and simple security for Flask applications
MIT License
1.63k stars 513 forks source link

Unable to Add custom fields to Register form #770

Open satyajeetnim opened 6 years ago

satyajeetnim commented 6 years ago

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.

jeffoneill commented 6 years ago

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.

satyajeetnim commented 6 years ago

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

greole commented 6 years ago

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

matiaskochlowski commented 5 years ago

Same problem here! Any news?

jwag956 commented 5 years ago

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)