diff --git a/BillingApp/models.py b/BillingApp/models.py index 7ea004c..4c7c408 100644 --- a/BillingApp/models.py +++ b/BillingApp/models.py @@ -46,22 +46,17 @@ class SubscribeOrder(BaseModel): 'user': self.user, 'subscribe': self.subscribe, 'last_paid_DT': datetime.now(), - 'paid_period_from_DT': datetime.now(), - 'paid_period_to_DT': datetime.now() + timedelta(hours=self.subscribe.period), 'receive_finish_subscribe_msg': True, - 'enable': True, + # 'enable': True, } - subscribe_for_user = SubscribeForUser.objects.create(**kwargs) + + from SubscribesApp.funcs import create_subscribe_by_data + subscribe_for_user = create_subscribe_by_data(kwargs) self.subscribe_for_user = subscribe_for_user self.enable = False self.save() - subscribes_for_user = SubscribeForUser.objects.filter( - user=self.user - ).exclude( - id=subscribe_for_user.id - ) - subscribes_for_user.update(enable=False) + subscribe_for_user.activate() return self diff --git a/SubscribesApp/funcs.py b/SubscribesApp/funcs.py index 4e4e37e..73b0c4b 100644 --- a/SubscribesApp/funcs.py +++ b/SubscribesApp/funcs.py @@ -4,6 +4,17 @@ from django.utils.translation import get_language, activate from datetime import datetime, timedelta import json +def create_subscribe_by_data(create_kwargs): + subscribe = create_kwargs['subscribe'] + create_kwargs.update({ + 'paid_period_from_DT': datetime.now(), + 'paid_period_to_DT': datetime.now() + timedelta(hours=subscribe.period), + 'enable': False + }) + subscribe_for_user = SubscribeForUser.objects.create(**create_kwargs) + return subscribe_for_user + + def check_option_in_cur_user_subscribe(user, option_name): if not user or not user.is_active or not user.is_authenticated: return False @@ -35,12 +46,13 @@ def subscribe_user_to_null_price_subscribe(user): if not subscribe: return None - subscribe_for_user = SubscribeForUser.objects.create( - user=user, - subscribe=subscribe, - paid_period_from_DT=datetime.now(), - paid_period_to_DT=datetime.now() + timedelta(hours=subscribe.period) - ) + kwargs = { + 'user': user, + 'subscribe': subscribe, + } + + subscribe_for_user = create_subscribe_by_data(kwargs) + subscribe_for_user = subscribe_for_user.activate() return subscribe_for_user @@ -54,6 +66,10 @@ def get_cur_user_subscribe(user): user_subscribe = SubscribeForUser.objects.get(enable=True, user=user) except SubscribeForUser.DoesNotExist: user_subscribe = subscribe_user_to_null_price_subscribe(user) + except SubscribeForUser.MultipleObjectsReturned: + user_subscribes = SubscribeForUser.objects.filter(enable=True, user=user).order_by('-paid_period_to_DT') + user_subscribe = user_subscribes[0] + user_subscribes.exclude(id=user_subscribe.id).delete() return user_subscribe diff --git a/SubscribesApp/models.py b/SubscribesApp/models.py index b687196..dd04b51 100644 --- a/SubscribesApp/models.py +++ b/SubscribesApp/models.py @@ -78,4 +78,17 @@ class SubscribeForUser(BaseModel): if not res: res += f' {str(self.id)}' - return res \ No newline at end of file + return res + + def activate(self): + self.enable = True + self.save(update_fields=['enable']) + + subscribes_for_user = SubscribeForUser.objects.filter( + user=self.user + ).exclude( + id=self.id + ) + subscribes_for_user.update(enable=False) + + return self \ No newline at end of file