Skip to content

Commit

Permalink
Made a bunch of flake8 fixes and also added flake8 testing to Travis …
Browse files Browse the repository at this point in the history
…and Tox
  • Loading branch information
epicserve committed Feb 15, 2016
1 parent 75b1496 commit 64686cd
Show file tree
Hide file tree
Showing 37 changed files with 162 additions and 148 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ branches:
- master

env:
- TOXENV=py27-lint
- TOXENV=py27-django18
- TOXENV=py27-django19
- TOXENV=py34-django18
Expand Down
1 change: 1 addition & 0 deletions categories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def get_version(short=False):

default_app_config = 'categories.apps.CategoriesConfig'


def register():
from categories import settings
from categories.registration import (_process_registry, registry)
Expand Down
6 changes: 3 additions & 3 deletions categories/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def label_from_instance(self, obj):
Creates labels which represent the tree level of each node when
generating option labels.
"""
return '%s %s' % (self.level_indicator * getattr(
obj, obj._mptt_meta.level_attr), obj)
return '%s %s' % (self.level_indicator * getattr(obj, obj._mptt_meta.level_attr), obj)

if RELATION_MODELS:
from .models import CategoryRelation

Expand Down Expand Up @@ -50,7 +50,7 @@ class CategoryAdmin(CategoryBaseAdmin):
}),
(_('Meta Data'), {
'fields': ('alternate_title', 'alternate_url', 'description',
'meta_keywords', 'meta_extra'),
'meta_keywords', 'meta_extra'),
'classes': ('collapse',),
}),
(_('Advanced'), {
Expand Down
7 changes: 3 additions & 4 deletions categories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class CategoryBase(MPTTModel):
This base model includes the absolute bare bones fields and methods. One
could simply subclass this model and do nothing else and it should work.
"""
parent = TreeForeignKey('self',
parent = TreeForeignKey(
'self',
blank=True,
null=True,
related_name='children',
Expand Down Expand Up @@ -99,9 +100,7 @@ def clean(self):
kwargs['parent__isnull'] = True
else:
kwargs['parent__pk'] = int(self.cleaned_data['parent'].id)
this_level_slugs = [c['slug'] for c in opts.model.objects.filter(
**kwargs).values('id', 'slug'
) if c['id'] != self.instance.id]
this_level_slugs = [c['slug'] for c in opts.model.objects.filter(**kwargs).values('id', 'slug') if c['id'] != self.instance.id]
if self.cleaned_data['slug'] in this_level_slugs:
raise forms.ValidationError(_('The slug must be unique among '
'the items at its level.'))
Expand Down
2 changes: 1 addition & 1 deletion categories/editor/models.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# Placeholder for Django
# Placeholder for Django
2 changes: 1 addition & 1 deletion categories/editor/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
DJANGO10_COMPAT = django.VERSION[0] < 1 or (django.VERSION[0] == 1 and django.VERSION[1] < 1)

STATIC_URL = getattr(settings, 'STATIC_URL', settings.MEDIA_URL)
if STATIC_URL == None:
if STATIC_URL is None:
STATIC_URL = settings.MEDIA_URL
MEDIA_PATH = getattr(settings, 'EDITOR_MEDIA_PATH', '%seditor/' % STATIC_URL)

Expand Down
18 changes: 11 additions & 7 deletions categories/editor/templatetags/admin_tree_list_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from django.template import Library
from django.contrib.admin.templatetags.admin_list import result_headers, _boolean_icon
try:
from django.contrib.admin.utils import lookup_field, display_for_field, label_for_field
from django.contrib.admin.utils import lookup_field, display_for_field
except ImportError:
from categories.editor.utils import lookup_field, display_for_field, label_for_field
from categories.editor.utils import lookup_field, display_for_field
from django.core.exceptions import ObjectDoesNotExist
from django.utils.encoding import smart_text, force_text
from django.utils.html import escape, conditional_escape
Expand Down Expand Up @@ -99,10 +99,12 @@ def items_for_tree_result(cl, result, form):
result_id = repr(force_text(value))[1:]
first = False
if django.VERSION[1] < 4:
yield mark_safe('<%s%s>%s<a href="%s"%s>%s</a></%s>' % \
yield mark_safe(
'<%s%s>%s<a href="%s"%s>%s</a></%s>' %
(table_tag, row_class, checkbox_value, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag))
else:
yield mark_safe('<%s%s><a href="%s"%s>%s</a></%s>' % \
yield mark_safe(
'<%s%s><a href="%s"%s>%s</a></%s>' %
(table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag))

else:
Expand Down Expand Up @@ -151,9 +153,11 @@ def result_tree_list(cl):
Displays the headers and data list together
"""
import django
result = {'cl': cl,
'result_headers': list(result_headers(cl)),
'results': list(tree_results(cl))}
result = {
'cl': cl,
'result_headers': list(result_headers(cl)),
'results': list(tree_results(cl))
}
if django.VERSION[1] > 2:
from django.contrib.admin.templatetags.admin_list import result_hidden_fields
result['result_hidden_fields'] = list(result_hidden_fields(cl))
Expand Down
12 changes: 6 additions & 6 deletions categories/editor/tree_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def iterator(self):
# will already be in include_pages when they are checked, thus not
# trigger additional queries.
for p in super(TreeEditorQuerySet, self.order_by('rght')).iterator():
if p.parent_id and p.parent_id not in include_pages and \
p.id not in include_pages:
if p.parent_id and p.parent_id not in include_pages and p.id not in include_pages:
ancestor_id_list = p.get_ancestors().values_list('id', flat=True)
include_pages.update(ancestor_id_list)

Expand Down Expand Up @@ -145,12 +144,14 @@ def old_changelist_view(self, request, extra_context=None):

try:
if django.VERSION[1] < 4:
params = (request, self.model, list_display,
params = (
request, self.model, list_display,
self.list_display_links, self.list_filter, self.date_hierarchy,
self.search_fields, self.list_select_related,
self.list_per_page, self.list_editable, self)
else:
params = (request, self.model, list_display,
params = (
request, self.model, list_display,
self.list_display_links, self.list_filter, self.date_hierarchy,
self.search_fields, self.list_select_related,
self.list_per_page, self.list_max_show_all,
Expand Down Expand Up @@ -244,8 +245,7 @@ def old_changelist_view(self, request, extra_context=None):
if django.VERSION[1] < 4:
context['root_path'] = self.admin_site.root_path
else:
selection_note_all = ungettext('%(total_count)s selected',
'All %(total_count)s selected', cl.result_count)
selection_note_all = ungettext('%(total_count)s selected', 'All %(total_count)s selected', cl.result_count)

context.update({
'module_name': force_text(opts.verbose_name_plural),
Expand Down
3 changes: 1 addition & 2 deletions categories/editor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def lookup_field(name, obj, model_admin=None):
if isinstance(name, collections.Callable):
attr = name
value = attr(obj)
elif (model_admin is not None and hasattr(model_admin, name) and
not name == '__str__' and not name == '__unicode__'):
elif (model_admin is not None and hasattr(model_admin, name) and not name == '__str__' and not name == '__unicode__'):
attr = getattr(model_admin, name)
value = attr(obj)
else:
Expand Down
2 changes: 2 additions & 0 deletions categories/genericcollection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import admin
from django.contrib.contenttypes.models import ContentType


class GenericCollectionInlineModelAdmin(admin.options.InlineModelAdmin):
ct_field = "content_type"
ct_fk_field = "object_id"
Expand All @@ -17,6 +18,7 @@ def get_formset(self, request, obj=None, **kwargs):
result.ct_fk_field = self.ct_fk_field
return result


class GenericCollectionTabularInline(GenericCollectionInlineModelAdmin):
template = 'admin/edit_inline/gen_coll_tabular.html'

Expand Down
6 changes: 3 additions & 3 deletions categories/management/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
try:
from south.db import db
from django.db.models.signals import post_syncdb
from categories.migration import migrate_app
from south.db import db # noqa
from django.db.models.signals import post_syncdb # noqa
from categories.migration import migrate_app # noqa

post_syncdb.connect(migrate_app)
except ImportError:
Expand Down
5 changes: 0 additions & 5 deletions categories/management/commands/add_category_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ def handle(self, *args, **options):
"""
Alter the tables
"""
from django.core.exceptions import ImproperlyConfigured
try:
from south.db import db
except ImportError:
raise ImproperlyConfigured("South must be installed for this command to work")

from categories.migration import migrate_app
from categories.settings import MODEL_REGISTRY
Expand Down
4 changes: 0 additions & 4 deletions categories/management/commands/drop_category_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ def handle(self, *args, **options):
"""
Alter the tables
"""
try:
from south.db import db
except ImportError:
raise ImproperlyConfigured("South must be installed for this command to work")

from categories.migration import drop_field
if len(args) != 3:
Expand Down
2 changes: 1 addition & 1 deletion categories/management/commands/import_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def make_category(self, string, parent=None, order=1):
cat = Category(
name=string.strip(),
slug=slugify(SLUG_TRANSLITERATOR(string.strip()))[:49],
#parent=parent,
# arent=parent,
order=order
)
cat._tree_manager.insert_node(cat, parent, 'last-child', True)
Expand Down
8 changes: 4 additions & 4 deletions categories/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

from django.utils.translation import ugettext_lazy as _

from .settings import (RELATION_MODELS, RELATIONS, THUMBNAIL_UPLOAD_PATH,
THUMBNAIL_STORAGE)
from .settings import (RELATION_MODELS, RELATIONS, THUMBNAIL_UPLOAD_PATH, THUMBNAIL_STORAGE)

from .base import CategoryBase

Expand Down Expand Up @@ -133,7 +132,8 @@ class CategoryRelation(models.Model):
ContentType, limit_choices_to=CATEGORY_RELATION_LIMITS, verbose_name=_('content type'))
object_id = models.PositiveIntegerField(verbose_name=_('object id'))
content_object = GenericForeignKey('content_type', 'object_id')
relation_type = models.CharField(verbose_name=_('relation type'),
relation_type = models.CharField(
verbose_name=_('relation type'),
max_length=200,
blank=True,
null=True,
Expand All @@ -145,7 +145,7 @@ def __unicode__(self):
return "CategoryRelation"

try:
from south.db import db # South is required for migrating. Need to check for it
from south.db import db # noqa, South is required for migrating. Need to check for it
from django.db.models.signals import post_syncdb
from categories.migration import migrate_app
post_syncdb.connect(migrate_app)
Expand Down
7 changes: 3 additions & 4 deletions categories/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def register_model(self, app, model_name, field_type, field_definitions):
if isinstance(field_definitions, str):
field_definitions = [field_definitions]
elif not isinstance(field_definitions, collections.Iterable):
raise ImproperlyConfigured(_('Field configuration for %(app)s should '
'be a string or iterable') % {'app': app})
raise ImproperlyConfigured(_('Field configuration for %(app)s should be a string or iterable') % {'app': app})

if field_type not in ('ForeignKey', 'ManyToManyField'):
raise ImproperlyConfigured(_('`field_type` must be either `"ForeignKey"` or `"ManyToManyField"`.'))
Expand Down Expand Up @@ -73,8 +72,8 @@ def register_model(self, app, model_name, field_type, field_definitions):
continue
extra_params.update(fld)
else:
raise ImproperlyConfigured(_("%(settings)s doesn't recognize the "
"value of %(app)s.%(model)s") % {
raise ImproperlyConfigured(
_("%(settings)s doesn't recognize the value of %(app)s.%(model)s") % {
'settings': 'CATEGORY_SETTINGS',
'app': app,
'model': model_name})
Expand Down
3 changes: 1 addition & 2 deletions categories/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

DEFAULT_SETTINGS = {
'ALLOW_SLUG_CHANGE': False,
'RELATION_MODELS': [],
'M2M_REGISTRY': {},
'FK_REGISTRY': {},
'THUMBNAIL_UPLOAD_PATH': 'uploads/categories/thumbnails',
Expand Down Expand Up @@ -37,7 +36,7 @@
# Add all the keys/values to the module's namespace
globals().update(DEFAULT_SETTINGS)

RELATIONS = [Q(app_label=al, model=m) for al, m in [x.split('.') for x in RELATION_MODELS]]
RELATIONS = [Q(app_label=al, model=m) for al, m in [x.split('.') for x in DEFAULT_SETTINGS['RELATION_MODELS']]]

# The field registry keeps track of the individual fields created.
# {'app.model.field': Field(**extra_params)}
Expand Down
15 changes: 5 additions & 10 deletions categories/templatetags/category_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ def get_category(category_string, model=Category):
if len(cat_list) == 0:
return None
try:
categories = model_class.objects.filter(name=cat_list[-1],
level=len(cat_list) - 1)
categories = model_class.objects.filter(name=cat_list[-1], level=len(cat_list) - 1)
if len(cat_list) == 1 and len(categories) > 1:
return None
# If there is only one, use it. If there is more than one, check
Expand Down Expand Up @@ -265,8 +264,7 @@ def get_top_level_categories(parser, token):
return TopLevelCategoriesNode(varname, model)


def get_latest_objects_by_category(category, app_label, model_name, set_name,
date_field='pub_date', num=15):
def get_latest_objects_by_category(category, app_label, model_name, set_name, date_field='pub_date', num=15):
m = apps.get_model(app_label, model_name)
if not isinstance(category, CategoryBase):
category = Category.objects.get(slug=str(category))
Expand Down Expand Up @@ -304,8 +302,7 @@ def render(self, context):
date_field = resolve(self.date_field, context)
num = resolve(self.num, context)

result = get_latest_objects_by_category(category, app_label, model_name,
set_name, date_field, num)
result = get_latest_objects_by_category(category, app_label, model_name, set_name, date_field, num)
context[self.var_name] = result

return ''
Expand Down Expand Up @@ -339,8 +336,7 @@ def do_get_latest_objects_by_category(parser, token):
num = FilterExpression(bits[6], parser)
else:
num = FilterExpression(None, parser)
return LatestObjectsNode(var_name, category, app_label, model_name, set_name,
date_field, num)
return LatestObjectsNode(var_name, category, app_label, model_name, set_name, date_field, num)

register.tag("get_latest_objects_by_category", do_get_latest_objects_by_category)

Expand Down Expand Up @@ -369,8 +365,7 @@ def tree_queryset(value):
# will already be in include_pages when they are checked, thus not
# trigger additional queries.
for p in qs2.order_by('rght').iterator():
if p.parent_id and p.parent_id not in include_pages and \
p.id not in include_pages:
if p.parent_id and p.parent_id not in include_pages and p.id not in include_pages:
ancestor_id_list = p.get_ancestors().values_list('id', flat=True)
include_pages.update(ancestor_id_list)

Expand Down
2 changes: 0 additions & 2 deletions categories/tests/test_category_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from categories.management.commands.import_categories import Command
from django.core.management.base import CommandError

from categories.registration import _process_registry, registry


@override_settings(INSTALLED_APPS=(app for app in settings.INSTALLED_APPS if app != 'django.contrib.flatpages'))
class CategoryImportTest(TestCase):
Expand Down
2 changes: 0 additions & 2 deletions categories/tests/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,3 @@ def test_m2m_string(self):
_process_registry(M2M_REGISTRY, registry.register_m2m)
from django.contrib.flatpages.models import FlatPage
self.assertTrue('category' in [f.name for f in FlatPage()._meta.get_fields()])


15 changes: 10 additions & 5 deletions categories/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,40 @@ def testBasicUsage(self):

# display_drilldown_as_ul
expected_resp = '<ul><li><a href="/categories/">Top</a><ul><li><a href="/categories/world/">World</a><ul><li><strong>Worldbeat</strong><ul><li><a href="/categories/world/worldbeat/afrobeat/">Afrobeat</a></li></ul></li></ul></li></ul></li></ul>'
resp = self.render_template('{% load category_tags %}'
resp = self.render_template(
'{% load category_tags %}'
'{% display_drilldown_as_ul "/World/Worldbeat" "categories.category" %}')
resp = re.sub(r'\n$', "", resp)
self.assertEqual(resp, expected_resp)

# breadcrumbs
expected_resp = '<a href="/categories/world/">World</a> &gt; Worldbeat'
resp = self.render_template('{% load category_tags %}'
resp = self.render_template(
'{% load category_tags %}'
'{% breadcrumbs "/World/Worldbeat" " &gt; " "categories.category" %}')
self.assertEqual(resp, expected_resp)

# get_top_level_categories
expected_resp = 'Avant-garde|Blues|Country|Easy listening|Electronic|Hip hop/Rap music|Jazz|Latin|Modern folk|Pop|Reggae|Rhythm and blues|Rock|World|'
resp = self.render_template('{% load category_tags %}'
resp = self.render_template(
'{% load category_tags %}'
'{% get_top_level_categories using "categories.category" as varname %}'
'{% for item in varname %}{{ item }}|{% endfor %}')
self.assertEqual(resp, expected_resp)

# get_category_drilldown
expected_resp = "World|World &gt; Worldbeat|"
resp = self.render_template('{% load category_tags %}'
resp = self.render_template(
'{% load category_tags %}'
'{% get_category_drilldown "/World" using "categories.category" as var %}'
'{% for item in var %}{{ item }}|{% endfor %}')
self.assertEqual(resp, expected_resp)

# recursetree
expected_resp = '<ul><li>Country<ul><li>Country pop<ul><li>Urban Cowboy</li></ul></li></ul></li><li>World<ul><li>Worldbeat<ul></ul></li></ul></li></ul>'
ctxt = {'nodes': Category.objects.filter(name__in=("Worldbeat", "Urban Cowboy"))}
resp = self.render_template('{% load category_tags %}'
resp = self.render_template(
'{% load category_tags %}'
'<ul>{% recursetree nodes|tree_queryset %}<li>{{ node.name }}'
'{% if not node.is_leaf_node %}<ul>{{ children }}'
'</ul>{% endif %}</li>{% endrecursetree %}</ul>', ctxt)
Expand Down

0 comments on commit 64686cd

Please sign in to comment.