def delete_user_data(user: User): if user.deleted_at is None: # user changed his mind return old_slug = str(user.slug) # anonymize user user.slug = random_string(length=32) user.email = f"{user.slug}@deleted.com" user.is_email_unsubscribed = True user.is_email_verified = False user.moderation_status = User.MODERATION_STATUS_REJECTED user.full_name = "💀 Юзер Удалился" user.avatar = None user.company = None user.position = None user.city = None user.country = None user.geo = None user.bio = None user.contact = None user.email_digest_type = User.EMAIL_DIGEST_TYPE_NOPE user.telegram_id = None user.telegram_data = None user.membership_platform_data = None user.save() # delete intro Post.objects.filter(author=user, type=Post.TYPE_INTRO).delete() # delete draft and unpublished posts Post.objects.filter(author=user, is_visible=False).delete() # transfer visible post ownership to "@deleted" user deleted_user = User.objects.filter(slug=settings.DELETED_USERNAME).first() if deleted_user: Post.objects.filter(author=user, is_visible=True).update(author=deleted_user) # replace nickname in replies new_slug = str(user.slug) Comment.objects\ .filter(reply_to__isnull=False, text__contains=f"@{old_slug}")\ .update( text=Replace("text", Value(f"@{old_slug}"), Value(f"@{new_slug}")), html=None ) # drop related data UserAchievement.objects.filter(user=user).delete() UserTag.objects.filter(user=user).delete() UserExpertise.objects.filter(user=user).delete() Session.objects.filter(user=user).delete() Code.objects.filter(user=user).delete() PostBookmark.objects.filter(user=user).delete()
def delete_user_data(user: User): if user.deleted_at is None: # user changed his mind return # anonymize user user.slug = random_string(length=32) user.email = f"{user.slug}@deleted.com" user.is_email_unsubscribed = True user.is_email_verified = False user.moderation_status = User.MODERATION_STATUS_REJECTED user.full_name = "💀 Юзер Удалился" user.avatar = None user.company = None user.position = None user.city = None user.country = None user.geo = None user.bio = None user.contact = None user.email_digest_type = User.EMAIL_DIGEST_TYPE_NOPE user.telegram_id = None user.telegram_data = None user.membership_platform_data = None user.save() # delete intro Post.objects.filter(author=user, type=Post.TYPE_INTRO).delete() # delete draft posts Post.objects.filter(author=user, is_visible=False).delete() # drop related data UserAchievement.objects.filter(user=user).delete() UserTag.objects.filter(user=user).delete() UserExpertise.objects.filter(user=user).delete() Session.objects.filter(user=user).delete() Code.objects.filter(user=user).delete() PostBookmark.objects.filter(user=user).delete() # cancel stripe subscriptions if user.stripe_id: stripe_subscriptions = stripe.Subscription.list( customer=user.stripe_id, limit=100) subscription_ids = [s["id"] for s in stripe_subscriptions["data"]] for subscription_id in subscription_ids: try: stripe.Subscription.delete(subscription_id) except Exception: pass