Fix various pep8/flake8/pylint errors

This commit is contained in:
Swen Kooij 2017-05-25 19:40:03 +03:00
parent 5a4f449363
commit 92a53bc3d7
11 changed files with 39 additions and 166 deletions

View File

@ -59,7 +59,7 @@ class LocalizedValueDescriptor:
def __set__(self, instance, value): def __set__(self, instance, value):
if isinstance(value, six.string_types): if isinstance(value, six.string_types):
self.__get__(instance).set(translation.get_language() or language = translation.get_language() or settings.LANGUAGE_CODE
settings.LANGUAGE_CODE, value) self.__get__(instance).set(language, value) # pylint: disable=no-member
else: else:
instance.__dict__[self.field.name] = value instance.__dict__[self.field.name] = value

View File

@ -1,4 +1,4 @@
from typing import Callable from typing import Callable, Tuple
from datetime import datetime from datetime import datetime
from django import forms from django import forms
@ -69,13 +69,7 @@ class LocalizedAutoSlugField(LocalizedField):
slugs = LocalizedValue() slugs = LocalizedValue()
for lang_code, _ in settings.LANGUAGES: for lang_code, value in self._get_populate_values(instance):
value = self._get_populate_from_value(
instance,
self.populate_from,
lang_code
)
if not value: if not value:
continue continue
@ -128,6 +122,30 @@ class LocalizedAutoSlugField(LocalizedField):
return unique_slug return unique_slug
def _get_populate_values(self, instance) -> Tuple[str, str]:
"""Gets all values (for each language) from the
specified's instance's `populate_from` field.
Arguments:
instance:
The instance to get the values from.
Returns:
A list of (lang_code, value) tuples.
"""
return [
(
lang_code,
self._get_populate_from_value(
instance,
self.populate_from,
lang_code
),
)
for lang_code, _ in settings.LANGUAGES
]
@staticmethod @staticmethod
def _get_populate_from_value(instance, field_name: str, language: str): def _get_populate_from_value(instance, field_name: str, language: str):
"""Gets the value to create a slug from in the specified language. """Gets the value to create a slug from in the specified language.

View File

@ -1,6 +1,5 @@
from datetime import datetime from datetime import datetime
from django.conf import settings
from django.utils.text import slugify from django.utils.text import slugify
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
@ -73,13 +72,7 @@ class LocalizedUniqueSlugField(LocalizedAutoSlugField):
slugs = LocalizedValue() slugs = LocalizedValue()
for lang_code, _ in settings.LANGUAGES: for lang_code, value in self._get_populate_values(instance):
value = self._get_populate_from_value(
instance,
self.populate_from,
lang_code
)
if not value: if not value:
continue continue

View File

@ -7,7 +7,6 @@ from .value import LocalizedValue
from .widgets import LocalizedFieldWidget from .widgets import LocalizedFieldWidget
class LocalizedFieldForm(forms.MultiValueField): class LocalizedFieldForm(forms.MultiValueField):
"""Form for a localized field, allows editing """Form for a localized field, allows editing
the field in multiple languages.""" the field in multiple languages."""
@ -38,7 +37,7 @@ class LocalizedFieldForm(forms.MultiValueField):
for f, w in zip(self.fields, self.widget.widgets): for f, w in zip(self.fields, self.widget.widgets):
w.is_required = f.required w.is_required = f.required
def compress(self, value: List[str]) -> value_class: def compress(self, value: List[str]) -> LocalizedValue:
"""Compresses the values from individual fields """Compresses the values from individual fields
into a single :see:LocalizedValue instance. into a single :see:LocalizedValue instance.

View File

@ -1,132 +0,0 @@
"""This module is unused, but should be contributed to Django."""
from typing import List
from django.db import models
class HStoreIndex(models.Index):
"""Allows creating a index on a specific HStore index.
Note: pieces of code in this class have been copied
from the base class. There was no way around this."""
def __init__(self, field: str, keys: List[str], unique: bool=False,
name: str=''):
"""Initializes a new instance of :see:HStoreIndex.
Arguments:
field:
Name of the hstore field for
which's keys to create a index for.
keys:
The name of the hstore keys to
create the index on.
unique:
Whether this index should
be marked as UNIQUE.
name:
The name of the index. If left
empty, one will be generated.
"""
self.field = field
self.keys = keys
self.unique = unique
# this will eventually set self.name
super(HStoreIndex, self).__init__(
fields=[field],
name=name
)
def get_sql_create_template_values(self, model, schema_editor, using):
"""Gets the values for the SQL template.
Arguments:
model:
The model this index applies to.
schema_editor:
The schema editor to modify the schema.
using:
Optional: "USING" statement.
Returns:
Dictionary of keys to pass into the SQL template.
"""
fields = [model._meta.get_field(field_name) for field_name, order in self.fields_orders]
tablespace_sql = schema_editor._get_index_tablespace_sql(model, fields)
quote_name = schema_editor.quote_name
columns = [
'(%s->\'%s\')' % (self.field, key)
for key in self.keys
]
return {
'table': quote_name(model._meta.db_table),
'name': quote_name(self.name),
'columns': ', '.join(columns),
'using': using,
'extra': tablespace_sql,
}
def create_sql(self, model, schema_editor, using=''):
"""Gets the SQL to execute when creating the index.
Arguments:
model:
The model this index applies to.
schema_editor:
The schema editor to modify the schema.
using:
Optional: "USING" statement.
Returns:
SQL string to execute to create this index.
"""
sql_create_index = schema_editor.sql_create_index
if self.unique:
sql_create_index = sql_create_index.replace('CREATE', 'CREATE UNIQUE')
sql_parameters = self.get_sql_create_template_values(model, schema_editor, using)
return sql_create_index % sql_parameters
def remove_sql(self, model, schema_editor):
"""Gets the SQL to execute to remove this index.
Arguments:
model:
The model this index applies to.
schema_editor:
The schema editor to modify the schema.
Returns:
SQL string to execute to remove this index.
"""
quote_name = schema_editor.quote_name
return schema_editor.sql_delete_index % {
'table': quote_name(model._meta.db_table),
'name': quote_name(self.name),
}
def deconstruct(self):
"""Gets the values to pass to :see:__init__ when
re-creating this object."""
path = '%s.%s' % (self.__class__.__module__, self.__class__.__name__)
return (path, (), {
'field': self.field,
'keys': self.keys,
'unique': self.unique,
'name': self.name
})

