<s> from django . db import models <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from product . models import Product , get_product_quantity_price , get_product_quantity_adjustments <EOL> from decimal import Decimal <EOL> import datetime <EOL> from satchmo_utils import add_month <EOL> from satchmo_utils . fields import CurrencyField <EOL> SATCHMO_PRODUCT = True <EOL> def get_product_types ( ) : <EOL> return ( '<STR_LIT>' , ) <EOL> class SubscriptionProduct ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> product = models . OneToOneField ( Product , verbose_name = _ ( "<STR_LIT>" ) , primary_key = True ) <EOL> recurring = models . BooleanField ( _ ( "<STR_LIT>" ) , help_text = _ ( "<STR_LIT>" ) , default = False ) <EOL> recurring_times = models . IntegerField ( _ ( "<STR_LIT>" ) , help_text = _ ( "<STR_LIT>" ) , null = True , blank = True ) <EOL> expire_length = models . IntegerField ( _ ( "<STR_LIT>" ) , help_text = _ ( "<STR_LIT>" ) , null = True , blank = True ) <EOL> SUBSCRIPTION_UNITS = ( <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , _ ( '<STR_LIT>' ) ) <EOL> ) <EOL> expire_unit = models . CharField ( _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:5> , choices = SUBSCRIPTION_UNITS , default = "<STR_LIT>" , null = False ) <EOL> SHIPPING_CHOICES = ( <EOL> ( '<STR_LIT:0>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:1>' , _ ( '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:2>' , _ ( '<STR_LIT>' ) ) , <EOL> ) <EOL> is_shippable = models . IntegerField ( _ ( "<STR_LIT>" ) , help_text = _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:1> , choices = SHIPPING_CHOICES ) <EOL> is_subscription = True <EOL> def _get_subtype ( self ) : <EOL> return '<STR_LIT>' <EOL> def __unicode__ ( self ) : <EOL> return self . product . slug <EOL> def _get_fullPrice ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_qty_price ( <NUM_LIT:1> ) <EOL> unit_price = property ( _get_fullPrice ) <EOL> def get_qty_price ( self , qty , show_trial = True , include_discount = True ) : <EOL> """<STR_LIT>""" <EOL> if show_trial : <EOL> trial = self . get_trial_terms ( <NUM_LIT:0> ) <EOL> else : <EOL> trial = None <EOL> if trial : <EOL> price = trial . price * qty <EOL> else : <EOL> if include_discount : <EOL> price = get_product_quantity_price ( self . product , qty ) <EOL> else : <EOL> adjustment = get_product_quantity_adjustments ( self , qty ) <EOL> if adjustment . price is not None : <EOL> price = adjustment . price . price <EOL> else : <EOL> price = None <EOL> if not price and qty == Decimal ( '<STR_LIT:1>' ) : <EOL> price = Decimal ( "<STR_LIT>" ) <EOL> elif not price : <EOL> price = self . product . _get_fullPrice ( ) <EOL> return price <EOL> def recurring_price ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_qty_price ( Decimal ( '<STR_LIT:1>' ) , show_trial = False ) <EOL> def get_trial_terms ( self , trial = None ) : <EOL> """<STR_LIT>""" <EOL> if trial is None : <EOL> return self . trial_set . all ( ) . order_by ( '<STR_LIT:id>' ) <EOL> else : <EOL> try : <EOL> return self . trial_set . all ( ) . order_by ( '<STR_LIT:id>' ) [ trial ] <EOL> except IndexError : <EOL> return None <EOL> def calc_expire_date ( self , date = None ) : <EOL> if date is None : <EOL> date = datetime . datetime . now ( ) <EOL> if self . expire_unit == "<STR_LIT>" : <EOL> expiredate = date + datetime . timedelta ( days = self . expire_length ) <EOL> else : <EOL> expiredate = add_month ( date , n = self . expire_length ) <EOL> return expiredate <EOL> def save ( self , ** kwargs ) : <EOL> if hasattr ( self . product , '<STR_LIT>' ) : <EOL> del self . product . _sub_types <EOL> super ( SubscriptionProduct , self ) . save ( ** kwargs ) <EOL> class Meta : <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> class Trial ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> subscription = models . ForeignKey ( SubscriptionProduct ) <EOL> price = CurrencyField ( _ ( "<STR_LIT>" ) , help_text = _ ( "<STR_LIT>" ) , max_digits = <NUM_LIT:10> , decimal_places = <NUM_LIT:2> , null = True , ) <EOL> expire_length = models . IntegerField ( _ ( "<STR_LIT>" ) , help_text = _ ( "<STR_LIT>" ) , null = True , blank = True ) <EOL> def __unicode__ ( self ) : <EOL> return unicode ( self . price ) <EOL> def _occurrences ( self ) : <EOL> if self . expire_length : <EOL> return int ( self . expire_length / self . subscription . expire_length ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> occurrences = property ( fget = _occurrences ) <EOL> def calc_expire_date ( self , date = None ) : <EOL> if date is None : <EOL> date = datetime . datetime . now ( ) <EOL> if self . subscription . expire_unit == "<STR_LIT>" : <EOL> expiredate = date + datetime . timedelta ( days = self . expire_length ) <EOL> else : <EOL> expiredate = add_month ( date , n = self . expire_length ) <EOL> return expiredate <EOL> class Meta : <EOL> ordering = [ '<STR_LIT>' ] <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) </s>
