Migrate user id to UUID

master
Mark Veidemanis 1 year ago
parent 682c42c0e8
commit fb5521c9f7
Signed by: m
GPG Key ID: 5ACFCEED46C0904F

@ -0,0 +1,47 @@
# Generated by Django 4.1.7 on 2023-02-24 12:20
import uuid
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0073_strategy_active_management_policy'),
]
operations = [
migrations.RemoveField(
model_name='session',
name='plan',
),
migrations.RemoveField(
model_name='session',
name='user',
),
migrations.RenameField(
model_name='user',
old_name='stripe_id',
new_name='billing_provider_id',
),
migrations.RemoveField(
model_name='user',
name='last_payment',
),
migrations.RemoveField(
model_name='user',
name='plans',
),
migrations.AlterField(
model_name='user',
name='id',
field=models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True),
),
migrations.DeleteModel(
name='Plan',
),
migrations.DeleteModel(
name='Session',
),
]

@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import uuid
from django.db import migrations, models
def fill_mymodel_uuid(apps, schema_editor):
db_alias = schema_editor.connection.alias
MyModel = apps.get_model('core', 'User')
for obj in MyModel.objects.using(db_alias).all():
obj.uuid = uuid.uuid4()
obj.save()
class Migration(migrations.Migration):
""" Change model with integer pk to UUID pk. This migration presumes there
are no db constraints (foreign keys) to this table.
"""
dependencies = [
('core', '0074_remove_session_plan_remove_session_user_and_more'),
]
operations = [
migrations.AddField(
model_name='user',
name='uuid',
field=models.UUIDField(null=True),
),
migrations.RunPython(fill_mymodel_uuid, migrations.RunPython.noop),
migrations.AlterField(
model_name='user',
name='uuid',
field=models.UUIDField(default=uuid.uuid4, serialize=False, editable=False, unique=True),
),
migrations.RemoveField('User', 'id'),
migrations.RenameField(
model_name='user',
old_name='uuid',
new_name='id'
),
migrations.AlterField(
model_name='user',
name='id',
field=models.UUIDField(primary_key=True, default=uuid.uuid4, serialize=False, editable=False, unique=True),
),
]

@ -1,15 +1,16 @@
import uuid
from datetime import timedelta
import stripe
from django.conf import settings
# import stripe
# from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.db import models
from core.exchanges.alpaca import AlpacaExchange
from core.exchanges.fake import FakeExchange
from core.exchanges.oanda import OANDAExchange
from core.lib.customers import get_or_create, update_customer_fields
# from core.lib.customers import get_or_create, update_customer_fields
from core.util import logs
log = logs.get_logger(__name__)
@ -79,61 +80,36 @@ ADJUST_CLOSE_NOTIFY_CHOICES = (
)
class Plan(models.Model):
name = models.CharField(max_length=255, unique=True)
description = models.CharField(max_length=1024, null=True, blank=True)
cost = models.IntegerField()
product_id = models.CharField(max_length=255, unique=True, null=True, blank=True)
image = models.CharField(max_length=1024, null=True, blank=True)
# class Plan(models.Model):
# name = models.CharField(max_length=255, unique=True)
# description = models.CharField(max_length=1024, null=True, blank=True)
# cost = models.IntegerField()
# product_id = models.CharField(max_length=255, unique=True, null=True, blank=True)
# image = models.CharField(max_length=1024, null=True, blank=True)
def __str__(self):
return f"{self.name}{self.cost})"
# def __str__(self):
# return f"{self.name} (£{self.cost})"
class User(AbstractUser):
# Stripe customer ID
stripe_id = models.CharField(max_length=255, null=True, blank=True)
last_payment = models.DateTimeField(null=True, blank=True)
plans = models.ManyToManyField(Plan, blank=True)
id = models.UUIDField(
default=uuid.uuid4, primary_key=True, editable=False, unique=True
)
# stripe_id = models.CharField(max_length=255, null=True, blank=True)
billing_provider_id = models.CharField(max_length=255, null=True, blank=True)
# last_payment = models.DateTimeField(null=True, blank=True)
# plans = models.ManyToManyField(Plan, blank=True)
email = models.EmailField(unique=True)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._original = self
# def __init__(self, *args, **kwargs):
# super().__init__(*args, **kwargs)
# self._original = self
# Override save to update attributes in Lago
def save(self, *args, **kwargs):
"""
Override the save function to create a Stripe customer.
"""
if settings.STRIPE_ENABLED:
if not self.stripe_id: # stripe ID not stored
self.stripe_id = get_or_create(
self.email, self.first_name, self.last_name
)
to_update = {}
if self.email != self._original.email:
to_update["email"] = self.email
if self.first_name != self._original.first_name:
to_update["first_name"] = self.first_name
if self.last_name != self._original.last_name:
to_update["last_name"] = self.last_name
update_customer_fields(self.stripe_id, **to_update)
super().save(*args, **kwargs)
def delete(self, *args, **kwargs):
if settings.STRIPE_ENABLED:
if self.stripe_id:
stripe.Customer.delete(self.stripe_id)
log.info(f"Deleted Stripe customer {self.stripe_id}")
super().delete(*args, **kwargs)
def has_plan(self, plan):
plan_list = [plan.name for plan in self.plans.all()]
return plan in plan_list
def get_notification_settings(self):
return NotificationSettings.objects.get_or_create(user=self)[0]
@ -150,9 +126,6 @@ class Account(models.Model):
supported_symbols = models.JSONField(default=list)
instruments = models.JSONField(default=list)
currency = models.CharField(max_length=255, null=True, blank=True)
# risk_model = models.ForeignKey(
# "core.RiskModel", on_delete=models.SET_NULL, null=True, blank=True
# )
initial_balance = models.FloatField(default=0)
def __str__(self):
@ -211,14 +184,6 @@ class Account(models.Model):
return cls.objects.get(id=account_id)
class Session(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
request = models.CharField(max_length=255, null=True, blank=True)
session = models.CharField(max_length=255, null=True, blank=True)
subscription_id = models.CharField(max_length=255, null=True, blank=True)
plan = models.ForeignKey(Plan, null=True, blank=True, on_delete=models.CASCADE)
class Hook(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=1024)

Loading…
Cancel
Save