Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1. Customer
- def validator(value):
- for char in value:
- if not char.isalpha() or not char.isspace():
- raise ValidationError("Name can only contain letters and spaces")
- def phone_validator(value):
- if not re.match(r'^\+359\d{9}$', value):
- raise ValidationError("Phone number must start with a '+359' followed by 9 digits")
- class Customer(models.Model):
- name = models.CharField(max_length=100, validators=[validator])
- age = models.PositiveIntegerField(validators=[MinValueValidator(18), message="Age must be greater than 18"])
- email = models.EmailFiled(error_message = {'invalid': "Enter a valid email address"})
- phone_number = models.CharField(max_length=13, validatiors=[phone_validator])
- website_url = models.URLField(error_message = {'invalid': "Enter a valid URL"})
- # 2. Media
- class BaseMedia(models.Model):
- title = models.CharField(max_length=100)
- description = models.TextField()
- genre = models.CharField(max_length=50)
- created_at = models.DateTimeField(auto_now_add=True)
- class Meta:
- abstract= True
- ordering = ["-created_at", "title"]
- class Book(BaseMedia):
- author = models.CharField(max_length=100, validators=[MinLengthValidator(5, message="Author must be at least 5 characters long")])
- isbn = models.CharField(max_length=20, unique=True, validators=[MinLengthValidator(6, message="ISBN must be at least 6 characters long")])
- class Meta(BaseMedia.Meta):
- verbose_name = "Model Book"
- verbose_name_plural = "Models of type - Book"
- class Movie(BaseMedia):
- director = models.CharField(max_length=100, validators=[MinLengthValidator(8, message="Director must be at least 8 characters long")])
- class Meta(BaseMedia.Meta):
- verbose_name = "Model Movie"
- verbose_name_plural = "Models of type - Movie"
- class Music(BaseMedia):
- artist = models.CharField(max_length=100, validators=[MinLengthValidator(9, message="Artist must be at least 9 characters long")])
- class Meta(BaseMedia.Meta):
- verbose_name = "Model Music"
- verbose_name_plural = "Models of type - Music "
- # 3. Digital Products
- class Product(models.Model):
- name = models.CharField(max_length=100)
- price = models.DecimaField(max_digits=10, decimal_places=2)
- def calculate_tax(self):
- return self.price * Decimal(0.08)
- # да проверя без Decimal дали минава
- def calculate_shipping_cost(weight: Decimal):
- return weight * Decimal(2.00)
- def format_product_name(self):
- return f"Product: {self.name}"
- # когато моделът Child споделя същата таблица като Parent class-a предоставя допълнителна функционалност, то той е class Meta: е proxy
- class DiscountedProduct(Product):
- class Meta:
- proxy = True
- def calculate_price_without_discount(self):
- return self.price * Decimal(1.20)
- def calculate_tax(self):
- return self.price * Decimal(0.05)
- # да проверя без Decimal дали минава
- def calculate_shipping_cost(weight: Decimal):
- return weight * Decimal(1.50)
- def format_product_name(self):
- return f"Discounted Product: {self.name}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement