Skip to content

Commit

Permalink
separate class for contact data import
Browse files Browse the repository at this point in the history
  • Loading branch information
bahron83 committed May 31, 2018
1 parent 1ad784c commit 225c213
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 43 deletions.
43 changes: 8 additions & 35 deletions koalixcrm/crm/contact/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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 "<a href='%s'>download</a>" % (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'
21 changes: 21 additions & 0 deletions koalixcrm/crm/contact/data_import.py
Original file line number Diff line number Diff line change
@@ -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 "<a href='%s'>download</a>" % (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'
2 changes: 1 addition & 1 deletion koalixcrm/crm/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 3 additions & 7 deletions koalixcrm/crm/management/commands/importcontactdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 225c213

Please sign in to comment.