def default_projects(apps, schema):
    User = apps.get_model('users', 'DataOceanUser')
    Project = apps.get_model('payment_system', 'Project')
    # UserProject = apps.get_model('payment_system', 'UserProject')
    ProjectSubscription = apps.get_model('payment_system',
                                         'ProjectSubscription')
    Subscription = apps.get_model('payment_system', 'Subscription')

    for user in User.objects.all():
        default_u2p = user.user_projects.filter(role='owner',
                                                is_default=True).first()

        if not default_u2p:
            new_project = Project.objects.create(
                name=settings.DEFAULT_PROJECT_NAME,
                description=settings.DEFAULT_PROJECT_DESCRIPTION,
                owner=user,
                token=generate_key(),
            )
            new_project.user_projects.create(
                user=user,
                role='owner',
                status='active',
                is_default=True,
            )

            default_subscription, created = Subscription.objects.get_or_create(
                is_default=True,
                defaults={
                    'requests_limit': 1000,
                    'name': settings.DEFAULT_SUBSCRIPTION_NAME,
                    'grace_period': 30,
                },
            )
            date_now = timezone.localdate()
            ProjectSubscription.objects.create(
                project=new_project,
                subscription=default_subscription,
                status='active',
                start_date=date_now,
                requests_left=default_subscription.requests_limit,
                duration=default_subscription.duration,
                grace_period=default_subscription.grace_period,
                expiring_date=date_now +
                timezone.timedelta(days=default_subscription.duration),
            )
def default_projects(apps, schema):
    User = apps.get_model('users', 'DataOceanUser')
    Project = apps.get_model('payment_system', 'Project')
    UserProject = apps.get_model('payment_system', 'UserProject')
    ProjectSubscription = apps.get_model('payment_system',
                                         'ProjectSubscription')
    Subscription = apps.get_model('payment_system', 'Subscription')

    for user in User.objects.all():
        u2p = user.user_projects.filter(role='initiator').first()
        if u2p:
            u2p.is_default = True
            u2p.save()
        else:
            new_project = Project.objects.create(
                name=settings.DEFAULT_PROJECT_NAME,
                description=settings.DEFAULT_PROJECT_DESCRIPTION,
                token=generate_key(),
            )
            UserProject.objects.create(
                project=new_project,
                user=user,
                role='initiator',
                status='active',
                is_default=True,
            )
            def_sub, created = Subscription.objects.get_or_create(
                name=settings.DEFAULT_SUBSCRIPTION_NAME,
                defaults={'requests_limit': 1000},
            )
            ProjectSubscription.objects.create(
                project=new_project,
                subscription=def_sub,
                status='active',
                expiring_date=timezone.localdate() +
                timezone.timedelta(days=30))
Esempio n. 3
0
 def get_token_safe():
     new_key = generate_key()
     if Project.objects.filter(token=new_key).exists():
         return get_token_safe()
     else:
         return new_key
Esempio n. 4
0
 def save(self, *args, **kwargs):
     if not self.token:
         self.token = generate_key()
     super().save(*args, **kwargs)
Esempio n. 5
0
 def refresh_token(self):
     self.token = generate_key()
     self.save(update_fields=['token'])