示例#1
0
def user_etl(rec):
    if rec == 'header':
        return ['member_number', 'user_name', 'role', 'password']
    user = User(member_id=get_member_id(rec['member_number']),
                user_name=rec['user_name'])
    user.set_password(rec['password'])
    user.role = UserRole.from_name(rec['role'])
    return user
示例#2
0
def save_member_contact_details(member_number, details, renewal, commit=True):
    member = get_member(member_number)
    update_member_details(member, details)
    if not member.user:
        member.user = User(role = UserRole.member, user_name=str(member_number))
    member.user.set_password(User.member_password(details['post_code']))

    member.type = member.member_type_at_renewal()
    if renewal:
        update_member_renewal(member, details)

    member.last_updated = datetime.date.today()

    if commit:
        db.session.commit()
    return member
示例#3
0
def activate_user(key):
    ok, user_id = User.validate_token(current_app, key)
    if ok:
        id, message, message_type = user_id, 'Account successfully activated, login', 'success'
    else:
        id, message, message_type = None, user_id, 'warning'
    return ok, id, message, message_type
示例#4
0
def register_user(member_number, user_name, password, email=None, role=UserRole.member, two_phase=False, activate_url=None):
    user_id = 0
    if user_name and password:
        member = get_member(member_number)
        if member:
            if not member.is_active():
                ok, message, message_type = False, 'Sorry, you are not a current member, please contact [email protected]', 'danger'
            else:
                if not match_string(member.email, email):
                    ok, message, message_type = False, 'Email does not match', 'warning'
                else:
                    user = get_user(user_name=user_name)
                    if user and user.member_id != member.id:
                        ok, message, message_type = False, 'User name already in use', 'warning'
                    else:
                        if role == UserRole.member:
                            ok, message, message_type = member.check_credentials(user_name, password)
                            if not ok:
                                return ok, user_id, message, message_type
                        if not member.user:
                            user = User(user_name=user_name, member_id=member.id)
                        else:
                            user = member.user
                            user.user_name = user_name
                        if not user.check_password(password):
                            ok, message, message_type = True, 'Password updated', 'success'
                        else:
                            if two_phase:
                                token, expires = user.get_token(current_app)
                                send_mail(
                                    to=member.email,
                                    sender='*****@*****.**',
                                    cc=[],
                                    subject='Dons Trust Members - registration',
                                    message=render_template('activate.txt',
                                                              full_url_for=full_url_for,
                                                              member=member,
                                                              token=token,
                                                              expires=expires)
                                )
                                ok, message, message_type = True, 'Activation email sent to {}'.format(member.email), 'success'
                            else:
                                ok, message, message_type = True, 'You are now a registered user', 'success'
                        user.set_password(password)
                        if not user.role:
                            user.role = role
                        # else:
                        #     if not role in [role.role for role in user.roles]:
                        #         user.roles += [Role(role=role)]
                        save_user(user)
                        user_id = user.id
        else:
            ok, message, message_type = False, 'Cannot find your membership', 'danger'
    else:
        ok, message, message_type = False, 'Missing user name/password', 'warning'
    return ok, user_id, message, message_type
示例#5
0
def user_reset_password(role, app, token):
    if current_user.is_authenticated:
        return redirect(full_url(role, 'index'))
    user = User.verify_reset_password_token(app, token)
    if not user:
        return redirect(full_url(role, 'index'))
    form = ResetPasswordForm()
    if form.validate_on_submit():
        user.set_password(form.password.data)
        save_user(user)
        flash('Your password has been reset', 'success')
        return redirect(full_url(role, 'user_login'))
    return render_template('user/reset_password.html', form=form)
示例#6
0
def save_member_details(member_number, details):
    if member_number > 0:
        member = get_member(member_number)
    else:
        member = get_new_member()
    update_member_details(member, details)
    if not member.user:
        member.user = User(role = UserRole.member, user_name=str(member_number))
    member.user.set_password(User.member_password(details['post_code']))
    member.status = MemberStatus(details['status'])
    member.start_date = details['start_date']
    member.end_date = details['end_date']
    update_member_payments(member, details)
    update_member_actions(member, details)
    update_member_comments(member, details)

    member.last_updated = datetime.date.today()

    if member.number == 0:
        member.number = next_member_number()
        db.session.add(member)

    db.session.commit()
    return member
示例#7
0
def member_login(next_page, member_number=None, app=None):
    form_name = 'member_login.html'
    form = MemberLoginForm()
    if form.is_submitted():
        if form.hidden_number.data:
            form.number.data = int(form.hidden_number.data)
        if form.validate_on_submit():
            no_number = not member_number
            if no_number:
                member_number = int('0' + get_digits(form.number.data))
            user_name = str(member_number)
            password = User.member_password(form.post_code.data)
            user = get_user(user_name=user_name)
            message = None
            message_type = 'danger'
            if user is None:
                ok, id, message, message_type = member_register(
                    member_number, user_name, password, form.email.data)
                if ok:
                    user = get_user(id=id)
            if not message:
                if user is None:
                    message = 'Email or post code do not match Membership number {}'.format(
                        member_number)
                elif not match_string(user.member.email, form.email.data):
                    message = 'Email does not match the Membership number'
                elif not user.check_password(password):
                    message = 'Post code does not match the Membership number'
            if message:
                flash(message, message_type)
                if not no_number:
                    form.populate(member_number)
                if message_type != 'success':
                    return render_template(form_name,
                                           title='Sign In',
                                           form=form)
            login_user(user, remember=form.remember_me.data)
            if not next_page:
                next_page = 'index'
            return redirect(next_page)
    else:
        form.populate(member_number)
    return render_template(form_name, title='Sign In', form=form)
示例#8
0
def get_new_user(role):
    return User(role=role)