View File

@ -17,7 +17,8 @@ class LocalizedValue(dict):
different language. different language.
""" """
self._interpret_value(keys); super().__init__({})
self._interpret_value(keys)
def get(self, language: str=None) -> str: def get(self, language: str=None) -> str:
"""Gets the underlying value in the specified or """Gets the underlying value in the specified or

View File

@ -15,12 +15,12 @@ class LocalizedFieldWidget(forms.MultiWidget):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
"""Initializes a new instance of :see:LocalizedFieldWidget.""" """Initializes a new instance of :see:LocalizedFieldWidget."""
widgets = [] initial_widgets = [
self.widget
for _ in settings.LANGUAGES
]
for _ in settings.LANGUAGES: super().__init__(initial_widgets, *args, **kwargs)
widgets.append(self.widget)
super(LocalizedFieldWidget, self).__init__(widgets, *args, **kwargs)
def decompress(self, value: LocalizedValue) -> List[str]: def decompress(self, value: LocalizedValue) -> List[str]:
"""Decompresses the specified value so """Decompresses the specified value so
@ -36,7 +36,6 @@ class LocalizedFieldWidget(forms.MultiWidget):
""" """
result = [] result = []
for lang_code, _ in settings.LANGUAGES: for lang_code, _ in settings.LANGUAGES:
if value: if value:
result.append(value.get(lang_code)) result.append(value.get(lang_code))
@ -78,7 +77,8 @@ class AdminLocalizedFieldWidget(LocalizedFieldWidget):
} }
return render_to_string(self.template, context) return render_to_string(self.template, context)
def build_widget_attrs(self, widget, value, attrs): @staticmethod
def build_widget_attrs(widget, value, attrs):
attrs = dict(attrs) # Copy attrs to avoid modifying the argument. attrs = dict(attrs) # Copy attrs to avoid modifying the argument.
if (not widget.use_required_attribute(value) or not widget.is_required) \ if (not widget.use_required_attribute(value) or not widget.is_required) \
and 'required' in attrs: and 'required' in attrs:

View File

@ -3,7 +3,6 @@ from django.db.migrations.executor import MigrationExecutor
from django.contrib.postgres.operations import HStoreExtension from django.contrib.postgres.operations import HStoreExtension
from localized_fields.models import LocalizedModel from localized_fields.models import LocalizedModel
from localized_fields.mixins import AtomicSlugRetryMixin
def define_fake_model(name='TestModel', fields=None): def define_fake_model(name='TestModel', fields=None):

View File

@ -3,7 +3,6 @@ import json
from django.conf import settings from django.conf import settings
from django.db.utils import IntegrityError from django.db.utils import IntegrityError
from django.test import TestCase from django.test import TestCase
from django.utils import translation
from localized_fields.fields import LocalizedField from localized_fields.fields import LocalizedField
from localized_fields.forms import LocalizedFieldForm from localized_fields.forms import LocalizedFieldForm

View File

@ -34,7 +34,6 @@ class LocalizedModelTestCase(TestCase):
assert isinstance(obj.title, LocalizedValue) assert isinstance(obj.title, LocalizedValue)
@classmethod @classmethod
def test_model_init_kwargs(cls): def test_model_init_kwargs(cls):
"""Tests whether all :see:LocalizedField """Tests whether all :see:LocalizedField

View File

@ -1,5 +1,3 @@
import json
from django.conf import settings from django.conf import settings
from django.test import TestCase from django.test import TestCase
from django.utils import translation from django.utils import translation
@ -164,4 +162,3 @@ class LocalizedValueTestCase(TestCase):
value = LocalizedValue('beer') value = LocalizedValue('beer')
assert value.get(settings.LANGUAGE_CODE) == 'beer' assert value.get(settings.LANGUAGE_CODE) == 'beer'