Skip to content

Commit

Permalink
- Refactor punctuation/records common code
Browse files Browse the repository at this point in the history
- Templates moves to subfolder
  • Loading branch information
SRJ9 committed Dec 28, 2016
1 parent 413459a commit 7ddc3f4
Show file tree
Hide file tree
Showing 35 changed files with 308 additions and 372 deletions.
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ recursive-include driver27/admin *.py
recursive-include driver27/fixtures *.json
recursive-include driver27/locale *
recursive-include driver27/migrations *.py
recursive-include driver27/records *.py
recursive-include driver27/static *.css
recursive-include driver27/templates *.html
recursive-include driver27/templatetags *.py
Expand Down
5 changes: 2 additions & 3 deletions driver27/admin/forms.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from django.db.models.fields import BLANK_CHOICE_DASH
from .common import *
from .. import punctuation
from ..punctuation import get_punctuation_label_dict
from ..models import Season


class SeasonAdminForm(AlwaysChangedModelForm):

def __init__(self, *args, **kwargs):
super(SeasonAdminForm, self).__init__(*args, **kwargs)
punctuation_dict = punctuation.DRIVER27_PUNCTUATION
punctuation_choices = [(punct['code'], punct['label']) for punct in punctuation_dict]
punctuation_choices = get_punctuation_label_dict()
self.fields['punctuation'] = forms.ChoiceField(choices=BLANK_CHOICE_DASH + list(punctuation_choices),
initial=None)

Expand Down
2 changes: 1 addition & 1 deletion driver27/admin/inlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class RaceInline(CompetitionFilterInline):
model = Race
extra = 1
readonly_fields = ('print_results_link', )
# readonly_fields = ('print_results_link', )
formset = RaceFormSet
form = AlwaysChangedModelForm

Expand Down
67 changes: 67 additions & 0 deletions driver27/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from django.conf import settings
from django.utils.translation import ugettext as _


def get_init_config():
return {
'RECORDS': {
'POLE': {'label': _('Pole'), 'filter': {'qualifying__exact': 1}},
'FIRST-ROW': {'label': _('First row'), 'filter': {'qualifying__gte': 1, 'qualifying__lte': 2}},
'COMEBACK-TO-TEN': {'label': _('Comeback to 10 firsts'), 'filter': {'qualifying__gte': 11,
'finish__gte': 1,
'finish__lte': 10}},
'WIN': {'label': _('Win'), 'filter': {'finish__exact': 1}},
'POLE-WIN': {'code': 'POLE-WIN', 'label': _('Pole and Win'), 'filter': {'qualifying__exact': 1,
'finish__exact': 1}},
'POLE-WIN-FL': {'label': _('Pole, Win, Fastest lap'), 'filter': {'qualifying__exact': 1,
'finish__exact': 1,
'fastest_lap': True}},
'FASTEST': {'label': _('Fastest lap'), 'filter': {'fastest_lap': True}},
'FIRST-TWO': {'label': _('Finish 1st or 2nd'), 'filter': {'finish__gte': 1, 'finish__lte': 2},
'doubles': True},
'PODIUM': {'label': _('Podium'), 'filter': {'finish__gte': 1, 'finish__lte': 3}, 'doubles': True},
'OUT': {'label': _('OUT'), 'filter': {'retired': True}, 'doubles': True},
'CHECKERED-FLAG': {'label': _('Checkered Flag'), 'filter': {'retired': False}, 'doubles': True},
'TOP5': {'label': _('Top 5'), 'filter': {'finish__gte': 1, 'finish__lte': 5}, 'doubles': True},
'TOP10': {'code': 'TOP10', 'label': _('Top 10'), 'filter': {'finish__gte': 1, 'finish__lte': 10},
'doubles': True},
},
'PUNCTUATION': {
'F1-25': {'type': 'full', 'finish': [25, 18, 15, 12, 10, 8, 6, 4, 2, 1], 'fastest_lap': 0,
'label': 'F1 (25p 1st)'},
'F1-10+8': {'type': 'full', 'finish': [10, 8, 6, 4, 3, 2, 1], 'fastest_lap': 0,
'label': 'F1 (10p 1st, 8p 2nd)'},
'F1-10+6': {'type': 'full', 'finish': [10, 6, 4, 3, 2, 1], 'fastest_lap': 0,
'label': 'F1 (10p 1st, 6p 2nd)'},
'MotoGP': {'type': 'full', 'finish': [25, 20, 16, 13, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], 'fastest_lap': 0,
'label': 'Moto GP'},
'MotoGP-92': {'type': 'full', 'finish': [20, 15, 12, 10, 8, 6, 4, 3, 2, 1], 'fastest_lap': 0,
'label': 'Moto GP (only 1992)'},
'MotoGP-88-91': {'code': 'MotoGP-88-91', 'type': 'full',
'finish': [20, 17, 15, 13, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1], 'fastest_lap': 0,
'label': 'Moto GP (1988-91)'},
'MotoGP-77-87': {'type': 'full', 'finish': [15, 12, 10, 8, 6, 6, 5, 3, 2, 1], 'fastest_lap': 0,
'label': 'Moto GP (1977-87)'},
}
}


def get_settings_config():
config_key = 'DR27_CONFIG'
config = {}
if hasattr(settings, config_key):
config.update(getattr(settings, config_key))
return config


def get_config(param, key=None):
config = {}
init_config = get_init_config().get(param)
if init_config:
config.update(init_config)
settings_config = get_settings_config().get(param)
if settings_config:
config.update(settings_config)
if key:
return config.get(key, None)
return config
9 changes: 2 additions & 7 deletions driver27/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext as _
from . import punctuation
from .punctuation import get_punctuation_config
from slugify import slugify
from django_countries.fields import CountryField
from exclusivebooleanfield.fields import ExclusiveBooleanField
Expand Down Expand Up @@ -179,12 +179,7 @@ class Season(models.Model):
def get_scoring(self, code=None):
if not code:
code = self.punctuation
for scoring in punctuation.DRIVER27_PUNCTUATION:
if scoring['code'] == code:
return scoring
return None


return get_punctuation_config(code)

def pending_races(self):
past_races = Race.objects.filter(season=self, results__pk__isnull=False).distinct().count()
Expand Down
66 changes: 11 additions & 55 deletions driver27/punctuation.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,13 @@
from django.conf import settings
from .config import get_config

DRIVER27_PUNCTUATION = [
{
'code': 'F1-25',
'type': 'full',
'finish': [25,18,15,12,10,8,6,4,2,1],
'fastest_lap': 0,
'label': 'F1 (25p 1st)'
},
{
'code': 'F1-10+8',
'type': 'full',
'finish': [10, 8, 6, 4, 3, 2, 1],
'fastest_lap': 0,
'label': 'F1 (10p 1st, 8p 2nd)'
},
{
'code': 'F1-10+6',
'type': 'full',
'finish': [10, 6, 4, 3, 2, 1],
'fastest_lap': 0,
'label': 'F1 (10p 1st, 6p 2nd)'
},
{
'code': 'MotoGP',
'type': 'full',
'finish': [25,20,16,13,11,10,9,8,7,6,5,4,3,2,1],
'fastest_lap': 0,
'label': 'Moto GP'
},
{
'code': 'MotoGP-92',
'type': 'full',
'finish': [20,15,12,10,8,6,4,3,2,1],
'fastest_lap': 0,
'label': 'Moto GP (only 1992)'
},
{
'code': 'MotoGP-88-91',
'type': 'full',
'finish': [20,17,15,13,11,10,9,8,7,6,5,4,3,2,1],
'fastest_lap': 0,
'label': 'Moto GP (1988-91)'
},
{
'code': 'MotoGP-77-87',
'type': 'full',
'finish': [15,12,10,8,6,6,5,3,2,1],
'fastest_lap': 0,
'label': 'Moto GP (1977-87)'
}
]

DRIVER27_PUNCTUATION_EXTEND = getattr(settings, 'DRIVER27_PUNCTUATION', None)
if DRIVER27_PUNCTUATION_EXTEND:
DRIVER27_PUNCTUATION.extend(DRIVER27_PUNCTUATION_EXTEND)
def get_punctuation_config(punctuation_code=None):
config = get_config('PUNCTUATION', key=punctuation_code)
return config


def get_punctuation_label_dict():
config = get_punctuation_config()
if config:
return [(punctuation_dict.get('label'), index) for index, punctuation_dict in config.items()]
return None
13 changes: 13 additions & 0 deletions driver27/records.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from .config import get_config


def get_record_config(record_code=None):
config = get_config('RECORDS', key=record_code)
return config


def get_record_label_dict():
config = get_record_config()
if config:
return [(record_dict.get('label'), index) for index, record_dict in config.items()]
return None
5 changes: 0 additions & 5 deletions driver27/records/__init__.py

This file was deleted.

31 changes: 0 additions & 31 deletions driver27/records/filters.py

This file was deleted.

46 changes: 0 additions & 46 deletions driver27/records/schema.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
{% extends 'driver27/base.html' %}
{% load bootstrap3 champion_filter i18n %}

{% block dr27_menu %}
{{ block.super }}
<li class="nav-item">
<a class="nav-link" href="{% url 'competition-view' competition.slug %}">{{ competition }}</a>
</li>
{% endblock %}

{% block dr27_content %}
{{ block.super }}
<table class="table table-condensed table-striped table-hover">
<tr>
<th class="col-md-2">{% trans 'Season' %}</th>
<th class="col-md-4">{% trans 'Driver leader' %}</th>
<th class="col-md-6">{% trans 'Team leader' %}</th>
</tr>
{% for season in competition.seasons.all %}
<tr>
<td>
<a href="{% url 'season-view' competition.slug season.year %}">
{{ season }}
</a>
</td>
<td>
{{ season.pk | champion_filter | safe}}
{{ season.leader.0 }} {{ season.leader.1 }} ({{ season.leader.2 }})
</td>
<td>
{{ season.team_leader.0 }} {{ season.team_leader.1 }}
</td>
</tr>
{% endfor %}
</table>


{% endblock %}
{% extends 'driver27/base.html' %}
{% load bootstrap3 champion_filter i18n %}

{% block dr27_menu %}
{{ block.super }}
<li class="nav-item">
<a class="nav-link" href="{% url 'competition-view' competition.slug %}">{{ competition }}</a>
</li>
{% endblock %}

{% block dr27_content %}
{{ block.super }}
<table class="table table-condensed table-striped table-hover">
<tr>
<th class="col-md-2">{% trans 'Season' %}</th>
<th class="col-md-4">{% trans 'Driver leader' %}</th>
<th class="col-md-6">{% trans 'Team leader' %}</th>
</tr>
{% for season in competition.seasons.all %}
<tr>
<td>
<a href="{% url 'season-view' competition.slug season.year %}">
{{ season }}
</a>
</td>
<td>
{{ season.pk | champion_filter | safe}}
{{ season.leader.0 }} {{ season.leader.1 }} ({{ season.leader.2 }})
</td>
<td>
{{ season.team_leader.0 }} {{ season.team_leader.1 }}
</td>
</tr>
{% endfor %}
</table>


{% endblock %}
23 changes: 0 additions & 23 deletions driver27/templates/driver27/driver-list.html

This file was deleted.

Loading

0 comments on commit 7ddc3f4

Please sign in to comment.