Ejemplo n.º 1
0
def search(request, is_adv=None):
    # Remove action
    user_action = isUserAction(request)
    if user_action == True:
        action = request.session["Action"]
        try:
            del request.session["Action"]
        except:
            pass
    else:
        action = ""

    if is_adv:
        return render_to_response("advanced_search.html", context_instance=RequestContext(request))
    if request.method == "POST":
        searchform = search_form(request.POST)
        if searchform.is_valid():
            clean_data = searchform.cleaned_data

            if clean_data["category_choices"]:
                # Fend chosen category to template for display
                category_choices = "'" + str(clean_data["category_choices"]) + "'" + " category"
                # Retrieve records matching the search query for given category
                try:
                    result_set = Thread.objects.filter(
                        Q(topic=clean_data["category_choices"]),
                        (
                            Q(thread_name__contains=clean_data["search_text"])
                            | Q(thread_body__contains=clean_data["search_text"])
                        ),
                        Q(parent_thread=None),
                    )
                except:
                    pass
            else:
                category_choices = "all forums"
                try:
                    result_set = Thread.objects.filter(
                        Q(thread_name__contains=clean_data["search_text"])
                        | Q(thread_body__contains=clean_data["search_text"]),
                        Q(parent_thread=None),
                    )
                except:
                    pass

            result_set_count = len(list(result_set))
            user_status = is_userLogged(request)
            return render_to_response(
                "search.html",
                {
                    "threads": result_set,
                    "threads_count": result_set_count,
                    "search_text": clean_data["search_text"],
                    "category_choices": category_choices,
                    "search_form": searchform,
                    "user_isLogged": user_status,
                    "action": action,
                },
                context_instance=RequestContext(request),
            )
        return render_to_response("search.html", {"search_form": searchform})
    return HttpResponseRedirect("/forum/")
Ejemplo n.º 2
0
def displayTopic(request, topic_id=None, parent_id=None):
    is_locked = ""
    category_name = ""
    thread_name = ""
    if request.method == "POST":
        reply_form = new_thread_form(request.POST)
        if reply_form.is_valid():
            clean_data = reply_form.cleaned_data
            logged_user = request.session["User_id"]

            try:
                owner_user = User.objects.get(id=uuid.UUID(logged_user))
                parentThread = Thread.objects.get(id=parent_id)
                categ_topic = Topic.objects.get(id=topic_id)
            except:
                return HttpResponseRedirect("/")
            new_thread = Thread(
                thread_name=clean_data["thread_name"],
                thread_body=clean_data["thread_body"],
                datetime_created=datetime.datetime.now(),
                keyword_dict=clean_data["keywords"],
                topic=categ_topic,
                owner=owner_user,
                parent_thread=parentThread,
                most_popular=0,
            )

            try:
                new_thread.save()
                id_list = (topic_id, parent_id)
                redirect_url = r"/forum/?topic_id=%s&thread_id=%s/" % id_list
                return HttpResponseRedirect(redirect_url)
            except:
                pass

    try:

        thread_id = ""
        cat_id = ""
        display_forum = 0
        display_thread = 0
        threadsList = []
        topic_id = ""
        if request.GET.get("cat_id"):
            cat_id = str(request.GET.get("cat_id"))
            cat_id = cat_id.rstrip("/")
            categories = ""
        if request.GET.get("topic_id"):
            topic_id = str(request.GET.get("topic_id"))
            topic_id = topic_id.rstrip("/")
            try:
                category_content = Topic.objects.get(id=topic_id)
                category_name = category_content.topic_name
            except:
                pass  # return HttpResponseRedirect('/')
            categories = ""

        if request.GET.get("thread_id"):
            thread_id = str(request.GET.get("thread_id"))
            thread_id = thread_id.rstrip("/")
            try:
                parentThread = Thread.objects.get(id=thread_id)
                thread_name = parentThread.thread_name
            except:
                pass
            categories = ""
    except ValueError:
        pass
    # if (not thread_id or thread_id == '' or len(thread_id) == 0):
    #     thread_id = None
    #     threadsList = get_threads_by_id(thread_id)
    # else:
    #     threadsList = get_threads_by_id(thread_id)
    if not thread_id and not cat_id:
        categories = "TRUE"
        is_locked = ""
    if request.GET.get("cat_id"):
        display_forum = "TRUE"
        try:
            category_content = Topic.objects.get(id=cat_id)
        except:
            return HttpResponseRedirect("/")
        is_locked = category_content.is_locked
        category_name = category_content.topic_name
    if request.GET.get("thread_id"):
        parent_id = request.GET.get("thread_id")
        parent_id = parent_id.rstrip("/")
        try:
            parentThread = Thread.objects.get(id=parent_id)
            thread_name = parentThread.thread_name
        except:
            pass
        #    return HttpResponseRedirect('/')
        display_thread = "TRUE"
        is_locked = ""

    user_status = is_userLogged(request)
    if user_status:
        logged_user = request.session["User_id"]
    else:
        logged_user = ""

    # Remove action
    user_action = isUserAction(request)
    if user_action == True:
        action = request.session["Action"]
        try:
            del request.session["Action"]
        except:
            pass
    else:
        action = ""
    reply_form = new_thread_form()
    template_search_form = search_form()
    return render_to_response(
        "forum_page.html",
        {  #'threads':threadsList,
            "display_forum": display_forum,
            "display_thread": display_thread,
            "category": cat_id,
            "thread": thread_id,
            "topic_id": topic_id,
            "display_category": categories,
            "path": request.get_full_path(),
            "user_isLogged": user_status,
            "reply_form": reply_form,
            "search_form": template_search_form,
            "owner": logged_user,
            "topic_is_locked": is_locked,
            "action": action,
            "category_name": category_name,
            "thread_name": thread_name,
        },
        context_instance=RequestContext(request),
    )