django-expense / expense / models.py

from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from managers import *
from templatetags import numberformat

class Type(models.Model):
    """
    Expense groups are represented by this model.
    """
    name = models.CharField(_('name'), max_length=50)

    objects = TypeManager()

    class Meta:
        verbose_name_plural = _('types')
        verbose_name = _('type')
        ordering = ['name']

    def __unicode__(self):
        return self.name

class Category(models.Model):
    """
    Expense categories are represented by this model.
    """
    name = models.CharField(_('name'), max_length=50)
    type = models.ForeignKey(Type, related_name='categories',
                             blank = False, null=True)

    class Meta:
        verbose_name_plural = _('categories')
        verbose_name = _('category')
        ordering = ['type__name', 'name']

    def __unicode__(self):
        return ("%s - %s" % (self.type.name,  self.name))

    def type_name(self):
        return self.type.name

class Expense(models.Model):
    """
    Expenses are represented by this model.
    """
    user = models.ForeignKey(User, blank = False, null=True)
    category = models.ForeignKey(Category, related_name='expenses', blank=True, null=True)
    date = models.DateField(_('date'), 'date')
    description = models.CharField(_('description'), max_length = 300)
    amount = models.IntegerField(_('amount'))

    objects = ExpenseManager()

    date.expense_date_filter =  True
    class Meta:
        verbose_name_plural = _('expenses')
        verbose_name = _('expense')
        ordering = ['-date', 'category__type__name', 'category__name']

    def __unicode__(self):
        return self.description

    def formatted_amount(self):
        return ('<div class="number">%s</div>' % (numberformat.numberformat(self.amount), ))

    formatted_amount.short_description = _('amount')
    formatted_amount.allow_tags = True

    def date_str(self):
        """
        Formats the date by "%Y.%m.%d." pattern.
        """
        return self.date.strftime("%Y.%m.%d.")

    date_str.short_description = _("date")
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.