class UserAdminForm(forms.Form): add_hat = forms.BooleanField(label="Выдать новую шапку", required=False) new_hat = forms.ChoiceField( label="Выбрать из популярных", choices=[(None, "---")] + [(key, value.get("title")) for key, value in HATS.items()], required=False, ) new_hat_name = forms.CharField( label="Создать новый титул", max_length=48, required=False ) new_hat_icon = ImageUploadField( label="Иконка", required=False, resize=(256, 256), ) new_hat_color = forms.CharField( label="Цвет", initial="#000000", max_length=16, required=False ) remove_hat = forms.BooleanField( label="Удалить текущую шапку", required=False ) new_achievement = forms.ChoiceField( label="Выдать новую ачивку", choices=[(None, "---")] + [(key, value.get("name")) for key, value in ACHIEVEMENTS], required=False, ) is_banned = forms.BooleanField( label="Забанить", required=False ) ban_days = forms.IntegerField( label="Бан истечет через N дней", initial=5, required=False ) ban_reason = forms.CharField( label="Причина бана", max_length=128, required=False ) is_unbanned = forms.BooleanField( label="Разбанить", required=False ) is_rejected = forms.BooleanField( label="Размодерирвать", required=False )
def do_user_admin_actions(request, user, data): if not request.me.is_moderator: raise AccessDenied() if user.is_god and not settings.DEBUG: raise AccessDenied(title="На этого юзера нельзя псить") # Hats if data["remove_hat"]: user.hat = None user.save() if data["add_hat"]: if data["new_hat"]: hat = HATS.get(data["new_hat"]) if hat: user.hat = {"code": data["new_hat"], **hat} user.save() else: user.hat = { "code": "custom", "title": data["new_hat_name"], "icon": data["new_hat_icon"], "color": data["new_hat_color"], } user.save() # Achievements if data["new_achievement"]: achievement = Achievement.objects.filter( code=data["new_achievement"]).first() if achievement: UserAchievement.objects.get_or_create( user=user, achievement=achievement, ) # Ban if data["is_banned"]: user.is_banned_until = datetime.utcnow() + timedelta( days=data["ban_days"]) user.save() # TODO: send email/bot with data["ban_reason"] # Unban if data["is_unbanned"]: user.is_banned_until = None user.save() # Unmoderate if data["is_rejected"]: user.moderation_status = User.MODERATION_STATUS_REJECTED user.save() return redirect("profile", user.slug)
def do_user_admin_actions(request, user, data): if not request.me.is_moderator: raise AccessDenied() if user.is_god and not settings.DEBUG: raise AccessDenied(title="На этого юзера нельзя псить") # Hats if data["remove_hat"]: user.hat = None user.save() if data["add_hat"]: if data["new_hat"]: hat = HATS.get(data["new_hat"]) if hat: user.hat = {"code": data["new_hat"], **hat} user.save() else: user.hat = { "code": "custom", "title": data["new_hat_name"], "icon": data["new_hat_icon"], "color": data["new_hat_color"], } user.save() # Achievements if data["add_achievement"]: achievement = ACHIEVEMENTS.get(data["new_achievement"]) if achievement: UserBadge.objects.get_or_create( user=user, type=data["new_achievement"], defaults=dict( name=achievement["title"], image=achievement["icon"], description=achievement.get("description"), style=achievement.get("style"), ), ) # Ban if data["is_banned"]: user.is_banned_until = datetime.utcnow() + timedelta( days=data["ban_days"] or 9999999) user.save() # TODO: send email/bot with data["ban_reason"] # Unmoderate if data["is_rejected"]: user.is_profile_rejected = True user.save() return redirect("profile", user.slug)
def do_user_admin_actions(request, user, data): if not request.me.is_moderator: raise AccessDenied() # Hats if data["remove_hat"]: user.hat = None user.save() if data["add_hat"]: if data["new_hat"]: hat = HATS.get(data["new_hat"]) if hat: user.hat = {"code": data["new_hat"], **hat} user.save() else: user.hat = { "code": "custom", "title": data["new_hat_name"], "icon": data["new_hat_icon"], "color": data["new_hat_color"], } user.save() # Achievements if data["new_achievement"]: achievement = Achievement.objects.filter( code=data["new_achievement"]).first() if achievement: UserAchievement.objects.get_or_create( user=user, achievement=achievement, ) # Ban if data["is_banned"]: if not user.is_god: user.is_banned_until = datetime.utcnow() + timedelta( days=data["ban_days"]) user.save() if data["ban_days"] > 0: send_banned_email(user, days=data["ban_days"], reason=data["ban_reason"]) # Unban if data["is_unbanned"]: user.is_banned_until = None user.save() # Unmoderate if data["is_rejected"]: user.moderation_status = User.MODERATION_STATUS_REJECTED user.save() send_unmoderated_email(user) # Ping if data["ping"]: send_ping_email(user, message=data["ping"]) notify_user_ping(user, message=data["ping"]) notify_admin_user_ping(user, message=data["ping"]) return redirect("profile", user.slug)
class UserAdminForm(forms.Form): role_action = forms.ChoiceField( label="Выбрать действие", choices=[(None, "---"), ("add", "Добавить роль"), ("delete", "Удалить роль")], required=False, ) role = forms.ChoiceField( label="Выбрать роль", choices=[(None, "---")] + User.ROLES, required=False, ) add_hat = forms.BooleanField(label="Выдать новую шапку", required=False) new_hat = forms.ChoiceField( label="Выбрать из популярных", choices=[(None, "---")] + [(key, value.get("title")) for key, value in HATS.items()], required=False, ) new_hat_name = forms.CharField(label="Создать новый титул", max_length=48, required=False) new_hat_icon = ImageUploadField( label="Иконка", required=False, resize=(256, 256), ) new_hat_color = forms.CharField(label="Цвет", initial="#000000", max_length=16, required=False) remove_hat = forms.BooleanField(label="Удалить текущую шапку", required=False) new_achievement = forms.ChoiceField( label="Выдать новую ачивку", choices=[(None, "---")] + [(key, value.get("name")) for key, value in ACHIEVEMENTS], required=False, ) is_banned = forms.BooleanField(label="Забанить", required=False) ban_days = forms.IntegerField(label="Бан истечет через N дней", initial=5, required=False) ban_reason = forms.CharField( label="Причина бана", max_length=5000, required=False, widget=forms.Textarea(attrs={"maxlength": 5000}), ) is_rejected = forms.BooleanField(label="Размодерирвать", required=False) delete_account = forms.BooleanField( label="Удалить аккаунт и обнулить подписку", required=False) ping = forms.CharField( label="Отправить сообщение", max_length=5000, widget=forms.Textarea(), required=False, ) add_membership_days = forms.IntegerField(label="Добавить дней членства", required=False)
def do_user_admin_actions(request, user, data): if not request.me.is_moderator: raise AccessDenied() # Hats if data["remove_hat"]: user.hat = None user.save() if data["add_hat"]: if data["new_hat"]: hat = HATS.get(data["new_hat"]) if hat: user.hat = {"code": data["new_hat"], **hat} user.save() else: user.hat = { "code": "custom", "title": data["new_hat_name"], "icon": data["new_hat_icon"], "color": data["new_hat_color"], } user.save() # Achievements if data["new_achievement"]: achievement = Achievement.objects.filter( code=data["new_achievement"]).first() if achievement: UserAchievement.objects.get_or_create( user=user, achievement=achievement, ) # Ban if data["is_banned"]: if not user.is_god: user.is_banned_until = datetime.utcnow() + timedelta( days=data["ban_days"]) user.save() if data["ban_days"] > 0: send_banned_email(user, days=data["ban_days"], reason=data["ban_reason"]) # Unban if data["is_unbanned"]: user.is_banned_until = None user.save() # Unmoderate if data["is_rejected"]: user.moderation_status = User.MODERATION_STATUS_REJECTED user.save() send_unmoderated_email(user) notify_admin_user_unmoderate(user) # Delete account if data["delete_account"] and request.me.is_god: user.membership_expires_at = datetime.utcnow() user.is_banned_until = datetime.utcnow() + timedelta(days=5000) # cancel recurring payments cancel_all_stripe_subscriptions(user.stripe_id) # mark user for deletion user.deleted_at = datetime.utcnow() user.save() # remove sessions Session.objects.filter(user=user).delete() # notify user send_delete_account_confirm_email(user=user, ) # notify admins send_telegram_message( chat=ADMIN_CHAT, text= f"💀 Юзер был удален админами: {settings.APP_HOST}/user/{user.slug}/", ) # Ping if data["ping"]: send_ping_email(user, message=data["ping"]) notify_user_ping(user, message=data["ping"]) notify_admin_user_ping(user, message=data["ping"]) return redirect("profile", user.slug)
def do_user_admin_actions(request, user, data): if not request.me.is_moderator: raise AccessDenied() # Roles if data["role"] and is_role_manageable_by_user(data["role"], request.me): role = data["role"] if data["role_action"] == "add" and role not in user.roles: user.roles.append(role) user.save() if data["role_action"] == "delete" and role in user.roles: user.roles.remove(role) user.save() # Hats if data["remove_hat"]: user.hat = None user.save() if data["add_hat"]: if data["new_hat"]: hat = HATS.get(data["new_hat"]) if hat: user.hat = {"code": data["new_hat"], **hat} user.save() else: user.hat = { "code": "custom", "title": data["new_hat_name"], "icon": data["new_hat_icon"], "color": data["new_hat_color"], } user.save() # Achievements if data["new_achievement"]: achievement = Achievement.objects.filter( code=data["new_achievement"]).first() if achievement: UserAchievement.objects.get_or_create( user=user, achievement=achievement, ) # Ban if data["is_banned"]: if not user.is_god: user.is_banned_until = datetime.utcnow() + timedelta( days=data["ban_days"]) user.save() if data["ban_days"] > 0: send_banned_email(user, days=data["ban_days"], reason=data["ban_reason"]) notify_admin_user_on_ban(user, days=data["ban_days"], reason=data["ban_reason"]) # Unmoderate if data["is_rejected"]: user.moderation_status = User.MODERATION_STATUS_REJECTED user.save() send_unmoderated_email(user) notify_admin_user_unmoderate(user) # Delete account if data["delete_account"] and request.me.is_god: user.membership_expires_at = datetime.utcnow() user.is_banned_until = datetime.utcnow() + timedelta(days=5000) # cancel recurring payments cancel_all_stripe_subscriptions(user.stripe_id) # mark user for deletion user.deleted_at = datetime.utcnow() user.save() # remove sessions Session.objects.filter(user=user).delete() # notify user send_delete_account_confirm_email(user=user, ) # notify admins send_telegram_message( chat=ADMIN_CHAT, text= f"💀 Юзер был удален админами: {settings.APP_HOST}/user/{user.slug}/", ) # Ping if data["ping"]: send_ping_email(user, message=data["ping"]) notify_user_ping(user, message=data["ping"]) notify_admin_user_ping(user, message=data["ping"]) # Add more days of membership if data["add_membership_days"] and int(data["add_membership_days"]) > 0: gift_membership_days( days=data["add_membership_days"], from_user=request.me, to_user=user, deduct_from_original_user=False, ) send_telegram_message( chat=ADMIN_CHAT, text= f"🎁 <b>Юзеру {user.slug} добавили {data['add_membership_days']} дней членства</b>", ) return redirect("profile", user.slug)