diff options
author | Adolfo Fitoria <fitoria@fitoria-laptop.(none)> | 2009-08-08 21:33:09 -0600 |
---|---|---|
committer | Adolfo Fitoria <fitoria@fitoria-laptop.(none)> | 2009-08-08 21:33:09 -0600 |
commit | 0a2a07e2ff1aac4d1ffaa45a8f587264a99f21a6 (patch) | |
tree | c2dbeaf9ee8d663379324935cbab11e5c48dd517 | |
parent | e9f58c4b384e621aadc1f44e891147825bac2bd7 (diff) | |
download | askbot-0a2a07e2ff1aac4d1ffaa45a8f587264a99f21a6.tar.gz askbot-0a2a07e2ff1aac4d1ffaa45a8f587264a99f21a6.tar.bz2 askbot-0a2a07e2ff1aac4d1ffaa45a8f587264a99f21a6.zip |
adding category to questions
-rw-r--r-- | forum/admin.py | 4 | ||||
-rw-r--r-- | forum/managers.py | 4 | ||||
-rw-r--r-- | forum/models.py | 9 | ||||
-rw-r--r-- | forum/views.py | 38 | ||||
-rw-r--r-- | templates/categories.html | 56 | ||||
-rw-r--r-- | urls.py | 2 |
6 files changed, 112 insertions, 1 deletions
diff --git a/forum/admin.py b/forum/admin.py index 482da048..c326a034 100644 --- a/forum/admin.py +++ b/forum/admin.py @@ -13,6 +13,9 @@ class QuestionAdmin(admin.ModelAdmin): class TagAdmin(admin.ModelAdmin): """Tag admin class""" +class CategoryAdmin(admin.ModelAdmin): + """Category admin class""" + class Answerdmin(admin.ModelAdmin): """Answer admin class""" @@ -58,6 +61,7 @@ class BookAuthorRssAdmin(admin.ModelAdmin): admin.site.register(Question, QuestionAdmin) admin.site.register(Tag, TagAdmin) +admin.site.register(Category, CategoryAdmin) admin.site.register(Answer, Answerdmin) admin.site.register(Comment, CommentAdmin) admin.site.register(Vote, VoteAdmin) diff --git a/forum/managers.py b/forum/managers.py index 2e3e4186..314f022f 100644 --- a/forum/managers.py +++ b/forum/managers.py @@ -18,6 +18,10 @@ class QuestionManager(models.Manager): def get_questions_by_tag(self, tagname, orderby): questions = self.filter(deleted=False, tags__name = unquote(tagname)).order_by(orderby) return questions + + def get_questions_by_category(self, categoryname, orderby): + questions = self.filter(category__name = categoryname).order_by(orderby) + return questions def get_unanswered_questions(self, orderby): questions = self.filter(deleted=False, answer_count=0).order_by(orderby) diff --git a/forum/models.py b/forum/models.py index b607bec3..7f51d2d2 100644 --- a/forum/models.py +++ b/forum/models.py @@ -17,6 +17,14 @@ import settings from forum.managers import *
from const import *
+class Category(models.Model):
+ name = models.CharField(max_length=50, unique = True)
+
+ def __unicode__(self):
+ return self.name
+
+ #TODO: url
+
class EmailFeed(models.Model):
#subscription key for unsubscribe by visiting emailed link
key = models.CharField(max_length=32)
@@ -122,6 +130,7 @@ class Question(models.Model): author = models.ForeignKey(User, related_name='questions')
added_at = models.DateTimeField(default=datetime.datetime.now)
tags = models.ManyToManyField(Tag, related_name='questions')
+ category = models.ForeignKey(Category, related_name='questions')
# Status
wiki = models.BooleanField(default=False)
wikified_at = models.DateTimeField(null=True, blank=True)
diff --git a/forum/views.py b/forum/views.py index 6ef7bd6d..ab5b90f6 100644 --- a/forum/views.py +++ b/forum/views.py @@ -105,7 +105,7 @@ def privacy(request): def unanswered(request): return questions(request, unanswered=True) -def questions(request, tagname=None, unanswered=False): +def questions(request, tagname=None, categoryname=None, unanswered=False): """ List of Questions, Tagged questions, and Unanswered questions. """ @@ -136,6 +136,8 @@ def questions(request, tagname=None, unanswered=False): #check if request is from unanswered questions template_file = "unanswered.html" objects = Question.objects.get_unanswered_questions(orderby) + elif categoryname: + objects = Question.objects.get_questions_by_category(categoryname, orderby) else: objects = Question.objects.get_questions(orderby) @@ -741,6 +743,40 @@ def tags(request): def tag(request, tag): return questions(request, tagname=tag) +def categories(request): + is_paginated = True + sortby = request.GET.get('sort', 'used') + try: + page = int(request.GET.get('page', '1')) + except ValueError: + page = 1 + + if request.method == "GET": + objects_list = Paginator(Category.objects.all(), DEFAULT_PAGE_SIZE) + + try: + categories = objects_list.page(page) + except (EmptyPage, InvalidPage): + categories = objects_list.page(objects_list.num_pages) + + return render_to_response('categories.html', { + "categories" : categories, + "context" : { + 'is_paginated' : is_paginated, + 'pages': objects_list.num_pages, + 'page': page, + 'has_previous': categories.has_previous(), + 'has_next': categories.has_next(), + 'previous': categories.previous_page_number(), + 'next': categories.next_page_number(), + 'base_url': '/categorias/' + } + + }, context_instance=RequestContext(request)) + +def category(request, category): + return questions(request, categoryname=category) + def vote(request, id): """ vote_type: diff --git a/templates/categories.html b/templates/categories.html new file mode 100644 index 00000000..c617f22d --- /dev/null +++ b/templates/categories.html @@ -0,0 +1,56 @@ +{% extends "base_content.html" %} +<!-- tags.html --> +{% load i18n %} +{% load extra_tags %} +{% load humanize %} +{% block title %}{% spaceless %}{% trans "Category list" %}{% endspaceless %}{% endblock %} +{% block forejs %} + <script type="text/javascript"> + $().ready(function(){ + $("#nav_tags").attr('className',"on"); + $("#ipSearchTag").focus(); + + var orderby = "{{ tab_id }}"; + if(orderby != "used" && orderby != "name") + orderby = "used"; + $("#sort_" + orderby).attr('className',"on"); + $("#type-tag").attr('checked',true); + + Hilite.exact = false; + Hilite.elementid = "searchtags"; + Hilite.debug_referrer = location.href; + }); + + </script> +{% endblock %} +{% block content %} +<!-- Tabs --> +<div class="tabBar"> + <div class="headQuestions">{% trans "Category list" %}</div> +</div> +<div id="searchtags"> +<p> +{% if not categories.object_list %} + <span>{% trans "Nothing found" %}</span> +{% endif %} +</p> +<ul class="tagsList tags"> +{% for category in categories.object_list %} + <li> + <a href="{% url forum.views.category category|urlencode %}" title="{% trans "see questions that matches" %}'{{ category }}'{% trans "category " %}" rel="tag"> + {{ category }} + </a> + <br> + + </li> +{% endfor %} +</ul> +</div> + +{% endblock %} +{% block tail %} +<div class="pager"> + {% cnprog_paginator context %} +</div> +{% endblock %} +<!-- end tags.html --> @@ -46,6 +46,8 @@ urlpatterns = patterns('', url(r'^preguntas/(?P<id>\d+)//*', app.question, name='question'), (r'^etiquetas/$', app.tags), (r'^etiquetas/(?P<tag>[^/]+)/$', app.tag), + (r'^categorias/$', app.categories), + (r'^categorias/(?P<category>[^/]+)/$', app.category), (r'^usuarios/$',app.users), url(r'^usuarios/(?P<id>\d+)/editar/$', app.edit_user, name='edit_user'), url(r'^usuarios/(?P<id>\d+)//*', app.user, name='user'), |