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
def __add_user(data, is_superuser=False): # Required values email = data["email"] password = data["password"] date_joined = data["date_joined"] if type(date_joined) is str: date_joined = dateutil.parser.parse(date_joined) date_joined = date_joined.astimezone(timezone('UTC')) # Optional data_first_name = data.get("first_name") data_last_name = data.get("last_name") data_is_active = data.get("is_active") data_is_staff = data.get("is_staff") # Check if the email is taken if User.objects(email=email).first() is not None: return validation_error( False, {"email": ["این ایمیل قبلا ثبت شده است."]}), 422 try: new_user = User( first_name=data_first_name, last_name=data_last_name, date_joined=date_joined, email=email, is_active=data_is_active, is_staff=data_is_staff, is_superuser=is_superuser, ) new_user.set_password_hash(password) new_user.save() # Load the new user's info user_info = user_schema.dump(new_user) resp = message(True, "کاربر با موفقیت ایجاد شد.") resp["user"] = user_info return resp, 201 except Exception as error: current_app.logger.error(error) return internal_err_resp()
def create(self, validated_data): # create and store the new user user = User(username=validated_data['username'], first_name=validated_data['first_name'], last_name=validated_data['last_name'], email=validated_data['email'], phone=validated_data['phone'], address=validated_data['address'], type_account=2) # set password hash user.set_password(validated_data['password']) user.save() # if the user admin add the of promo model then will be create if self.promo_data[0] in validated_data: Promo(user=user, **{k: validated_data[k] for k in self.promo_data}).save() return user