From 0d1d1ef3a3416b5bcd4eb867356484b28f0c7f05 Mon Sep 17 00:00:00 2001 From: hrcerqueira Date: Thu, 4 Mar 2010 21:40:08 +0000 Subject: Started implementing cache. Added placeholders in log folder, and the new cache folder so git wont ignore them. --- .gitignore | 1 + .idea/workspace.xml | 241 +++++++++++------------ cache/README.TXT | 1 + forum/models/question.py | 28 ++- forum/skins/default/templates/footer.html | 2 +- forum/skins/default/templates/index.html | 41 +--- forum/skins/default/templates/question.html | 18 +- forum/skins/default/templates/question_list.html | 42 ++++ forum/skins/default/templates/questions.html | 47 +---- forum/utils/lists.py | 4 + forum/views/readers.py | 1 + forum_modules/robotstxt/DISABLED | 0 log/README.TXT | 1 + settings_local.py.dist | 6 +- 14 files changed, 208 insertions(+), 225 deletions(-) create mode 100755 cache/README.TXT create mode 100755 forum/skins/default/templates/question_list.html create mode 100755 forum_modules/robotstxt/DISABLED create mode 100755 log/README.TXT diff --git a/.gitignore b/.gitignore index 70910647..49cba440 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.pyc *.swp *.log +cache/?? osqa.wsgi nbproject settings_local.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a17f2c4c..0fe5d429 100755 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -29,8 +29,8 @@ - + @@ -85,6 +85,7 @@ + @@ -176,14 +177,15 @@ - + + @@ -225,8 +227,8 @@ - + @@ -249,6 +251,7 @@ + @@ -312,7 +315,9 @@ + + @@ -379,46 +384,82 @@ - - + + - + - - + + - + - - + + - + - - + + - + - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -434,22 +475,22 @@ @@ -518,7 +559,7 @@ + + @@ -962,7 +949,7 @@ - + @@ -978,7 +965,7 @@ - + @@ -1018,114 +1005,114 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/cache/README.TXT b/cache/README.TXT new file mode 100755 index 00000000..54247a82 --- /dev/null +++ b/cache/README.TXT @@ -0,0 +1 @@ +this file is just a placeholder so the empty directory is not ignored by version control \ No newline at end of file diff --git a/forum/models/question.py b/forum/models/question.py index 1c2cf749..f351dff0 100755 --- a/forum/models/question.py +++ b/forum/models/question.py @@ -1,6 +1,8 @@ from base import * from tag import Tag +from forum.utils.lists import LazyList + class QuestionManager(models.Manager): def create_new(self, title=None,author=None,added_at=None, wiki=False,tagnames=None,summary=None, text=None): question = Question( @@ -98,17 +100,25 @@ class QuestionManager(models.Manager): Questions with the individual tags will be added to list if above questions are not full. """ #print datetime.datetime.now() - questions = list(self.filter(tagnames = question.tagnames, deleted=False).all()) - tags_list = question.tags.all() - for tag in tags_list: - extend_questions = self.filter(tags__id = tag.id, deleted=False)[:50] - for item in extend_questions: - if item not in questions and len(questions) < 10: - questions.append(item) + manager = self + + def get_data(): + questions = list(manager.filter(tagnames = question.tagnames, deleted=False).all()) + + tags_list = question.tags.all() + for tag in tags_list: + extend_questions = manager.filter(tags__id = tag.id, deleted=False)[:50] + for item in extend_questions: + if item not in questions and len(questions) < 10: + questions.append(item) + + #print datetime.datetime.now() + return questions + + return LazyList(get_data) + - #print datetime.datetime.now() - return questions class Question(Content, DeletableContent): title = models.CharField(max_length=300) diff --git a/forum/skins/default/templates/footer.html b/forum/skins/default/templates/footer.html index 2f95921d..66ea2bc5 100755 --- a/forum/skins/default/templates/footer.html +++ b/forum/skins/default/templates/footer.html @@ -21,7 +21,7 @@ {% endspaceless %}

- + powered by OSQA

diff --git a/forum/skins/default/templates/index.html b/forum/skins/default/templates/index.html index 42fa175d..eb4dc224 100755 --- a/forum/skins/default/templates/index.html +++ b/forum/skins/default/templates/index.html @@ -5,6 +5,7 @@ {% load humanize %} {% load extra_filters %} {% load smart_if %} +{% load cache %} {% block title %}{% spaceless %}{% trans "Home" %}{% endspaceless %}{% endblock %} {% block meta %} {% endblock %} @@ -31,41 +32,7 @@ -
- {% for question in questions.object_list %} -
-
-
-
{{question.score|intcomma}}
-
{% trans "votes" %}
-
-
-
{{question.answer_count|intcomma}}
-
{% trans "answers" %}
-
-
-
{{question.view_count|cnprog_intword|safe}}
-
{% trans "views" %}
-
-
- -

{{question.title}}

- -
- {% diff_date question.last_activity_at %} - {% if question.last_activity_by %} - {{ question.last_activity_by }} {% get_score_badge question.last_activity_by %} - {% endif %} -
- -
- {% for tag in question.tagname_list %} - - {% endfor %} -
-
- {% endfor %} -
+{% include "question_list.html" %} {% endblock %} {% block sidebar %} @@ -85,10 +52,12 @@

{% trans "Recent tags" %}

+ {% cache 60 recent_tags %} {% for tag in tags %} {% endfor %} + {% endcache %}
@@ -98,6 +67,7 @@

{% trans "Recent awards" %}

diff --git a/forum/skins/default/templates/question.html b/forum/skins/default/templates/question.html index 23c24113..3df778dc 100755 --- a/forum/skins/default/templates/question.html +++ b/forum/skins/default/templates/question.html @@ -5,6 +5,7 @@ {% load smart_if %} {% load humanize %} {% load i18n %} +{% load cache %} {% block title %}{% spaceless %}{{ question.get_question_title }}{% endspaceless %}{% endblock %} {% block forejs %} @@ -164,8 +165,8 @@ {% endjoinitems %}
- {% post_contributor_info question "original_author" %} - {% post_contributor_info question "last_updater" %} + {% post_contributor_info question "original_author" %} + {% post_contributor_info question "last_updater" %}
{% for comment in question.get_comments|slice:":5" %} @@ -313,8 +314,8 @@ {% endjoinitems %}
- {% post_contributor_info answer "original_author" %} - {% post_contributor_info answer "last_updater" %} + {% post_contributor_info answer "original_author" %} + {% post_contributor_info answer "last_updater" %}
{% for comment in answer.get_comments|slice:":5" %} @@ -468,6 +469,7 @@ {% endblock %} {% block sidebar %} +{% cache 600 questions_tags question.id %}

{% trans "Question tags" %}: @@ -489,17 +491,21 @@ {% trans "last updated" %}: {% diff_date question.last_activity_at %}

- +{% endcache %} +{% cache 1800 related_questions question.id %}

{% trans "Related questions" %}

+{% endcache %} {% endblock %} diff --git a/forum/skins/default/templates/question_list.html b/forum/skins/default/templates/question_list.html new file mode 100755 index 00000000..cf69d133 --- /dev/null +++ b/forum/skins/default/templates/question_list.html @@ -0,0 +1,42 @@ +{% load cache %} +{% load i18n %} +{% load humanize %} +{% load extra_filters %} +{% load extra_tags %} + +
+ {% for question in questions.object_list %} + {% cache 60 question_in_list question.id %} +
+
+
+
{{question.score|intcomma}}
+
{% trans "votes" %}
+
+
+
{{question.answer_count|intcomma}}
+
{% trans "answers" %}
+
+
+
{{question.view_count|cnprog_intword|safe}}
+
{% trans "views" %}
+
+
+ +

{{question.title}}

+
+ {% diff_date question.last_activity_at %} + {% if question.last_activity_by %} + {{ question.last_activity_by }} {% get_score_badge question.last_activity_by %} + {% endif %} +
+ +
+ {% for tag in question.tagname_list %} + + {% endfor %} +
+
+ {% endcache %} + {% endfor %} +
\ No newline at end of file diff --git a/forum/skins/default/templates/questions.html b/forum/skins/default/templates/questions.html index 58ad4146..de6076ac 100755 --- a/forum/skins/default/templates/questions.html +++ b/forum/skins/default/templates/questions.html @@ -50,52 +50,7 @@ {% trans "most voted" %}
-
- {% for question in questions.object_list %} -
-
-
-
{{question.score|intcomma}}
-
{% trans "votes" %}
-
-
-
{{question.answer_count|intcomma}}
-
{% trans "answers" %}
-
-
-
{{question.view_count|cnprog_intword|safe}}
-
{% trans "views" %}
-
-
- -

{{question.title}}

- -
- {% diff_date question.last_activity_at %} - {% if question.last_activity_by %} - {{ question.last_activity_by }} {% get_score_badge question.last_activity_by %} - {% endif %} -
- -
- {% for tag in question.tagname_list %} - - {% endfor %} -
-
- {% endfor %} - {% if searchtitle %} - {% if questions_count == 0 %} -

- {% trans "Did not find anything?" %} - {% else %} -

- {% trans "Did not find what you were looking for?" %} - {% endif %} - {% trans "Please, post your question!" %} -

- {% endif %} -
+{% include "question_list.html" %} {% endblock %} {% block tail %} diff --git a/forum/utils/lists.py b/forum/utils/lists.py index bbcfae98..f69c8f20 100755 --- a/forum/utils/lists.py +++ b/forum/utils/lists.py @@ -1,5 +1,9 @@ """Utilities for working with lists and sequences.""" +class LazyList(list): + def __init__(self, get_data): + self.data = get_data + def flatten(x): """ Returns a single, flat list which contains all elements retrieved diff --git a/forum/views/readers.py b/forum/views/readers.py index c4ce5ddf..938fa133 100644 --- a/forum/views/readers.py +++ b/forum/views/readers.py @@ -14,6 +14,7 @@ from django.utils.translation import ugettext as _ from django.template.defaultfilters import slugify from django.core.urlresolvers import reverse from django.utils.datastructures import SortedDict +from django.views.decorators.cache import cache_page from forum.utils.html import sanitize_html from markdown2 import Markdown diff --git a/forum_modules/robotstxt/DISABLED b/forum_modules/robotstxt/DISABLED new file mode 100755 index 00000000..e69de29b diff --git a/log/README.TXT b/log/README.TXT new file mode 100755 index 00000000..54247a82 --- /dev/null +++ b/log/README.TXT @@ -0,0 +1 @@ +this file is just a placeholder so the empty directory is not ignored by version control \ No newline at end of file diff --git a/settings_local.py.dist b/settings_local.py.dist index 3169e940..a27946af 100755 --- a/settings_local.py.dist +++ b/settings_local.py.dist @@ -36,7 +36,11 @@ DATABASE_ENGINE = '' #mysql, etc DATABASE_HOST = '' DATABASE_PORT = '' -#Moved from settings.py for better organization. (please check it up to clean up settings.py) +#set this value to 'dummy://' if you don't want to use cache, or set up your favourite caching mechanism +#see http://docs.djangoproject.com/en/1.1/topics/cache/ for details +#example (set local file system cache in a cache folder in the root of the osqa install): +#CACHE_BACKEND = 'file://%s' % os.path.join(os.path.dirname(__file__),'cache').replace('\\','/') +CACHE_BACKEND = 'dummy://' #email server settings SERVER_EMAIL = '' -- cgit v1.2.3-1-g7c22