diff options
author | tilly-Q <nattilypigeonfowl@gmail.com> | 2013-08-29 17:31:19 -0400 |
---|---|---|
committer | tilly-Q <nattilypigeonfowl@gmail.com> | 2013-08-29 17:31:19 -0400 |
commit | 1bb367f6136ae4cbcdf6dd86af65eb613913dbd8 (patch) | |
tree | c3565801fcf21de1383b5bdb764208b3a98c8e49 /mediagoblin/moderation/tools.py | |
parent | dfd66b789cd6cc9470c2a98bcbda9ee5e0f3ad0f (diff) | |
download | mediagoblin-1bb367f6136ae4cbcdf6dd86af65eb613913dbd8.tar.lz mediagoblin-1bb367f6136ae4cbcdf6dd86af65eb613913dbd8.tar.xz mediagoblin-1bb367f6136ae4cbcdf6dd86af65eb613913dbd8.zip |
This is a quick commit. I gave admins the ability to ban or unban users
straight from the moderation.users_detail page. I also changed the
UserBan.expiration_date type from DateTime into Date. I also began work on the
Terms of Service, pulled from another website (which will be cited clearly
before I'm done). I added new tests as well for the ban/unbanning. Lastly,
I added a few `user_not_banned` decorators to relevant views, so banned users
cannot access any pages.
Diffstat (limited to 'mediagoblin/moderation/tools.py')
-rw-r--r-- | mediagoblin/moderation/tools.py | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/mediagoblin/moderation/tools.py b/mediagoblin/moderation/tools.py index d58df3a8..49d4381e 100644 --- a/mediagoblin/moderation/tools.py +++ b/mediagoblin/moderation/tools.py @@ -43,11 +43,9 @@ def take_punitive_actions(request, form, report, user): if u'userban' in form.action_to_resolve.data: reason = form.resolution_content.data + \ "<br>"+request.user.username - user_ban = UserBan( - user_id=form.targeted_user.data, + user_ban = ban_user(form.targeted_user.data, expiration_date=form.user_banned_until.data, - reason= form.why_user_was_banned.data - ) + reason=form.why_user_was_banned.data) Session.add(user_ban) if form.user_banned_until.data is not None: @@ -162,3 +160,40 @@ def give_privileges(user,*privileges): return (give_privileges(user, privileges[0]) and \ give_privileges(user, *privileges[1:])) +def ban_user(user_id, expiration_date=None, reason=None): + """ + This function is used to ban a user. If the user is already banned, the + function returns False. If the user is not already banned, this function + bans the user using the arguments to build a new UserBan object. + + :returns False if the user is already banned and the ban is not updated + :returns UserBan object if there is a new ban that was created. + """ + user_ban =UserBan.query.filter( + UserBan.user_id==user_id) + if user_ban.count(): + return False + new_user_ban = UserBan( + user_id=user_id, + expiration_date=expiration_date, + reason=reason) + return new_user_ban + +def unban_user(user_id): + """ + This function is used to unban a user. If the user is not currently banned, + nothing happens. + + :returns True if the operation was completed successfully and the user + has been unbanned + :returns False if the user was never banned. + """ + user_ban = UserBan.query.filter( + UserBan.user_id==user_id) + if user_ban.count() == 0: + return False + user_ban.first().delete() + return True + + + |