diff --git a/boards/models/base.py b/boards/models/base.py new file mode 100644 --- /dev/null +++ b/boards/models/base.py @@ -0,0 +1,10 @@ +__author__ = 'neko259' + + +class Viewable(): + def __init__(self): + pass + + def get_view(self, *args, **kwargs): + """Get an HTML view for a model""" + pass \ No newline at end of file diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -10,10 +10,12 @@ import hashlib from django.core.cache import cache from django.core.urlresolvers import reverse from django.db import models, transaction +from django.template.loader import render_to_string from django.utils import timezone from markupfield.fields import MarkupField +from boards.models.base import Viewable + from boards.models.thread import Thread - from neboard import settings from boards import thumbs @@ -193,7 +195,7 @@ class PostManager(models.Manager): return ppd -class Post(models.Model): +class Post(models.Model, Viewable): """A post is a message.""" objects = PostManager() @@ -357,4 +359,38 @@ class Post(models.Model): return self.thread_new def get_referenced_posts(self): - return self.referenced_posts.only('id', 'thread_new') \ No newline at end of file + return self.referenced_posts.only('id', 'thread_new') + + def get_text(self): + return self.text + + def get_view(self, moderator=False, need_open_link=False, + truncated=False, *args, **kwargs): + if 'is_opening' in kwargs: + is_opening = kwargs['is_opening'] + else: + is_opening = self.is_opening() + + if 'thread' in kwargs: + thread = kwargs['thread'] + else: + thread = self.get_thread() + + if 'can_bump' in kwargs: + can_bump = kwargs['can_bump'] + else: + can_bump = thread.can_bump() + + opening_post_id = thread.get_opening_post_id() + + return render_to_string('boards/post.html', { + 'post': self, + 'moderator': moderator, + 'is_opening': is_opening, + 'thread': thread, + 'bumpable': can_bump, + 'need_open_link': need_open_link, + 'truncated': truncated, + 'opening_post_id': opening_post_id, + }) + diff --git a/boards/models/tag.py b/boards/models/tag.py --- a/boards/models/tag.py +++ b/boards/models/tag.py @@ -1,6 +1,9 @@ +from django.template.loader import render_to_string from boards.models import Thread, Post from django.db import models from django.db.models import Count, Sum +from django.core.urlresolvers import reverse +from boards.models.base import Viewable __author__ = 'neko259' @@ -26,7 +29,7 @@ class TagManager(models.Manager): return tags -class Tag(models.Model): +class Tag(models.Model, Viewable): """ A tag is a text node assigned to the thread. The tag serves as a board section. There can be multiple tags for each thread @@ -126,3 +129,11 @@ class Tag(models.Model): posts_count = 0 return posts_count + + def get_url(self): + return reverse('tag', kwargs={'tag_name': self.name}) + + def get_view(self, *args, **kwargs): + return render_to_string('boards/tag.html', { + 'tag': self, + }) \ No newline at end of file diff --git a/boards/search_indexes.py b/boards/search_indexes.py --- a/boards/search_indexes.py +++ b/boards/search_indexes.py @@ -1,5 +1,5 @@ from haystack import indexes -from boards.models import Post +from boards.models import Post, Tag __author__ = 'neko259' @@ -11,4 +11,14 @@ class PostIndex(indexes.SearchIndex, ind return Post def index_queryset(self, using=None): - return self.get_model().objects.all() \ No newline at end of file + return self.get_model().objects.all() + + +class TagIndex(indexes.SearchIndex, indexes.Indexable): + text = indexes.CharField(document=True, use_template=True) + + def get_model(self): + return Tag + + def index_queryset(self, using=None): + return self.get_model().objects.get_not_empty_tags() diff --git a/boards/templates/boards/post.html b/boards/templates/boards/tag.html copy from boards/templates/boards/post.html copy to boards/templates/boards/tag.html --- a/boards/templates/boards/post.html +++ b/boards/templates/boards/tag.html @@ -1,96 +1,3 @@ -{% load i18n %} -{% load board %} -{% load cache %} - -{% get_current_language as LANGUAGE_CODE %} - -{% spaceless %} - {% cache 600 post post.id post.last_edit_time thread.archived bumpable truncated moderator LANGUAGE_CODE need_open_link %} - {% if thread.archived %} -
- {% elif bumpable %} -
- {% else %} -
- {% endif %} - - - {% if post.image %} -
- {{ post.id }} - -
- {% endif %} -
- {% autoescape off %} - {% if truncated %} - {{ post.text.rendered|truncatewords_html:50 }} - {% else %} - {{ post.text.rendered }} - {% endif %} - {% endautoescape %} - {% if post.is_referenced %} -
- {% autoescape off %} - {% trans "Replies" %}: {{ post.refmap }} - {% endautoescape %} -
- {% endif %} -
- {% endcache %} - {% if is_opening %} - {% cache 600 post_thread thread.id thread.last_edit_time LANGUAGE_CODE need_open_link %} - - {% endcache %} - {% endif %} -
-{% endspaceless %} +
+ #{{ tag.name }} +
\ No newline at end of file diff --git a/boards/templatetags/board.py b/boards/templatetags/board.py --- a/boards/templatetags/board.py +++ b/boards/templatetags/board.py @@ -1,8 +1,7 @@ from django.shortcuts import get_object_or_404 -from boards.models import Post -from boards.views import thread, api from django import template + register = template.Library() actions = [ @@ -21,7 +20,7 @@ actions = [ def post_url(*args, **kwargs): post_id = args[0] - post = get_object_or_404(Post, id=post_id) + post = get_object_or_404('Post', id=post_id) return post.get_url() @@ -53,6 +52,7 @@ def image_actions(*args, **kwargs): return result +# TODO Use get_view of a post instead of this @register.inclusion_tag('boards/post.html', name='post_view') def post_view(post, moderator=False, need_open_link=False, truncated=False, **kwargs): diff --git a/requirements.txt b/requirements.txt --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +haystack pillow django>=1.6 django_cleanup diff --git a/templates/search/indexes/boards/post_text.txt b/templates/search/indexes/boards/tag_text.txt copy from templates/search/indexes/boards/post_text.txt copy to templates/search/indexes/boards/tag_text.txt --- a/templates/search/indexes/boards/post_text.txt +++ b/templates/search/indexes/boards/tag_text.txt @@ -1,2 +1,1 @@ -{{ object.title }} -{{ object.text }} \ No newline at end of file +{{ object.name }} \ No newline at end of file diff --git a/templates/search/search.html b/templates/search/search.html --- a/templates/search/search.html +++ b/templates/search/search.html @@ -14,9 +14,9 @@ {% if query %} {% for result in page.object_list %} - {% post_view result.object %} + {{ result.object.get_view }} {% empty %} -

{% trans 'No results found.' %}

+
{% trans 'No results found.' %}
{% endfor %} {% if page.has_previous or page.has_next %}