Advertisement
GeorgiLukanov87

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

May 17th, 2023
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 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. # forms.py
  6. from django import forms
  7.  
  8. from games_play_app.my_web.models import Profile, Game
  9.  
  10.  
  11. # PROFILE FORMS
  12. class ProfileBaseForm(forms.ModelForm):
  13.     class Meta:
  14.         model = Profile
  15.         fields = ["email", "age", "password", ]
  16.         # Password input(hidden).
  17.         widgets = {
  18.             'password': forms.PasswordInput(),
  19.         }
  20.  
  21.  
  22. class ProfileCreateForm(ProfileBaseForm):
  23.     pass
  24.  
  25.  
  26. class ProfileHiddenDeleteForm(forms.ModelForm):
  27.     class Meta:
  28.         model = Profile
  29.         fields = ()
  30.  
  31.  
  32. class ProfileEditForm(ProfileBaseForm):
  33.     class Meta:
  34.         model = Profile
  35.         fields = '__all__'
  36.         widgets = {
  37.             'password': forms.PasswordInput(),
  38.         }
  39.  
  40.  
  41. class ProfileDeleteForm(ProfileHiddenDeleteForm):
  42.     def __init__(self, *args, **kwargs):
  43.         super().__init__(*args, **kwargs)
  44.         self.__set_disabled_fields()
  45.  
  46.     def save(self, commit=True):
  47.         if commit:
  48.             Game.objects.all().delete()
  49.             self.instance.delete()
  50.  
  51.         return self.instance
  52.  
  53.     def __set_disabled_fields(self):
  54.         for _, field in self.fields.items():
  55.             if _ == 'password':
  56.                 # To successful delete profile , skip password input.
  57.                 continue
  58.             field.widget.attrs['readonly'] = 'readonly'
  59.  
  60.  
  61. # GAME FORMS
  62. class GameBaseForm(forms.ModelForm):
  63.     class Meta:
  64.         model = Game
  65.         fields = '__all__'
  66.  
  67.  
  68. class GameCreateForm(GameBaseForm):
  69.     pass
  70.  
  71.  
  72. class GameEditForm(GameBaseForm):
  73.     pass
  74.  
  75.  
  76. class GameDeleteForm(GameBaseForm):
  77.     def __init__(self, *args, **kwargs):
  78.         super().__init__(*args, **kwargs)
  79.         self.__set_disabled_fields()
  80.  
  81.     def save(self, commit=True):
  82.         if commit:
  83.             self.instance.delete()
  84.  
  85.         return self.instance
  86.  
  87.     def __set_disabled_fields(self):
  88.         for _, field in self.fields.items():
  89.             field.widget.attrs['readonly'] = 'readonly'
  90.  
  91.  
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement