Пример #1
0
def update_password(email):
    """
    To update password with authenticated user
    :param email: current login user
    :return: if form is submitted, redirect to users.account
    At default, render update_password.html, title, form
    """
    select_group = current_user.get_current_group()
    user_list = current_user.user_groups
    form = UpdatePasswordForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=email).first()
        if user and bcrypt.check_password_hash(user.password,
                                               form.oldpassword.data):
            hashed_password = bcrypt.generate_password_hash(
                form.password.data).decode('utf-8')
            current_user.password = hashed_password
            db.session.commit()
            flash('Your password has been updated', 'success')
            return redirect(url_for('users.account'))

    return render_template('update_password.html',
                           title='Update Password',
                           form=form,
                           user_list=user_list,
                           select_group=int(select_group))
Пример #2
0
def show_groups():
    """
    To update the groups
    :return:
    """
    select_group = current_user.get_current_group()
    user_list = current_user.user_groups

    form = GroupForm()

    if form.validate_on_submit():
        check_group = Group.query.filter_by(group_name=form.group_name.data,
                                            created_by=current_user.id).all()
        if check_group:
            flash('Group name is already exist', 'warning')
        else:
            group = Group(group_name=form.group_name.data,
                          created_by=current_user.id)
            current_user.user_groups.append(group)
            db.session.commit()
            flash('The group has been created', 'success')
            return redirect(url_for('users.show_groups'))
    groups = [r.to_dict() for r in current_user.user_groups]

    return render_template('show_groups.html',
                           title='Groups List',
                           groups=groups,
                           user_list=user_list,
                           select_group=int(select_group),
                           form=form)
Пример #3
0
def update_groups(group_id):
    """
    To update the groups
    :return:
    """

    select_group = current_user.get_current_group()
    user_list = current_user.user_groups
    group = Group.query.filter_by(id=group_id).first()
    form = GroupForm()
    invite_form = RequestInviteForm()
    if invite_form.validate_on_submit():
        user = User.query.filter_by(email=invite_form.email.data).first()
        if user not in group.users:
            group = Group.query.filter_by(id=group_id).first()
            # send email
            # send_invite_email(user.email, group)
            send_invite_email(invite_form.email.data, group)
            flash('An email has been sent with instructions to invite', 'info')
            return redirect(url_for('users.show_groups'))
        else:
            flash('The email is already member', 'info')
    elif form.validate_on_submit():
        group.group_name = form.group_name.data
        db.session.commit()
        flash('Your group has been updated', 'success')
        return redirect(url_for('users.show_groups'))

    form.group_name.data = group.group_name
    created_by = User.query.filter_by(id=group.created_by).first()
    members = [r.to_dict() for r in group.users.all()]

    return render_template('update_groups.html',
                           title='Update Group',
                           form=form,
                           group=group,
                           user_list=user_list,
                           select_group=int(select_group),
                           invite_form=invite_form,
                           created_by=created_by,
                           members=members)
Пример #4
0
def upload_recipe():
    """
    To upload the list of recipes
    :return:
    """
    select_group = current_user.get_current_group()
    user_list = current_user.user_groups

    if request.method == 'POST':
        f = request.files['send_file']
        data_xls = pd.read_excel(f)
        for index, data in data_xls.iterrows():
            if data.isnull().any():
                flash('Blank is not allowed, please review the uploaded list',
                      'warning')
                return redirect(url_for('uploads.upload_recipe'))

            user_attr = current_user.to_dict()

            def check_group():
                for group_list in user_attr['user_groups']:
                    if group_list['group_name'] == data['group_name']:
                        return group_list['id']

                return None

            group_id = check_group()

            if not group_id:
                group = Group(group_name=data['group_name'])
                current_user.user_groups.append(group)
                db.session.commit()
                group_id = group.id

            category = Category.query.filter_by(group_id=group_id).\
                filter_by(category_name=data['category_name']).first()

            if not category:
                category = Category(category_name=data['category_name'],
                                    group_id=group_id)
                db.session.add(category)
                db.session.commit()
            recipe = Recipe(
                recipe_name=data['recipe_name'],
                recipe_url=data['recipe_url'],
                description=data['description'],
                category_id=category.id,
            )
            db.session.add(recipe)

        db.session.commit()
        flash('Your list has been uploaded', 'success')
        return redirect(url_for('main.home'))

    excelformat = url_for('static',
                          filename='recipe_format/' + "recipe_format.xlsx")
    return render_template('upload_recipe.html',
                           title='Upload Recipes',
                           excelformat=excelformat,
                           user_list=user_list,
                           select_group=int(select_group))