Skip to content

Commit c690d63

Browse files
committed
Fixed sqlite compatibility with atomic
Fixed a few pep8 issues
1 parent f8f85d6 commit c690d63

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

simple_import/views.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
from django.core.exceptions import SuspiciousOperation
88
from django.core.urlresolvers import reverse
99
from django.db.models import Q, ForeignKey
10-
from django.db import transaction
11-
from django.db import IntegrityError
10+
from django.db import transaction, IntegrityError
1211
from django.core.exceptions import ObjectDoesNotExist
1312
from django.forms.models import inlineformset_factory
1413
from django.http import HttpResponseRedirect
@@ -19,13 +18,15 @@
1918
from django.utils.encoding import smart_text
2019

2120
from simple_import.compat import User
22-
from simple_import.models import ImportLog, ImportSetting, ColumnMatch, ImportedObject, RelationalMatch
21+
from simple_import.models import (ImportLog, ImportSetting, ColumnMatch,
22+
ImportedObject, RelationalMatch)
2323
from simple_import.forms import ImportForm, MatchForm, MatchRelationForm
2424

2525

26-
if sys.version_info >= (3,0):
26+
if sys.version_info >= (3, 0):
2727
unicode = str
2828

29+
2930
def is_foreign_key_id_name(field_name, field_object):
3031
""" Determines if field name is a ForeignKey
3132
ending in "_id"
@@ -34,6 +35,7 @@ def is_foreign_key_id_name(field_name, field_object):
3435
if field_name[-3:] == "_id" and isinstance(field_object, ForeignKey):
3536
return True
3637

38+
3739
def validate_match_columns(import_log, model_class, header_row):
3840
""" Perform some basic pre import validation to make sure it's
3941
even possible the import can work
@@ -44,13 +46,14 @@ def validate_match_columns(import_log, model_class, header_row):
4446
field_names = model_class._meta.get_all_field_names()
4547
for field_name in field_names:
4648
field_object, model, direct, m2m = model_class._meta.get_field_by_name(field_name)
47-
# Skip if update only and skip ptr which suggests it's a django inherited field
48-
# Also some hard coded ones for Django Auth
49-
if (import_log.import_type != "O" and
49+
# Skip if update only and skip ptr which suggests it's a django
50+
# inherited field. Also some hard coded ones for Django Auth
51+
if (import_log.import_type != "O" and
5052
field_name[-3:] != "ptr" and
51-
not field_name in ['password', 'date_joined', 'last_login'] and
53+
field_name not in ['password', 'date_joined', 'last_login'] and
5254
not is_foreign_key_id_name(field_name, field_object)):
53-
if (direct and model and not field_object.blank) or (not getattr(field_object, "blank", True)):
55+
if ((direct and model and not field_object.blank) or
56+
(not getattr(field_object, "blank", True))):
5457
field_matches = column_matches.filter(field_name=field_name)
5558
match_in_header = False
5659
if field_matches:
@@ -70,7 +73,9 @@ def get_custom_fields_from_model(model_class):
7073
if 'custom_field' in settings.INSTALLED_APPS:
7174
from custom_field.models import CustomField
7275
try:
73-
content_type = ContentType.objects.get(model=model_class._meta.module_name,app_label=model_class._meta.app_label)
76+
content_type = ContentType.objects.get(
77+
model=model_class._meta.module_name,
78+
app_label=model_class._meta.app_label)
7479
except ContentType.DoesNotExist:
7580
content_type = None
7681
custom_fields = CustomField.objects.filter(content_type=content_type)
@@ -158,7 +163,7 @@ def match_columns(request, import_log_id):
158163
else:
159164
field_verbose = field_name
160165

161-
if direct and not field_object.blank:
166+
if direct and not field_object.blank:
162167
field_verbose += " (Required)"
163168
if direct and field_object.unique:
164169
field_verbose += " (Unique)"
@@ -364,6 +369,7 @@ def do_import(request, import_log_id):
364369
key_index = i
365370

366371
with transaction.atomic():
372+
sid = transaction.savepoint()
367373
for row in import_data:
368374
try:
369375
with transaction.atomic():
@@ -449,8 +455,7 @@ def do_import(request, import_log_id):
449455
error_data += [row + ["Unknown Error"]]
450456
fail_count += 1
451457
if not commit:
452-
from django.db import connection
453-
connection._rollback()
458+
transaction.savepoint_rollback(sid)
454459

455460

456461
if fail_count:

0 commit comments

Comments
 (0)