๐
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
pizza = baker.make('order.Pizza') | |
topping1 = baker.make('order.Topping') | |
topping2 = baker.make('order.Topping') | |
pizza.toppings.add(topping1) | |
pizza.toppings.add(topping2) |
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
pizza = Pizza.objects.create(name='Margarita') | |
topping1 = Topping.objects.create(description='Pizza sauce', price=1.5) | |
topping2 = Topping.objects.create(description='Mozzarella', price=1.65) | |
pizza.toppings.add(topping1) | |
pizza.toppings.add(topping2) |
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
[pytest] | |
DJANGO_SETTINGS_MODULE = pizza_app.test_settings | |
# -- recommended but optional: | |
python_files = tests.py test_*.py *_tests.py | |
addopts = --reuse-db --nomigrations |
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
import pytest | |
from order.models import Pizza, Topping | |
@pytest.mark.django_db | |
def test_pizza_price_with_toppings(): | |
# Preparation phase | |
pizza = Pizza.objects.create(name='Margarita') | |
topping1 = Topping.objects.create(description='Pizza sauce', price=1.5) |
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
class Pizza(models.Model): | |
name = models.CharField(max_length=100) | |
description = models.TextField(blank=True) | |
toppings = models.ManyToManyField(Topping, related_name='pizzas') | |
@property | |
def total_price(self) -> Decimal: | |
""" | |
Total price is sum of all topping prices. | |
""" |
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
@property | |
def total_price(self) -> Decimal: | |
""" | |
Total price is sum of the following: | |
- Pizza total price | |
- Size price | |
- Sum of toppings | |
""" | |
price = self.pizza.total_price + self.size.price | |
for topping in self.extra_toppings.all(): |
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
@property | |
def total_price(self) -> Decimal: | |
""" | |
Total price is sum of pizza prices in this order. | |
""" | |
price = Decimal(0) | |
for pizza in self.pizzas.all(): | |
price += pizza.total_price | |
return price |
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
@property | |
def total_price(self) -> Decimal: | |
""" | |
Total price is sum of all topping prices. | |
""" | |
price = Decimal(0) | |
for topping in self.toppings.all(): | |
price += topping.price | |
return price |
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
AUTH_PASSWORD_VALIDATORS = [ | |
{ | |
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', | |
}, | |
{ | |
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', | |
}, | |
{ | |
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', | |
}, |
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
import re | |
from django.core.exceptions import ValidationError | |
from django.utils.translation import ugettext as _ | |
class NumberValidator(object): | |
@staticmethod | |
def validate(password, user=None): |