add new database fields and migrate the data

This commit is contained in:
2021-05-11 07:35:56 +02:00
parent 2575e1e1d1
commit 5ac5f1c2d7
3 changed files with 86 additions and 1 deletions
@@ -0,0 +1,38 @@
# Generated by Django 3.2.2 on 2021-05-11 05:28
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('transactions', '0002_transaction_trade_id'),
]
operations = [
migrations.AddField(
model_name='transaction',
name='crypto_amount',
field=models.DecimalField(blank=True, decimal_places=10, max_digits=24, null=True),
),
migrations.AddField(
model_name='transaction',
name='crypto_fee',
field=models.DecimalField(blank=True, decimal_places=10, max_digits=24, null=True),
),
migrations.AddField(
model_name='transaction',
name='fiat_amount',
field=models.DecimalField(blank=True, decimal_places=10, max_digits=24, null=True),
),
migrations.AddField(
model_name='transaction',
name='fiat_fee',
field=models.DecimalField(blank=True, decimal_places=10, max_digits=24, null=True),
),
migrations.AddField(
model_name='transaction',
name='transaction_type',
field=models.CharField(choices=[('B', 'Buy Crypto'), ('S', 'Sell Crypto'), ('T', 'Transfer Crypto')], default='B', max_length=1),
),
]
@@ -0,0 +1,35 @@
# Generated by Django 3.2 on 2021-05-11 05:03
from django.db import migrations
def separate_fees(apps, schema_editor):
Person = apps.get_model('transactions', 'Transaction')
for tr in Transaction.objects.all():
if tr.amount_before_fee > 0:
tr.crypto_fee = tr.amount_before_fee - tr.amount_after_fee
tr.crypto_amount = tr.amount_before_fee
tr.transaction_type = 'B'
elif tr.amount_before_fee < 0:
tr.crypto_fee = -1 * tr.amount_before_fee + tr.amount_after_fee
tr.crypto_amount = -1 * tr.amount_before_fee
tr.transaction_type = 'S'
else:
tr.crypto_fee = 0
tr.crypto_amount = 0
tr.transaction_type = 'T'
tr.fiat_amount = tr.amount
tr.fiat_fee = 0
tr.save()
class Migration(migrations.Migration):
dependencies = [
('transactions', '0003_auto_20210511_0703'),
]
operations = [
migrations.RunPython(separate_fees),
]
+13 -1
View File
@@ -3,14 +3,26 @@ from django.contrib.auth.models import User
# Create your models here.
class Transaction(models.Model):
TRANSACTION_TYPES = (
('B', 'Buy Crypto'),
('S', 'Sell Crypto'),
('T', 'Transfer Crypto'),
)
trade_id = models.CharField(max_length=20, default='MANUAL')
crypto_currency = models.CharField(max_length=10)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
crypto_currency = models.CharField(max_length=10)
crypto_amount = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True)
crypto_fee = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True)
amount_before_fee = models.DecimalField(max_digits=24, decimal_places=10)
amount_after_fee = models.DecimalField(max_digits=24, decimal_places=10)
exchange_rate = models.DecimalField(max_digits=24, decimal_places=10)
fiat_currency = models.CharField(max_length=10)
fiat_amount = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True)
fiat_fee = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True)
amount = models.DecimalField(max_digits=24, decimal_places=10)
date_valid = models.DateTimeField('date transfered')
transaction_type = models.CharField(max_length=1, choices=TRANSACTION_TYPES, default='B')
class Meta:
db_table = "transaction"