This repository has been archived by the owner on Jan 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.py
139 lines (108 loc) · 4.48 KB
/
admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from django.contrib import admin
from django.db import models
from django.db.models import Q
from django.db.models.signals import m2m_changed
from django.forms import TextInput, Textarea, IntegerField
from .models import Team, Season, Schedule, Standings, Player, STANDINGS_ORDER
from django.utils import timezone
def standings_save(instance):
season = Season.objects.get(pk=instance.pk)
for team in season.teams.all():
obj, created = Standings.objects.get_or_create(season = season, team = team)
standings = Standings.objects.filter(season = season).exclude(team__in = season.teams.all())
for team in standings:
team.delete()
def standings_position_update(season):
order = STANDINGS_ORDER[season.standings_order][1]
standings = Standings.objects.filter(season = season.pk).order_by(*order)
position = 0
for team in standings:
position += 1
team.position = position
team.save()
def standings_update(instance):
standings = Standings.objects.filter(season = instance.pk)
now = timezone.now()
for standing in standings:
points = 0
wins = 0
lost = 0
draws = 0
matches = 0
score = 0
score_lost = 0
team = standing.team
team_schedule = Schedule.objects.filter(Q(home_team=team) | Q(away_team=team), season = instance.pk, date__lte=now )
for match in team_schedule:
matches += 1
if not match.home_team_score:
match.home_team_score = 0
if not match.away_team_score:
match.away_team_score = 0
if match.home_team == team:
score += match.home_team_score
score_lost += match.away_team_score
if match.home_team_score > match.away_team_score:
wins += 1
points += instance.win_points
elif match.home_team_score < match.away_team_score:
lost += 1
points += instance.lost_points
else:
draws += 1
points += instance.draw_points
if match.away_team == team:
score += match.away_team_score
score_lost += match.home_team_score
if match.away_team_score > match.home_team_score:
wins += 1
points += instance.win_points
elif match.away_team_score < match.home_team_score:
lost += 1
points += instance.lost_points
else:
draws += 1
points += instance.draw_points
standing.points = points
standing.win = wins
standing.lost = lost
standing.draws = draws
standing.score = score
standing.score_lost = score_lost
standing.matches = matches
standing.save()
standings_position_update(instance)
class TeamAdmin(admin.ModelAdmin):
list_display = ('name', 'short_name', 'my_team')
prepopulated_fields = {'slug': ('short_name',), }
class ScheduleInline(admin.TabularInline):
model = Schedule
formfield_overrides = {
models.IntegerField: {'widget': TextInput(attrs={'style':'width: 20px;'})},
}
class StandingsInline(admin.TabularInline):
model = Standings
ordering = ('position', '-points')
exclude = ('matches', 'win', 'lost', 'draws', 'score', 'score_lost')
max_num=0
actions = []
readonly_fields = ('team',)
fields = ('team', 'roster_image', 'points', 'position')
class SeasonAdmin(admin.ModelAdmin):
inlines = [
StandingsInline,
ScheduleInline,
]
prepopulated_fields = {'slug': ('name', 'league',), }
def save_model(self, request, obj, form, change):
obj.save()
form.save_m2m()
standings_save(obj)
standings_update(obj)
class PlayerAdmin(admin.ModelAdmin):
list_display = ('name', 'surename', 'weight', 'height', 'team', 'season')
list_filter = ('team', 'season')
admin.site.register(Team, TeamAdmin)
admin.site.register(Season, SeasonAdmin)
admin.site.register(Player, PlayerAdmin)
# Register your models here.