fixes some of the pep8 warnings
This commit is contained in:
parent
1bfbd05528
commit
93de969ea7
|
@ -35,7 +35,8 @@ except ImportError:
|
|||
from django.utils.encoding import force_unicode as force_text
|
||||
|
||||
SKIP_ADMIN_LOG = getattr(settings, 'IMPORT_EXPORT_SKIP_ADMIN_LOG', False)
|
||||
TMP_STORAGE_CLASS = getattr(settings, 'IMPORT_EXPORT_TMP_STORAGE_CLASS', TempFolderStorage)
|
||||
TMP_STORAGE_CLASS = getattr(settings, 'IMPORT_EXPORT_TMP_STORAGE_CLASS',
|
||||
TempFolderStorage)
|
||||
if isinstance(TMP_STORAGE_CLASS, six.string_types):
|
||||
try:
|
||||
# Nod to tastypie's use of importlib.
|
||||
|
|
|
@ -4,20 +4,24 @@ from django.db import transaction
|
|||
|
||||
# transaction management for Django < 1.6
|
||||
|
||||
|
||||
def atomic(*args, **kw):
|
||||
def noop_decorator(func):
|
||||
return func # pass through
|
||||
|
||||
return noop_decorator
|
||||
|
||||
|
||||
def savepoint(*args, **kwargs):
|
||||
transaction.enter_transaction_management()
|
||||
transaction.managed(True)
|
||||
|
||||
|
||||
def savepoint_rollback(*args, **kwargs):
|
||||
transaction.rollback()
|
||||
transaction.leave_transaction_management()
|
||||
|
||||
|
||||
def savepoint_commit(*args, **kwargs):
|
||||
transaction.commit()
|
||||
transaction.leave_transaction_management()
|
||||
|
|
|
@ -24,7 +24,8 @@ class Field(object):
|
|||
``readonly`` boolean value defines that if this field will be assigned
|
||||
to object during import.
|
||||
|
||||
``default`` value returned by :meth`clean` if returned value evaluates to False
|
||||
``default`` value returned by :meth`clean` if returned value evaluates to
|
||||
False
|
||||
"""
|
||||
|
||||
def __init__(self, attribute=None, column_name=None, widget=None,
|
||||
|
@ -56,7 +57,8 @@ class Field(object):
|
|||
value = data[self.column_name]
|
||||
except KeyError:
|
||||
raise KeyError("Column '%s' not found in dataset. Available "
|
||||
"columns are: %s" % (self.column_name, list(data.keys())))
|
||||
"columns are: %s" % (self.column_name,
|
||||
list(data.keys())))
|
||||
|
||||
try:
|
||||
value = self.widget.clean(value)
|
||||
|
|
|
@ -63,7 +63,8 @@ class Format(object):
|
|||
return ""
|
||||
|
||||
def get_content_type(self):
|
||||
# For content types see http://www.iana.org/assignments/media-types/media-types.xhtml
|
||||
# For content types see
|
||||
# http://www.iana.org/assignments/media-types/media-types.xhtml
|
||||
return 'application/octet-stream'
|
||||
|
||||
def can_import(self):
|
||||
|
@ -95,8 +96,9 @@ class TablibFormat(Format):
|
|||
return self.get_format().export_set(dataset)
|
||||
|
||||
def get_extension(self):
|
||||
# we support both 'extentions' and 'extensions' because currently tablib's master
|
||||
# branch uses 'extentions' (which is a typo) but it's dev branch already uses 'extension'.
|
||||
# we support both 'extentions' and 'extensions' because currently
|
||||
# tablib's master branch uses 'extentions' (which is a typo) but it's
|
||||
# dev branch already uses 'extension'.
|
||||
# TODO - remove this once the typo is fixxed in tablib's master branch
|
||||
if hasattr(self.get_format(), 'extentions'):
|
||||
return self.get_format().extentions[0]
|
||||
|
@ -138,7 +140,8 @@ class JSON(TextFormat):
|
|||
|
||||
class YAML(TextFormat):
|
||||
TABLIB_MODULE = 'tablib.formats._yaml'
|
||||
CONTENT_TYPE = 'text/yaml' # See http://stackoverflow.com/questions/332129/yaml-mime-type
|
||||
# See http://stackoverflow.com/questions/332129/yaml-mime-type
|
||||
CONTENT_TYPE = 'text/yaml'
|
||||
|
||||
|
||||
class TSV(TextFormat):
|
||||
|
|
|
@ -9,12 +9,12 @@ from django.utils.translation import ugettext_lazy as _
|
|||
|
||||
class ImportForm(forms.Form):
|
||||
import_file = forms.FileField(
|
||||
label=_('File to import')
|
||||
)
|
||||
label=_('File to import')
|
||||
)
|
||||
input_format = forms.ChoiceField(
|
||||
label=_('Format'),
|
||||
choices=(),
|
||||
)
|
||||
label=_('Format'),
|
||||
choices=(),
|
||||
)
|
||||
|
||||
def __init__(self, import_formats, *args, **kwargs):
|
||||
super(ImportForm, self).__init__(*args, **kwargs)
|
||||
|
@ -40,9 +40,9 @@ class ConfirmImportForm(forms.Form):
|
|||
|
||||
class ExportForm(forms.Form):
|
||||
file_format = forms.ChoiceField(
|
||||
label=_('Format'),
|
||||
choices=(),
|
||||
)
|
||||
label=_('Format'),
|
||||
choices=(),
|
||||
)
|
||||
|
||||
def __init__(self, formats, *args, **kwargs):
|
||||
super(ExportForm, self).__init__(*args, **kwargs)
|
||||
|
|
|
@ -88,8 +88,8 @@ class ResourceOptions(object):
|
|||
transactions. Default value is ``None`` meaning
|
||||
``settings.IMPORT_EXPORT_USE_TRANSACTIONS`` will be evaluated.
|
||||
|
||||
* ``skip_unchanged`` - Controls if the import should skip unchanged records.
|
||||
Default value is False
|
||||
* ``skip_unchanged`` - Controls if the import should skip unchanged
|
||||
records. Default value is False
|
||||
|
||||
* ``report_skipped`` - Controls if the result reports skipped rows
|
||||
Default value is True
|
||||
|
@ -113,9 +113,9 @@ class DeclarativeMetaclass(type):
|
|||
declared_fields = []
|
||||
meta = ResourceOptions()
|
||||
|
||||
# If this class is subclassing another Resource, add that Resource's fields.
|
||||
# Note that we loop over the bases in *reverse*. This is necessary in
|
||||
# order to preserve the correct order of fields.
|
||||
# If this class is subclassing another Resource, add that Resource's
|
||||
# fields. Note that we loop over the bases in *reverse*. This is
|
||||
# necessary in order to preserve the correct order of fields.
|
||||
for base in bases[::-1]:
|
||||
if hasattr(base, 'fields'):
|
||||
declared_fields = list(six.iteritems(base.fields)) + declared_fields
|
||||
|
@ -135,7 +135,7 @@ class DeclarativeMetaclass(type):
|
|||
|
||||
attrs['fields'] = OrderedDict(declared_fields)
|
||||
new_class = super(DeclarativeMetaclass, cls).__new__(cls, name,
|
||||
bases, attrs)
|
||||
bases, attrs)
|
||||
|
||||
# Add direct options
|
||||
options = getattr(new_class, 'Meta', None)
|
||||
|
@ -264,7 +264,8 @@ class Resource(six.with_metaclass(DeclarativeMetaclass)):
|
|||
Returns ``True`` if ``row`` importing should be skipped.
|
||||
|
||||
Default implementation returns ``False`` unless skip_unchanged == True.
|
||||
Override this method to handle skipping rows meeting certain conditions.
|
||||
Override this method to handle skipping rows meeting certain
|
||||
conditions.
|
||||
"""
|
||||
if not self._meta.skip_unchanged:
|
||||
return False
|
||||
|
@ -313,7 +314,7 @@ class Resource(six.with_metaclass(DeclarativeMetaclass)):
|
|||
|
||||
@atomic()
|
||||
def import_data(self, dataset, dry_run=False, raise_errors=False,
|
||||
use_transactions=None, **kwargs):
|
||||
use_transactions=None, **kwargs):
|
||||
"""
|
||||
Imports data from ``dataset``.
|
||||
|
||||
|
@ -363,12 +364,12 @@ class Resource(six.with_metaclass(DeclarativeMetaclass)):
|
|||
if new:
|
||||
row_result.import_type = RowResult.IMPORT_TYPE_SKIP
|
||||
row_result.diff = self.get_diff(None, None,
|
||||
real_dry_run)
|
||||
real_dry_run)
|
||||
else:
|
||||
row_result.import_type = RowResult.IMPORT_TYPE_DELETE
|
||||
self.delete_instance(instance, real_dry_run)
|
||||
row_result.diff = self.get_diff(original, None,
|
||||
real_dry_run)
|
||||
real_dry_run)
|
||||
else:
|
||||
self.import_obj(instance, row, real_dry_run)
|
||||
if self.skip_row(instance, original):
|
||||
|
@ -380,7 +381,7 @@ class Resource(six.with_metaclass(DeclarativeMetaclass)):
|
|||
row_result.object_repr = force_text(instance)
|
||||
row_result.object_id = instance.pk
|
||||
row_result.diff = self.get_diff(original, instance,
|
||||
real_dry_run)
|
||||
real_dry_run)
|
||||
except Exception as e:
|
||||
# There is no point logging a transaction error for each row
|
||||
# when only the original error is likely to be relevant
|
||||
|
@ -393,7 +394,7 @@ class Resource(six.with_metaclass(DeclarativeMetaclass)):
|
|||
savepoint_rollback(sp1)
|
||||
six.reraise(*sys.exc_info())
|
||||
if (row_result.import_type != RowResult.IMPORT_TYPE_SKIP or
|
||||
self._meta.report_skipped):
|
||||
self._meta.report_skipped):
|
||||
result.rows.append(row_result)
|
||||
|
||||
if use_transactions:
|
||||
|
@ -405,8 +406,8 @@ class Resource(six.with_metaclass(DeclarativeMetaclass)):
|
|||
return result
|
||||
|
||||
def get_export_order(self):
|
||||
order = tuple (self._meta.export_order or ())
|
||||
return order + tuple (k for k in self.fields.keys() if k not in order)
|
||||
order = tuple(self._meta.export_order or ())
|
||||
return order + tuple(k for k in self.fields.keys() if k not in order)
|
||||
|
||||
def export_field(self, field, obj):
|
||||
field_name = self.get_field_name(field)
|
||||
|
@ -419,7 +420,8 @@ class Resource(six.with_metaclass(DeclarativeMetaclass)):
|
|||
return [self.export_field(field, obj) for field in self.get_fields()]
|
||||
|
||||
def get_export_headers(self):
|
||||
headers = [force_text(field.column_name) for field in self.get_fields()]
|
||||
headers = [
|
||||
force_text(field.column_name) for field in self.get_fields()]
|
||||
return headers
|
||||
|
||||
def export(self, queryset=None):
|
||||
|
@ -446,7 +448,7 @@ class ModelDeclarativeMetaclass(DeclarativeMetaclass):
|
|||
|
||||
def __new__(cls, name, bases, attrs):
|
||||
new_class = super(ModelDeclarativeMetaclass,
|
||||
cls).__new__(cls, name, bases, attrs)
|
||||
cls).__new__(cls, name, bases, attrs)
|
||||
|
||||
opts = new_class._meta
|
||||
|
||||
|
@ -467,12 +469,12 @@ class ModelDeclarativeMetaclass(DeclarativeMetaclass):
|
|||
continue
|
||||
|
||||
field = new_class.field_from_django_field(f.name, f,
|
||||
readonly=False)
|
||||
readonly=False)
|
||||
field_list.append((f.name, field, ))
|
||||
|
||||
new_class.fields.update(OrderedDict(field_list))
|
||||
|
||||
#add fields that follow relationships
|
||||
# add fields that follow relationships
|
||||
if opts.fields is not None:
|
||||
field_list = []
|
||||
for field_name in opts.fields:
|
||||
|
@ -490,13 +492,14 @@ class ModelDeclarativeMetaclass(DeclarativeMetaclass):
|
|||
f = model._meta.get_field_by_name(attr)[0]
|
||||
except FieldDoesNotExist as e:
|
||||
logging.exception(e)
|
||||
raise FieldDoesNotExist("%s: %s has no field named '%s'" %
|
||||
raise FieldDoesNotExist(
|
||||
"%s: %s has no field named '%s'" %
|
||||
(verbose_path, model.__name__, attr))
|
||||
|
||||
if i < len(attrs) - 1:
|
||||
# We're not at the last attribute yet, so check that
|
||||
# we're looking at a relation, and move on to the
|
||||
# next model.
|
||||
# We're not at the last attribute yet, so check
|
||||
# that we're looking at a relation, and move on to
|
||||
# the next model.
|
||||
if isinstance(f, ForeignObjectRel):
|
||||
if RelatedObject is None:
|
||||
model = f.related_model
|
||||
|
@ -505,14 +508,15 @@ class ModelDeclarativeMetaclass(DeclarativeMetaclass):
|
|||
model = f.model
|
||||
else:
|
||||
if f.rel is None:
|
||||
raise KeyError('%s is not a relation' % verbose_path)
|
||||
raise KeyError(
|
||||
'%s is not a relation' % verbose_path)
|
||||
model = f.rel.to
|
||||
|
||||
if isinstance(f, ForeignObjectRel):
|
||||
f = f.field
|
||||
|
||||
field = new_class.field_from_django_field(field_name, f,
|
||||
readonly=True)
|
||||
readonly=True)
|
||||
field_list.append((field_name, field))
|
||||
|
||||
new_class.fields.update(OrderedDict(field_list))
|
||||
|
@ -535,18 +539,19 @@ class ModelResource(six.with_metaclass(ModelDeclarativeMetaclass, Resource)):
|
|||
internal_type = f.get_internal_type()
|
||||
if internal_type in ('ManyToManyField', ):
|
||||
result = functools.partial(widgets.ManyToManyWidget,
|
||||
model=f.rel.to)
|
||||
model=f.rel.to)
|
||||
if internal_type in ('ForeignKey', 'OneToOneField', ):
|
||||
result = functools.partial(widgets.ForeignKeyWidget,
|
||||
model=f.rel.to)
|
||||
model=f.rel.to)
|
||||
if internal_type in ('DecimalField', ):
|
||||
result = widgets.DecimalWidget
|
||||
if internal_type in ('DateTimeField', ):
|
||||
result = widgets.DateTimeWidget
|
||||
elif internal_type in ('DateField', ):
|
||||
result = widgets.DateWidget
|
||||
elif internal_type in ('IntegerField', 'PositiveIntegerField', 'BigIntegerField',
|
||||
'PositiveSmallIntegerField', 'SmallIntegerField', 'AutoField'):
|
||||
elif internal_type in ('IntegerField', 'PositiveIntegerField',
|
||||
'BigIntegerField', 'PositiveSmallIntegerField',
|
||||
'SmallIntegerField', 'AutoField'):
|
||||
result = widgets.IntegerWidget
|
||||
elif internal_type in ('BooleanField', 'NullBooleanField'):
|
||||
result = widgets.BooleanWidget
|
||||
|
|
Loading…
Reference in New Issue