Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
La Quadrature du Net
piphone
campaign
Commits
8022545a
Commit
8022545a
authored
Sep 05, 2017
by
Okhin
Browse files
Fixing importer type detction
parent
b5de4d77
Pipeline
#1192
passed with stages
in 2 minutes and 32 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
picampaign/importer/models.py
View file @
8022545a
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'
]
=
[]
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment