Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
AE
Sith
Commits
06b67f1d
Commit
06b67f1d
authored
May 30, 2017
by
Skia
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Still reducing the number of queries on the Forum
Signed-off-by:
Skia
<
skia@libskia.so
>
parent
ba65dc5d
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
26 additions
and
22 deletions
+26
-22
forum/migrations/0004_auto_20170530_2058.py
forum/migrations/0004_auto_20170530_2058.py
+12
-7
forum/models.py
forum/models.py
+6
-9
forum/templates/forum/forum.jinja
forum/templates/forum/forum.jinja
+1
-1
forum/templates/forum/macros.jinja
forum/templates/forum/macros.jinja
+1
-1
forum/views.py
forum/views.py
+5
-3
migrate.py
migrate.py
+1
-1
No files found.
forum/migrations/0004_auto_201705
2
0_
1235
.py
→
forum/migrations/0004_auto_201705
3
0_
2058
.py
View file @
06b67f1d
...
...
@@ -22,36 +22,41 @@ class Migration(migrations.Migration):
migrations
.
AddField
(
model_name
=
'forum'
,
name
=
'_last_message'
,
field
=
models
.
ForeignKey
(
related_name
=
'forums_where_its_last'
,
to
=
'forum.ForumMessage'
,
null
=
True
,
verbose_name
=
'the last message'
),
field
=
models
.
ForeignKey
(
related_name
=
'forums_where_its_last'
,
null
=
True
,
to
=
'forum.ForumMessage'
,
verbose_name
=
'the last message'
),
),
migrations
.
AddField
(
model_name
=
'forum'
,
name
=
'_topic_number'
,
field
=
models
.
IntegerField
(
default
=
0
,
verbose_name
=
'number of topics'
),
field
=
models
.
IntegerField
(
verbose_name
=
'number of topics'
,
default
=
0
),
),
migrations
.
AddField
(
model_name
=
'forummessage'
,
name
=
'_deleted'
,
field
=
models
.
BooleanField
(
default
=
False
,
verbose_name
=
'is deleted'
),
field
=
models
.
BooleanField
(
verbose_name
=
'is deleted'
,
default
=
False
),
),
migrations
.
AddField
(
model_name
=
'forumtopic'
,
name
=
'_last_message'
,
field
=
models
.
ForeignKey
(
related_name
=
'+'
,
to
=
'forum.ForumMessage'
,
null
=
True
,
verbose_name
=
'the last message'
),
field
=
models
.
ForeignKey
(
related_name
=
'+'
,
null
=
True
,
to
=
'forum.ForumMessage'
,
verbose_name
=
'the last message'
),
),
migrations
.
AddField
(
model_name
=
'forumtopic'
,
name
=
'_message_number'
,
field
=
models
.
IntegerField
(
verbose_name
=
'number of messages'
,
default
=
0
),
),
migrations
.
AddField
(
model_name
=
'forumtopic'
,
name
=
'_title'
,
field
=
models
.
CharField
(
max_length
=
64
,
blank
=
True
,
verbose_name
=
'title'
),
field
=
models
.
CharField
(
max_length
=
64
,
verbose_name
=
'title'
,
blank
=
True
),
),
migrations
.
AlterField
(
model_name
=
'forum'
,
name
=
'description'
,
field
=
models
.
CharField
(
max_length
=
512
,
default
=
''
,
verbose_name
=
'description'
),
field
=
models
.
CharField
(
max_length
=
512
,
verbose_name
=
'description'
,
default
=
''
),
),
migrations
.
AlterField
(
model_name
=
'forum'
,
name
=
'id'
,
field
=
models
.
AutoField
(
primary_key
=
True
,
serialize
=
False
,
db_index
=
True
),
field
=
models
.
AutoField
(
serialize
=
False
,
db_index
=
True
,
primary_key
=
True
),
),
]
forum/models.py
View file @
06b67f1d
...
...
@@ -183,6 +183,7 @@ class ForumTopic(models.Model):
description
=
models
.
CharField
(
_
(
'description'
),
max_length
=
256
,
default
=
""
)
_last_message
=
models
.
ForeignKey
(
'ForumMessage'
,
related_name
=
"+"
,
verbose_name
=
_
(
"the last message"
),
null
=
True
)
_title
=
models
.
CharField
(
_
(
'title'
),
max_length
=
64
,
blank
=
True
)
_message_number
=
models
.
IntegerField
(
_
(
"number of messages"
),
default
=
0
)
class
Meta
:
ordering
=
[
'-_last_message__date'
]
...
...
@@ -220,12 +221,7 @@ class ForumTopic(models.Model):
@
cached_property
def
title
(
self
):
if
self
.
_title
:
return
self
.
_title
else
:
self
.
_title
=
self
.
messages
.
order_by
(
'id'
).
first
().
title
self
.
save
()
return
self
.
_title
return
self
.
_title
class
ForumMessage
(
models
.
Model
):
"""
...
...
@@ -250,10 +246,10 @@ class ForumMessage(models.Model):
super
(
ForumMessage
,
self
).
save
(
*
args
,
**
kwargs
)
if
self
.
is_last_in_topic
():
self
.
topic
.
_last_message_id
=
self
.
id
self
.
topic
.
save
()
if
self
.
is_first_in_topic
():
self
.
topic
.
_title
=
self
.
title
self
.
topic
.
save
()
self
.
topic
.
_message_number
=
self
.
topic
.
messages
.
count
()
self
.
topic
.
save
()
def
is_first_in_topic
(
self
):
return
bool
(
self
.
id
==
self
.
topic
.
messages
.
order_by
(
'date'
).
first
().
id
)
...
...
@@ -285,7 +281,8 @@ class ForumMessage(models.Model):
def
mark_as_read
(
self
,
user
):
try
:
# Need the try/except because of AnonymousUser
self
.
readers
.
add
(
user
)
if
not
self
.
is_read
(
user
):
self
.
readers
.
add
(
user
)
except
:
pass
def
is_read
(
self
,
user
):
...
...
forum/templates/forum/forum.jinja
View file @
06b67f1d
...
...
@@ -36,7 +36,7 @@
</div>
</div>
{{
display_forum
(
forum
,
user
,
True
)
}}
{%
for
f
in
forum.children.all
()
%}
{%
for
f
in
forum.children.all
()
.
select_related
(
"_last_message__author"
,
"_last_message__topic"
)
%}
{{
display_forum
(
f
,
user
)
}}
{%
endfor
%}
{%
endif
%}
...
...
forum/templates/forum/macros.jinja
View file @
06b67f1d
...
...
@@ -69,7 +69,7 @@
{{
user_profile_link
(
topic.author
)
}}
</p>
<p
class=
"ib w_medium"
style=
"text-align: center;"
>
{{
topic.message
s.count
()
}}
{{
topic.
_
message
_number
}}
</p>
</div>
<p
class=
"ib w_medium"
style=
"text-align: center;"
>
...
...
forum/views.py
View file @
06b67f1d
...
...
@@ -41,7 +41,7 @@ from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMi
from
forum.models
import
Forum
,
ForumMessage
,
ForumTopic
,
ForumMessageMeta
class
ForumMainView
(
ListView
):
queryset
=
Forum
.
objects
.
filter
(
parent
=
None
).
select
_related
(
"_last_message__author"
,
"_last_message__topic
___title
"
)
queryset
=
Forum
.
objects
.
filter
(
parent
=
None
).
prefetch
_related
(
"
children__
_last_message__author"
,
"
children__
_last_message__topic"
)
template_name
=
"forum/main.jinja"
class
ForumMarkAllAsRead
(
RedirectView
):
...
...
@@ -122,7 +122,8 @@ class ForumDetailView(CanViewMixin, DetailView):
def
get_context_data
(
self
,
**
kwargs
):
kwargs
=
super
(
ForumDetailView
,
self
).
get_context_data
(
**
kwargs
)
qs
=
self
.
object
.
topics
.
order_by
(
'-_last_message__date'
).
select_related
(
'_last_message'
)
qs
=
self
.
object
.
topics
.
order_by
(
'-_last_message__date'
).
select_related
(
'_last_message__author'
,
'author'
)
\
.
prefetch_related
(
"forum__edit_groups"
)
paginator
=
Paginator
(
qs
,
settings
.
SITH_FORUM_PAGE_LENGTH
)
page
=
self
.
request
.
GET
.
get
(
'topic_page'
)
...
...
@@ -178,7 +179,8 @@ class ForumTopicDetailView(CanViewMixin, DetailView):
kwargs
[
'first_unread_message_id'
]
=
msg
.
id
except
:
kwargs
[
'first_unread_message_id'
]
=
float
(
"inf"
)
paginator
=
Paginator
(
self
.
object
.
messages
.
select_related
(
'author__avatar_pict'
).
order_by
(
'date'
),
paginator
=
Paginator
(
self
.
object
.
messages
.
select_related
(
'author__avatar_pict'
)
\
.
prefetch_related
(
'topic__forum__edit_groups'
,
'readers'
).
order_by
(
'date'
),
settings
.
SITH_FORUM_PAGE_LENGTH
)
page
=
self
.
request
.
GET
.
get
(
'page'
)
try
:
...
...
migrate.py
View file @
06b67f1d
...
...
@@ -1245,7 +1245,7 @@ def migrate_forum():
id
=
r
[
'id_sujet'
],
author
=
author
or
root
,
forum
=
parent
or
saloon
,
_title
=
to_unicode
(
r
[
'titre_sujet'
]),
_title
=
to_unicode
(
r
[
'titre_sujet'
])
[:
64
]
,
description
=
to_unicode
(
r
[
'soustitre_sujet'
]),
)
topic
.
save
()
...
...
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