示例#1
0
def delete_report(report_id):
    """Delete the specified report"""
    report = Report.query.get(report_id)
    if report is None:
        flash(
            "Report not found!",
            "alert-warning",
        )
    elif not current_user.is_admin and not report.user.id == current_user.id:
        flash(
            "You don't have permission to delete that.",
            "alert-warning",
        )
    else:
        # Before deleting the report, check to see if any other users have
        # favorited this report. If so, simply transfer ownership to them
        current_user.unfavorite(report)
        if report.favorite_users:
            user = report.favorite_users[0]
            report.user = user
            db.session.commit()
            flash(
                "Report ownership was transferred to {{ user.name }} since "
                "the report was in that user's favorites list.",
                "alert-success",
            )
        else:
            db.session.delete(report)
            db.session.commit()
            flash(
                "Report deleted",
                "alert-success",
            )
    return redirect(request.args.get('next') or url_for('reports.my_reports'))
示例#2
0
def delete_report(report_id):
    """Delete the specified report"""
    report = Report.query.get(report_id)
    if report is None:
        flash(
            "Report not found!",
            "alert-warning",
        )
    elif not current_user.is_admin and not report.user.id == current_user.id:
        flash(
            "You don't have permission to delete that.",
            "alert-warning",
        )
    else:
        # Before deleting the report, check to see if any other users have
        # favorited this report. If so, simply transfer ownership to them
        current_user.unfavorite(report)
        if report.favorite_users:
            user = report.favorite_users[0]
            report.user = user
            db.session.commit()
            flash(
                "Report ownership was transferred to {{ user.name }} since "
                "the report was in that user's favorites list.",
                "alert-success",
            )
        else:
            db.session.delete(report)
            db.session.commit()
            flash(
                "Report deleted",
                "alert-success",
            )
    return redirect(request.args.get('next') or url_for('reports.my_reports'))
示例#3
0
def favorite(note_id):
    note = Note.query.filter_by(id=note_id).first()
    if not current_user.get_favorite(note):
        current_user.favorite(note)
    else:
        current_user.unfavorite(note)
    db.session.commit()
    return redirect(request.referrer)
示例#4
0
def unfavorite(topic_id):
    topic = Topic.query.filter_by(id=topic_id).first()
    if topic is None:
        flash('话题不存在!')
        return redirect(url_for('.index'))
    if not current_user.is_favoriting(topic):
        flash('您未收藏该话题!')
        return redirect(url_for('.topic', id=topic_id))
    current_user.unfavorite(topic)
    flash('取消成功!')
    return redirect(url_for('.topic', id=topic_id))
示例#5
0
def unfavorite(project_id):
    project = Project.query.filter_by(id=project_id).first()
    if project is None:
        flash('Project not found.')
        return redirect(url_for('project.project', project_id=project_id))
    if not current_user.is_favorited(project):
        flash('You already unfavorited this project.')
        return redirect(url_for('project.project', project_id=project_id))
    current_user.unfavorite(project)
    db.session.commit()
    flash('You unfavorited this project!')
    return redirect(url_for('project.project', project_id=project_id))
示例#6
0
def unfavorite(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash(_('User %(username)s not found.', username=username))
        return redirect(url_for('main.index'))
    if user == current_user:
        flash('You cannot unfavorite yourself!')
        return redirect(url_for('main.user', username=username))
    current_user.unfavorite(user)
    db.session.commit()
    flash(
        _('User %(username)s has been removed from your favorites.',
          username=username))
    return redirect(url_for('main.user', username=username))
示例#7
0
def unfavorite_report(report_id):
    """Remove a report from the user's favorite reports"""
    report = Report.query.get(report_id)
    if report is None:
        flash(
            "No report with that report_id found!",
            "alert-warning",
        )
    else:
        current_user.unfavorite(report)
        db.session.commit()
        flash(
            "Removed Report: {name} from favorites list".format(
                name=report.name, ),
            "alert-success",
        )
    return redirect(request.args.get('next') or url_for('reports.my_reports'))
示例#8
0
def unfavorite_report(report_id):
    """Remove a report from the user's favorite reports"""
    report = Report.query.get(report_id)
    if report is None:
        flash(
            "No report with that report_id found!",
            "alert-warning",
        )
    else:
        current_user.unfavorite(report)
        db.session.commit()
        flash(
            "Removed Report: {name} from favorites list".format(
                name=report.name,
            ),
            "alert-success",
        )
    return redirect(request.args.get('next') or url_for('reports.my_reports'))
示例#9
0
def delete_user(user_id):
    """Delete a user

    First, perform a check that the user is an admin.
    Arguments:
    user_id - The id of the user to be deleted, as defined in the db
    """

    if not current_user.is_admin:
        flash(
            "Sorry! You don't have permission to access that page.",
            "alert-warning",
        )
        return redirect(url_for('default.home'))

    if current_user.id == user_id:
        flash(
            "Now why would you try to delete your own account?",
            "alert-danger",
        )
        return redirect(url_for('admin.home'))

    user = User.query.get(user_id)
    if user is not None:
        for report in user.reports:
            current_user.unfavorite(report)
            if report.favorite_users:
                new_owner = report.favorite_users[0]
                report.user = new_owner
            else:
                db.session.delete(report)
        db.session.delete(user)
        db.session.commit()

        flash(
            "User deleted successfully.",
            "alert-success",
        )

    return redirect(url_for('admin.edit_users'))
示例#10
0
def delete_user(user_id):
    """Delete a user

    First, perform a check that the user is an admin.
    Arguments:
    user_id - The id of the user to be deleted, as defined in the db
    """

    if not current_user.is_admin:
        flash(
            "Sorry! You don't have permission to access that page.",
            "alert-warning",
        )
        return redirect(url_for('default.home'))

    if current_user.id == user_id:
        flash(
            "Now why would you try to delete your own account?",
            "alert-danger",
        )
        return redirect(url_for('admin.home'))

    user = User.query.get(user_id)
    if user is not None:
        for report in user.reports:
            current_user.unfavorite(report)
            if report.favorite_users:
                new_owner = report.favorite_users[0]
                report.user = new_owner
            else:
                db.session.delete(report)
        db.session.delete(user)
        db.session.commit()

        flash(
            "User deleted successfully.",
            "alert-success",
        )

    return redirect(url_for('admin.edit_users'))