From: Jan Szejko Date: Tue, 8 Nov 2016 14:52:49 +0000 (+0100) Subject: pep8 X-Git-Tag: 0.5~2 X-Git-Url: https://git.mdrn.pl/django-migdal.git/commitdiff_plain/f41fb09cefa2fca8d5c1e9314bcaa3591590a74c?hp=e335d8d992b0e8c9f8926c1fe92b3526c3fa1508 pep8 --- diff --git a/migdal/__init__.py b/migdal/__init__.py index fe90406..dda9569 100644 --- a/migdal/__init__.py +++ b/migdal/__init__.py @@ -27,26 +27,24 @@ class Settings(AppSettings): MAIN_PAGE_ENTRY = None - TYPES_DICT = None def _more_TYPES_DICT(self, value): return dict((t.db, t) for t in self.TYPES) + TYPES_DICT = None - TYPES_ON_MAIN = None def _more_TYPES_ON_MAIN(self, value): return tuple(t.db for t in self.TYPES if t.on_main) + TYPES_ON_MAIN = None - TYPES_PROMOTABLE = None def _more_TYPES_PROMOTABLE(self, value): return tuple(t.db for t in self.TYPES if t.promotable) + TYPES_PROMOTABLE = None - OBLIGATORY_LANGUAGES = None def _more_OBLIGATORY_LANGUAGES(self, value): - return value or tuple(lang for lang in settings.LANGUAGES - if lang[0] == settings.LANGUAGE_CODE) + return value or tuple(lang for lang in settings.LANGUAGES if lang[0] == settings.LANGUAGE_CODE) + OBLIGATORY_LANGUAGES = None - OPTIONAL_LANGUAGES = None def _more_OPTIONAL_LANGUAGES(self, value): - return tuple(lang for lang in settings.LANGUAGES - if lang not in self.OBLIGATORY_LANGUAGES) + return tuple(lang for lang in settings.LANGUAGES if lang not in self.OBLIGATORY_LANGUAGES) + OPTIONAL_LANGUAGES = None app_settings = Settings('MIGDAL') diff --git a/migdal/admin.py b/migdal/admin.py index 8ed11ea..6d4bffe 100644 --- a/migdal/admin.py +++ b/migdal/admin.py @@ -78,16 +78,15 @@ def filtered_entry_admin(typ): for lang_code, lang_name in settings.LANGUAGES ]) - list_display = translated_fields(('title',), - app_settings.OBLIGATORY_LANGUAGES) + \ - ('date', 'author') + \ - _promo_if_necessary + \ - ('in_stream', 'first_published_at',) + \ - translated_fields(('published_at',)) + \ - translated_fields(('needed',), app_settings.OPTIONAL_LANGUAGES) + list_display = translated_fields(('title',), app_settings.OBLIGATORY_LANGUAGES) + \ + ('date', 'author') + \ + _promo_if_necessary + \ + ('in_stream', 'first_published_at',) + \ + translated_fields(('published_at',)) + \ + translated_fields(('needed',), app_settings.OPTIONAL_LANGUAGES) list_filter = _promo_if_necessary + \ - translated_fields(('published',)) + \ - translated_fields(('needed',), app_settings.OPTIONAL_LANGUAGES) + translated_fields(('published',)) + \ + translated_fields(('needed',), app_settings.OPTIONAL_LANGUAGES) inlines = (AttachmentInline,) search_fields = ('title_pl', 'title_en') return EntryAdmin diff --git a/migdal/api.py b/migdal/api.py index abd3b21..fd89352 100644 --- a/migdal/api.py +++ b/migdal/api.py @@ -2,14 +2,12 @@ # This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # -from itertools import chain from migdal.models import Entry from migdal import app_settings from django.utils.translation import get_language -def entry_list(entry_type=None, category=None, promobox=False, - for_feed=False): +def entry_list(entry_type=None, category=None, promobox=False, for_feed=False): lang = get_language() object_list = Entry.objects.filter(**{"published_%s" % lang: True}) @@ -26,13 +24,11 @@ def entry_list(entry_type=None, category=None, promobox=False, if category: object_list = object_list.filter(categories=category) - if promobox: - promo = list(object_list.filter(promo=True)[:promobox]) - #object_list = object_list.exclude(pk__in=[p.pk for p in promo]) - object_list = object_list.filter(in_stream=True) if promobox: + promo = list(object_list.filter(promo=True)[:promobox]) + # object_list = object_list.exclude(pk__in=[p.pk for p in promo]) object_list.promobox = promo return object_list diff --git a/migdal/fields.py b/migdal/fields.py index 843c44e..c9fe7fc 100644 --- a/migdal/fields.py +++ b/migdal/fields.py @@ -4,15 +4,16 @@ # from django.db import models + class SlugNullField(models.SlugField): description = "SlugField that stores NULL instead of blank value." - def to_python(self, value, **kwargs): - value = super(SlugNullField, self).to_python(value, **kwargs) + def to_python(self, value): + value = super(SlugNullField, self).to_python(value) return value if value is not None else u"" def get_prep_value(self, value, **kwargs): - value = super(SlugNullField, self).get_prep_value(value, **kwargs) + value = super(SlugNullField, self).get_prep_value(value) return value or None diff --git a/migdal/forms.py b/migdal/forms.py index 63eba61..649980c 100644 --- a/migdal/forms.py +++ b/migdal/forms.py @@ -7,7 +7,7 @@ from django.core.mail import mail_managers from django import forms from django import template from django.utils.translation import ugettext_lazy as _, get_language -from fnpdjango.utils.text.slughifi import slughifi +from slugify import slugify from migdal.models import Entry from migdal import app_settings @@ -18,8 +18,7 @@ def get_submit_form(*args, **kwargs): class SubmitForm(forms.ModelForm): class Meta: model = Entry - fields = ['title_%s' % lang, 'lead_%s' % lang, - 'author', 'author_email', 'categories'] + fields = ['title_%s' % lang, 'lead_%s' % lang, 'author', 'author_email', 'categories'] required = ['title_%s' % lang] def __init__(self, *args, **kwargs): @@ -34,7 +33,7 @@ def get_submit_form(*args, **kwargs): def clean(self): data = super(SubmitForm, self).clean() data['type'] = app_settings.TYPE_SUBMIT - orig_slug = slughifi(data.get('title_%s' % lang, ''))[:47] + orig_slug = slugify(data.get('title_%s' % lang, ''))[:47] slug = orig_slug number = 2 while Entry.objects.filter(**{'slug_%s' % lang: slug}).exists(): @@ -54,7 +53,8 @@ def get_submit_form(*args, **kwargs): setattr(entry, f, self.cleaned_data[f]) entry.save() entry = super(SubmitForm, self).save(*args, **kwargs) - mail_managers(u"Nowy wpis", + mail_managers( + u"Nowy wpis", template.loader.get_template( 'migdal/mail/manager_new_entry.txt').render( template.Context({ @@ -62,4 +62,4 @@ def get_submit_form(*args, **kwargs): 'site': Site.objects.get_current(), }))) - return SubmitForm(*args, **kwargs) \ No newline at end of file + return SubmitForm(*args, **kwargs) diff --git a/migdal/helpers.py b/migdal/helpers.py index 8c06c16..e62333d 100644 --- a/migdal/helpers.py +++ b/migdal/helpers.py @@ -2,9 +2,10 @@ # This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # + + class EntryType(object): - def __init__(self, db, slug, commentable=False, on_main=False, - promotable=False, categorized=False): + def __init__(self, db, slug, commentable=False, on_main=False, promotable=False, categorized=False): self.db = db self.slug = slug self.commentable = commentable diff --git a/migdal/models.py b/migdal/models.py index 75529d6..b3238b7 100644 --- a/migdal/models.py +++ b/migdal/models.py @@ -5,22 +5,21 @@ import re from datetime import datetime from django.conf import settings -from django.contrib.comments.signals import comment_will_be_posted from django.contrib.sites.models import Site from django.core.exceptions import ValidationError from django.core.mail import mail_managers, send_mail from django.db import models from django.template import loader, Context -from django.utils.translation import get_language, ugettext_lazy as _, ugettext +from django.utils.translation import ugettext_lazy as _, ugettext from django_comments_xtd.models import XtdComment from markupfield.fields import MarkupField from fnpdjango.utils.models.translation import add_translatable, tQ from migdal import app_settings from migdal.fields import SlugNullField + class Category(models.Model): - taxonomy = models.CharField(_('taxonomy'), max_length=32, - choices=app_settings.TAXONOMIES) + taxonomy = models.CharField(_('taxonomy'), max_length=32, choices=app_settings.TAXONOMIES) class Meta: verbose_name = _('category') @@ -31,7 +30,7 @@ class Category(models.Model): @models.permalink def get_absolute_url(self): - return ('migdal_category', [self.slug]) + return 'migdal_category', [self.slug] add_translatable(Category, { @@ -46,21 +45,24 @@ class PublishedEntryManager(models.Manager): tQ(published=True) ) + class Entry(models.Model): - type = models.CharField(max_length=16, - choices=((t.db, t.slug) for t in app_settings.TYPES), - db_index=True) + type = models.CharField( + max_length=16, + choices=((t.db, t.slug) for t in app_settings.TYPES), + db_index=True) date = models.DateTimeField(_('created at'), auto_now_add=True, db_index=True) changed_at = models.DateTimeField(_('changed at'), auto_now=True, db_index=True) author = models.CharField(_('author'), max_length=128) - author_email = models.EmailField(_('author email'), max_length=128, null=True, blank=True, - help_text=_('Used only to display gravatar and send notifications.')) + author_email = models.EmailField( + _('author email'), max_length=128, null=True, blank=True, + help_text=_('Used only to display gravatar and send notifications.')) image = models.ImageField(_('image'), upload_to='entry/image/', null=True, blank=True) promo = models.BooleanField(_('promoted'), default=False) in_stream = models.BooleanField(_('in stream'), default=True) categories = models.ManyToManyField(Category, null=True, blank=True, verbose_name=_('categories')) first_published_at = models.DateTimeField(_('published at'), null=True, blank=True) - canonical_url = models.URLField(_('canonical link'), null = True, blank = True) + canonical_url = models.URLField(_('canonical link'), null=True, blank=True) objects = models.Manager() published_objects = PublishedEntryManager() @@ -96,7 +98,7 @@ class Entry(models.Model): @models.permalink def get_absolute_url(self): - return ('migdal_entry_%s' % self.type, [self.slug]) + return 'migdal_entry_%s' % self.type, [self.slug] def get_type(self): return dict(app_settings.TYPES_DICT)[self.type] @@ -131,10 +133,12 @@ add_translatable(Entry, languages=app_settings.OPTIONAL_LANGUAGES, fields={ add_translatable(Entry, { 'slug': SlugNullField(unique=True, db_index=True, null=True, blank=True), 'title': models.CharField(_('title'), max_length=255, null=True, blank=True), - 'lead': MarkupField(_('lead'), markup_type='textile_pl', null=True, blank=True, - help_text=_('Use Textile syntax.')), - 'body': MarkupField(_('body'), markup_type='textile_pl', null=True, blank=True, - help_text=_('Use Textile syntax.')), + 'lead': MarkupField( + _('lead'), markup_type='textile_pl', null=True, blank=True, + help_text=_('Use Textile syntax.')), + 'body': MarkupField( + _('body'), markup_type='textile_pl', null=True, blank=True, + help_text=_('Use Textile syntax.')), 'published': models.BooleanField(_('published'), default=False), 'published_at': models.DateTimeField(_('published at'), null=True, blank=True), }) @@ -148,10 +152,8 @@ class Attachment(models.Model): return self.file.url if self.file else '' - def notify_new_comment(sender, instance, created, **kwargs): - if (created and isinstance(instance.content_object, Entry) and - instance.content_object.author_email): + if created and isinstance(instance.content_object, Entry) and instance.content_object.author_email: site = Site.objects.get_current() mail_text = loader.get_template('migdal/mail/new_comment.txt').render( Context({ @@ -169,13 +171,15 @@ models.signals.post_save.connect(notify_new_comment, sender=XtdComment) def spamfilter(sender, comment, **kwargs): """Very simple spam filter. Just don't let any HTML links go through.""" if re.search(r"[^/]*)/$'), 'migdal.views.entry_list', name='migdal_category'), @@ -47,9 +49,9 @@ urlpatterns = i18n_patterns('', if 'django.contrib.sitemaps' in settings.INSTALLED_APPS: from .sitemap import sitemaps - urlpatterns += patterns('', + urlpatterns += patterns( + '', url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', { 'sitemaps': sitemaps }), ) - diff --git a/migdal/views.py b/migdal/views.py index 1f769a9..0d4d996 100644 --- a/migdal/views.py +++ b/migdal/views.py @@ -45,8 +45,7 @@ def entry_list(request, type_db=None, category_slug=None): promobox = 5 if entry_type is None and category is None else None - object_list = api.entry_list(entry_type=entry_type, category=category, - promobox=promobox) + object_list = api.entry_list(entry_type=entry_type, category=category, promobox=promobox) return render(request, templates, { 'object_list': object_list, @@ -84,6 +83,7 @@ def submit(request): 'submit_form': submit_form, }) + def submit_thanks(request): return render(request, "migdal/entry/submit_thanks.html") @@ -95,9 +95,10 @@ class SearchPublishedView(SearchView): def get_results(self): results = super(SearchPublishedView, self).get_results() lang_code = get_language() + def is_published(entity): if isinstance(entity, Entry): - return getattr(entity, "published_%s" % lang_code) == True + return getattr(entity, "published_%s" % lang_code) else: return True results = filter(lambda r: is_published(r.object), results) diff --git a/setup.py b/setup.py index 371846c..03986fc 100755 --- a/setup.py +++ b/setup.py @@ -4,10 +4,11 @@ import os.path from setuptools import setup, find_packages + def whole_trees(package_dir, paths): def whole_tree(prefix, path): files = [] - for f in (f for f in os.listdir(os.path.join(prefix, path)) if not f[0]=='.'): + for f in (f for f in os.listdir(os.path.join(prefix, path)) if f[0] != '.'): new_path = os.path.join(path, f) if os.path.isdir(os.path.join(prefix, new_path)): files.extend(whole_tree(prefix, new_path)) @@ -25,10 +26,9 @@ setup( version='0.4', author='Radek Czajka', author_email='radoslaw.czajka@nowoczesnapolska.org.pl', - url = '', + url='', packages=find_packages(), - package_data={'migdal': whole_trees('migdal', - ['templates', 'locale'])}, + package_data={'migdal': whole_trees('migdal', ['templates', 'locale'])}, license='LICENSE', description='.', long_description="",