Advertisement
GeorgiLukanov87

models.py - Python Web Basics Retake Exam - 19 April 2022

May 17th, 2023
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. # Python Web Basics Retake Exam - 19 April 2022
  2. # 1. GamesPlay App
  3. # https://judge.softuni.org/Contests/Practice/Index/3437#0
  4.  
  5.  
  6. # models.py
  7. from django.core.exceptions import ValidationError
  8. from django.core.validators import MinValueValidator
  9. from django.db import models
  10.  
  11.  
  12. def validate_rating(value):
  13.     if float(value) < 0.1 or float(value) > 5.0:
  14.         raise ValidationError('The rating can be between 0.1 and 5.0 (both inclusive)')
  15.  
  16.  
  17. class Profile(models.Model):
  18.     PASSWORD_MAX_LEN = 30
  19.     FIRST_NAME_MAX_LEN = 30
  20.     LAST_NAME_MAX_LEN = 30
  21.     AGE_MIN_VALUE = 12
  22.  
  23.     email = models.EmailField(
  24.         blank=False,
  25.         null=False,
  26.     )
  27.  
  28.     age = models.PositiveIntegerField(
  29.         validators=(
  30.             MinValueValidator(AGE_MIN_VALUE, message=None),
  31.         ),
  32.         blank=False,
  33.         null=False,
  34.     )
  35.  
  36.     password = models.CharField(
  37.         max_length=PASSWORD_MAX_LEN,
  38.         blank=False,
  39.         null=False,
  40.     )
  41.  
  42.     first_name = models.CharField(
  43.         max_length=FIRST_NAME_MAX_LEN,
  44.         blank=True,
  45.         null=True,
  46.     )
  47.  
  48.     last_name = models.CharField(
  49.         max_length=LAST_NAME_MAX_LEN,
  50.         blank=True,
  51.         null=True,
  52.     )
  53.  
  54.     profile_picture = models.URLField(
  55.         blank=True,
  56.         null=True,
  57.     )
  58.  
  59.     def __str__(self):
  60.         return f'id={self.id}, email={self.email}'
  61.  
  62.     class Meta:
  63.         ordering = ('id',)
  64.  
  65.  
  66. class Game(models.Model):
  67.     GAME_TITLE_MAX_LEN = 30
  68.     GAME_CATEGORY_MAX_LEN = 15
  69.     GAME_MAX_LEVEL_LIMIT = 1
  70.  
  71.     ACTION = 'Action'
  72.     ADVENTURE = 'Adventure'
  73.     PUZZLE = 'Puzzle'
  74.     STRATEGY = 'Strategy'
  75.     SPORTS = 'Sports'
  76.     BOARD_CARD_GAME = "Board/Card Game"
  77.     OTHER = 'Other'
  78.  
  79.     GAME_CATEGORIES = (
  80.         (ACTION, ACTION),
  81.         (PUZZLE, PUZZLE),
  82.         (STRATEGY, STRATEGY),
  83.         (SPORTS, SPORTS),
  84.         (BOARD_CARD_GAME, BOARD_CARD_GAME),
  85.         (OTHER, OTHER),
  86.     )
  87.  
  88.     title = models.CharField(
  89.         max_length=GAME_TITLE_MAX_LEN,
  90.         unique=True,
  91.         blank=False,
  92.         null=False,
  93.     )
  94.  
  95.     category = models.CharField(
  96.         max_length=GAME_CATEGORY_MAX_LEN,
  97.         choices=GAME_CATEGORIES,
  98.         blank=False,
  99.         null=False,
  100.     )
  101.  
  102.     rating = models.FloatField(
  103.         validators=(
  104.             validate_rating,
  105.         ),
  106.         blank=False,
  107.         null=False,
  108.     )
  109.  
  110.     max_level = models.PositiveIntegerField(
  111.         validators=(
  112.             MinValueValidator(GAME_MAX_LEVEL_LIMIT, message=None),
  113.         ),
  114.  
  115.         blank=True,
  116.         null=True,
  117.     )
  118.  
  119.     image_url = models.URLField(
  120.         blank=False,
  121.         null=False,
  122.     )
  123.  
  124.     summary = models.TextField(
  125.         blank=True,
  126.         null=True,
  127.     )
  128.  
  129.     def __str__(self):
  130.         return f'id={self.id}, {self.title}'
  131.  
  132.     class Meta:
  133.         ordering = ('id',)
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement