forked from pythonprobr/pythonpro-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
84 lines (66 loc) · 3.3 KB
/
models.py
File metadata and controls
84 lines (66 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from datetime import timedelta
from django.contrib.auth import get_user_model
from django.contrib.postgres.fields import ArrayField
from django.db import models
_ETERNAL_IN_HUMAM_LIFE_DAYS = 365 * 200
class SubscriptionType(models.Model):
id = models.IntegerField(unique=True, primary_key=True)
name = models.CharField(max_length=128)
email_marketing_tags = ArrayField(models.CharField(max_length=64), default=list)
discourse_groups = ArrayField(models.CharField(max_length=64), default=list)
include_on_cohort = models.BooleanField(default=False, verbose_name='Incluir na última turma')
days_of_access = models.IntegerField(default=_ETERNAL_IN_HUMAM_LIFE_DAYS)
def __str__(self):
return f'Assinatura: {self.name}'
class Meta:
verbose_name = 'Tipo de Assinatura'
verbose_name_plural = 'Tipos de Assinaturas'
class PaymentItemConfigToSubscriptionType(models.Model):
payment_item = models.OneToOneField('django_pagarme.PagarmeItemConfig', null=False, on_delete=models.CASCADE,
related_name='subscription_type_relation')
subscription_type = models.ForeignKey(SubscriptionType, null=False, on_delete=models.CASCADE,
related_name='payment_items_relation')
def __str__(self):
return f'{self.payment_item} -> {self.subscription_type}'
class Subscription(models.Model):
class Meta:
verbose_name = 'Assinatura'
verbose_name_plural = 'Assinaturas'
class Status(models.TextChoices):
ACTIVE = 'A', 'Ativo'
INACTIVE = 'I', 'Inativo'
status = models.CharField(max_length=1, choices=Status.choices)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
days_of_access = models.IntegerField(default=_ETERNAL_IN_HUMAM_LIFE_DAYS)
payment = models.OneToOneField('django_pagarme.PagarmePayment', on_delete=models.DO_NOTHING, null=True, blank=True)
subscription_types = models.ManyToManyField(SubscriptionType, related_name='subscriptions')
subscriber = models.ForeignKey(get_user_model(), on_delete=models.DO_NOTHING, null=True,
related_name='subscriptions')
responsible = models.ForeignKey(get_user_model(), on_delete=models.DO_NOTHING, null=True,
related_name='created_subscriptions')
observation = models.TextField(verbose_name='Observação', blank=True, default='')
activated_at = models.DateTimeField(null=True, default=None)
memberkit_user_id = models.IntegerField(null=True)
@property
def include_on_cohort(self):
return self.subscription_types.filter(include_on_cohort=True).exists()
@property
def expires_at(self):
if self.activated_at:
return self.activated_at + timedelta(days=self.days_of_access)
return '--'
@property
def email_marketing_tags(self):
tags = []
for s in self.subscription_types.all():
tags.extend(s.email_marketing_tags)
return tags
@property
def discourse_groups(self):
groups = []
for s in self.subscription_types.all():
groups.extend(s.discourse_groups)
return groups
def __str__(self):
return f'Assinatura: {self.id} de {self.subscriber}'