Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Helios Server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TO
Helios Server
Commits
e3f3ad42
Commit
e3f3ad42
authored
Sep 17, 2021
by
Liz Fong-Jones
Browse files
Options
Downloads
Patches
Plain Diff
avoid double-creating users
parent
99e1c179
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
helios/models.py
+42
-25
42 additions, 25 deletions
helios/models.py
helios/views.py
+4
-6
4 additions, 6 deletions
helios/views.py
with
46 additions
and
31 deletions
helios/models.py
+
42
−
25
View file @
e3f3ad42
...
...
@@ -16,6 +16,7 @@ import bleach
import
unicodecsv
from
django.conf
import
settings
from
django.db
import
models
,
transaction
from
validate_email
import
validate_email
from
helios
import
datatypes
from
helios
import
utils
...
...
@@ -768,20 +769,33 @@ class VoterFile(models.Model):
if
len
(
voter_fields
)
<
2
:
continue
return_dict
=
{
'
voter_type
'
:
voter_fields
[
0
].
strip
(),
'
voter_id
'
:
voter_fields
[
1
].
strip
()}
voter_type
=
voter_fields
[
0
].
strip
()
voter_id
=
voter_fields
[
1
].
strip
()
if
not
voter_type
in
AUTH_SYSTEMS
:
raise
Exception
(
"
invalid voter type
'
%s
'
for voter id
'
%s
'"
%
(
voter_type
,
voter_id
))
# default to having email be the same as voter_id
voter_email
=
voter_id
if
len
(
voter_fields
)
>
2
:
return_dict
[
'
email
'
]
=
voter_fields
[
2
].
strip
()
else
:
# assume single field means the email is the same field as voter_id
r
eturn_dict
[
'
email
'
]
=
voter_fields
[
1
].
strip
(
)
# but if it's supplied, it will be the 3rd field.
voter_email
=
voter_fields
[
2
].
strip
()
if
voter_type
==
"
password
"
and
not
validate_email
(
voter_email
):
r
aise
Exception
(
"
invalid voter email
'
%s
'
for voter id
'
%s
'"
%
(
voter_email
,
voter_id
)
)
# same thing for voter display name.
voter_name
=
voter_email
if
len
(
voter_fields
)
>
3
:
return_dict
[
'
name
'
]
=
voter_fields
[
3
].
strip
()
else
:
return_dict
[
'
name
'
]
=
return_dict
[
'
email
'
]
# which is supplied as the 4th field if known.
voter_name
=
voter_fields
[
3
].
strip
()
yield
{
'
voter_type
'
:
voter_type
,
'
voter_id
'
:
voter_id
,
'
email
'
:
voter_email
,
'
name
'
:
voter_name
,
}
yield
return_dict
if
close
:
voter_stream
.
close
()
...
...
@@ -794,14 +808,12 @@ class VoterFile(models.Model):
random
.
shuffle
(
voters
)
for
voter
in
voters
:
if
voter
[
'
voter_type
'
]
==
'
password
'
:
# does voter for this user already exist
existing_voter
=
Voter
.
get_by_election_and_voter_id
(
self
.
election
,
voter
[
'
voter_id
'
])
if
existing_voter
:
continue
# create the voter
if
not
existing_voter
:
if
voter
[
'
voter_type
'
]
!=
'
password
'
:
user
,
_
=
User
.
objects
.
get_or_create
(
user_type
=
voter
[
'
voter_type
'
],
user_id
=
voter
[
'
voter_id
'
],
defaults
=
{
'
name
'
:
voter
[
'
voter_id
'
],
'
info
'
:
{},
'
token
'
:
None
})
Voter
.
register_user_in_election
(
user
,
self
.
election
)
else
:
voter_uuid
=
str
(
uuid
.
uuid4
())
new_voter
=
Voter
(
uuid
=
voter_uuid
,
user
=
None
,
voter_login_id
=
voter
[
'
voter_id
'
],
voter_name
=
voter
[
'
name
'
],
voter_email
=
voter
[
'
email
'
],
election
=
self
.
election
)
...
...
@@ -811,6 +823,11 @@ class VoterFile(models.Model):
alias_num
=
election
.
last_alias_num
+
1
new_voter
.
alias
=
"
V%s
"
%
alias_num
new_voter
.
save
()
else
:
user
,
_
=
User
.
objects
.
get_or_create
(
user_type
=
voter
[
'
voter_type
'
],
user_id
=
voter
[
'
voter_id
'
],
defaults
=
{
'
name
'
:
voter
[
'
voter_id
'
],
'
info
'
:
{},
'
token
'
:
None
})
existing_voter
=
Voter
.
get_by_election_and_user
(
self
.
election
,
user
)
if
not
existing_voter
:
Voter
.
register_user_in_election
(
user
,
self
.
election
)
self
.
processing_finished_at
=
datetime
.
datetime
.
utcnow
()
self
.
save
()
...
...
This diff is collapsed.
Click to expand it.
helios/views.py
+
4
−
6
View file @
e3f3ad42
...
...
@@ -1298,13 +1298,11 @@ def voters_upload(request, election):
# import the first few lines to check
try
:
voters
=
[
v
for
v
in
voter_file_obj
.
itervoters
()][:
5
]
except
:
if
len
(
voters
)
==
0
:
raise
Exception
(
"
no valid lines found in voter file
"
)
except
Exception
as
e
:
voters
=
[]
problems
.
append
(
"
your CSV file could not be processed. Please check that it is a proper CSV file.
"
)
# check if voter emails look like emails
if
False
in
[
v
[
'
voter_type
'
]
in
AUTH_SYSTEMS
for
v
in
voters
]:
problems
.
append
(
"
those don
'
t look like valid auth methods. Are you sure you uploaded a file with voter type as first field?
"
)
problems
.
append
(
"
your CSV file could not be processed because %s
"
%
str
(
e
))
return
render_template
(
request
,
'
voters_upload_confirm
'
,
{
'
election
'
:
election
,
'
voters
'
:
voters
,
'
problems
'
:
problems
})
else
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment