Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ SESSION_COOKIE_SECURE=True
STRIPE_PUBLIC_KEY=your-stripe-public-key
STRIPE_SECRET_KEY=your-stripe-secret-key

# Currency (optional): Philippine pesos per US dollar, used to show prices in PHP
USD_TO_PHP_RATE=59.41

# Production Settings
# DEBUG=False
# ALLOWED_HOSTS=yourdomain.com,www.yourdomain.com
Expand Down
4 changes: 4 additions & 0 deletions src/main/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.messages import constants as messages
from dotenv import load_dotenv
from decouple import config
from decimal import Decimal
from pathlib import Path
import dj_database_url
import os
Expand Down Expand Up @@ -108,6 +109,9 @@
RECAPTCHA_SITE_KEY = config('RECAPTCHA_SITE_KEY')
RECAPTCHA_SECRET_KEY = config('RECAPTCHA_SECRET_KEY')

# Philippine pesos per US dollar. Product prices are stored in USD and shown in PHP.
USD_TO_PHP_RATE = config('USD_TO_PHP_RATE', default='59.41', cast=Decimal)

# Django Allauth Settings
SOCIALACCOUNT_PROVIDERS = {
'google': {
Expand Down
4 changes: 2 additions & 2 deletions src/store/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def __str__(self):

@property
def price_in_peso_value(self):
PHILIPPINE_PESO_TO_USD = Decimal("59.41")
return (self.price * PHILIPPINE_PESO_TO_USD).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
usd_to_php = settings.USD_TO_PHP_RATE
return (self.price * usd_to_php).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)

@property
def price_in_peso(self):
Expand Down
24 changes: 24 additions & 0 deletions src/store/tests/test_exchange_rate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from decimal import Decimal

from django.conf import settings
from django.test import TestCase, override_settings

from store.models import Category, Product


class ExchangeRateTests(TestCase):
def setUp(self):
self.category = Category.objects.create(name='Gadgets')

def test_default_rate_keeps_displayed_prices_unchanged(self):
self.assertEqual(settings.USD_TO_PHP_RATE, Decimal('59.41'))
product = Product.objects.create(name='Mug', category=self.category, price=Decimal('10.00'))

self.assertEqual(product.price_in_peso, '₱594.10')

@override_settings(USD_TO_PHP_RATE=Decimal('50.00'))
def test_peso_price_uses_the_rate_from_settings(self):
product = Product.objects.create(name='Mug', category=self.category, price=Decimal('10.00'))

self.assertEqual(product.price_in_peso_value, Decimal('500.00'))
self.assertEqual(product.discounted_price, Decimal('500.00'))
Loading