Skip to content

Commit 6ca14d3

Browse files
committed
#99 split out ofm views into new package
1 parent 5d71550 commit 6ca14d3

File tree

9 files changed

+943
-915
lines changed

9 files changed

+943
-915
lines changed

core/ofm_urls.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from django.conf.urls import url
22

3-
from core.ofm_views import PlayerDetailView, PlayerStatisticsView, PlayerStatisticsAsJsonView, FinanceDataView, \
4-
FinancesAsJsonView, MatchesView, MatchesAsJsonView, StadiumStatisticsView, StadiumStatisticsAsJsonView, \
5-
StadiumDetailView, StadiumStandStatisticsView, FinanceIncomeChartView, FinanceExpensesChartView, \
6-
FinanceBalanceChartView, PlayerChartView, MatchesSummaryJsonView, StadiumStandStatisticsChartView
3+
from core.rename_me_views.stadium_views import StadiumStatisticsView, StadiumStatisticsAsJsonView, StadiumDetailView, \
4+
StadiumStandStatisticsView, StadiumStandStatisticsChartView
5+
from core.rename_me_views.match_views import MatchesView, MatchesAsJsonView, MatchesSummaryJsonView
6+
from core.rename_me_views.finance_views import FinanceDataView, FinancesAsJsonView, FinanceBalanceChartView, \
7+
FinanceIncomeChartView, FinanceExpensesChartView
8+
from core.rename_me_views.player_views import PlayerStatisticsView, PlayerStatisticsAsJsonView, PlayerDetailView, PlayerChartView
79

