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
AE UTBM
Sith
Commits
9d9c86ea
Commit
9d9c86ea
authored
Dec 23, 2016
by
Sli
Browse files
Refactored has_voted
parent
37decde0
Changes
4
Hide whitespace changes
Inline
Side-by-side
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