Advertisement
GeorgiLukanov87

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

May 17th, 2023 (edited)
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.80 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.  
  7. # views.py
  8. from django.shortcuts import render, redirect
  9.  
  10. from games_play_app.my_web.forms import ProfileCreateForm, GameCreateForm, ProfileEditForm, ProfileDeleteForm, \
  11.     GameEditForm, GameDeleteForm
  12. from games_play_app.my_web.models import Profile, Game
  13.  
  14.  
  15. def __calculate_average_rating(_all_games):
  16.     average_rating = sum([float(game.rating) for game in _all_games])
  17.     return average_rating
  18.  
  19.  
  20. def get_profile():
  21.     try:
  22.         profile = Profile.objects.get()
  23.         return profile
  24.     except Profile.DoesNotExist as ex:
  25.         return None
  26.  
  27.  
  28. def show_index(request):
  29.     games = Game.objects.all()
  30.     profile = get_profile()
  31.     context = {
  32.         'hide_nav': True,
  33.     }
  34.     if profile is None:
  35.         return render(
  36.             request,
  37.             'core/home-page.html',
  38.             context,
  39.         )
  40.  
  41.     context = {
  42.         'games': games,
  43.         'hide_nav': False,
  44.  
  45.     }
  46.     return render(
  47.         request,
  48.         'core/home-page.html',
  49.         context,
  50.     )
  51.  
  52.  
  53. def show_dashboard(request):
  54.     games = Game.objects.all()
  55.  
  56.     context = {
  57.         'games': games,
  58.     }
  59.     return render(
  60.         request,
  61.         'core/dashboard.html',
  62.         context,
  63.     )
  64.  
  65.  
  66. def create_profile(request):
  67.     if request.method == 'GET':
  68.         form = ProfileCreateForm()
  69.     else:
  70.         form = ProfileCreateForm(request.POST)
  71.         if form.is_valid():
  72.             form.save()
  73.             return redirect('show dashboard')
  74.  
  75.     context = {
  76.         'form': form,
  77.         'hide_nav': True,
  78.     }
  79.  
  80.     return render(
  81.         request,
  82.         'profile/create-profile.html',
  83.         context,
  84.     )
  85.  
  86.  
  87. def details_profile(request):
  88.     profile = get_profile()
  89.     games = Game.objects.all()
  90.     count = len(games)
  91.     average_rating = 0.0
  92.     if count:
  93.         average_rating = __calculate_average_rating(games) / count
  94.  
  95.     context = {
  96.         'profile': profile,
  97.         'games': games,
  98.         'count': count,
  99.         'average_rating': average_rating,
  100.     }
  101.  
  102.     return render(
  103.         request,
  104.         'profile/details-profile.html',
  105.         context,
  106.     )
  107.  
  108.  
  109. def edit_profile(request):
  110.     profile = get_profile()
  111.  
  112.     if request.method == 'GET':
  113.         form = ProfileEditForm(instance=profile)
  114.     else:
  115.         form = ProfileEditForm(request.POST, instance=profile)
  116.         if form.is_valid():
  117.             form.save()
  118.             return redirect('details profile')
  119.  
  120.     context = {
  121.         'form': form,
  122.     }
  123.  
  124.     return render(
  125.         request,
  126.         'profile/edit-profile.html',
  127.         context,
  128.     )
  129.  
  130.  
  131. def delete_profile(request):
  132.     profile = get_profile()
  133.  
  134.     if request.method == 'GET':
  135.         form = ProfileDeleteForm(instance=profile)
  136.     else:
  137.         form = ProfileDeleteForm(request.POST, instance=profile)
  138.         if form.is_valid():
  139.             form.save()
  140.             return redirect('show index')
  141.  
  142.     context = {
  143.         'form': form,
  144.     }
  145.  
  146.     return render(
  147.         request,
  148.         'profile/delete-profile.html',
  149.         context,
  150.     )
  151.  
  152.  
  153. def create_game(request):
  154.     if request.method == 'GET':
  155.         form = GameCreateForm()
  156.     else:
  157.         form = GameCreateForm(request.POST)
  158.         if form.is_valid():
  159.             form.save()
  160.             return redirect('show dashboard')
  161.  
  162.     context = {
  163.         'form': form,
  164.         'hide_nav': False,
  165.     }
  166.  
  167.     return render(
  168.         request,
  169.         'game/create-game.html',
  170.         context,
  171.     )
  172.  
  173.  
  174. def details_game(request, pk):
  175.     game = Game.objects.filter(pk=pk).get()
  176.  
  177.     context = {
  178.         'game': game,
  179.     }
  180.  
  181.     return render(
  182.         request,
  183.         'game/details-game.html',
  184.         context,
  185.     )
  186.  
  187.  
  188. def edit_game(request, pk):
  189.     game = Game.objects.filter(pk=pk).get()
  190.  
  191.     if request.method == 'GET':
  192.         form = GameEditForm(instance=game)
  193.     else:
  194.         form = GameEditForm(request.POST, instance=game)
  195.         if form.is_valid():
  196.             form.save()
  197.             return redirect('show dashboard')
  198.  
  199.     context = {
  200.         'form': form,
  201.         'game': game,
  202.     }
  203.  
  204.     return render(
  205.         request,
  206.         'game/edit-game.html',
  207.         context,
  208.     )
  209.  
  210.  
  211. def delete_game(request, pk):
  212.     game = Game.objects.filter(pk=pk).get()
  213.  
  214.     if request.method == 'GET':
  215.         form = GameDeleteForm(instance=game)
  216.     else:
  217.         form = GameDeleteForm(request.POST, instance=game)
  218.         if form.is_valid():
  219.             form.save()
  220.             return redirect('show dashboard')
  221.  
  222.     context = {
  223.         'form': form,
  224.         'game': game,
  225.     }
  226.  
  227.     return render(
  228.         request,
  229.         'game/delete-game.html',
  230.         context,
  231.     )
  232.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement