Commit 8022545a authored by okhin's avatar okhin 🚴

Fixing importer type detction

parent b5de4d77
Pipeline #1192 passed with stages
in 2 minutes and 32 seconds
from csv import DictReader from csv import DictReader
import json import json
import requests
import re
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from picampaign.contact.models import Contact, Phone
from picampaign.contact.models import Contact
from picampaign.campaign.models import Campaign, CampaignContact from picampaign.campaign.models import Campaign, CampaignContact
from picampaign.organization.models import Group, GroupType from picampaign.organization.models import Group, GroupType
class Importer(models.Model): class Importer(models.Model):
"""Importer model. Used to populate campaign with contacts""" """Importer model. Used to populate campaign with contacts"""
id = models.AutoField(primary_key=True) id = models.AutoField(primary_key=True)
name = models.CharField(max_length=64) name = models.CharField(max_length=64)
category = models.CharField(max_length=6, choices=(('update', _('CSV Updater')) category = models.CharField(max_length=6, choices=(('update', _('CSV Updater')),
, ('repr', _("Representative format")))) ('repr', _("Representative format"))))
file = models.FileField(upload_to='imports/', blank=True, null=True) file = models.FileField(upload_to='imports/', blank=True, null=True)
url = models.URLField(blank=True, null=True) url = models.URLField(blank=True, null=True)
campaign = models.ForeignKey(Campaign, on_delete=models.SET_NULL, null=True) campaign = models.ForeignKey(Campaign, on_delete=models.SET_NULL, null=True)
...@@ -30,10 +31,10 @@ class Importer(models.Model): ...@@ -30,10 +31,10 @@ class Importer(models.Model):
def kind(self): def kind(self):
if self.url is None and self.file.name is None: if self.url is None and self.file.name is None:
return None return None
if self.url is '': if self.url is not '':
return 'file'
else:
return 'url' return 'url'
else:
return 'file'
def representative(self, import_data): def representative(self, import_data):
""" """
...@@ -47,7 +48,7 @@ class Importer(models.Model): ...@@ -47,7 +48,7 @@ class Importer(models.Model):
inserted = 0 inserted = 0
for import_contact in import_data: for import_contact in import_data:
# We want to know if all mandates exists # We want to know if all mandates exists
if not 'birth_date' in import_contact or not 'first_name' in import_contact or not 'last_name' in import_contact: if 'birth_date' not in import_contact or 'first_name' not in import_contact or 'last_name' not in import_contact:
# We do not have enough field to discriminate our contacts # We do not have enough field to discriminate our contacts
continue continue
updated_contact = {} updated_contact = {}
...@@ -63,14 +64,14 @@ class Importer(models.Model): ...@@ -63,14 +64,14 @@ class Importer(models.Model):
# This mandate has ended # This mandate has ended
continue continue
# We want to only use the groups own by the organisation # We want to only use the groups own by the organisation
for groupType in GroupType.objects.filter(organization = self.campaign.organization, for groupType in GroupType.objects.filter(organization=self.campaign.organization,
name = mandate['group']['kind']): name=mandate['group']['kind']):
if 'abbreviation' in mandate['group']: if 'abbreviation' in mandate['group']:
group, added = Group.objects.get_or_create(name = mandate['group']['abbreviation'], group, added = Group.objects.get_or_create(name=mandate['group']['abbreviation'],
type = groupType) type=groupType)
else: else:
group, added = Group.objects.get_or_create(name = mandate['group']['name'], group, added=Group.objects.get_or_create(name=mandate['group']['name'],
type = groupType) type=groupType)
groups.append(group) groups.append(group)
if groups == []: if groups == []:
# The contact have no groups active, he is not to be bothered. # The contact have no groups active, he is not to be bothered.
...@@ -103,10 +104,9 @@ class Importer(models.Model): ...@@ -103,10 +104,9 @@ class Importer(models.Model):
# Let's update_or_create the contact # Let's update_or_create the contact
contact, updated = Contact.objects.update_or_create(first_name=import_contact['first_name'], contact, updated = Contact.objects.update_or_create(first_name=import_contact['first_name'],
last_name=import_contact['last_name'], last_name=import_contact['last_name'],
birthdate=import_contact['birth_date'], birthdate=import_contact['birth_date'],
defaults=updated_contact defaults=updated_contact)
)
# We want to create the phones # We want to create the phones
for phone in phones: for phone in phones:
...@@ -156,11 +156,9 @@ class Importer(models.Model): ...@@ -156,11 +156,9 @@ class Importer(models.Model):
for key in [k for k in item if k.startswith('mandates:')]: for key in [k for k in item if k.startswith('mandates:')]:
for group in item[key].split(','): for group in item[key].split(','):
mandates.append({'group': mandates.append({'group':
{'kind':key.split(':')[1], {'kind': key.split(':')[1],
'name': group 'name': group},
}, 'end_date': ''})
'end_date': ''
})
# Same for phones and websites # Same for phones and websites
contacts = {} contacts = {}
contacts['phones'] = [] contacts['phones'] = []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment