forked from chrisglass/django-shop-multiplecurrencies
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a719f5
commit c55cd73
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |