Skip to content

Instantly share code, notes, and snippets.

View klement97's full-sized avatar
🌍
Open for opportunities

Klement Omeri klement97

🌍
Open for opportunities
View GitHub Profile
pizza = baker.make('order.Pizza')
topping1 = baker.make('order.Topping')
topping2 = baker.make('order.Topping')
pizza.toppings.add(topping1)
pizza.toppings.add(topping2)
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)
[pytest]
DJANGO_SETTINGS_MODULE = pizza_app.test_settings
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py
addopts = --reuse-db --nomigrations
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)
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.
"""
@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():
@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
@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
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
import re
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext as _
class NumberValidator(object):
@staticmethod
def validate(password, user=None):