Best Python code snippet using yandex-tank
views.py
Source:views.py
1from django.shortcuts import render2from django.http import HttpResponse, HttpResponseRedirect, JsonResponse3from django.urls import reverse4from django.utils import timezone5from django.views.decorators.csrf import csrf_exempt6import requests7from bs4 import BeautifulSoup8from datetime import date9from .models import NBA_Player10# Create your views here.11def index(request):12 return render(request, 'data/index.html')13def nba(request):14 return render(request, 'data/nba.html')15def nba_players(request):16 players = NBA_Player.objects.all()17 context = {'players': players}18 return render(request, 'data/nba_players.html', context)19def admin_nba_players(request):20 if request.user.is_superuser:21 players = NBA_Player.objects.all()22 context = {'players': players}23 return render(request, 'data/admin_nba_players.html', context)24 25 return HttpResponse('Unauthorized', status=401)26def _validate_player(player):27 errors = {}28 if not player.get('stats_player_id'):29 errors['stats_player_id'] = { 'msg': 'stats_player_id can not be blank.' }30 if not player.get('name'):31 errors['name'] = { 'msg': 'name can not be blank.' }32 return errors33def admin_nba_players_update(request):34 if request.user.is_superuser:35 if request.method == 'POST':36 stats_player_id = request.POST.get('stats_player_id')37 page = requests.get('https://stats.nba.com/player/' + str(stats_player_id) + '/')38 soup = BeautifulSoup(page.content, 'html.parser')39 el_player = soup.find('div', { 'class': 'player-summary' })40 first_name = el_player.find('p', { 'class': 'player-summary__first-name'}).text41 last_name = el_player.find('p', { 'class': 'player-summary__last-name'}).text42 name = '{} {}'.format(first_name, last_name)43 text = el_player.find('span', { 'class': 'player-summary__player-number'}).text44 number = int(text[1:])45 46 team = el_player.find('span', { 'class': 'player-summary__team-name'}).text47 48 stats_item = el_player.find('div', { 'class': 'player-stats__height'})49 text = stats_item.find('span', { 'class': 'player-stats__stat-value'}).text50 tokens = text.split('-')51 ht = (float(tokens[0]) * 12 + float(tokens[1])) * 2.5452 53 stats_item = el_player.find('div', { 'class': 'player-stats__weight'})54 text = stats_item.find('span', { 'class': 'player-stats__stat-value'}).text55 tokens = text.split()56 wt = float(tokens[0])57 58 stats_item = el_player.find('div', { 'class': 'player-stats__birthdate'})59 text = stats_item.find('span', { 'class': 'player-stats__stat-value'}).text60 tokens = text.split('/')61 born = date(int(tokens[2]), int(tokens[0]), int(tokens[1]))62 stats_item = el_player.find('div', { 'class': 'player-stats__prior'})63 prior = stats_item.find('span', { 'class': 'player-stats__stat-value'}).text64 stats_item = el_player.find('div', { 'class': 'player-stats__draft'})65 draft = stats_item.find('span', { 'class': 'player-stats__stat-value'}).text66 stats_item = el_player.find('div', { 'class': 'player-stats__experience'})67 text = stats_item.find('div', { 'class': 'player-stats__stat-value'}).text68 tokens = text.split()69 exp = float(tokens[0])70 stats_item = el_player.find('div', { 'class': 'player-stats__pts'})71 text = stats_item.find('span', { 'class': 'player-stats__stat-value'}).text72 pts = float(text)73 stats_item = el_player.find('div', { 'class': 'player-stats__reb'})74 text = stats_item.find('span', { 'class': 'player-stats__stat-value'}).text75 reb = float(text)76 stats_item = el_player.find('div', { 'class': 'player-stats__ast'})77 text = stats_item.find('span', { 'class': 'player-stats__stat-value'}).text78 ast = float(text)79 stats_item = el_player.find('div', { 'class': 'player-stats__pie'})80 text = stats_item.find('span', { 'class': 'player-stats__stat-value'}).text81 pie = float(text)82 83 '''84 print("---------------------------------------")85 print(first_name)86 print(last_name)87 print(number)88 print(team)89 print(ht)90 print(wt)91 print(born)92 print(prior)93 print(draft)94 print(exp)95 print(pts)96 print(reb)97 print(ast)98 print(pie)99 '''100 players = NBA_Player.objects.filter(stats_player_id=stats_player_id)101 player = None if len(players) == 0 else players[0]102 if not player:103 errors = _validate_player({104 'stats_player_id': stats_player_id,105 'name': name, 106 })107 if errors:108 return JsonResponse({ 'errors': errors })109 110 player = NBA_Player(name=name, stats_player_id=stats_player_id, number=number, 111 team=team, ht=ht, wt=wt, born=born, prior=prior, draft=draft, exp=exp, 112 pts=pts, reb=reb, ast=ast, pie=pie)113 player.save()114 115 return JsonResponse({ 'status': 200, 'msg': 'success' })116 else:117 errors = _validate_player({118 'stats_player_id': stats_player_id,119 'name': name, 120 })121 if errors:122 return JsonResponse({ 'errors': errors })123 player.name = name124 player.stats_player_id = stats_player_id 125 player.number = number126 player.team=team127 player.ht=ht128 player.wt=wt 129 player.born=born130 player.prior=prior 131 player.draft=draft132 player.xp=exp133 player.pts=pts 134 player.reb=reb 135 player.ast=ast 136 player.pie=pie137 player.save()138 139 return JsonResponse({ 'status': 200, 'msg': 'success' })...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!