django-pim / pcbank / models.py

# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
import datetime

class Transaction(models.Model):
    from_ct = models.ForeignKey(ContentType, related_name='fromtrans_set')
    from_id = models.PositiveIntegerField()
    from_obj = generic.GenericForeignKey('from_ct', 'from_id')
    amount = models.DecimalField(max_digits=6, decimal_places=2)
    memo = models.CharField(max_length=60, blank=True, null=True)
    date = models.DateField(auto_now_add=True)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey()
    class Meta:
        ordering = ['-date']
    def __unicode__(self):
        return "%s to %s of %d" % (self.from_obj, self.content_object, self.amount)
    @models.permalink
    def get_absolute_url(self):
        return ('view-transaction', [str(self.pk)])

class Income(models.Model):
    title = models.CharField(max_length=80)
    amount = models.DecimalField(max_digits=6, decimal_places=2)
    per_month = models.IntegerField()
    active = models.BooleanField()
    added_on = models.DateField(auto_now_add=True)
    transactions = generic.GenericRelation(Transaction, content_type_field='from_ct', object_id_field='from_id')
    def __unicode__(self):
        return u"%s - $%d" % (self.title, self.amount)
    def monthly_amount(self):
        return self.amount * self.per_month
    def yearly_amount(self):
        return self.monthly_amount() * 12
    @models.permalink
    def payday(self):
        return ('pcbank.views.payday', [str(self.pk)])
    @models.permalink
    def get_edit_url(self):
        return ('edit-income', [str(self.pk)])
    @models.permalink
    def get_absolute_url(self):
        return ('view-income', [str(self.pk)])

class CreditCard(models.Model):
    title = models.CharField(max_length=80)
    credit = models.DecimalField(max_digits=6, decimal_places=2)
    balance = models.DecimalField(max_digits=6, decimal_places=2)
    min_payment = models.DecimalField(max_digits=6, decimal_places=2)
    due_date = models.DateField()
    active = models.BooleanField()
    added_on = models.DateField(auto_now_add=True)
    url = models.URLField('Online Banking site', verify_exists=False)
    transactions = generic.GenericRelation(Transaction, content_type_field='from_ct', object_id_field='from_id')
    def __unicode__(self):
        return u"%s - $%d" % (self.title, self.balance)
    def available_credit(self):
        return self.credit - self.balance
    @models.permalink
    def get_edit_url(self):
        return ('edit-cc', [str(self.pk)])
    @models.permalink
    def get_absolute_url(self):
        return ('view-cc', [str(self.pk)])

class Payee(models.Model):
    title = models.CharField(max_length=80)
    account = models.CharField('Account #', max_length=40)
    split_by = models.IntegerField('Split by how many people')
    due_date = models.DateField()
    active = models.BooleanField()
    added_on = models.DateField(auto_now_add=True)
    url = models.URLField('Account site', verify_exists=False, blank=True, null=True)
    transactions = generic.GenericRelation(Transaction)
    class Meta:
        ordering = ['due_date']
    def __unicode__(self):
        return u"%s" % self.title
    @models.permalink
    def add_bill(self):
        return ('pcbank.views.add_bill', [str(self.pk)])
    @models.permalink
    def get_edit_url(self):
        return ('edit-payee', [str(self.pk)])
    @models.permalink
    def get_absolute_url(self):
        return ('view-payee', [str(self.pk)])

class Bill(models.Model):
    payee = models.ForeignKey(Payee)
    amount = models.DecimalField(max_digits=5, decimal_places=2)
    added_on = models.DateField(auto_now_add=True)
    paid = models.BooleanField()
    def __unicode__(self):
        return u"%s - $%s" % (self.payee.title, self.amount)
    def split_amount(self):
        return self.amount / self.payee.split_by
    @models.permalink
    def get_edit_url(self):
        return ('edit-bill', [str(self.pk)])
    @models.permalink
    def mark_paid(self):
        return ('pcbank.views.mark_paid', [str(self.pk)])
    @models.permalink
    def bank_payment(self):
        return ('pcbank.views.make_bank_payment', [str(self.pk)])
    @models.permalink
    def cc_payment(self):
        return ('pcbank.views.make_cc_payment', [str(self.pk)])
    @models.permalink
    def get_absolute_url(self):
        return ('view-bill', [str(self.pk)])

class Bank(models.Model):
    title = models.CharField(max_length=80)
    card = models.CharField('Card Number', max_length=40)
    balance = models.DecimalField(max_digits=6, decimal_places=2)
    added_on = models.DateField(auto_now_add=True)
    active = models.BooleanField()
    url = models.URLField('Online Banking site', verify_exists=False)
    transactions = generic.GenericRelation(Transaction, content_type_field='from_ct', object_id_field='from_id')
    income = generic.GenericRelation(Transaction, related_name='incomes')
    def __unicode__(self):
        return u"%s - $%s" % (self.title, self.balance)
    @models.permalink
    def get_edit_url(self):
        return ('edit-bank', [str(self.pk)])
    @models.permalink
    def get_absolute_url(self):
        return ('view-bank', [str(self.pk)])

class Roommate(models.Model):
    name = models.CharField(max_length=20)
    total_given = models.DecimalField(max_digits=6, decimal_places=2)
    added_on = models.DateField(auto_now_add=True)
    active = models.BooleanField()
    transactions = generic.GenericRelation(Transaction, content_type_field='from_ct', object_id_field='from_id')
    def __unicode__(self):
        return u"%s" % self.name
    @models.permalink
    def get_edit_url(self):
        return ('edit-roommate', [str(self.pk)])
    @models.permalink
    def add_bill(self):
        return ('pcbank.views.add_roommatebill', [str(self.pk)])
    @models.permalink
    def get_absolute_url(self):
        return ('view-roommate', [str(self.pk)])

class RoommateBill(models.Model):
    roommate = models.ForeignKey(Roommate)
    for_bill = models.ForeignKey(Bill)
    paid = models.BooleanField()
    pay_date = models.DateField(blank=True, null=True)
    def __unicode__(self):
        return u"%s" % self.for_bill.payee
    def amount(self):
        return self.for_bill.amount / self.for_bill.payee.split_by
    def past_due(self):
        if self.pay_date != None:
            if self.pay_date > self.for_bill.payee.due_date:
                return True
            elif datetime.date.today() > self.for_bill.payee.due_date:
                return True
        return False
    @models.permalink
    def make_payment(self):
        return ('pcbank.views.roommate_payment', [str(self.pk)])
    @models.permalink
    def get_absolute_url(self):
        return ('view-roommatebill', [str(self.pk)])

class Cheque(models.Model):
    number = models.PositiveIntegerField()
    amount = models.DecimalField(max_digits=6, decimal_places=2)
    transaction = models.ForeignKey(Transaction, blank=True, null=True)
    image = models.ImageField(upload_to='cheques', blank=True, null=True)
    memo = models.TextField()
    post_date = models.DateField()
    def __unicode__(self):
        return "Cheque #%s" % self.number
    @models.permalink
    def get_absolute_url(self):
        return ('view-cheque', [str(self.number)])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.