Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-wiki
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
django-wiki
Commits
7c6af7be
Commit
7c6af7be
authored
Aug 19, 2012
by
Bridger Maxwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added caching of ancestors for urlpath to reduce sql queries.
parent
df4fd062
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
7 deletions
+26
-7
wiki/models/urlpath.py
+25
-6
wiki/templates/wiki/includes/breadcrumbs.html
+1
-1
No files found.
wiki/models/urlpath.py
View file @
7c6af7be
...
@@ -15,6 +15,8 @@ from wiki.core.exceptions import NoRootURL, MultipleRootURLs
...
@@ -15,6 +15,8 @@ from wiki.core.exceptions import NoRootURL, MultipleRootURLs
from
wiki.models.article
import
ArticleRevision
,
ArticleForObject
,
Article
from
wiki.models.article
import
ArticleRevision
,
ArticleForObject
,
Article
from
django.contrib.contenttypes.models
import
ContentType
from
django.contrib.contenttypes.models
import
ContentType
URLPATH_PREFECTED_PROPERTIES
=
[
"parent"
,
"article__current_revision"
]
class
URLPath
(
MPTTModel
):
class
URLPath
(
MPTTModel
):
"""
"""
Strategy: Very few fields go here, as most has to be managed through an
Strategy: Very few fields go here, as most has to be managed through an
...
@@ -36,16 +38,29 @@ class URLPath(MPTTModel):
...
@@ -36,16 +38,29 @@ class URLPath(MPTTModel):
parent
=
TreeForeignKey
(
'self'
,
null
=
True
,
blank
=
True
,
related_name
=
'children'
)
parent
=
TreeForeignKey
(
'self'
,
null
=
True
,
blank
=
True
,
related_name
=
'children'
)
@property
@property
def
cached_ancestors
(
self
):
if
not
hasattr
(
self
,
"_cached_ancestors"
):
self
.
_cached_ancestors
=
list
(
self
.
get_ancestors
()
.
select_related
(
*
URLPATH_PREFECTED_PROPERTIES
)
)
return
self
.
_cached_ancestors
@cached_ancestors.setter
def
cached_ancestors
(
self
,
ancestors
):
self
.
_cached_ancestors
=
ancestors
@property
def
path
(
self
):
def
path
(
self
):
if
not
self
.
parent
:
return
""
if
not
self
.
parent
:
return
""
if
not
hasattr
(
self
,
'_cachedpath'
):
self
.
_cachedpath
=
"/"
.
join
([
obj
.
slug
if
obj
.
slug
else
""
for
obj
in
self
.
get_ancestors
(
include_self
=
True
)
.
exclude
(
parent
=
None
)])
+
"/"
ancestors
=
filter
(
lambda
ancestor
:
ancestor
.
parent
is
not
None
,
self
.
cached_ancestors
)
return
self
.
_cachedpath
slugs
=
[
obj
.
slug
if
obj
.
slug
else
""
for
obj
in
ancestors
+
[
self
]
]
return
"/"
.
join
(
slugs
)
+
"/"
@classmethod
@classmethod
def
root
(
cls
):
def
root
(
cls
):
site
=
Site
.
objects
.
get_current
()
site
=
Site
.
objects
.
get_current
()
root_nodes
=
list
(
cls
.
objects
.
root_nodes
()
.
filter
(
site
=
site
))
root_nodes
=
list
(
cls
.
objects
.
root_nodes
()
.
filter
(
site
=
site
)
.
select_related
(
*
URLPATH_PREFECTED_PROPERTIES
)
)
# We fetch the nodes as a list and use len(), not count() because we need
# We fetch the nodes as a list and use len(), not count() because we need
# to get the result out anyway. This only takes one sql query
# to get the result out anyway. This only takes one sql query
no_paths
=
len
(
root_nodes
)
no_paths
=
len
(
root_nodes
)
...
@@ -108,9 +123,13 @@ class URLPath(MPTTModel):
...
@@ -108,9 +123,13 @@ class URLPath(MPTTModel):
parent
=
cls
.
root
()
parent
=
cls
.
root
()
for
slug
in
slugs
:
for
slug
in
slugs
:
if
settings
.
URL_CASE_SENSITIVE
:
if
settings
.
URL_CASE_SENSITIVE
:
parent
=
parent
.
get_children
()
.
get
(
slug
=
slug
)
child
=
parent
.
get_children
()
.
select_related
(
*
URLPATH_PREFECTED_PROPERTIES
)
.
get
(
slug
=
slug
)
child
.
cached_ancestors
=
parent
.
cached_ancestors
+
[
parent
]
parent
=
child
else
:
else
:
parent
=
parent
.
get_children
()
.
get
(
slug__iexact
=
slug
)
child
=
parent
.
get_children
()
.
select_related
(
*
URLPATH_PREFECTED_PROPERTIES
)
.
get
(
slug__iexact
=
slug
)
child
.
cached_ancestors
=
parent
.
cached_ancestors
+
[
parent
]
parent
=
child
level
+=
1
level
+=
1
return
parent
return
parent
...
...
wiki/templates/wiki/includes/breadcrumbs.html
View file @
7c6af7be
{% load i18n %}{% load url from future %}
{% load i18n %}{% load url from future %}
{% if urlpath %}
{% if urlpath %}
<ul
class=
"breadcrumb pull-left"
class=
""
>
<ul
class=
"breadcrumb pull-left"
class=
""
>
{% for ancestor in urlpath.
get_ancestors.all
%}
{% for ancestor in urlpath.
cached_ancestors
%}
<span
class=
"divider"
>
/
</span>
<span
class=
"divider"
>
/
</span>
<li><a
href=
"{% url 'wiki:get' path=ancestor.path %}"
>
{{ ancestor.article.current_revision.title }}
</a></li>
<li><a
href=
"{% url 'wiki:get' path=ancestor.path %}"
>
{{ ancestor.article.current_revision.title }}
</a></li>
{% endfor %}
{% endfor %}
...
...
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