Commit 8022545a authored by Okhin's avatar Okhin
Browse files

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 import Contact, Phone
from 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 is None:
return None
if self.url is '':
return 'file'
if self.url is not '':
return 'url'
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
updated_contact = {}
......@@ -63,14 +64,14 @@ class Importer(models.Model):
# This mandate has ended
# 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,
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'],
group, added = Group.objects.get_or_create(name = mandate['group']['name'],
type = groupType)
group, added=Group.objects.get_or_create(name=mandate['group']['name'],
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'],
# 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(','):
'name': group
'end_date': ''
{'kind': key.split(':')[1],
'name': group},
'end_date': ''})
# Same for phones and websites
contacts = {}
contacts['phones'] = []
Supports Markdown
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