Author: Anonymous Language: python
Description: No description Timestamp: 2010-02-01 04:34:46 -0500
View raw paste Reply
  1. from datetime import datetime, timedelta
  2. from django.db import models
  3. from django.contrib.auth.models import User as AdminUser
  4. from django.utils.translation import ugettext as _
  5.  
  6. from cms_sample.cms_models import Media_biz, Currency, Item
  7. from exceptions import *
  8.  
  9. class Timeable(models.Model):
  10.     creation_date = models.DateTimeField('added date', editable=False, auto_now=True)
  11.     update_date = models.DateTimeField('update date', editable=False, auto_now_add=True)
  12.  
  13. class BillingGroup(Timeable):
  14.     name = models.CharField(max_length=250, unique=True)
  15.     comment = models.CharField(max_length=500, blank=True, null=True)
  16.  
  17.     def __unicode__(self):
  18.         return self.name
  19.  
  20. class BillingUser(Timeable):
  21.     username = models.CharField(max_length=250, default='user')
  22.     pin = models.CharField(max_length=256, default='8654', help_text=_("4 digit pin code"))
  23.     sbox_id = models.CharField(max_length=50) # Related to SBox at aaa app.
  24.     parental_code = models.CharField(max_length=256, default='3344', help_text=_("4 digit parental control code"))
  25.     active = models.BooleanField(default=True)
  26.     # sbox_id is not ForiegnKey, to allow the AppServer to be remote.
  27.  
  28.     class Meta:
  29.         unique_together = (("username", "sbox_id"),)
  30.  
  31.     def __unicode__(self):
  32.         return "%s (%s)" %(self.username, self.sbox_id)
  33.  
  34. class Wallet(Timeable):
  35.     user = models.OneToOneField(BillingUser)
  36.     amount = models.FloatField(default=0.0)
  37.     minmum = models.FloatField(default=0.0, help_text='Wallet will be block/empty bellow this value. Can be negative (for credit)')
  38.     currency = models.ForeignKey(Currency)
  39.  
  40.     def __unicode__(self):
  41.         if self.user:
  42.             return "%s's wallet (%f)" %(self.user.username, self.amount)
  43.         return "N/A"
  44.  
  45. class UserInfo(Timeable):
  46.     user = models.OneToOneField(BillingUser)
  47.     group = models.ForeignKey(BillingGroup)
  48.     first_name = models.CharField(max_length=250)
  49.     last_name = models.CharField(max_length=250)
  50.     client_id = models.CharField(max_length=50, blank=True, null=True)
  51.     phone = models.CharField(max_length=50,blank=True, null=True)
  52.     cell = models.CharField(max_length=50,blank=True, null=True)
  53.     address = models.CharField(max_length=150,blank=True, null=True)
  54.     email = models.EmailField(max_length=150,blank=True, null=True)
  55.     accountcode = models.CharField(max_length=50,blank=True, null=True)
  56.     last_visit = models.DateTimeField(blank=True, null=True)
  57.     birthday = models.DateField(blank=True, null=True)
  58.     box_model = models.CharField(max_length=50,blank=True, null=True)
  59.     mx_version = models.CharField(max_length=50,blank=True, null=True)
  60.  
  61.     def __unicode__(self):
  62.         if self.user:
  63.             return "%s %s (%s)" %(self.first_name, self.last_name, self.user.username)
  64.         return "N/A"
  65.  
  66. class Rental(models.Model):
  67.     wallet = models.ForeignKey(Wallet)
  68.     item = models.ForeignKey(Item)
  69.     end_time = models.DateTimeField()
  70.     time = models.DateTimeField()
  71.     value = models.FloatField()
  72.     index = models.IntegerField(help_text='Number of time that the user rent the item')
  73.     info = models.CharField(max_length=500, blank=True, null=True)
  74.  
  75.     class Meta:
  76.         unique_together = [("wallet", "item", "index"),]
  77.  
  78.     def save(self):
  79.         if not self.id:
  80.             self.time = datetime.now()
  81.             rental_period = self.item.metadata.media_biz.rental_period # in hours
  82.             self.end_time = self.time + timedelta(hours=int(rental_period))
  83.         else:
  84.             raise BillExcep_rental('Rental object can not be changed. Create new object instead.')
  85.         super(Rental, self).save()
  86.  
  87.     def __unicode__(self):
  88.         if self.wallet:
  89.             return "%s can watch %s until %s" %(self.wallet.user.username, self.item, self.end_time)
  90.         return "N/A"
  91.  
  92. class PriceCategory(Timeable):
  93.     PRICING_METHOD = (
  94.         ('MN', 'Monthly. Recurring.'),
  95.         ('YR', 'Yearly. Recurring.'),
  96.         ('M1', 'One Month'),
  97.         ('O1', 'Once'),
  98.         ('FR', 'Free'),
  99.     )
  100.  
  101.     name = models.CharField(max_length=250)
  102.     price = models.FloatField()
  103.     currency = models.ForeignKey(Currency)
  104.     pricing_method = models.CharField(max_length=2, choices=PRICING_METHOD, default='MN')
  105.  
  106.     def __unicode__(self):
  107.         return "%s (%f)" %(self.name, self.price)
  108.  
  109. class Package(Timeable):
  110.     name = models.CharField(max_length=250)
  111.     pricecategory = models.ForeignKey(PriceCategory)
  112.     parent = models.ManyToManyField('self', symmetrical=False, related_name="childs", blank=True, null=True)
  113.     items = models.ManyToManyField(Item, blank=True, null=True)
  114.  
  115.     def __unicode__(self):
  116.         return "%s (%s)" %(self.name, self.pricecategory.name)
  117.  
  118. #class Channel(Timeable):
  119.     #item = models.ForeignKey(Item)
  120.     #package = models.ForeignKey(Package, blank=True, null=True)
  121.  
  122.     #def __unicode__(self):
  123.         #if self.item:
  124.                 #return "Chennel of %s " %(self.item.title)
  125.         #return "N/A"
  126.  
  127. class Subscription(Timeable):
  128.     user = models.ForeignKey(BillingUser)
  129.     package = models.ForeignKey(Package, blank=True, null=True)
  130.     #channel = models.ForeignKey(Channel, blank=True, null=True)
  131.     item = models.ForeignKey(Item, blank=True, null=True)
  132.  
  133.     class Meta:
  134.         unique_together = (("user", "package"),)
  135.  
  136.     def __unicode__(self):
  137.         if self.user:
  138.             if self.package:
  139.                 return "%s subscribe to package %s" %(self.user.username, self.package.name)
  140.             if self.item:
  141.                 return "%s subscribe to channel %s" %(self.user.username, self.item.title)
  142.         return "N/A"
  143.  
  144.     def save(self):
  145.         if self.user and ((self.package and not self.item) or (not self.package and self.item)):
  146.             # Save only if we have user and xor of item/package)
  147.             return super(Subscription, self).save()
  148.  
  149. class Event(Timeable):
  150.     EVENT_CHOICES = (
  151.         ('DF', 'Default event'),
  152.         ('WL', 'Wallet event'),
  153.         ('SB', 'Subscription event'),
  154.         ('MC', 'miscellaneous event'),
  155.         ('CS', 'Customer support event'),
  156.     )
  157.  
  158.     etype = models.CharField(max_length=2, choices=EVENT_CHOICES, default='DF')
  159.     value = models.FloatField(blank=True, null=True)
  160.     description = models.CharField(max_length=1000, default = '<No description>')
  161.     wallet = models.ForeignKey(Wallet, blank=True, null=True)
  162.     subscription = models.ForeignKey(Subscription, blank=True, null=True)
  163.     admin = models.ForeignKey(AdminUser, blank=True, null=True)
  164.     billing_user = models.ForeignKey(BillingUser, blank=True, null=True)
  165.  
View raw paste Reply