810
app_name = 'ofm'
911
urlpatterns = [

core/ofm_views.py

Lines changed: 0 additions & 911 deletions
This file was deleted.

core/rename_me_views/__init__.py

Whitespace-only changes.

core/rename_me_views/finance_views.py

Lines changed: 354 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import locale
2+
3+
from braces.views import CsrfExemptMixin, JsonRequestResponseMixin
4+
from django.contrib.auth.decorators import login_required
5+
from django.utils.decorators import method_decorator
6+
from django.views import View
7+
from django.views.generic import TemplateView
8+
9+
from core.models import Matchday, Match
10+
11+
12+
@method_decorator(login_required, name='dispatch')
13+
class MatchesView(TemplateView):
14+
template_name = 'core/ofm/matches.html'
15+
16+
def get_context_data(self, **kwargs):
17+
context = super(MatchesView, self).get_context_data(**kwargs)
18+
19+
matchdays = Matchday.objects.filter(matches__isnull=False).distinct()
20+
seasons = set(m.season.number for m in matchdays)
21+
22+
context['seasons'] = sorted(seasons, reverse=True)
23+
24+
return context
25+
26+
27+
@method_decorator(login_required, name='dispatch')
28+
class MatchesAsJsonView(CsrfExemptMixin, JsonRequestResponseMixin, View):
29+
def get(self, request, *args, **kwargs):
30+
season = self.request.GET.get('season', default=Matchday.objects.all()[0].season.number)
31+
matches = Match.objects.filter(user=self.request.user, matchday__season__number=season)
32+
33+
match_json = [self._get_match_in_json(match) for match in matches]
34+
35+
return self.render_json_response(match_json)
36+
37+
@staticmethod
38+
def _get_match_in_json(match):
39+
"""
40+
Returns:
41+
A dictionary of match data.
42+
"""
43+
locale.setlocale(locale.LC_ALL, '')
44+
45+
if match.is_home_match:
46+
home_team_name = "<span class='users-team'>" + match.home_team_statistics.team_name + "</span>"
47+
guest_team_name = match.guest_team_statistics.team_name
48+
if hasattr(match, 'stadium_statistics'):
49+
venue = "<a href='" + match.stadium_statistics.get_absolute_url() + "'>" + match.venue + "</a>"
50+
else:
51+
venue = match.venue
52+
else:
53+
home_team_name = match.home_team_statistics.team_name
54+
guest_team_name = "<span class='users-team'>" + match.guest_team_statistics.team_name + "</span>"
55+
venue = match.venue
56+
57+
result_score = str(match.home_team_statistics.score) + ":" + str(match.guest_team_statistics.score)
58+
if match.is_in_future:
59+
result = "<span class='match_scheduled alert-info'>-:-</span>"
60+
elif match.is_won:
61+
result = "<span class='match_won alert-success'>" + result_score + "</span>"
62+
elif match.is_draw:
63+
result = "<span class='match_draw alert-warning'>" + result_score + "</span>"
64+
else:
65+
result = "<span class='match_lost alert-danger'>" + result_score + "</span>"
66+
67+
match_stat = dict()
68+
match_stat['home_team'] = home_team_name
69+
match_stat['guest_team'] = guest_team_name
70+
match_stat['result'] = result
71+
home_strength = match.home_team_statistics.strength
72+
if home_strength == int(home_strength):
73+
match_stat['home_strength'] = int(home_strength)
74+
else:
75+
match_stat['home_strength'] = locale.format("%.1f", home_strength)
76+
guest_strength = match.guest_team_statistics.strength
77+
if guest_strength == int(guest_strength):
78+
match_stat['guest_strength'] = int(guest_strength)
79+
else:
80+
match_stat['guest_strength'] = locale.format("%.1f", guest_strength)
81+
match_stat['home_ball_possession'] = locale.format("%.1f", match.home_team_statistics.ball_possession) + " %"
82+
match_stat['guest_ball_possession'] = locale.format("%.1f", match.guest_team_statistics.ball_possession) + " %"
83+
match_stat['home_chances'] = match.home_team_statistics.chances
84+
match_stat['guest_chances'] = match.guest_team_statistics.chances
85+
match_stat['home_yellow_cards'] = match.home_team_statistics.yellow_cards
86+
match_stat['guest_yellow_cards'] = match.guest_team_statistics.yellow_cards
87+
match_stat['home_red_cards'] = match.home_team_statistics.red_cards
88+
match_stat['guest_red_cards'] = match.guest_team_statistics.red_cards
89+
match_stat['venue'] = venue
90+
match_stat['matchday'] = match.matchday.number
91+
92+
return match_stat
93+
94+
95+
@method_decorator(login_required, name='dispatch')
96+
class MatchesSummaryJsonView(CsrfExemptMixin, JsonRequestResponseMixin, View):
97+
def get(self, request, *args, **kwargs):
98+
current_season = Matchday.objects.all()[0].season
99+
season_number = self.request.GET.get('season_number', current_season.number)
100+
matches_won = len(
101+
[match for match in Match.objects.filter(matchday__season__number=season_number) if match.is_won])
102+
matches_draw = len(
103+
[match for match in Match.objects.filter(matchday__season__number=season_number) if match.is_draw])
104+
matches_lost = len(
105+
[match for match in Match.objects.filter(matchday__season__number=season_number) if match.is_lost])
106+
107+
json = {
108+
"matches_won": matches_won,
109+
"matches_draw": matches_draw,
110+
"matches_lost": matches_lost
111+
}
112+
113+
return self.render_json_response(json)
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
from braces.views import CsrfExemptMixin, JsonRequestResponseMixin
2+
from django.contrib.auth.decorators import login_required
3+
from django.utils.decorators import method_decorator
4+
from django.views import View
5+
from django.views.generic import TemplateView, DetailView
6+
7+
from core.models import Matchday, Contract, PlayerStatistics, AwpBoundaries, Player
8+
from core.rename_me_views.view_utils import validate_filtered_field
9+
10+
11+
@method_decorator(login_required, name='dispatch')
12+
class PlayerStatisticsView(TemplateView):
13+
template_name = 'core/ofm/player_statistics.html'
14+
15+
def get_context_data(self, **kwargs):
16+
matchdays = Matchday.objects.filter(player_statistics__isnull=False).distinct()
17+
18+
context = super(PlayerStatisticsView, self).get_context_data(**kwargs)
19+
context['matchdays'] = matchdays
20+
context['players_count'] = Contract.objects.filter(user=self.request.user, sold_on_matchday=None).count()
21+
22+
return context
23+
24+
25+
@method_decorator(login_required, name='dispatch')
26+
class PlayerStatisticsAsJsonView(CsrfExemptMixin, JsonRequestResponseMixin, View):
27+
def get(self, request, *args, **kwargs):
28+
contracts = Contract.objects.filter(user=self.request.user, sold_on_matchday=None)
29+
players = [contract.player for contract in contracts]
30+
current_matchday = Matchday.objects.all()[0]
31+
32+
newer_matchday_season = self.request.GET.get('newer_matchday_season', default=current_matchday.season.number)
33+
newer_matchday = self.request.GET.get('newer_matchday', default=current_matchday.number)
34+
older_matchday_season = self.request.GET.get('older_matchday_season')
35+
older_matchday = self.request.GET.get('older_matchday')
36+
diff_mode_enabled = older_matchday and older_matchday_season
37+
38+
player_statistics_tuples = []
39+
for player in players:
40+
newer_player_statistics, older_player_statistics = self._get_statistics_from_player_and_matchday(
41+
player,
42+
newer_matchday_season, newer_matchday,
43+
older_matchday_season, older_matchday
44+
)
45+
if newer_player_statistics and (older_player_statistics or not diff_mode_enabled):
46+
player_statistics_tuples.append((newer_player_statistics, older_player_statistics))
47+
48+
player_statistics_json = [
49+
self._get_player_statistics_diff_in_json(newer_player_statistics, older_player_statistics)
50+
for (newer_player_statistics, older_player_statistics) in player_statistics_tuples
51+
]
52+
53+
return self.render_json_response(player_statistics_json)
54+
55+
@staticmethod
56+
def _get_statistics_from_player_and_matchday(player,
57+
newer_matchday_season, newer_matchday,
58+
older_matchday_season, older_matchday):
59+
60+
newer_player_statistics = PlayerStatistics.objects.filter(
61+
player=player,
62+
matchday__season__number=newer_matchday_season,
63+
matchday__number=newer_matchday
64+
)
65+
older_player_statistics = PlayerStatistics.objects.filter(
66+
player=player,
67+
matchday__season__number=older_matchday_season,
68+
matchday__number=older_matchday
69+
)
70+
71+
newer_player_statistics = validate_filtered_field(newer_player_statistics)
72+
older_player_statistics = validate_filtered_field(older_player_statistics)
73+
74+
if not newer_player_statistics:
75+
newer_player_statistics = PlayerStatistics.objects.filter(
76+
player=player,
77+
matchday__season__number=newer_matchday_season
78+
).order_by('matchday')[0]
79+
80+
return newer_player_statistics, older_player_statistics
81+
82+
@staticmethod
83+
def _get_player_statistics_diff_in_json(newer_player_statistics, older_player_statistics):
84+
"""
85+
Args:
86+
newer_player_statistics: newer statistic
87+
older_player_statistics: older statistic
88+
89+
Returns:
90+
A dictionary of player statistics data. If st2 is None st1 is returned
91+
"""
92+
93+
strength = newer_player_statistics.strength
94+
if older_player_statistics:
95+
strength = newer_player_statistics.strength - older_player_statistics.strength
96+
ep = newer_player_statistics.ep
97+
if older_player_statistics:
98+
ep = newer_player_statistics.ep - older_player_statistics.ep
99+
tp = newer_player_statistics.tp
100+
if older_player_statistics:
101+
tp = newer_player_statistics.tp - older_player_statistics.tp
102+
awp = newer_player_statistics.awp
103+
if older_player_statistics:
104+
awp = newer_player_statistics.awp - older_player_statistics.awp
105+
freshness = newer_player_statistics.freshness
106+
if older_player_statistics:
107+
freshness = newer_player_statistics.freshness - older_player_statistics.freshness
108+
109+
awp_boundaries = AwpBoundaries.get_from_matchday(newer_player_statistics.matchday)
110+
awp_to_next_bound = awp_boundaries[newer_player_statistics.strength + 1] - newer_player_statistics.awp
111+
112+
statistic_diff = dict()
113+
statistic_diff['position'] = newer_player_statistics.player.position
114+
statistic_diff['age'] = newer_player_statistics.age
115+
statistic_diff['strength'] = strength
116+
statistic_diff['name'] = '<a href="%s">%s</a>' % (newer_player_statistics.player.get_absolute_url(),
117+
newer_player_statistics.player.name)
118+
statistic_diff['ep'] = ep
119+
statistic_diff['tp'] = tp
120+
statistic_diff['awp'] = awp
121+
statistic_diff['freshness'] = freshness
122+
statistic_diff['games_in_season'] = newer_player_statistics.games_in_season
123+
statistic_diff['goals_in_season'] = newer_player_statistics.goals_in_season
124+
statistic_diff['won_tacklings_in_season'] = newer_player_statistics.won_tacklings_in_season
125+
statistic_diff['lost_tacklings_in_season'] = newer_player_statistics.lost_tacklings_in_season
126+
statistic_diff['won_friendly_tacklings_in_season'] = newer_player_statistics.won_friendly_tacklings_in_season
127+
statistic_diff['lost_friendly_tacklings_in_season'] = newer_player_statistics.lost_friendly_tacklings_in_season
128+
statistic_diff['yellow_cards_in_season'] = newer_player_statistics.yellow_cards_in_season
129+
statistic_diff['red_cards_in_season'] = newer_player_statistics.red_cards_in_season
130+
statistic_diff['equity'] = newer_player_statistics.equity
131+
statistic_diff['awp_to_next_bound'] = awp_to_next_bound
132+
133+
return statistic_diff
134+
135+
136+
@method_decorator(login_required, name='dispatch')
137+
class PlayerDetailView(DetailView):
138+
context_object_name = 'player'
139+
template_name = 'core/ofm/player_detail.html'
140+
queryset = Player.objects.all()
141+
142+
def get_context_data(self, **kwargs):
143+
context = super(PlayerDetailView, self).get_context_data(**kwargs)
144+
145+
player = self.get_object()
146+
current_season = Matchday.objects.all()[0].season
147+
seasons = []
148+
player_stats = PlayerStatistics.objects.filter(player=player).order_by('matchday')
149+
for player_stat in player_stats:
150+
if player_stat.matchday.season not in seasons:
151+
seasons.append(player_stat.matchday.season)
152+
153+
context['seasons'] = seasons
154+
if player:
155+
context['player_age'] = current_season.number - player.birth_season.number
156+
context['player_strength'] = player_stats[0].strength
157+
158+
return context
159+
160+
def get_object(self, **kwargs):
161+
player = super(PlayerDetailView, self).get_object()
162+
contracts = Contract.objects.filter(user=self.request.user, player=player, sold_on_matchday=None)
163+
return player if contracts.count() > 0 else None
164+
165+
166+
@method_decorator(login_required, name='dispatch')
167+
class PlayerChartView(CsrfExemptMixin, JsonRequestResponseMixin, View):
168+
def get(self, request, *args, **kwargs):
169+
current_season_number = Matchday.objects.all()[0].season.number
170+
season_number = self.request.GET.get('season_number', default=current_season_number)
171+
player_id = self.request.GET.get('player_id')
172+
player = Player.objects.filter(id=player_id)
173+
player_statistics = PlayerStatistics.objects.filter(player=player, matchday__season__number=season_number)
174+
awps = [player_stat.awp for player_stat in player_statistics]
175+
176+
chart_json = {
177+
"series": [{
178+
"name": 'AWP',
179+
"data": awps
180+
}],
181+
"categories": [player_stat.matchday.number for player_stat in player_statistics]
182+
}
183+
184+
matchdays = [p.matchday for p in player_statistics]
185+
current_player_statistics = PlayerStatistics.objects.filter(player=player).order_by('matchday')[0]
186+
187+
current_awp_boundaries = AwpBoundaries.get_from_matchday(current_player_statistics.matchday)
188+
189+
for strength in current_awp_boundaries:
190+
if current_awp_boundaries[strength] >= min(awps) and strength > current_player_statistics.strength:
191+
awp_boundary_values = [AwpBoundaries.get_from_matchday(matchday)[strength] for matchday in matchdays]
192+
chart_json['series'].append({'name': 'AWP-Grenze: %s' % strength, 'data': awp_boundary_values})
193+
if current_awp_boundaries[strength] >= max(awps):
194+
break
195+
196+
return self.render_json_response(chart_json)

0 commit comments

Comments
 (0)