Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

makes grant info calculation imdipident #4582

Merged
merged 3 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions app/grants/migrations/0024_auto_20190605_0147.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 2.1.7 on 2019-06-05 01:47

import logging
from django.db import migrations
from grants.models import Grant

logger = logging.getLogger(__name__)

def forwards(apps, schema_editor):
for grant in Grant.objects.all():
grant.save()

def backwards(apps, schema_editor):
pass

class Migration(migrations.Migration):

dependencies = [
('grants', '0023_auto_20190604_1552'),
]

operations = [
migrations.RunPython(forwards, backwards),
]

30 changes: 26 additions & 4 deletions app/grants/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from django.contrib.postgres.fields import ArrayField, JSONField
from django.db import models
from django.db.models import Q
from django.db.models.signals import pre_save
from django.dispatch import receiver
from django.utils import timezone
from django.utils.timezone import localtime
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -659,14 +661,17 @@ def get_converted_amount(self):
return None

def get_converted_monthly_amount(self):
converted_amount = self.get_converted_amount()
if self.num_tx_approved == 1:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats this line for? Checking if its a one-time contribution?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

return Decimal(0)

converted_amount = self.get_converted_amount()
total_sub_seconds = Decimal(self.real_period_seconds) * Decimal(self.num_tx_approved)
one_month = 30 * 24 * 60 * 60

if total_sub_seconds < 2592000:
if total_sub_seconds < one_month:
result = Decimal(converted_amount * Decimal(self.num_tx_approved))
elif total_sub_seconds >= 2592000:
result = Decimal(converted_amount * (Decimal(2592000) / Decimal(self.real_period_seconds)))
elif total_sub_seconds >= one_month:
result = Decimal(converted_amount * (Decimal(one_month) / Decimal(self.real_period_seconds)))

return result

Expand Down Expand Up @@ -700,6 +705,23 @@ def successful_contribution(self, tx_id):
return contribution


@receiver(pre_save, sender=Grant, dispatch_uid="psave_grant")
def psave_grant(sender, instance, **kwargs):

instance.amount_received = 0
instance.monthly_amount_subscribed = 0
#print(instance.id)
for subscription in instance.subscriptions.filter(error=False):
value_usdt = subscription.get_converted_amount()
for contrib in subscription.subscription_contribution.filter(success=True):
if value_usdt:
instance.amount_received += Decimal(value_usdt)

if subscription.num_tx_processed <= subscription.num_tx_approved and value_usdt:
instance.monthly_amount_subscribed += subscription.get_converted_monthly_amount()
#print("-", subscription.id, value_usdt, instance.monthly_amount_subscribed )


class ContributionQuerySet(models.QuerySet):
"""Define the Contribution default queryset and manager."""

Expand Down