Skip to content

Commit

Permalink
First rush of models. WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisglass committed May 8, 2011
1 parent 1a719f5 commit c55cd73
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions shop_multiplecurrencies/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
from django.db import models
from shop.util.fields import CurrencyField
from shop.models.productmodels import Product

# Create your models here.

class Currency(models.Model):
name = models.CharField(max_length=255)
symbol = models.CharField(max_length=10) # Should be short
prefix = models.Boolean(default=False) # Should the symbol be prefixed?

class Price(models.Model):
"""
A class representing a Price. A price is made of a value (the numerical
part), and a currency. They are tied to a product.
"""
# Product is polymorphic, so this is actually a key to Product subclasses
product = models.ForeignKey(Product)
value = CurrencyField()
currency = models.ForeignKey(Currency)

def __unicode__(self):
if self.currency.prefix:
return "%s%s" % (self.currency.symbol, self.value)
else:
return "%s%s" % (self.value, self.currency.symbol)


class MultipleCurrencyMixin(object):
"""
A mixin to add to your product subclasses to make them multi-currencies
aware.
"""
def get_price(self):
pass # TODO: Use a default currency setting or something

def get_price_in_currency(self, symbol):
"""
Returns the price for this product in the currency matching `symbol`
"""
price = Price.objects.filter(product=self, currency__symbol=symbol)
if len(price):
price = price[0]

0 comments on commit c55cd73

Please sign in to comment.