SciPost Code Repository
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SciPost
Manage
Activity
Members
Labels
Plan
Issues
118
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
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
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SciPost
SciPost
Commits
7273960b
Commit
7273960b
authored
6 years ago
by
Jean-Sébastien Caux
Browse files
Options
Downloads
Patches
Plain Diff
Simplify create-confirm views for Post creation
parent
13a926f4
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
forums/forms.py
+0
-13
0 additions, 13 deletions
forums/forms.py
forums/templates/forums/post_card.html
+1
-1
1 addition, 1 deletion
forums/templates/forums/post_card.html
forums/views.py
+37
-26
37 additions, 26 deletions
forums/views.py
with
38 additions
and
40 deletions
forums/forms.py
+
0
−
13
View file @
7273960b
...
@@ -58,16 +58,3 @@ class PostForm(forms.ModelForm):
...
@@ -58,16 +58,3 @@ class PostForm(forms.ModelForm):
self
.
fields
[
'
needs_vetting
'
].
widget
=
forms
.
HiddenInput
()
self
.
fields
[
'
needs_vetting
'
].
widget
=
forms
.
HiddenInput
()
self
.
fields
[
'
parent_content_type
'
].
widget
=
forms
.
HiddenInput
()
self
.
fields
[
'
parent_content_type
'
].
widget
=
forms
.
HiddenInput
()
self
.
fields
[
'
parent_object_id
'
].
widget
=
forms
.
HiddenInput
()
self
.
fields
[
'
parent_object_id
'
].
widget
=
forms
.
HiddenInput
()
class
PostFormHidden
(
PostForm
):
class
Meta
:
model
=
Post
fields
=
[
'
posted_by
'
,
'
posted_on
'
,
'
needs_vetting
'
,
'
parent_content_type
'
,
'
parent_object_id
'
,
'
subject
'
,
'
text
'
]
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
self
.
fields
[
'
subject
'
].
widget
=
forms
.
HiddenInput
()
self
.
fields
[
'
text
'
].
widget
=
forms
.
HiddenInput
()
This diff is collapsed.
Click to expand it.
forums/templates/forums/post_card.html
+
1
−
1
View file @
7273960b
...
@@ -18,7 +18,7 @@
...
@@ -18,7 +18,7 @@
{% if post.followup_posts.all|length > 0 %}
{% if post.followup_posts.all|length > 0 %}
<span
class=
"badge badge-primary badge-pill"
>
{% with nr_followups=post.nr_followups %}{{ nr_followups }} repl{{ nr_followups|pluralize:"y,ies" }}{% endwith %}
</span>
<span
class=
"badge badge-primary badge-pill"
>
{% with nr_followups=post.nr_followups %}{{ nr_followups }} repl{{ nr_followups|pluralize:"y,ies" }}{% endwith %}
</span>
{% endif %}
{% endif %}
<a
href=
"{% url 'forums:post_create' slug=forum.slug parent_model='post' parent_id=post.id %}"
>
Reply to th
is
post
</a>
<a
href=
"{% url 'forums:post_create' slug=forum.slug parent_model='post' parent_id=post.id %}"
>
Reply to th
e above
post
</a>
</div>
</div>
{% if post.followup_posts.all|length > 0 %}
{% if post.followup_posts.all|length > 0 %}
<div
class=
"followupPosts"
>
<div
class=
"followupPosts"
>
...
...
This diff is collapsed.
Click to expand it.
forums/views.py
+
37
−
26
View file @
7273960b
...
@@ -2,6 +2,7 @@ __copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
...
@@ -2,6 +2,7 @@ __copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__
=
"
AGPL v3
"
__license__
=
"
AGPL v3
"
from
django
import
forms
from
django.contrib
import
messages
from
django.contrib
import
messages
from
django.contrib.auth.models
import
Group
from
django.contrib.auth.models
import
Group
from
django.contrib.auth.mixins
import
LoginRequiredMixin
,
UserPassesTestMixin
from
django.contrib.auth.mixins
import
LoginRequiredMixin
,
UserPassesTestMixin
...
@@ -21,7 +22,7 @@ from guardian.shortcuts import (assign_perm, remove_perm,
...
@@ -21,7 +22,7 @@ from guardian.shortcuts import (assign_perm, remove_perm,
from
.models
import
Forum
,
Post
from
.models
import
Forum
,
Post
from
.forms
import
(
ForumForm
,
ForumGroupPermissionsForm
,
ForumOrganizationPermissionsForm
,
from
.forms
import
(
ForumForm
,
ForumGroupPermissionsForm
,
ForumOrganizationPermissionsForm
,
PostForm
,
PostFormHidden
)
PostForm
)
from
scipost.mixins
import
PermissionsMixin
from
scipost.mixins
import
PermissionsMixin
...
@@ -132,6 +133,14 @@ class ForumListView(LoginRequiredMixin, ListView):
...
@@ -132,6 +133,14 @@ class ForumListView(LoginRequiredMixin, ListView):
class
PostCreateView
(
UserPassesTestMixin
,
CreateView
):
class
PostCreateView
(
UserPassesTestMixin
,
CreateView
):
"""
First step of a two-step Post creation process.
This view, upon successful POST, redirects to the
PostConfirmCreateView confirmation view.
To transfer form data from this view to the next (confirmation) one,
two session variables are used, ``post_subject`` and ``post_text``.
"""
model
=
Post
model
=
Post
form_class
=
PostForm
form_class
=
PostForm
...
@@ -178,36 +187,38 @@ class PostCreateView(UserPassesTestMixin, CreateView):
...
@@ -178,36 +187,38 @@ class PostCreateView(UserPassesTestMixin, CreateView):
'
parent_id
'
:
self
.
kwargs
.
get
(
'
parent_id
'
)}))
'
parent_id
'
:
self
.
kwargs
.
get
(
'
parent_id
'
)}))
class
PostConfirmCreateView
(
UserPassesTestMixin
,
CreateView
):
class
PostConfirmCreateView
(
PostCreateView
):
form_class
=
PostFormHidden
"""
Second (confirmation) step of Post creation process.
Upon successful POST, the Post object is saved and the
two session variables ``post_subject`` and ``post_text`` are deleted.
"""
form_class
=
PostForm
template_name
=
'
forums/post_confirm_create.html
'
template_name
=
'
forums/post_confirm_create.html
'
def
test_func
(
self
):
def
get_form
(
self
,
form_class
=
None
):
if
self
.
request
.
user
.
has_perm
(
'
forums.add_forum
'
):
form
=
super
().
get_form
(
form_class
)
return
True
form
.
fields
[
'
subject
'
].
widget
=
forms
.
HiddenInput
()
forum
=
get_object_or_404
(
Forum
,
slug
=
self
.
kwargs
.
get
(
'
slug
'
))
form
.
fields
[
'
text
'
].
widget
=
forms
.
HiddenInput
()
if
self
.
request
.
user
.
has_perm
(
'
can_post_to_forum
'
,
forum
):
return
form
return
True
else
:
raise
PermissionDenied
def
get_initial
(
self
,
*
args
,
**
kwargs
):
def
get_initial
(
self
,
*
args
,
**
kwargs
):
initial
=
super
().
get_initial
(
*
args
,
**
kwargs
)
initial
=
super
().
get_initial
(
*
args
,
**
kwargs
)
parent_model
=
self
.
kwargs
.
get
(
'
parent_model
'
)
parent_object_id
=
self
.
kwargs
.
get
(
'
parent_id
'
)
if
parent_model
==
'
forum
'
:
parent_content_type
=
ContentType
.
objects
.
get
(
app_label
=
'
forums
'
,
model
=
'
forum
'
)
elif
parent_model
==
'
post
'
:
parent_content_type
=
ContentType
.
objects
.
get
(
app_label
=
'
forums
'
,
model
=
'
post
'
)
parent
=
parent_content_type
.
get_object_for_this_type
(
pk
=
parent_object_id
)
else
:
raise
Http404
initial
.
update
({
initial
.
update
({
'
posted_by
'
:
self
.
request
.
user
,
'
subject
'
:
self
.
request
.
session
.
get
(
'
post_subject
'
),
'
posted_on
'
:
timezone
.
now
(),
'
text
'
:
self
.
request
.
session
.
get
(
'
post_text
'
),
'
parent_content_type
'
:
parent_content_type
,
'
parent_object_id
'
:
parent_object_id
,
'
subject
'
:
self
.
request
.
session
[
'
post_subject
'
],
'
text
'
:
self
.
request
.
session
[
'
post_text
'
],
})
})
return
initial
return
initial
def
form_valid
(
self
,
form
):
"""
After deleting the session variables used for the confirmation step,
simply perform the form_valid calls of form_valid from ancestor classes
ModelFormMixin and FormMixin, due to the fact that the form_valid
method in the PostCreateView superclass was overriden to a redirect.
"""
del
self
.
request
.
session
[
'
post_subject
'
]
del
self
.
request
.
session
[
'
post_text
'
]
self
.
object
=
form
.
save
()
return
redirect
(
self
.
get_success_url
())
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
register
or
sign in
to comment