From 7a267dbc3fe956e9dc6a48937ed0d3447ac53a61 Mon Sep 17 00:00:00 2001 From: Skia Date: Wed, 20 Jul 2016 00:28:49 +0200 Subject: [PATCH] Create the customer when subscribing if it does not exists yet --- counter/models.py | 4 ++++ subscription/models.py | 12 ++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/counter/models.py b/counter/models.py index f7475f3e..377d4379 100644 --- a/counter/models.py +++ b/counter/models.py @@ -5,6 +5,7 @@ from django.conf import settings from django.core.urlresolvers import reverse from datetime import timedelta +from random import randrange from club.models import Club from accounting.models import CurrencyField @@ -27,6 +28,9 @@ class Customer(models.Model): def __str__(self): return self.user.username + def generate_account_id(): + return randrange(0, 4000) # TODO: improve me! + class ProductType(models.Model): """ This describes a product type diff --git a/subscription/models.py b/subscription/models.py index 38b0ad9b..f2771886 100644 --- a/subscription/models.py +++ b/subscription/models.py @@ -35,10 +35,7 @@ class Subscription(models.Model): # TODO add location! class Meta: - permissions = ( - ('change_subscription', 'Can make someone become a subscriber'), - ('view_subscription', 'Can view who is a subscriber'), - ) + ordering = ['subscription_start',] def clean(self): try: @@ -50,8 +47,11 @@ class Subscription(models.Model): # TODO see SubscriptionForm's clean method raise ValidationError(_("You are trying to create a subscription without member")) - class Meta: - ordering = ['subscription_start',] + def save(self): + super(Subscription, self).save() + from counter.models import Customer + if not Customer.objects.filter(user=self.member).exists(): + Customer(user=self.member, account_id=Customer.generate_account_id(), amount=0).save() def get_absolute_url(self): return reverse('core:user_profile', kwargs={'user_id': self.member.pk})