コード例 #1
0
ファイル: views.py プロジェクト: jhpyle/docassemble
def invite():
    """ Allows users to send invitations to register an account """
    user_manager = current_app.user_manager

    next = request.args.get('next',
                            _endpoint_url(user_manager.after_invite_endpoint))

    user_role = Role.query.filter_by(name='user').first()
    invite_form = MyInviteForm(request.form)
    invite_form.role_id.choices = [(text_type(r.id), text_type(r.name)) for r in db.session.query(Role).filter(and_(Role.name != 'cron', Role.name != 'admin')).order_by('name')]
    invite_form.role_id.default = text_type(user_role.id)
    if text_type(invite_form.role_id.data) == 'None':
        invite_form.role_id.data = text_type(user_role.id)
    if request.method=='POST' and invite_form.validate():
        email = invite_form.email.data

        the_role_id = None
        
        for role in Role.query.order_by('id'):
            if role.id == int(invite_form.role_id.data) and role.name != 'admin' and role.name != 'cron':
                the_role_id = role.id

        if the_role_id is None:
            the_role_id = user_role.id

        user, user_email = user_manager.find_user_by_email(email)
        if user:
            flash(word("A user with that e-mail has already registered"), "error")
            return redirect(url_for('invite'))
        else:
            user_invite = MyUserInvitation(email=email, role_id=the_role_id, invited_by_user_id=current_user.id)
            db.session.add(user_invite)
            db.session.commit()
        token = user_manager.generate_token(user_invite.id)
        accept_invite_link = url_for('user.register',
                                     token=token,
                                     _external=True)

        user_invite.token = token
        db.session.commit()
        #docassemble.webapp.daredis.clear_user_cache()
        try:
            logmessage("Trying to send e-mail to " + text_type(user_invite.email))
            emails.send_invite_email(user_invite, accept_invite_link)
        except Exception as e:
            logmessage("Failed to send e-mail")
            db.session.delete(user_invite)
            db.session.commit()
            #docassemble.webapp.daredis.clear_user_cache()
            flash(word('Unable to send e-mail.  Error was: ') + text_type(e), 'error')
            return redirect(url_for('invite'))
        flash(word('Invitation has been sent.'), 'success')
        return redirect(next)

    return render_template('flask_user/invite.html', version_warning=None, bodyclass='adminbody', page_title=word('Invite User'), tab_title=word('Invite User'), form=invite_form)
コード例 #2
0
ファイル: views.py プロジェクト: simon-tietze/docassemble
def invite():
    """ Allows users to send invitations to register an account """
    user_manager = current_app.user_manager

    next = request.args.get('next',
                            _endpoint_url(user_manager.after_invite_endpoint))

    user_role = Role.query.filter_by(name='user').first()
    invite_form = MyInviteForm(request.form)
    invite_form.role_id.choices = [(text_type(r.id), text_type(r.name)) for r in db.session.query(Role).filter(and_(Role.name != 'cron', Role.name != 'admin')).order_by('name')]
    invite_form.role_id.default = text_type(user_role.id)
    if text_type(invite_form.role_id.data) == 'None':
        invite_form.role_id.data = text_type(user_role.id)
    if request.method=='POST' and invite_form.validate():
        email = invite_form.email.data

        the_role_id = None
        
        for role in Role.query.order_by('id'):
            if role.id == int(invite_form.role_id.data) and role.name != 'admin' and role.name != 'cron':
                the_role_id = role.id

        if the_role_id is None:
            the_role_id = user_role.id

        user, user_email = user_manager.find_user_by_email(email)
        if user:
            flash(word("A user with that e-mail has already registered"), "error")
            return redirect(url_for('invite'))
        else:
            user_invite = MyUserInvitation(email=email, role_id=the_role_id, invited_by_user_id=current_user.id)
            db.session.add(user_invite)
            db.session.commit()
        token = user_manager.generate_token(user_invite.id)
        accept_invite_link = url_for('user.register',
                                     token=token,
                                     _external=True)

        user_invite.token = token
        db.session.commit()
        #docassemble.webapp.daredis.clear_user_cache()
        try:
            logmessage("Trying to send e-mail to " + text_type(user_invite.email))
            emails.send_invite_email(user_invite, accept_invite_link)
        except Exception as e:
            logmessage("Failed to send e-mail")
            db.session.delete(user_invite)
            db.session.commit()
            #docassemble.webapp.daredis.clear_user_cache()
            flash(word('Unable to send e-mail.  Error was: ') + text_type(e), 'error')
            return redirect(url_for('invite'))
        flash(word('Invitation has been sent.'), 'success')
        return redirect(next)

    return render_template('flask_user/invite.html', version_warning=None, bodyclass='daadminbody', page_title=word('Invite User'), tab_title=word('Invite User'), form=invite_form)
