Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify programmatically sending invitations #181

Open
matthijskooijman opened this issue Sep 2, 2017 · 4 comments
Open

Simplify programmatically sending invitations #181

matthijskooijman opened this issue Sep 2, 2017 · 4 comments

Comments

@matthijskooijman
Copy link

I'm working on an application that has USER_ENABLE_INVITATION and USER_REQUIRE_INVITATION set. In the initial database, there are no users and an elegant way to bootstrap that would seem to run a flask cli command to send an initial invitation.

With the code shown below, I can send an invitation as follows:

SERVER_NAME=localhost:8000 FLASK_APP=app flask invite info@example.org

However, the below code is quite long and mostly copy-pasted from the views.invite function. It would probably be more elegant if UserManager exposed an invite() method (or something like that), that could be used from the application as well as from views.invite.

Here's what I'm using now:

@app.cli.command('invite')
@click.argument('email')
def initdb_command(email):
    """Invite a user to this project."""
    user_invite = db_adapter.add_object(
                db_adapter.UserInvitationClass,                                
                    email = email,                                             
                    invited_by_user_id = -1,                                   
                )                                                              
    db_adapter.commit()                                                        
    user_invite.token = user_manager.generate_token(user_invite.id)            
    db_adapter.commit()

    accept_invite_link = flask.url_for('user.register', token=user_invite.token, _external=True)
                                                                               
    try:
        # Send 'invite' email                                                  
        flask_user.emails.send_invite_email(user_invite, accept_invite_link)   
    except Exception as e:
        # delete new User object if send fails
        db_adapter.delete_object(user_invite)
        db_adapter.commit()    
        raise
    
    print('Sent invite to ' + email)  
@lingthio
Copy link
Owner

lingthio commented Sep 4, 2017

Nice to find out that someone is actually using the USER_ENABLE_INVITATION and USER_REQUIRE_INVITATION features!

Great minds think alike: Flask-User v0.9 (alpha) and v1.0 (stable) will provide customizable view methods inside a customizable CustomUserManager class. My plan is to offer low level 'building block' methods that views can use. This building blocks will also be available for custom use such as your command line invitation. Stay tuned.

Here is a v0.9 docs preview: http://flask-user.readthedocs.io/en/latest/

@northtree
Copy link

@lingthio , I start from basic_app.py. Flask-User==0.6.19

All the user related URLs are accessible except the user/invite, which returns 404 Not Found. Could I know how to enable user invitation?

@douglasmccormickjr
Copy link

I know this is an old thread, but here goes.

I commented out a raise exception that was preventing the /user/invite page to show... I guess that's for @northtree -- I'll post later the file and line numbers

Now when I invite a user (via email address) I get a 500 code...It's odd since it does do the right flash() response when an email address is already present in the system....looking at the error log on the server, it's point to this:

user_invitation = self.UserInvitationClass(**kwargs)
TypeError: 'NoneType' object is not callable

from here: /site-packages/flask_user/db_manager.py", line 125, in add_user_invitation

any help or ideas would be greatly appreciated...I'm thinking of trying to build my own invite system that sends a URL with an AUTH token into the the Registration page (I'm guessing that's exactly how this setup in flask-user should work but it seems to be a bit buggy here).

Thanks for reading this!
-Doug Jr.

@gilwi
Copy link

gilwi commented Mar 3, 2020

Hi everyone,
@douglasmccormickjr I had a similar issue while I was resetting my project's dependencies. Erasing the database led to a similar behaviour due to cookies being present and database empty.

@lingthio I really enjoy your work here! I read your comment about the upcoming custom classes you'll be introducing.
Are you still considering an invite method as a possible feature or do you prefer to leave this mechanism to each programmer's custom class?
Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants