diff --git a/koalixcrm/crm/contact/contact.py b/koalixcrm/crm/contact/contact.py index 5911eeeb..0a869dc5 100644 --- a/koalixcrm/crm/contact/contact.py +++ b/koalixcrm/crm/contact/contact.py @@ -14,7 +14,6 @@ from koalixcrm.crm.const.purpose import * from koalixcrm.globalSupportFunctions import xstr from koalixcrm.crm.inlinemixin import LimitedAdminInlineMixin -#from koalixcrm.crm.forms import ImportDataContactForm from django.utils import timezone @@ -240,11 +239,10 @@ def lookups(self, request, model_admin): ) def queryset(self, request, queryset): - for p in PostalAddressForContact.objects.all(): - if self.value() == str(p.state): - address_per_company = PostalAddressForContact.objects.filter(state=p.state) - ids = [(a.company.id) for a in address_per_company] - return queryset.filter(pk__in=ids) + if self.value(): + matching_addresses = PostalAddressForContact.objects.filter(state=self.value()) + ids = [(a.company.id) for a in matching_addresses] + return queryset.filter(pk__in=ids) return queryset class CityFilter(admin.SimpleListFilter): @@ -263,33 +261,8 @@ def lookups(self, request, model_admin): ) def queryset(self, request, queryset): - for p in PostalAddressForContact.objects.all(): - if self.value() == str(p.town): - address_per_company = PostalAddressForContact.objects.filter(town=p.town) - ids = [(c.company.id) for c in address_per_company] - return queryset.filter(pk__in=ids) + if self.value(): + matching_addresses = PostalAddressForContact.objects.filter(town=self.value()) + ids = [(a.company.id) for a in matching_addresses] + return queryset.filter(pk__in=ids) return queryset - - -#DATA IMPORT -class ContactImportData(models.Model): - data_file = models.FileField(upload_to='data_files', max_length=255) - - contact_type = models.CharField(verbose_name=_("Contact Type"), max_length=1, choices=CONTACTTYPE) - - def file_link(self): - if self.data_file: - return "download" % (self.data_file.url,) - else: - return "No attachment" - - file_link.allow_tags = True - - def __str__(self): - return '{}'.format(self.data_file.name) - - class Meta: - """ - """ - verbose_name = 'Contact: Import Data from XLSX file' - verbose_name_plural = 'Contacts: Import Data from XLSX file' diff --git a/koalixcrm/crm/contact/data_import.py b/koalixcrm/crm/contact/data_import.py new file mode 100644 index 00000000..cbe0de2a --- /dev/null +++ b/koalixcrm/crm/contact/data_import.py @@ -0,0 +1,21 @@ +class ContactImportData(models.Model): + data_file = models.FileField(upload_to='data_files', max_length=255) + + contact_type = models.CharField(verbose_name=_("Contact Type"), max_length=1, choices=CONTACTTYPE) + + def file_link(self): + if self.data_file: + return "download" % (self.data_file.url,) + else: + return "No attachment" + + file_link.allow_tags = True + + def __str__(self): + return '{}'.format(self.data_file.name) + + class Meta: + """ + """ + verbose_name = 'Contact: Import Data from XLSX file' + verbose_name_plural = 'Contacts: Import Data from XLSX file' diff --git a/koalixcrm/crm/forms.py b/koalixcrm/crm/forms.py index 073617e2..c69e1238 100644 --- a/koalixcrm/crm/forms.py +++ b/koalixcrm/crm/forms.py @@ -4,7 +4,7 @@ from django.core.files.base import ContentFile from django import forms from django.forms import models -from koalixcrm.crm.contact.contact import ContactImportData +from koalixcrm.crm.contact.data_import import ContactImportData from koalixcrm.crm.tasks import import_contact_data class ImportDataContactForm(models.ModelForm): diff --git a/koalixcrm/crm/management/commands/importcontactdata.py b/koalixcrm/crm/management/commands/importcontactdata.py index 82fdc267..44695a23 100644 --- a/koalixcrm/crm/management/commands/importcontactdata.py +++ b/koalixcrm/crm/management/commands/importcontactdata.py @@ -196,16 +196,14 @@ def prepare_product_args(self, product_type, sheet, row_num): def add_product(self, product_type, contact, sheet, row_num): product_args, relation_args = self.prepare_product_args(product_type, sheet, row_num) if product_args is None: return - #with open('log.txt', 'w') as logfile: - #logfile.write("Value : %s" % product_args.values()) + product, created = Product.objects.update_or_create(**product_args) if product_type == PHONE_SYSTEM_P_TYPE: switchboard, created = SwitchboardForCustomer.objects.get_or_create( customer=contact, product=product, ) - #with open('log.txt', 'w') as logfile: - #logfile.write("Value : %s" % relation_args.values()) + updated = SwitchboardForCustomer.objects.filter(pk=switchboard.pk).update(**relation_args) elif product_type == ANALOG_PHONE_P_TYPE: analogphone, created = AnalogPhoneForCustomer.objects.get_or_create( @@ -448,9 +446,7 @@ def handle(self, **options): supplier.save() contact = supplier else: - raise CommandError("Cannot determine contact type") - - + raise CommandError("Cannot determine contact type") return '{}'.format(count)