|
| 1 | +from collections import OrderedDict |
| 2 | + |
| 3 | +import tablib |
| 4 | + |
| 5 | +from django.utils.translation import ugettext_lazy as _ |
| 6 | + |
| 7 | + |
| 8 | +class RowResult(object): |
| 9 | + |
| 10 | + def __init__(self): |
| 11 | + self.errors = [] |
| 12 | + self.orig_fields = [] |
| 13 | + self.fields = [] |
| 14 | + |
| 15 | + |
| 16 | +class Result(list): |
| 17 | + |
| 18 | + def __init__(self, *args, **kwargs): |
| 19 | + super(Result, self).__init__(*args, **kwargs) |
| 20 | + self.base_errors = [] |
| 21 | + |
| 22 | + def row_errors(self): |
| 23 | + return [(i, row.errors) for i, row in enumerate(self) if row.errors] |
| 24 | + |
| 25 | + def has_errors(self): |
| 26 | + return self.base_errors or self.row_errors() |
| 27 | + |
| 28 | + |
| 29 | +class Importer(object): |
| 30 | + model = None |
| 31 | + format = None |
| 32 | + import_code = "ID" |
| 33 | + raise_errors = True |
| 34 | + dry_run = True |
| 35 | + mapping = None |
| 36 | + |
| 37 | + def __init__(self, f, **kwargs): |
| 38 | + self.f = f |
| 39 | + for key, value in kwargs.iteritems(): |
| 40 | + setattr(self, key, value) |
| 41 | + |
| 42 | + def get_mapping(self): |
| 43 | + if self.mapping: |
| 44 | + return self.mapping |
| 45 | + mapping = [(f.verbose_name, f.name) for f in self.model._meta.fields] |
| 46 | + return OrderedDict(mapping) |
| 47 | + |
| 48 | + def load_dataset(self): |
| 49 | + text = unicode(self.f.read(), 'cp1250').encode('utf-8') |
| 50 | + if not self.format: |
| 51 | + self.data = tablib.import_set(text) |
| 52 | + else: |
| 53 | + self.data = tablib.Dataset() |
| 54 | + self.format.import_set(self.data, text) |
| 55 | + |
| 56 | + def get_instance(self, row): |
| 57 | + return self.model.objects.get(**{ |
| 58 | + self.get_mapping()[self.import_code]: row[self.import_code] |
| 59 | + }) |
| 60 | + |
| 61 | + def init_instance(self, row): |
| 62 | + return self.model() |
| 63 | + |
| 64 | + def get_or_init_instance(self, row): |
| 65 | + try: |
| 66 | + instance = self.get_instance(row) |
| 67 | + except self.model.DoesNotExist: |
| 68 | + instance = self.init_instance(row) |
| 69 | + return instance |
| 70 | + |
| 71 | + def set_instance_attr(self, instance, row, field): |
| 72 | + setattr(instance, self.get_mapping()[field], row[field]) |
| 73 | + |
| 74 | + def save_instance(self, instance): |
| 75 | + if not self.dry_run: |
| 76 | + instance.save() |
| 77 | + |
| 78 | + def get_representation(self, instance): |
| 79 | + return [unicode(getattr(instance, f)) |
| 80 | + for f in self.get_mapping().values()] |
| 81 | + |
| 82 | + def run(self): |
| 83 | + result = Result() |
| 84 | + try: |
| 85 | + self.load_dataset() |
| 86 | + except Exception, e: |
| 87 | + result.base_errors.append(_('Loading error') + |
| 88 | + u': %s' % unicode(e)) |
| 89 | + if self.raise_errors: |
| 90 | + raise |
| 91 | + return result |
| 92 | + |
| 93 | + for row in self.data.dict: |
| 94 | + try: |
| 95 | + row_result = RowResult() |
| 96 | + instance = self.get_or_init_instance(row) |
| 97 | + row_result.orig_fields = self.get_representation(instance) |
| 98 | + for field in self.get_mapping().keys(): |
| 99 | + self.set_instance_attr(instance, row, field) |
| 100 | + self.save_instance(instance) |
| 101 | + row_result.fields = self.get_representation(instance) |
| 102 | + except Exception, e: |
| 103 | + row_result.errors.append(unicode(e)) |
| 104 | + if self.raise_errors: |
| 105 | + raise |
| 106 | + result.append(row_result) |
| 107 | + return result |
0 commit comments