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
import json
import requests
import re
from django.db import models
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.organization.models import Group, GroupType
class Importer(models.Model):
"""Importer model. Used to populate campaign with contacts"""
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=64)
category = models.CharField(max_length=6, choices=(('update', _('CSV Updater'))
, ('repr', _("Representative format"))))
category = models.CharField(max_length=6, choices=(('update', _('CSV Updater')),
('repr', _("Representative format"))))
file = models.FileField(upload_to='imports/', blank=True, null=True)
url = models.URLField(blank=True, null=True)
campaign = models.ForeignKey(Campaign, on_delete=models.SET_NULL, null=True)
......@@ -30,10 +31,10 @@ class Importer(models.Model):
def kind(self):
if self.url is None and self.file.name is None:
return None
if self.url is '':
return 'file'
else:
if self.url is not '':
return 'url'
else:
return 'file'
def representative(self, import_data):
"""
......@@ -47,7 +48,7 @@ class Importer(models.Model):
inserted = 0
for import_contact in import_data:
# 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
continue
updated_contact = {}
......@@ -63,14 +64,14 @@ class Importer(models.Model):
# This mandate has ended
continue
# We want to only use the groups own by the organisation
for groupType in GroupType.objects.filter(organization = self.campaign.organization,
name = mandate['group']['kind']):
for groupType in GroupType.objects.filter(organization=self.campaign.organization,
name=mandate['group']['kind']):
if 'abbreviation' in mandate['group']:
group, added = Group.objects.get_or_create(name = mandate['group']['abbreviation'],
type = groupType)
group, added = Group.objects.get_or_create(name=mandate['group']['abbreviation'],
type=groupType)
else:
group, added = Group.objects.get_or_create(name = mandate['group']['name'],
type = groupType)
group, added=Group.objects.get_or_create(name=mandate['group']['name'],
type=groupType)
groups.append(group)
if groups == []:
# The contact have no groups active, he is not to be bothered.
......@@ -103,10 +104,9 @@ class Importer(models.Model):
# Let's update_or_create the contact
contact, updated = Contact.objects.update_or_create(first_name=import_contact['first_name'],
last_name=import_contact['last_name'],
birthdate=import_contact['birth_date'],
defaults=updated_contact
)
last_name=import_contact['last_name'],
birthdate=import_contact['birth_date'],
defaults=updated_contact)
# We want to create the phones
for phone in phones:
......@@ -156,11 +156,9 @@ class Importer(models.Model):
for key in [k for k in item if k.startswith('mandates:')]:
for group in item[key].split(','):
mandates.append({'group':
{'kind':key.split(':')[1],
'name': group
},
'end_date': ''
})
{'kind': key.split(':')[1],
'name': group},
'end_date': ''})
# Same for phones and websites
contacts = {}
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