From 0b59b4b140853e53841f6202b6250ff626bb0c9b Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 27 Jan 2018 23:12:09 +0100 Subject: [PATCH] Enforce some pep8 code style --- khaganat/settings.py | 5 ++- khaganat/urls.py | 2 +- logs/admin.py | 2 +- logs/models.py | 1 + logs/urls.py | 5 ++- logs/views.py | 60 +++++++++++++++++++++++++++++------ navbar/admin.py | 5 ++- navbar/models.py | 21 ++++++++++-- navbar/templatetags/navbar.py | 8 ++++- neluser/models.py | 6 ++-- pages/admin.py | 5 ++- pages/models.py | 1 + pages/views.py | 8 +++-- 13 files changed, 105 insertions(+), 24 deletions(-) diff --git a/khaganat/settings.py b/khaganat/settings.py index 41eb692..ed34c59 100644 --- a/khaganat/settings.py +++ b/khaganat/settings.py @@ -26,7 +26,10 @@ SECRET_KEY = os.getenv('KHAGANAT_SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = bool(os.getenv('KHAGANAT_DEBUG', default='')) -ALLOWED_HOSTS = [_ for _ in os.getenv('KHAGANAT_HOSTNAMES', default='').split(',') if _ != ''] +ALLOWED_HOSTS = [ + _ for _ in os.getenv('KHAGANAT_HOSTNAMES', default='').split(',') + if _ != '' +] # Application definition diff --git a/khaganat/urls.py b/khaganat/urls.py index 9205bba..6a3f0cf 100644 --- a/khaganat/urls.py +++ b/khaganat/urls.py @@ -16,7 +16,7 @@ Including another URLconf from django.views.generic.base import RedirectView from django.conf.urls.i18n import i18n_patterns from django.contrib import admin -from django.urls import include,path +from django.urls import include, path from pages.views import index urlpatterns = [ diff --git a/logs/admin.py b/logs/admin.py index 19a3f73..fe46e23 100644 --- a/logs/admin.py +++ b/logs/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from .models import Source,Entry +from .models import Source, Entry admin.site.register(Source) admin.site.register(Entry) diff --git a/logs/models.py b/logs/models.py index c83b978..6684f8f 100644 --- a/logs/models.py +++ b/logs/models.py @@ -1,5 +1,6 @@ from django.db import models + class Source(models.Model): name = models.CharField(max_length=128) slug = models.CharField(max_length=128, unique=True) diff --git a/logs/urls.py b/logs/urls.py index 5a8c81e..a204c22 100644 --- a/logs/urls.py +++ b/logs/urls.py @@ -4,5 +4,8 @@ from . import views urlpatterns = [ path('', views.IndexView.as_view(), name='log_index'), - path('////', views.EntriesView.as_view(), name='log_day'), + path( + '////', + views.EntriesView.as_view(), name='log_day' + ), ] diff --git a/logs/views.py b/logs/views.py index fd40014..ca0b4eb 100644 --- a/logs/views.py +++ b/logs/views.py @@ -2,21 +2,41 @@ from django.db.models.functions import TruncDate from django.db.models import Count from django.views import generic from django.conf import settings -from .models import Source,Entry +from .models import Source, Entry import datetime + class IndexView(generic.ListView): template_name = 'logs/index.html' context_object_name = 'sources' def get_queryset(self): now = datetime.date.today() - begin_date = now - datetime.timedelta(days=settings.KHAGANAT_LOGS_MAX_DAYS) - end_date = now - datetime.timedelta(days=settings.KHAGANAT_LOGS_MIN_DAYS - 1) + start_date = now - datetime.timedelta( + days=settings.KHAGANAT_LOGS_MAX_DAYS + ) + end_date = now - datetime.timedelta( + days=settings.KHAGANAT_LOGS_MIN_DAYS - 1 + ) out = [] - for src in Entry.objects.filter(hidden=False, source__hidden=False, created__range=(begin_date, end_date)).values('source', 'source__name', 'source__slug').annotate(nb=Count('id')): - src['stats'] = Entry.objects.filter(hidden=False, source=src['source'], created__range=(begin_date, end_date)).annotate(date=TruncDate('created')).values('date').annotate(nb=Count('date')).order_by('-date') + qs = Entry.objects.filter( + hidden=False, + source__hidden=False, + created__range=(start_date, end_date) + ).values('source', 'source__name', 'source__slug').annotate( + nb=Count('id') + ) + + for src in qs: + src['stats'] = Entry.objects.filter( + hidden=False, + source=src['source'], + created__range=(start_date, end_date) + ).annotate( + date=TruncDate('created') + ).values('date').annotate(nb=Count('date')).order_by('-date') out.append(src) + return out @@ -27,18 +47,38 @@ class EntriesView(generic.ListView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - context['date'] = datetime.date(self.kwargs['year'], self.kwargs['month'], self.kwargs['day']) - context['source_channel'] = Source.objects.get(slug=self.kwargs['source']), + context['date'] = datetime.date( + self.kwargs['year'], + self.kwargs['month'], + self.kwargs['day'] + ) + context['source_channel'] = Source.objects.get( + slug=self.kwargs['source'] + ) if isinstance(context['source_channel'], tuple): context['source_channel'] = context['source_channel'][0] return context def get_queryset(self): - dt = datetime.date(self.kwargs['year'], self.kwargs['month'], self.kwargs['day']) + dt = datetime.date( + self.kwargs['year'], + self.kwargs['month'], + self.kwargs['day'] + ) now = datetime.date.today() - if (now - dt).days > settings.KHAGANAT_LOGS_MAX_DAYS or (now - dt).days < settings.KHAGANAT_LOGS_MIN_DAYS: + out_of_bounds = any(( + (now - dt).days > settings.KHAGANAT_LOGS_MAX_DAYS, + (now - dt).days < settings.KHAGANAT_LOGS_MIN_DAYS, + )) + if out_of_bounds: return Entry.objects.none() src = Source.objects.get(slug=self.kwargs['source']) if src is None or src.hidden: return Entry.objects.none() - return Entry.objects.filter(hidden=False, source=src, created__year=dt.year, created__month=dt.month, created__day=dt.day).order_by('created') + return Entry.objects.filter( + hidden=False, + source=src, + created__year=dt.year, + created__month=dt.month, + created__day=dt.day + ).order_by('created') diff --git a/navbar/admin.py b/navbar/admin.py index fe0bdc6..f64690a 100644 --- a/navbar/admin.py +++ b/navbar/admin.py @@ -1,11 +1,14 @@ from django.contrib import admin -from .models import Element,ElementDescription +from .models import Element, ElementDescription + class ElementDescriptionInline(admin.StackedInline): model = ElementDescription extra = 2 + class ElementAdmin(admin.ModelAdmin): inlines = [ElementDescriptionInline] + admin.site.register(Element, ElementAdmin) diff --git a/navbar/models.py b/navbar/models.py index 270626b..07a7132 100644 --- a/navbar/models.py +++ b/navbar/models.py @@ -3,11 +3,22 @@ from django.conf import settings from django.db import models import os + class Element(models.Model): - parent = models.ForeignKey('Element', on_delete=models.CASCADE, null=True, blank=True) + parent = models.ForeignKey( + 'Element', + on_delete=models.CASCADE, + null=True, + blank=True + ) link = models.CharField(max_length=512, blank=True) new_window = models.BooleanField(default=False) - icon = models.FilePathField(path=os.path.join(settings.BASE_DIR, 'navbar/static/icons'), match=".png", null=True, blank=True) + icon = models.FilePathField( + path=os.path.join(settings.BASE_DIR, 'navbar/static/icons'), + match=".png", + null=True, + blank=True + ) weight = models.PositiveSmallIntegerField() def icon_path(self): @@ -17,12 +28,16 @@ class Element(models.Model): return Element.objects.filter(parent=self.id).order_by('weight') def description(self): - return ElementDescription.objects.filter(element=self.id, language=get_language()).first() + return ElementDescription.objects.filter( + element=self.id, + language=get_language() + ).first() def __str__(self): desc = self.description() return desc.short_name if desc is not None else self.link + class ElementDescription(models.Model): element = models.ForeignKey(Element, on_delete=models.CASCADE) language = models.CharField(max_length=10, choices=settings.LANGUAGES) diff --git a/navbar/templatetags/navbar.py b/navbar/templatetags/navbar.py index 9520fad..57376ca 100644 --- a/navbar/templatetags/navbar.py +++ b/navbar/templatetags/navbar.py @@ -1,4 +1,6 @@ -from django.utils.translation import get_language,gettext_lazy as _,activate as activate_lang +from django.utils.translation import activate as activate_lang +from django.utils.translation import gettext_lazy as _ +from django.utils.translation import get_language from django.template.response import TemplateResponse from django.utils.safestring import mark_safe from django.shortcuts import render @@ -7,17 +9,21 @@ from django.conf import settings from django import template import re + register = template.Library() path_re = re.compile('^(/[a-z]+)/(.*)') + def get_lang_name(lang_code): activate_lang(lang_code) return str([e for e in settings.LANGUAGES if e[0] == lang_code].pop()[1]) + def reduce_path(path): m = path_re.match(path) return m.group(2) if m else path + @register.simple_tag(takes_context=True) def navbar(context): request = context['request'] diff --git a/neluser/models.py b/neluser/models.py index 9d71338..665d727 100644 --- a/neluser/models.py +++ b/neluser/models.py @@ -1,4 +1,4 @@ -from django.contrib.auth.models import AbstractBaseUser,PermissionsMixin +from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin from django.contrib.auth.models import BaseUserManager from django.utils.translation import ugettext_lazy as _ from django.core.mail import send_mail @@ -41,7 +41,9 @@ class NelUser(AbstractBaseUser, PermissionsMixin): is_staff = models.BooleanField( _('staff status'), default=False, - help_text=_('Designates whether the user can log into this admin site.'), + help_text=_( + 'Designates whether the user can log into this admin site.' + ), ) is_active = models.BooleanField( _('active'), diff --git a/pages/admin.py b/pages/admin.py index e7b494a..28c87af 100644 --- a/pages/admin.py +++ b/pages/admin.py @@ -1,11 +1,14 @@ from django.contrib import admin -from .models import Page,PageContent +from .models import Page, PageContent + class PageContentInline(admin.StackedInline): model = PageContent extra = 2 + class PageAdmin(admin.ModelAdmin): inlines = [PageContentInline] + admin.site.register(Page, PageAdmin) diff --git a/pages/models.py b/pages/models.py index d4fbd4a..77f7c80 100644 --- a/pages/models.py +++ b/pages/models.py @@ -9,6 +9,7 @@ class Page(models.Model): def __str__(self): return self.slug + class PageContent(models.Model): page = models.ForeignKey(Page, on_delete=models.CASCADE) language = models.CharField(max_length=10, choices=settings.LANGUAGES) diff --git a/pages/views.py b/pages/views.py index ab3feef..0708390 100644 --- a/pages/views.py +++ b/pages/views.py @@ -2,17 +2,21 @@ from django.utils.translation import get_language from django.http import HttpResponseRedirect from django.views import generic from django.urls import reverse -from .models import Page,PageContent +from .models import Page, PageContent def index(request): default_page = Page.objects.filter(default=True).first() return HttpResponseRedirect(reverse('page', args=(default_page.slug, ))) + class PageView(generic.DetailView): model = PageContent context_object_name = 'page' template_name = 'pages/page.html' def get_object(self): - return PageContent.objects.filter(language=get_language(), page__slug=self.kwargs['slug']).first() + return PageContent.objects.filter( + language=get_language(), + page__slug=self.kwargs['slug'] + ).first()