コード例 #3
0
ファイル: views.py プロジェクト: leo62ann/docassemble
def invite():
    """ Allows users to send invitations to register an account """
    setup_translation()
    is_admin = bool(current_user.has_roles('admin'))
    user_manager = current_app.user_manager

    the_next = request.args.get('next',
                                _endpoint_url(user_manager.after_invite_endpoint))

    user_role = db.session.execute(select(Role).filter_by(name='user')).scalar_one()
    invite_form = MyInviteForm(request.form)
    if is_admin:
        invite_form.role_id.choices = [(str(r.id), str(r.name)) for r in db.session.execute(select(Role.id, Role.name).where(and_(Role.name != 'cron', Role.name != 'admin')).order_by('name'))]
    else:
        invite_form.role_id.choices = [(str(r.id), str(r.name)) for r in db.session.execute(select(Role.id, Role.name).where(and_(Role.name != 'cron', Role.name != 'admin', Role.name != 'developer', Role.name != 'advocate')).order_by('name'))]
    if request.method=='POST' and invite_form.validate():
        email_addresses = []
        for email_address in re.split(r'[\n\r]+', invite_form.email.data.strip()):
            (part_one, part_two) = email.utils.parseaddr(email_address)
            email_addresses.append(part_two)

        the_role_id = None

        for role in db.session.execute(select(Role).order_by('id')).scalars():
            if not is_admin and role.name in ('admin', 'developer', 'advocate'):
                continue
            if role.id == int(invite_form.role_id.data) and role.name != 'admin' and role.name != 'cron':
                the_role_id = role.id

        if the_role_id is None:
            the_role_id = user_role.id

        has_error = False
        for email_address in email_addresses:
            user, user_email = user_manager.find_user_by_email(email_address)
            if user:
                flash(word("A user with that e-mail has already registered") + " (" + email_address + ")", "error")
                has_error = True
                continue
            user_invite = MyUserInvitation(email=email_address, role_id=the_role_id, invited_by_user_id=current_user.id)
            db.session.add(user_invite)
            db.session.commit()
            token = user_manager.generate_token(user_invite.id)
            accept_invite_link = url_for('user.register',
                                         token=token,
                                         _external=True)

            user_invite.token = token
            db.session.commit()
            #docassemble.webapp.daredis.clear_user_cache()
            try:
                logmessage("Trying to send invite e-mail to " + str(user_invite.email))
                emails.send_invite_email(user_invite, accept_invite_link)
                logmessage("Sent e-mail invite to " + str(user_invite.email))
            except Exception as e:
                try:
                    logmessage("Failed to send invite e-mail: " + e.__class__.__name__ + ': ' + str(e))
                except:
                    logmessage("Failed to send invite e-mail")
                db.session.delete(user_invite)
                db.session.commit()
                #docassemble.webapp.daredis.clear_user_cache()
                flash(word('Unable to send e-mail.  Error was: ') + str(e), 'error')
                has_error = True
        if has_error:
            return redirect(url_for('invite'))
        if len(email_addresses) > 1:
            flash(word('Invitations have been sent.'), 'success')
        else:
            flash(word('Invitation has been sent.'), 'success')
        return redirect(the_next)
    if invite_form.role_id.data is None:
        invite_form.role_id.process_data(str(user_role.id))
    response = make_response(render_template('flask_user/invite.html', version_warning=None, bodyclass='daadminbody', page_title=word('Invite User'), tab_title=word('Invite User'), form=invite_form), 200)
    response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
    return response