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
02913d91
Commit
02913d91
authored
Dec 24, 2016
by
Sli
Browse files
Refactors Candidate form
parent
9d9c86ea
Changes
3
Hide whitespace changes
Inline
Side-by-side
election/templates/election/candidate_form.jinja
View file @
02913d91
...
...
@@ -5,9 +5,12 @@
{%
endblock
%}
{%
block
content
%}
<form
action=
""
method=
"post"
>
{%
csrf_token
%}
{{
form.as_p
()
}}
<p><input
type=
"submit"
value=
"
{%
trans
%}
Save
{%
endtrans
%}
"
/></p>
</form>
{%
-
if
election.can_candidate
(
user
)
or
user.can_edit
(
election
)
%}
<section
class=
"election__add-candidature"
>
<form
action=
"
{{
url
(
'election:candidate'
,
election_id
=
election.id
)
}}
"
method=
"post"
>
{{
form.as_p
()
}}
<p><input
type=
"submit"
value=
"
{%
trans
%}
Candidate
{%
endtrans
%}
"
/></p>
{%
csrf_token
%}
</form>
</section>
{%
-
endif
%}
{%
endblock
content
%}
\ No newline at end of file
election/templates/election/election_detail.jinja
View file @
02913d91
...
...
@@ -344,20 +344,15 @@ th {
<button
class=
"election__sumbit-button"
form=
"vote-form"
>
{%
trans
%}
Submit the vote !
{%
endtrans
%}
</button>
</section>
{%
-
endif
%}
{%
-
if
user.can_edit
(
election
)
%}
<section
class=
"election__add-elements"
>
{%
-
if
election.can_candidate
(
user
)
or
user.can_edit
(
election
)
%}
<a
href=
"
{{
url
(
'election:candidate'
,
election_id
=
object.id
)
}}
"
>
{%
trans
%}
Candidate
{%
endtrans
%}
</a>
{%
-
endif
%}
<a
href=
"
{{
url
(
'election:create_list'
)
}}
"
>
{%
trans
%}
Add a new list
{%
endtrans
%}
</a>
{%
-
if
user.can_edit
(
election
)
%}
<a
href=
"
{{
url
(
'election:create_role'
)
}}
"
>
{%
trans
%}
Add a new role
{%
endtrans
%}
</a>
</section>
{%
-
endif
%}
{%
-
if
election.can_candidate
(
user
)
or
user.can_edit
(
election
)
%}
<section
class=
"election__add-candidature"
>
<form
action=
"
{{
url
(
'election:candidate'
,
election_id
=
election.id
)
}}
"
method=
"post"
>
{{
candidate_form
}}
{%
csrf_token
%}
<p><input
type=
"submit"
value=
"
{%
trans
%}
Candidate
{%
endtrans
%}
"
/></p>
</form>
</section>
{%
-
endif
%}
{%
endblock
%}
{%
block
script
%}
...
...
election/views.py
View file @
02913d91
...
...
@@ -21,7 +21,7 @@ from ajax_select.fields import AutoCompleteSelectField
# Custom form field
class
Vo
teCheckbox
(
forms
.
ModelMultipleChoiceField
):
class
Limi
te
d
Checkbox
Field
(
forms
.
ModelMultipleChoiceField
):
"""
Used to replace ModelMultipleChoiceField but with
automatic backend verification
...
...
@@ -30,11 +30,11 @@ class VoteCheckbox(forms.ModelMultipleChoiceField):
initial
=
None
,
help_text
=
''
,
*
args
,
**
kwargs
):
self
.
max_choice
=
max_choice
widget
=
forms
.
CheckboxSelectMultiple
()
super
(
Vo
teCheckbox
,
self
).
__init__
(
queryset
,
None
,
required
,
widget
,
label
,
super
(
Limi
te
d
Checkbox
Field
,
self
).
__init__
(
queryset
,
None
,
required
,
widget
,
label
,
initial
,
help_text
,
*
args
,
**
kwargs
)
def
clean
(
self
,
value
):
qs
=
super
(
Vo
teCheckbox
,
self
).
clean
(
value
)
qs
=
super
(
Limi
te
d
Checkbox
Field
,
self
).
clean
(
value
)
self
.
validate
(
qs
)
return
qs
...
...
@@ -46,15 +46,26 @@ class VoteCheckbox(forms.ModelMultipleChoiceField):
# Forms
class
CandidateForm
(
forms
.
Form
):
class
CandidateForm
(
forms
.
Model
Form
):
""" Form to candidate """
class
Meta
:
model
=
Candidature
fields
=
[
'user'
,
'role'
,
'program'
,
'election_list'
]
widgets
=
{
'program'
:
forms
.
Textarea
}
user
=
AutoCompleteSelectField
(
'users'
,
label
=
_
(
'User to candidate'
),
help_text
=
None
,
required
=
True
)
program
=
forms
.
CharField
(
widget
=
forms
.
Textarea
)
def
__init__
(
self
,
election_id
,
*
args
,
**
kwargs
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
election_id
=
kwargs
.
pop
(
'election_id'
,
None
)
can_edit
=
kwargs
.
pop
(
'can_edit'
,
False
)
super
(
CandidateForm
,
self
).
__init__
(
*
args
,
**
kwargs
)
self
.
fields
[
'role'
]
=
forms
.
ModelChoiceField
(
Role
.
objects
.
filter
(
election__id
=
election_id
))
self
.
fields
[
'election_list'
]
=
forms
.
ModelChoiceField
(
ElectionList
.
objects
.
filter
(
election__id
=
election_id
))
if
election_id
:
self
.
fields
[
'role'
].
queryset
=
Role
.
objects
.
filter
(
election__id
=
election_id
).
all
()
self
.
fields
[
'election_list'
].
queryset
=
ElectionList
.
objects
.
filter
(
election__id
=
election_id
).
all
()
if
not
can_edit
:
self
.
fields
[
'user'
].
widget
=
forms
.
HiddenInput
()
class
VoteForm
(
forms
.
Form
):
...
...
@@ -64,7 +75,7 @@ class VoteForm(forms.Form):
for
role
in
election
.
roles
.
all
():
cand
=
role
.
candidatures
if
role
.
max_choice
>
1
:
self
.
fields
[
role
.
title
]
=
Vo
teCheckbox
(
cand
,
role
.
max_choice
,
required
=
False
)
self
.
fields
[
role
.
title
]
=
Limi
te
d
Checkbox
Field
(
cand
,
role
.
max_choice
,
required
=
False
)
else
:
self
.
fields
[
role
.
title
]
=
forms
.
ModelChoiceField
(
cand
,
required
=
False
,
widget
=
forms
.
RadioSelect
(),
empty_label
=
_
(
"Blank vote"
))
...
...
@@ -112,66 +123,13 @@ 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
.
object
.
id
)
kwargs
[
'election_form'
]
=
VoteForm
(
self
.
object
,
self
.
request
.
user
)
kwargs
[
'election_results'
]
=
self
.
object
.
results
print
(
self
.
object
.
results
)
return
kwargs
# Form view
class
CandidatureCreateView
(
CanCreateMixin
,
FormView
):
"""
View dedicated to a cundidature creation
"""
form_class
=
CandidateForm
template_name
=
'election/election_detail.jinja'
def
dispatch
(
self
,
request
,
*
arg
,
**
kwargs
):
self
.
election_id
=
kwargs
[
'election_id'
]
self
.
election
=
get_object_or_404
(
Election
,
pk
=
self
.
election_id
)
return
super
(
CandidatureCreateView
,
self
).
dispatch
(
request
,
*
arg
,
**
kwargs
)
def
get_form_kwargs
(
self
):
kwargs
=
super
(
CandidatureCreateView
,
self
).
get_form_kwargs
()
kwargs
[
'election_id'
]
=
self
.
election_id
return
kwargs
def
create_candidature
(
self
,
data
):
cand
=
Candidature
(
role
=
data
[
'role'
],
user
=
data
[
'user'
],
election_list
=
data
[
'election_list'
],
program
=
data
[
'program'
]
)
cand
.
save
()
def
form_valid
(
self
,
form
):
"""
Verify that the selected user is in candidate group
"""
data
=
form
.
clean
()
res
=
super
(
FormView
,
self
).
form_valid
(
form
)
data
[
'election'
]
=
Election
.
objects
.
get
(
id
=
self
.
election_id
)
if
(
data
[
'election'
].
can_candidate
(
data
[
'user'
])):
self
.
create_candidature
(
data
)
return
res
return
res
def
get_context_data
(
self
,
**
kwargs
):
""" Add additionnal data to the template """
kwargs
=
super
(
CandidatureCreateView
,
self
).
get_context_data
(
**
kwargs
)
kwargs
[
'candidate_form'
]
=
self
.
get_form
()
kwargs
[
'object'
]
=
self
.
election
kwargs
[
'election'
]
=
self
.
election
kwargs
[
'election_form'
]
=
VoteForm
(
self
.
election
,
self
.
request
.
user
)
return
kwargs
def
get_success_url
(
self
,
**
kwargs
):
return
reverse_lazy
(
'election:detail'
,
kwargs
=
{
'election_id'
:
self
.
election_id
})
class
VoteFormView
(
CanCreateMixin
,
FormView
):
"""
Alows users to vote
...
...
@@ -223,7 +181,6 @@ class VoteFormView(CanCreateMixin, FormView):
def
get_context_data
(
self
,
**
kwargs
):
""" Add additionnal data to the template """
kwargs
=
super
(
VoteFormView
,
self
).
get_context_data
(
**
kwargs
)
kwargs
[
'candidate_form'
]
=
CandidateForm
(
self
.
election
.
id
)
kwargs
[
'object'
]
=
self
.
election
kwargs
[
'election'
]
=
self
.
election
kwargs
[
'election_form'
]
=
self
.
get_form
()
...
...
@@ -232,6 +189,48 @@ class VoteFormView(CanCreateMixin, FormView):
# Create views
class
CandidatureCreateView
(
CanCreateMixin
,
CreateView
):
"""
View dedicated to a cundidature creation
"""
form_class
=
CandidateForm
model
=
Candidature
template_name
=
'election/candidate_form.jinja'
def
dispatch
(
self
,
request
,
*
arg
,
**
kwargs
):
self
.
election
=
get_object_or_404
(
Election
,
pk
=
kwargs
[
'election_id'
])
return
super
(
CandidatureCreateView
,
self
).
dispatch
(
request
,
*
arg
,
**
kwargs
)
def
get_initial
(
self
):
init
=
{}
self
.
can_edit
=
self
.
request
.
user
.
can_edit
(
self
.
election
)
init
[
'user'
]
=
self
.
request
.
user
.
id
return
init
def
get_form_kwargs
(
self
):
kwargs
=
super
(
CandidatureCreateView
,
self
).
get_form_kwargs
()
kwargs
[
'election_id'
]
=
self
.
election
.
id
kwargs
[
'can_edit'
]
=
self
.
can_edit
return
kwargs
def
form_valid
(
self
,
form
):
"""
Verify that the selected user is in candidate group
"""
obj
=
form
.
instance
obj
.
election
=
Election
.
objects
.
get
(
id
=
self
.
election
.
id
)
if
(
obj
.
election
.
can_candidate
(
obj
.
user
))
and
(
obj
.
user
==
self
.
request
.
user
or
self
.
can_edit
):
return
super
(
CreateView
,
self
).
form_valid
(
form
)
raise
PermissionDenied
def
get_context_data
(
self
,
**
kwargs
):
kwargs
=
super
(
CandidatureCreateView
,
self
).
get_context_data
(
**
kwargs
)
kwargs
[
'election'
]
=
self
.
election
return
kwargs
def
get_success_url
(
self
,
**
kwargs
):
return
reverse_lazy
(
'election:detail'
,
kwargs
=
{
'election_id'
:
self
.
election
.
id
})
class
ElectionCreateView
(
CanCreateMixin
,
CreateView
):
model
=
Election
...
...
@@ -251,6 +250,14 @@ class ElectionCreateView(CanCreateMixin, CreateView):
})
template_name
=
'core/page_prop.jinja'
def
form_valid
(
self
,
form
):
"""
Verify that the user is suscribed
"""
res
=
super
(
CreateView
,
self
).
form_valid
(
form
)
if
self
.
request
.
user
.
is_subscribed
():
return
res
def
get_success_url
(
self
,
**
kwargs
):
return
reverse_lazy
(
'election:detail'
,
kwargs
=
{
'election_id'
:
self
.
object
.
id
})
...
...
@@ -265,11 +272,10 @@ class RoleCreateView(CanCreateMixin, CreateView):
Verify that the user can edit proprely
"""
obj
=
form
.
instance
res
=
super
(
CreateView
,
self
).
form_valid
if
obj
.
election
:
for
grp
in
obj
.
election
.
edit_groups
.
all
():
if
self
.
request
.
user
.
is_in_group
(
grp
):
return
res
(
form
)
return
super
(
CreateView
,
self
).
form_valid
(
form
)
raise
PermissionDenied
def
get_success_url
(
self
,
**
kwargs
):
...
...
@@ -287,14 +293,13 @@ class ElectionListCreateView(CanCreateMixin, CreateView):
Verify that the user can vote on this election
"""
obj
=
form
.
instance
res
=
super
(
CreateView
,
self
).
form_valid
if
obj
.
election
:
for
grp
in
obj
.
election
.
candidature_groups
.
all
():
if
self
.
request
.
user
.
is_in_group
(
grp
):
return
res
(
form
)
return
super
(
CreateView
,
self
).
form_valid
(
form
)
for
grp
in
obj
.
election
.
edit_groups
.
all
():
if
self
.
request
.
user
.
is_in_group
(
grp
):
return
res
(
form
)
return
super
(
CreateView
,
self
).
form_valid
(
form
)
raise
PermissionDenied
def
get_success_url
(
self
,
**
kwargs
):
...
...
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