You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.7 KiB
Python
100 lines
3.7 KiB
Python
3 years ago
|
# Generated by Django 3.2.4 on 2021-07-21 19:31
|
||
|
|
||
|
from datetime import timedelta
|
||
|
from django.conf import settings
|
||
|
from django.db import migrations, models
|
||
|
import django.db.models.deletion
|
||
|
import jsonfield.fields
|
||
|
|
||
|
def field_to_plan_id(model_name, field_name):
|
||
|
def fun(apps, schema_editor):
|
||
|
keys = settings.PLANS.keys()
|
||
|
model = apps.get_model('payments', model_name)
|
||
|
for s in model.objects.all():
|
||
|
d = getattr(s, field_name)
|
||
|
if s.plan_id is not None:
|
||
|
continue
|
||
|
s.plan_id = str(round(d / timedelta(days=30))) + 'm'
|
||
|
if s.plan_id not in keys:
|
||
|
raise Exception(f"unknown plan: {s.plan_id}")
|
||
|
s.save()
|
||
|
return fun
|
||
|
|
||
|
|
||
|
class Migration(migrations.Migration):
|
||
|
|
||
|
dependencies = [
|
||
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||
|
('payments', '0007_auto_20201114_1730'),
|
||
|
]
|
||
|
|
||
|
operations = [
|
||
|
migrations.RenameField("Subscription", 'period', 'plan_id'),
|
||
|
|
||
|
# Add plan_id to payments and convert from the time field
|
||
|
migrations.AddField(
|
||
|
model_name='payment',
|
||
|
name='plan_id',
|
||
|
field=models.CharField(choices=[('1m', 'Every 1 month'), ('3m', 'Every 3 months'), ('6m', 'Every 6 months'), ('12m', 'Every 12 months')], max_length=16, null=True),
|
||
|
),
|
||
|
migrations.RunPython(field_to_plan_id('Payment', 'time'), lambda x, y: ()),
|
||
|
|
||
|
# Make those two columns non-null once converted
|
||
|
migrations.AlterField(
|
||
|
model_name='payment',
|
||
|
name='plan_id',
|
||
|
field=models.CharField(choices=[('1m', 'Every 1 month'), ('3m', 'Every 3 months'), ('6m', 'Every 6 months'), ('12m', 'Every 12 months')], max_length=16),
|
||
|
),
|
||
|
migrations.AlterField(
|
||
|
model_name='subscription',
|
||
|
name='plan_id',
|
||
|
field=models.CharField(choices=[('1m', 'Every 1 month'), ('3m', 'Every 3 months'), ('6m', 'Every 6 months'), ('12m', 'Every 12 months')], max_length=16),
|
||
|
),
|
||
|
|
||
|
migrations.AddField(
|
||
|
model_name='payment',
|
||
|
name='ip_address',
|
||
|
field=models.GenericIPAddressField(blank=True, null=True),
|
||
|
),
|
||
|
migrations.AddField(
|
||
|
model_name='payment',
|
||
|
name='refund_date',
|
||
|
field=models.DateTimeField(blank=True, null=True),
|
||
|
),
|
||
|
migrations.AddField(
|
||
|
model_name='payment',
|
||
|
name='refund_text',
|
||
|
field=models.CharField(blank=True, max_length=200),
|
||
|
),
|
||
|
migrations.AddField(
|
||
|
model_name='subscription',
|
||
|
name='next_payment_date',
|
||
|
field=models.DateTimeField(blank=True, null=True),
|
||
|
),
|
||
|
migrations.AlterField(
|
||
|
model_name='payment',
|
||
|
name='backend_data',
|
||
|
field=jsonfield.fields.JSONField(blank=True),
|
||
|
),
|
||
|
migrations.AlterField(
|
||
|
model_name='payment',
|
||
|
name='backend_id',
|
||
|
field=models.CharField(choices=[('bitcoin', 'Bitcoin'), ('paypal', 'PayPal'), ('stripe', 'Stripe')], max_length=16),
|
||
|
),
|
||
|
migrations.AlterField(
|
||
|
model_name='payment',
|
||
|
name='user',
|
||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
|
||
|
),
|
||
|
migrations.AlterField(
|
||
|
model_name='subscription',
|
||
|
name='backend_data',
|
||
|
field=jsonfield.fields.JSONField(blank=True),
|
||
|
),
|
||
|
migrations.AlterField(
|
||
|
model_name='subscription',
|
||
|
name='backend_id',
|
||
|
field=models.CharField(choices=[('bitcoin', 'Bitcoin'), ('paypal', 'PayPal'), ('stripe', 'Stripe')], max_length=16),
|
||
|
),
|
||
|
]
|