Skip to content

Commit

Permalink
Create second SeasonAdmin for Seat/Season relation, because if seat/s…
Browse files Browse the repository at this point in the history
…eason is created at the same time than team/season, is bugged because is not possible to know if seat.team is present in season before save.
  • Loading branch information
SRJ9 committed Nov 20, 2016
1 parent 131f3d9 commit 9306d96
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 14 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ Versions
========
- 0.14c (Fernando Alonso 14)
- 0.16c (Race to Championship '16)
- 0.17x
- 0.19x
- 0.27-VIL (Gilles Villeneuve 27)

0.17x (in process)
0.19x (in process)
==================
Spanish translation
- Spanish translation
- Link to copy season copying teams and races to add_view. Seats is potentially bugged by team dependency (both would be create at the same time).
- Fix bugs founded in previous versions.

0.16
====
Expand Down
8 changes: 5 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ Versions

- 0.14c (Fernando Alonso 14)
- 0.16c (Race to Championship '16)
- 0.17x
- 0.19x
- 0.27-VIL (Gilles Villeneuve 27)

0.17x (in process)
=================
0.19x (in process)
==================
- Spanish translation
- Link to copy season copying teams and races to add_view. Seats is potentially bugged by team dependency (both would be create at the same time).
- Fix bugs founded in previous versions.

0.16
====
Expand Down
47 changes: 41 additions & 6 deletions driver27/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
from tabbed_admin import TabbedModelAdmin

from . import punctuation
from .models import Contender, Seat, TeamSeason, SeatSeason
from .models import Contender, Seat, TeamSeason, SeatSeason, SeatsSeason
from .models import ContenderSeason
from .models import Driver, Team, Competition, Circuit, Season, GrandPrix, Race, Result

lr_diff = lambda l, r: list(set(l).difference(r))
lr_intr = lambda l, r: list(set(l).intersection(r))


# http://stackoverflow.com/a/34567383
class AlwaysChangedModelForm(forms.ModelForm):
def is_empty_form(self, *args, **kwargs):
Expand Down Expand Up @@ -62,7 +63,9 @@ def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
elif db_field.name == 'grand_prix':
kwargs['queryset'] = GrandPrix.objects.filter(competitions__exact=request._obj_.competition)
elif db_field.name == 'seat':
kwargs['queryset'] = Seat.objects.filter(contender__competition__exact=request._obj_.competition)
kwargs['queryset'] = Seat.objects.filter(
contender__competition__exact=request._obj_.competition,
team__seasons__exact=request._obj_)
return super(CompetitionFilterInline, self).formfield_for_foreignkey(db_field, request, **kwargs)


Expand Down Expand Up @@ -139,7 +142,7 @@ def __init__(self, *args, **kwargs):


class SeatSeasonInline(CompetitionFilterInline):
model = Seat.seasons.through
model = SeatSeason
ordering = ('seat',)
formset = SeatSeasonFormSet
form = AlwaysChangedModelForm
Expand Down Expand Up @@ -274,8 +277,7 @@ class Meta:
fields = ('year', 'competition', 'rounds', 'punctuation')


class SeasonAdmin(TabbedModelAdmin):
form = SeasonAdminForm
class SeatSeasonAdmin(TabbedModelAdmin):
tab_overview = (
(None, {
'fields': ('year', 'competition', 'rounds', 'punctuation')
Expand All @@ -287,13 +289,45 @@ class SeasonAdmin(TabbedModelAdmin):
tab_drivers = (
SeatSeasonInline,
)
tabs = [
('Overview', tab_overview),
('Drivers', tab_drivers),
]

readonly_fields = ('year', 'competition', 'rounds', 'punctuation')
list_filter = ('competition',)
list_display = ('competition', 'year',)

def has_add_permission(self, request):
return False

def get_form(self, request, obj=None, **kwargs):
# just save obj reference for future processing in Inline
if request and obj:
request._obj_ = obj
return super(SeatSeasonAdmin, self).get_form(request=request, obj=obj, **kwargs)


class SeasonAdmin(TabbedModelAdmin):
form = SeasonAdminForm
tab_overview = (
(None, {
'fields': ('year', 'competition', 'rounds', 'punctuation')
}),
)
tab_teams = (
TeamSeasonInline,
)
# tab_drivers = (
# SeatSeasonInline,
# )
tab_races = (
RaceInline,
)
tabs = [
('Overview', tab_overview),
('Teams', tab_teams),
('Drivers', tab_drivers),
# ('Drivers', tab_drivers),
('Races', tab_races),
]
readonly_fields = ('print_copy_season',)
Expand Down Expand Up @@ -550,3 +584,4 @@ def get_form(self, request, obj=None, **kwargs):

# m2m admin
admin.site.register(Contender, ContenderAdmin)
admin.site.register(SeatsSeason, SeatSeasonAdmin)
10 changes: 9 additions & 1 deletion driver27/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def __str__(self):

class Meta:
unique_together = ('team', 'contender')
ordering = ['contender__driver__last_name', 'team']
ordering = ['current', 'contender__driver__last_name', 'team']
verbose_name = _('Seat')
verbose_name_plural = _('Seats')

Expand Down Expand Up @@ -268,6 +268,14 @@ class Meta:
verbose_name_plural = _('Seasons')


class SeatsSeason(Season):
# aux proxy class to use in Admin for Season/Seat relation from Season
class Meta:
proxy = True
verbose_name = _('Seats by season')
verbose_name_plural = _('Seats by season')


@python_2_unicode_compatible
class Race(models.Model):
ALTER_PUNCTUATION = (
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='django-driver27',
version='0.17x',
version='0.19x',
include_package_data=True,
packages=find_packages(),
url='https://github.com/SRJ9/django-driver27.git',
Expand Down

0 comments on commit 9306d96

Please sign in to comment.