]> git.openstreetmap.org Git - osqa.git/blobdiff - forum_modules/books/views.py
Several improvements in the sx importer.
[osqa.git] / forum_modules / books / views.py
old mode 100755 (executable)
new mode 100644 (file)
index 31c8297..35e9f0f
-from django.shortcuts import render_to_response, get_object_or_404\r
-from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden, Http404\r
-from django.template import RequestContext\r
-from django.contrib.auth.decorators import login_required\r
-from django.core.urlresolvers import reverse\r
-from django.utils.html import *\r
-\r
-from models import *\r
-\r
-from forum.forms import AskForm\r
-from forum.views.readers import _get_tags_cache_json\r
-from forum.models import *\r
-from forum.utils.html import sanitize_html\r
-\r
-def books(request):\r
-    return HttpResponseRedirect(reverse('books') + '/mysql-zhaoyang')\r
-\r
-def book(request, short_name, unanswered=False):\r
-    """\r
-    1. questions list\r
-    2. book info\r
-    3. author info and blog rss items\r
-    """\r
-    """\r
-    List of Questions, Tagged questions, and Unanswered questions.\r
-    """\r
-    books = Book.objects.extra(where=['short_name = %s'], params=[short_name])\r
-    match_count = len(books)\r
-    if match_count == 0:\r
-        raise Http404\r
-    else:\r
-        # the book info\r
-        book = books[0]\r
-        # get author info\r
-        author_info = BookAuthorInfo.objects.get(book=book)\r
-        # get author rss info\r
-        author_rss = BookAuthorRss.objects.filter(book=book)\r
-\r
-        # get pagesize from session, if failed then get default value\r
-        user_page_size = request.session.get("pagesize", QUESTIONS_PAGE_SIZE)\r
-        # set pagesize equal to logon user specified value in database\r
-        if request.user.is_authenticated() and request.user.questions_per_page > 0:\r
-            user_page_size = request.user.questions_per_page\r
-\r
-        try:\r
-            page = int(request.GET.get('page', '1'))\r
-        except ValueError:\r
-            page = 1\r
-\r
-        view_id = request.GET.get('sort', None)\r
-        view_dic = {"latest":"-added_at", "active":"-last_activity_at", "hottest":"-answer_count", "mostvoted":"-score" }\r
-        try:\r
-            orderby = view_dic[view_id]\r
-        except KeyError:\r
-            view_id = "latest"\r
-            orderby = "-added_at"\r
-\r
-        # check if request is from tagged questions\r
-        if unanswered:\r
-            # check if request is from unanswered questions\r
-            # Article.objects.filter(publications__id__exact=1)\r
-            objects = Question.objects.filter(book__id__exact=book.id, deleted=False, answer_count=0).order_by(orderby)\r
-        else:\r
-            objects = Question.objects.filter(book__id__exact=book.id, deleted=False).order_by(orderby)\r
-\r
-        # RISK - inner join queries\r
-        objects = objects.select_related();\r
-        objects_list = Paginator(objects, user_page_size)\r
-        questions = objects_list.page(page)\r
-\r
-        return render_to_response('book.html', {\r
-            "book" : book,\r
-            "author_info" : author_info,\r
-            "author_rss" : author_rss,\r
-            "questions" : questions,\r
-            "context" : {\r
-                'is_paginated' : True,\r
-                'pages': objects_list.num_pages,\r
-                'page': page,\r
-                'has_previous': questions.has_previous(),\r
-                'has_next': questions.has_next(),\r
-                'previous': questions.previous_page_number(),\r
-                'next': questions.next_page_number(),\r
-                'base_url' : request.path + '?sort=%s&' % view_id,\r
-                'pagesize' : user_page_size\r
-            }\r
-        }, context_instance=RequestContext(request))\r
-\r
-@login_required\r
-def ask_book(request, short_name):\r
-    if request.method == "POST":\r
-        form = AskForm(request.POST)\r
-        if form.is_valid():\r
-            added_at = datetime.datetime.now()\r
-            html = sanitize_html(markdowner.convert(form.cleaned_data['text']))\r
-            question = Question(\r
-                title            = strip_tags(form.cleaned_data['title']),\r
-                author           = request.user,\r
-                added_at         = added_at,\r
-                last_activity_at = added_at,\r
-                last_activity_by = request.user,\r
-                wiki             = form.cleaned_data['wiki'],\r
-                tagnames         = form.cleaned_data['tags'].strip(),\r
-                html             = html,\r
-                summary          = strip_tags(html)[:120]\r
-            )\r
-            if question.wiki:\r
-                question.last_edited_by = question.author\r
-                question.last_edited_at = added_at\r
-                question.wikified_at = added_at\r
-\r
-            question.save()\r
-\r
-            # create the first revision\r
-            QuestionRevision.objects.create(\r
-                question   = question,\r
-                revision   = 1,\r
-                title      = question.title,\r
-                author     = request.user,\r
-                revised_at = added_at,\r
-                tagnames   = question.tagnames,\r
-                summary    = CONST['default_version'],\r
-                text       = form.cleaned_data['text']\r
-            )\r
-\r
-            books = Book.objects.extra(where=['short_name = %s'], params=[short_name])\r
-            match_count = len(books)\r
-            if match_count == 1:\r
-                # the book info\r
-                book = books[0]\r
-                book.questions.add(question)\r
-\r
-            return HttpResponseRedirect(question.get_absolute_url())\r
-    else:\r
-        form = AskForm()\r
-\r
-    tags = _get_tags_cache_json()\r
-    return render_to_response('ask.html', {\r
-        'form' : form,\r
-        'tags' : tags,\r
-        'email_validation_faq_url': reverse('faq') + '#validate',\r
+from django.shortcuts import render_to_response, get_object_or_404
+from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden, Http404
+from django.template import RequestContext
+from django.contrib.auth.decorators import login_required
+from django.core.urlresolvers import reverse
+from django.utils.html import *
+
+from models import *
+
+from forum.forms import AskForm
+from forum.views.readers import _get_tags_cache_json
+from forum.models import *
+from forum.utils.html import sanitize_html
+
+def books(request):
+    return HttpResponseRedirect(reverse('books') + '/mysql-zhaoyang')
+
+def book(request, short_name, unanswered=False):
+    """
+    1. questions list
+    2. book info
+    3. author info and blog rss items
+    """
+    """
+    List of Questions, Tagged questions, and Unanswered questions.
+    """
+    books = Book.objects.extra(where=['short_name = %s'], params=[short_name])
+    match_count = len(books)
+    if match_count == 0:
+        raise Http404
+    else:
+        # the book info
+        book = books[0]
+        # get author info
+        author_info = BookAuthorInfo.objects.get(book=book)
+        # get author rss info
+        author_rss = BookAuthorRss.objects.filter(book=book)
+
+        # get pagesize from session, if failed then get default value
+        user_page_size = request.session.get("pagesize", QUESTIONS_PAGE_SIZE)
+        # set pagesize equal to logon user specified value in database
+        if request.user.is_authenticated() and request.user.questions_per_page > 0:
+            user_page_size = request.user.questions_per_page
+
+        try:
+            page = int(request.GET.get('page', '1'))
+        except ValueError:
+            page = 1
+
+        view_id = request.GET.get('sort', None)
+        view_dic = {"latest":"-added_at", "active":"-last_activity_at", "hottest":"-answer_count", "mostvoted":"-score" }
+        try:
+            orderby = view_dic[view_id]
+        except KeyError:
+            view_id = "latest"
+            orderby = "-added_at"
+
+        # check if request is from tagged questions
+        if unanswered:
+            # check if request is from unanswered questions
+            # Article.objects.filter(publications__id__exact=1)
+            objects = Question.objects.filter(book__id__exact=book.id, deleted=False, answer_count=0).order_by(orderby)
+        else:
+            objects = Question.objects.filter(book__id__exact=book.id, deleted=False).order_by(orderby)
+
+        # RISK - inner join queries
+        objects = objects.select_related();
+        objects_list = Paginator(objects, user_page_size)
+        questions = objects_list.page(page)
+
+        return render_to_response('book.html', {
+            "book" : book,
+            "author_info" : author_info,
+            "author_rss" : author_rss,
+            "questions" : questions,
+            "context" : {
+                'is_paginated' : True,
+                'pages': objects_list.num_pages,
+                'page': page,
+                'has_previous': questions.has_previous(),
+                'has_next': questions.has_next(),
+                'previous': questions.previous_page_number(),
+                'next': questions.next_page_number(),
+                'base_url' : request.path + '?sort=%s&' % view_id,
+                'pagesize' : user_page_size
+            }
+        }, context_instance=RequestContext(request))
+
+@login_required
+def ask_book(request, short_name):
+    if request.method == "POST":
+        form = AskForm(request.POST)
+        if form.is_valid():
+            added_at = datetime.datetime.now()
+            html = sanitize_html(markdowner.convert(form.cleaned_data['text']))
+            question = Question(
+                title            = strip_tags(form.cleaned_data['title']),
+                author           = request.user,
+                added_at         = added_at,
+                last_activity_at = added_at,
+                last_activity_by = request.user,
+                wiki             = form.cleaned_data['wiki'],
+                tagnames         = form.cleaned_data['tags'].strip(),
+                html             = html,
+                summary          = strip_tags(html)[:120]
+            )
+            if question.wiki:
+                question.last_edited_by = question.author
+                question.last_edited_at = added_at
+                question.wikified_at = added_at
+
+            question.save()
+
+            # create the first revision
+            QuestionRevision.objects.create(
+                question   = question,
+                revision   = 1,
+                title      = question.title,
+                author     = request.user,
+                revised_at = added_at,
+                tagnames   = question.tagnames,
+                summary    = CONST['default_version'],
+                text       = form.cleaned_data['text']
+            )
+
+            books = Book.objects.extra(where=['short_name = %s'], params=[short_name])
+            match_count = len(books)
+            if match_count == 1:
+                # the book info
+                book = books[0]
+                book.questions.add(question)
+
+            return HttpResponseRedirect(question.get_absolute_url())
+    else:
+        form = AskForm()
+
+    tags = _get_tags_cache_json()
+    return render_to_response('ask.html', {
+        'form' : form,
+        'tags' : tags,
+        'email_validation_faq_url': reverse('faq') + '#validate',
         }, context_instance=RequestContext(request))
\ No newline at end of file
         }, context_instance=RequestContext(request))
\ No newline at end of file