-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pep8 for schedules app #23
Changes from all commits
28e3aca
f16c522
5baccd5
69e7586
458dffb
108ee6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,9 +78,6 @@ def copy_ScheduleBoard(self, request, queryset): | |
copy_ScheduleBoard.short_description = _("Make a Copy of calendar") | ||
|
||
|
||
admin.site.register(ScheduleBoard, ScheduleBoardAdmin) | ||
|
||
|
||
class FullcalendarAdmin(admin.ModelAdmin): | ||
def schedule_detail(self, request): | ||
return HttpResponseRedirect(reverse("dashboard:schedule_editor")) | ||
|
@@ -97,8 +94,8 @@ def get_urls(self): | |
'app_name': self.model._meta.app_label, | ||
'model_name': self.model._meta.model_name, | ||
} | ||
custom_urls = patterns('', | ||
url( | ||
custom_urls = patterns( | ||
'', url( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was mangled by the pep8 tool (continuation line under-indented for visual indent) |
||
r'^$', | ||
self.admin_site.admin_view(self.schedule_detail), | ||
{}, | ||
|
@@ -111,11 +108,12 @@ def get_urls(self): | |
return custom_urls + urls | ||
|
||
def response_change(self, request, obj): | ||
msg = _('%(obj)s was changed successfully.') % {'obj': force_unicode(obj)} | ||
msg = _('{obj} was changed successfully.'.format(obj=force_unicode(obj))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer format() over "%" syntax, but this is just personal taste. Feel free to mangle this change. |
||
if '_continue' in request.POST: | ||
return HttpResponseRedirect(request.path) | ||
else: | ||
return HttpResponseRedirect("../../") | ||
|
||
|
||
admin.site.register(ScheduleBoard, ScheduleBoardAdmin) | ||
admin.site.register(Schedule, FullcalendarAdmin) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bunch admin registration at the end of module |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
|
||
from apps.programmes.models import Programme, Episode | ||
|
||
|
||
emission_type = ( | ||
("L", _("live")), | ||
("B", _("broadcast")), | ||
|
@@ -61,22 +62,21 @@ def clean(self): | |
if self.start_date > self.end_date: | ||
raise ValidationError(_('end date must be greater than or equal to start date.')) | ||
# check date collision | ||
qs = ScheduleBoard.objects.filter( | ||
start_date__lte=self.end_date, end_date__isnull=True | ||
) | ScheduleBoard.objects.filter( | ||
start_date__lte=self.end_date, end_date__gte=self.start_date | ||
qs = ( | ||
ScheduleBoard.objects.filter(start_date__lte=self.end_date, end_date__isnull=True) | | ||
ScheduleBoard.objects.filter(start_date__lte=self.end_date, end_date__gte=self.start_date) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make code more readable. The important part is to apply multiple filters, so the code should not spread this across multiple indentation levels, but should reflect this by intuitive reading. |
||
) | ||
|
||
if self.pk is not None: | ||
qs = qs.exclude(pk=self.pk) | ||
if qs.exists(): | ||
raise ValidationError(_('there is another object between this dates.')) | ||
|
||
else: | ||
# start_date != None and end_date == None only one can exist | ||
qs = ScheduleBoard.objects.filter( | ||
start_date__isnull=False, end_date__isnull=True | ||
) | ScheduleBoard.objects.filter( | ||
end_date__gte=self.start_date | ||
qs = ( | ||
ScheduleBoard.objects.filter(start_date__isnull=False, end_date__isnull=True) | | ||
ScheduleBoard.objects.filter(end_date__gte=self.start_date) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make code more readable. The important part is to apply multiple filters, so the code should not spread this across multiple indentation levels, but should reflect this by intuitive reading. |
||
) | ||
if self.pk is not None: | ||
qs = qs.exclude(pk=self.pk) | ||
|
@@ -86,9 +86,6 @@ def clean(self): | |
|
||
elif self.end_date: | ||
raise ValidationError(_('start date cannot be null if end date exists')) | ||
else: | ||
# both None = disable | ||
pass | ||
|
||
def save(self, *args, **kwargs): | ||
# rearrange episodes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has no effect at all. |
||
|
@@ -104,11 +101,10 @@ def save(self, *args, **kwargs): | |
|
||
@classmethod | ||
def get_current(cls, dt): | ||
schedule_board = cls.objects.filter( | ||
start_date__lte=dt, end_date__isnull=True | ||
).order_by('-start_date') | cls.objects.filter( | ||
start_date__lte=dt, end_date__gte=dt | ||
).order_by('-start_date') | ||
schedule_board = ( | ||
cls.objects.filter(start_date__lte=dt, end_date__isnull=True).order_by('-start_date') | | ||
cls.objects.filter(start_date__lte=dt, end_date__gte=dt).order_by('-start_date') | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make code more readable. The important part is to apply multiple filters, so the code should not spread this across multiple indentation levels, but should reflect this by intuitive reading. |
||
return schedule_board.first() | ||
|
||
class Meta: | ||
|
@@ -149,24 +145,23 @@ def __get_rrule(self): | |
# Due to rrule we need to add 1 day | ||
end_date = end_date + datetime.timedelta(days=1) | ||
return rrule.rrule( | ||
rrule.WEEKLY, byweekday=[self.day], dtstart=datetime.datetime.combine( | ||
start_date, self.start_hour | ||
), until=end_date | ||
) | ||
rrule.WEEKLY, byweekday=[self.day], | ||
dtstart=datetime.datetime.combine(start_date, self.start_hour), | ||
until=end_date) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make code more readable, do not split datetime.combine function call across multiple lines resulting in spread rrule params where we are not forced to do so. |
||
else: | ||
end_date = self.schedule_board.end_date | ||
if end_date: | ||
# Due to rrule we need to add 1 day | ||
end_date = end_date + datetime.timedelta(days=1) | ||
return rrule.rrule( | ||
rrule.WEEKLY, byweekday=[self.day], dtstart=datetime.datetime.combine( | ||
start_date, self.start_hour | ||
), until=end_date | ||
rrule.WEEKLY, byweekday=[self.day], | ||
dtstart=datetime.datetime.combine(start_date, self.start_hour), | ||
until=end_date | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make code more readable, do not split datetime.combine function call across multiple lines resulting in spread rrule params where we are not forced to do so. |
||
) | ||
else: | ||
return rrule.rrule( | ||
rrule.WEEKLY, byweekday=[self.day], dtstart=datetime.datetime.combine( | ||
start_date, self.start_hour) | ||
rrule.WEEKLY, byweekday=[self.day], | ||
dtstart=datetime.datetime.combine(start_date, self.start_hour) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make code more readable, do not split datetime.combine function call across multiple lines resulting in spread rrule params where we are not forced to do so. |
||
) | ||
|
||
def dates_between(self, after, before): | ||
|
@@ -209,16 +204,11 @@ def clean(self): | |
start_date = date | ||
end_date = start_date + schedule.runtime() | ||
raise ValidationError( | ||
_( | ||
'This settings collides with: %(name)s [%(start_date)s %(start_day)s/%(start_month)s/%(start_year)s - %(end_date)s %(end_day)s/%(end_month)s/%(end_year)s ]' | ||
) % { | ||
'name': schedule.programme.name, 'start_date': start_date.strftime("%H:%M"), | ||
'start_day': start_date.strftime("%d"), | ||
'start_month': start_date.strftime("%m"), | ||
'start_year': start_date.strftime("%Y"), | ||
'end_date': end_date.strftime("%H:%M"), 'end_day': end_date.strftime("%d"), | ||
'end_month': end_date.strftime("%m"), 'end_year': end_date.strftime("%Y"), | ||
} | ||
_('This settings collides with: {name} [{start_date} - {end_date}]').format( | ||
name=schedule.programme.name, | ||
start_date=start_date.strftime("%H:%M %d/%m/%Y"), | ||
end_date=end_date.strftime("%H:%M %d/%m/%Y") | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. datetime.strftime() is already dedicated to perform date string formating. We do not need to increase complexity to format the date string a second time on multiple levels. |
||
) | ||
index += 1 | ||
|
||
|
@@ -230,23 +220,22 @@ def save(self, *args, **kwargs): | |
|
||
@classmethod | ||
def between(cls, after, before, exclude=None, live=False, schedule_board=None): | ||
list_schedules = cls.objects.filter( | ||
programme__start_date__lte=before, programme__end_date__isnull=True | ||
) | cls.objects.filter( | ||
programme__start_date__lte=before, programme__end_date__gte=after | ||
list_schedules = ( | ||
cls.objects.filter(programme__start_date__lte=before, programme__end_date__isnull=True) | | ||
cls.objects.filter(programme__start_date__lte=before, programme__end_date__gte=after) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make code more readable. The important part is to apply multiple filters, so the code should not spread this across multiple indentation levels, but should reflect this by intuitive reading. |
||
) | ||
if live: | ||
list_schedules = list_schedules.filter(type='L') | ||
if schedule_board: | ||
list_schedules = list_schedules.filter(schedule_board=schedule_board) | ||
else: | ||
list_schedules = list_schedules.filter( | ||
schedule_board__start_date__lte=before, schedule_board__end_date__isnull=True | ||
) | list_schedules.filter( | ||
schedule_board__start_date__lte=before, schedule_board__end_date__gte=after | ||
list_schedules = ( | ||
list_schedules.filter(schedule_board__start_date__lte=before, schedule_board__end_date__isnull=True) | | ||
list_schedules.filter(schedule_board__start_date__lte=before, schedule_board__end_date__gte=after) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make code more readable. The important part is to apply multiple filters, so the code should not spread this across multiple indentation levels, but should reflect this by intuitive reading. |
||
) | ||
if exclude: | ||
list_schedules = list_schedules.exclude(id=exclude.id) | ||
|
||
list_schedules = list_schedules.order_by('-programme__start_date').select_related( | ||
'programme', 'schedule_board', 'source__programme', 'source__schedule_board' | ||
) | ||
|
@@ -262,18 +251,16 @@ def between(cls, after, before, exclude=None, live=False, schedule_board=None): | |
|
||
@classmethod | ||
def schedule(cls, dt, exclude=None): | ||
list_schedules = cls.objects.filter( | ||
programme__start_date__lte=dt, programme__end_date__isnull=True | ||
) | cls.objects.filter( | ||
programme__start_date__lte=dt, programme__end_date__gte=dt | ||
list_schedules = ( | ||
cls.objects.filter(programme__start_date__lte=dt, programme__end_date__isnull=True) | | ||
cls.objects.filter(programme__start_date__lte=dt, programme__end_date__gte=dt) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make code more readable. The important part is to apply multiple filters, so the code should not spread this across multiple indentation levels, but should reflect this by intuitive reading. |
||
) | ||
if exclude: | ||
list_schedules = list_schedules.exclude(id=exclude.id) | ||
list_schedules = list_schedules.select_related('programme', 'schedule_board') | ||
earlier_date = None | ||
earlier_schedule = None | ||
for schedule in list_schedules: | ||
# if schedule != exclude: | ||
date = schedule.date_before(dt) | ||
if date and (earlier_date is None or date > earlier_date): | ||
earlier_date = date | ||
|
@@ -285,20 +272,17 @@ def schedule(cls, dt, exclude=None): | |
@classmethod | ||
def get_next_date(cls, programme, after): | ||
list_schedules = cls.objects.filter(programme=programme, type='L') | ||
list_schedules = list_schedules.filter( | ||
programme__end_date__isnull=True | ||
) | list_schedules.filter( | ||
programme__end_date__gte=after | ||
list_schedules = ( | ||
list_schedules.filter(programme__end_date__isnull=True) | | ||
list_schedules.filter(programme__end_date__gte=after) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make code more readable. The important part is to apply multiple filters, so the code should not spread this across multiple indentation levels, but should reflect this by intuitive reading. |
||
) | ||
list_schedules = list_schedules.filter( | ||
schedule_board__start_date__isnull=False, schedule_board__end_date__isnull=True | ||
) | list_schedules.filter( | ||
schedule_board__end_date__gte=after | ||
list_schedules = ( | ||
list_schedules.filter(schedule_board__start_date__isnull=False, schedule_board__end_date__isnull=True) | | ||
list_schedules.filter(schedule_board__end_date__gte=after) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make code more readable. The important part is to apply multiple filters, so the code should not spread this across multiple indentation levels, but should reflect this by intuitive reading. |
||
) | ||
closer_date = None | ||
closer_schedule = None | ||
for schedule in list_schedules: | ||
# if schedule != exclude: | ||
date = schedule.date_after(after) | ||
if date and (closer_date is None or date < closer_date): | ||
closer_date = date | ||
|
@@ -308,7 +292,7 @@ def get_next_date(cls, programme, after): | |
return closer_schedule, closer_date | ||
|
||
def __unicode__(self): | ||
return self.get_day_display() + ' - ' + self.start_hour.strftime('%H:%M') | ||
return ' - '.join([self.get_day_display(), self.start_hour.strftime('%H:%M')]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is classical string joining, so no need for hacky string arithmetic. |
||
|
||
class Meta: | ||
verbose_name = _('schedule') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bunch admin registration at the end of module to not spread registration logic across the code