Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Sith
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
59
Issues
59
List
Boards
Labels
Service Desk
Milestones
Merge Requests
9
Merge Requests
9
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
AE
Sith
Commits
9d9c86ea
Commit
9d9c86ea
authored
Dec 23, 2016
by
Sli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored has_voted
parent
37decde0
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
21 deletions
+41
-21
election/migrations/0006_auto_20161223_2315.py
election/migrations/0006_auto_20161223_2315.py
+25
-0
election/models.py
election/models.py
+8
-14
election/templates/election/candidate_form.jinja
election/templates/election/candidate_form.jinja
+1
-1
election/views.py
election/views.py
+7
-6
No files found.
election/migrations/0006_auto_20161223_2315.py
0 → 100644
View file @
9d9c86ea
# -*- coding: utf-8 -*-
from
__future__
import
unicode_literals
from
django.db
import
migrations
,
models
from
django.conf
import
settings
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
migrations
.
swappable_dependency
(
settings
.
AUTH_USER_MODEL
),
(
'election'
,
'0005_auto_20161223_2240'
),
]
operations
=
[
migrations
.
RemoveField
(
model_name
=
'role'
,
name
=
'has_voted'
,
),
migrations
.
AddField
(
model_name
=
'election'
,
name
=
'voters'
,
field
=
models
.
ManyToManyField
(
to
=
settings
.
AUTH_USER_MODEL
,
verbose_name
=
'has voted'
,
related_name
=
'has_voted'
),
),
]
election/models.py
View file @
9d9c86ea
...
...
@@ -22,6 +22,7 @@ class Election(models.Model):
view_groups
=
models
.
ManyToManyField
(
Group
,
related_name
=
"viewable_elections"
,
verbose_name
=
_
(
"view groups"
),
blank
=
True
)
vote_groups
=
models
.
ManyToManyField
(
Group
,
related_name
=
"votable_elections"
,
verbose_name
=
_
(
"vote groups"
),
blank
=
True
)
candidature_groups
=
models
.
ManyToManyField
(
Group
,
related_name
=
"candidate_elections"
,
verbose_name
=
_
(
"candidature groups"
),
blank
=
True
)
voters
=
models
.
ManyToManyField
(
User
,
verbose_name
=
(
'voters'
),
related_name
=
'voted_elections'
)
def
__str__
(
self
):
return
self
.
title
...
...
@@ -40,12 +41,6 @@ class Election(models.Model):
now
=
timezone
.
now
()
return
bool
(
now
<=
self
.
end_candidature
and
now
>=
self
.
start_candidature
)
def
has_voted
(
self
,
user
):
for
role
in
self
.
roles
.
all
():
if
role
.
user_has_voted
(
user
):
return
True
return
False
def
can_candidate
(
self
,
user
):
for
group
in
self
.
candidature_groups
.
all
():
if
user
.
is_in_group
(
group
):
...
...
@@ -60,11 +55,15 @@ class Election(models.Model):
return
True
return
False
def
has_voted
(
self
,
user
):
return
self
.
voters
.
filter
(
id
=
user
.
id
).
exists
()
@
property
def
results
(
self
):
results
=
{}
total_vote
=
self
.
voters
.
count
()
for
role
in
self
.
roles
.
all
():
results
[
role
.
title
]
=
role
.
results
results
[
role
.
title
]
=
role
.
results
(
total_vote
)
return
results
# Permissions
...
...
@@ -77,16 +76,11 @@ class Role(models.Model):
election
=
models
.
ForeignKey
(
Election
,
related_name
=
'roles'
,
verbose_name
=
_
(
"election"
))
title
=
models
.
CharField
(
_
(
'title'
),
max_length
=
255
)
description
=
models
.
TextField
(
_
(
'description'
),
null
=
True
,
blank
=
True
)
has_voted
=
models
.
ManyToManyField
(
User
,
verbose_name
=
(
'has voted'
),
related_name
=
'has_voted'
)
max_choice
=
models
.
IntegerField
(
_
(
'max choice'
),
default
=
1
)
def
user_has_voted
(
self
,
user
):
return
self
.
has_voted
.
filter
(
id
=
user
.
id
).
exists
()
@
property
def
results
(
self
):
def
results
(
self
,
total_vote
):
results
=
{}
total_vote
=
self
.
has_voted
.
count
()
*
self
.
max_choice
total_vote
*=
self
.
max_choice
if
total_vote
==
0
:
return
None
non_blank
=
0
...
...
election/templates/election/
vo
te_form.jinja
→
election/templates/election/
candida
te_form.jinja
View file @
9d9c86ea
{%
extends
"core/base.jinja"
%}
{%
block
title
%}
{%
trans
%}
Vo
te
{%
endtrans
%}
{%
trans
%}
Candida
te
{%
endtrans
%}
{%
endblock
%}
{%
block
content
%}
...
...
election/views.py
View file @
9d9c86ea
...
...
@@ -60,8 +60,8 @@ class CandidateForm(forms.Form):
class
VoteForm
(
forms
.
Form
):
def
__init__
(
self
,
election
,
user
,
*
args
,
**
kwargs
):
super
(
VoteForm
,
self
).
__init__
(
*
args
,
**
kwargs
)
for
role
in
election
.
roles
.
all
(
):
if
not
role
.
user_has_voted
(
user
):
if
not
election
.
has_voted
(
user
):
for
role
in
election
.
roles
.
all
(
):
cand
=
role
.
candidatures
if
role
.
max_choice
>
1
:
self
.
fields
[
role
.
title
]
=
VoteCheckbox
(
cand
,
role
.
max_choice
,
required
=
False
)
...
...
@@ -112,9 +112,10 @@ class ElectionDetailView(CanViewMixin, DetailView):
def
get_context_data
(
self
,
**
kwargs
):
""" Add additionnal data to the template """
kwargs
=
super
(
ElectionDetailView
,
self
).
get_context_data
(
**
kwargs
)
kwargs
[
'candidate_form'
]
=
CandidateForm
(
self
.
get_object
().
id
)
kwargs
[
'election_form'
]
=
VoteForm
(
self
.
get_object
(),
self
.
request
.
user
)
kwargs
[
'election_results'
]
=
self
.
get_object
().
results
kwargs
[
'candidate_form'
]
=
CandidateForm
(
self
.
object
.
id
)
kwargs
[
'election_form'
]
=
VoteForm
(
self
.
object
,
self
.
request
.
user
)
kwargs
[
'election_results'
]
=
self
.
object
.
results
print
(
self
.
object
.
results
)
return
kwargs
...
...
@@ -196,7 +197,7 @@ class VoteFormView(CanCreateMixin, FormView):
vote
=
Vote
(
role
=
election_data
[
role_title
].
role
)
vote
.
save
()
vote
.
candidature
.
add
(
election_data
[
role_title
])
self
.
election
.
roles
.
get
(
title
=
role_title
).
has_voted
.
add
(
self
.
request
.
user
)
self
.
election
.
voters
.
add
(
self
.
request
.
user
)
def
get_form_kwargs
(
self
):
kwargs
=
super
(
VoteFormView
,
self
).
get_form_kwargs
()
...
...
Write
Preview
Markdown
is supported
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