當前位置: 首頁>>代碼示例>>Python>>正文


Python User.delete_instance方法代碼示例

本文整理匯總了Python中streetsign_server.models.User.delete_instance方法的典型用法代碼示例。如果您正苦於以下問題:Python User.delete_instance方法的具體用法?Python User.delete_instance怎麽用?Python User.delete_instance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在streetsign_server.models.User的用法示例。


在下文中一共展示了User.delete_instance方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: user_edit

# 需要導入模塊: from streetsign_server.models import User [as 別名]
# 或者: from streetsign_server.models.User import delete_instance [as 別名]
def user_edit(userid=-1):
    ''' edit one user.  Admins can edit any user, but other users
        can only edit themselves. if userid is -1, create a new user. '''

    try:
        current_user = user_session.get_user()
    except user_session.NotLoggedIn as e:
        flash("Sorry, you're not logged in!")
        return permission_denied("You're not logged in!")

    userid = int(userid)

    if userid != -1:
        try:
            user = User.get(id=userid)
        except User.DoesNotExist:
            return not_found(title="User doesn't exist",
                             message="Sorry, that user does not exist!")
    else:
        if not current_user.is_admin:
            flash('Sorry! Only admins can create new users!')
            return permission_denied("Admins only!")

        try:
            user = User.get(loginname=request.form['loginname'])
            return permission_denied("Username already exists!")
        except peewee.DoesNotExist:
            pass

        user = User() #pylint: disable=no-value-for-parameter

    if request.method == 'POST':
        if current_user != user and not current_user.is_admin:
            return permission_denied("Sorry, you may not edit this user.")

        update_user(user, request.form, current_user)

        # save:

        try:
            user.save()
            if userid == -1:
                flash('New user created.')
                return redirect(url_for('user_edit', userid=user.id))
            else:
                flash('Saved')

        except peewee.IntegrityError as err:
            flash('Cannot Save:' + str(err))

    elif request.method == 'DELETE':
        if not current_user.is_admin:
            return 'Sorry, only admins can delete users', 403

        if user.id == current_user.id:
            return 'Sorry! You cannot delete yourself!', 403

        user.delete_instance(recursive=True)

        return 'User: %s deleted. (And all their posts)' % user.displayname

    users_posts = Post.select().where(Post.author == user) \
                               .order_by(Post.write_date.desc()) \
                               .limit(10)

    return render_template('user.html',
                           allgroups=Group.select(),
                           posts=users_posts, user=user)
開發者ID:Fivebread,項目名稱:streetsign,代碼行數:70,代碼來源:users_and_auth.py


注:本文中的streetsign_server.models.User.delete_instance方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。