Example #1
0
def displayAllThreads(request, view_all=None):
    user_status = is_userLogged(request)
    if user_status:
        user_id = request.session["User_id"]
    else:
        user_id = ""
    # Remove action
    user_action = isUserAction(request)
    if user_action == True:
        action = request.session["Action"]
        try:
            del request.session["Action"]
        except:
            pass
    else:
        action = ""
    return render_to_response(
        "forum_thread_page.html",
        {"user_isLogged": user_status, "owner": user_id, "action": action},
        context_instance=RequestContext(request),
    )
Example #2
0
def homepage(request):
    """
    # URL recieved e.g. http://healthpark.ca/99/9999
    """
    user_status = is_userLogged(request)
    if user_status: 
        user_id = request.session['User_id']
    else: 
        user_id = ''   
    #Remove action     
    user_action = isUserAction(request);
    if user_action == True:
        action = request.session['Action']
        try:
            del request.session['Action']
        except:
            pass    
    else:
        action = ''          
    a = request.get_full_path()
    return render_to_response('home_content.html', {'user_isLogged':user_status, 'user_id':user_id,'path':a,'action':action}, context_instance=RequestContext(request))
Example #3
0
def editThread(request, thread_id=None, view_page=None, topic_id=None, parent_id=None):
    """
    @AUTHOR:Fouzia Riaz
    @DESCRIPTION:Edit thread to the database with given relations.
    """
    # Check if a user is logged in. Otherwise redirect to login page.
    user_status = is_userLogged(request)
    # Remove action
    user_action = isUserAction(request)
    if user_action == True:
        action = request.session["Action"]
        try:
            del request.session["Action"]
        except:
            pass
    else:
        action = ""

    try:
        logged_user = request.session["User_id"]
        if request.method == "POST":
            editThreadForm = edit_thread_form(request.POST)
            if editThreadForm.is_valid():
                clean_data = editThreadForm.cleaned_data
                owner_user = User.objects.get(id=uuid.UUID(logged_user))
                if not clean_data["thread_name"]:
                    return render_to_response(
                        "new_thread.html",
                        {
                            "threadForm": editThreadForm,
                            "emptyThreadName": True,
                            "actions": "edit",
                            "user_isLogged": user_status,
                            "action": action,
                        },
                        context_instance=RequestContext(request),
                    )

                if not clean_data["thread_body"]:
                    return render_to_response(
                        "new_thread.html",
                        {
                            "threadForm": editThreadForm,
                            "emptyThreadBody": True,
                            "actions": "edit",
                            "user_isLogged": user_status,
                            "action": action,
                        },
                        context_instance=RequestContext(request),
                    )

                # Edit thread instance to save to the db.
                try:
                    edit_thread = Thread.objects.get(id=thread_id)
                    edit_thread.thread_name = clean_data["thread_name"]
                    edit_thread.thread_body = clean_data["thread_body"]
                    edit_thread.keyword_dict = clean_data["keywords"]
                    # edit_thread.topic           = clean_data['category_id']
                except:
                    return HttpResponseRedirect("/")

                try:
                    if edit_thread.owner == owner_user:
                        edit_thread.save()
                        try:
                            allowed_content_list = [
                                "application/pdf",
                                "application/zip",
                                "image/gif",
                                "image/jpeg",
                                "image/pjpeg",
                            ]
                            docfile1 = request.FILES["docfile"]
                            c_type = docfile1.content_type
                            if "docfile" in request.FILES and c_type in allowed_content_list:
                                newdoc = hptb_attachments(
                                    hptable_ID="th", hprecord_id=thread_id, docfile=request.FILES["docfile"]
                                )
                                newdoc.save()
                        except:
                            pass
                    if view_page == "home":
                        redirect_url = "/"
                    if view_page == "forum":
                        redirect_url = r"/forum/?cat_id=%s/" % (topic_id)
                    if view_page == "thread":
                        redirect_url = r"/forum/?topic_id=%s&thread_id=%s/" % (topic_id, parent_id)
                    if view_page == "view-all":
                        redirect_url = "/forum/view-all/"
                    request.session["Action"] = "edit-post"
                    return HttpResponseRedirect(redirect_url)

                except:
                    return render_to_response(
                        "new_thread.html",
                        {
                            "threadFrom": editThreadForm,
                            "dberror": True,
                            "actions": "edit",
                            "user_isLogged": user_status,
                            "action": action,
                        },
                        context_instance=RequestContext(request),
                    )
    except KeyError:
        return HttpResponseRedirect("/login/")
    # Select Values From Database
    try:
        thread_content = Thread.objects.get(id=thread_id)
        category_content = Topic.objects.get(id=topic_id)
        threadForm = edit_thread_form(
            initial={  #'category_id':category_content,
                "thread_name": thread_content.thread_name,
                "thread_body": thread_content.thread_body,
                "keywords": thread_content.keyword_dict,
            }
        )
        return render_to_response(
            "new_thread.html",
            {
                "threadForm": threadForm,
                "actions": "edit",
                "user_isLogged": user_status,
                "category_name": category_content.topic_name,
                "cat_id": category_content.id,
                "action": action,
            },
            context_instance=RequestContext(request),
        )
    except:
        return HttpResponseRedirect("/")
Example #4
0
def editCategory(request, category_id=None, topic_id=None, thread_id=None):
    """
    @AUTHOR:Fouzia Riaz
    @DESCRIPTION:Function For Change Category
    """
    # Remove action
    user_action = isUserAction(request)
    if user_action == True:
        action = request.session["Action"]
        try:
            del request.session["Action"]
        except:
            pass
    else:
        action = ""

    try:
        # Check if a user is logged in. Otherwise redirect to login page.
        logged_user = request.session["User_id"]
        user_status = is_userLogged(request)
        if request.method == "POST":
            editTopicForm = edit_category_form(request.POST)
            if editTopicForm.is_valid():
                clean_data = editTopicForm.cleaned_data
                owner_user = User.objects.get(id=uuid.UUID(logged_user))
                try:
                    # owner_user = User.objects.get(id=uuid.UUID(logged_user))
                    if len(clean_data["category_name"]) == 0:
                        return render_to_response(
                            "edit_category.html",
                            {
                                "editTopicForm": editTopicForm,
                                "emptyCategory": True,
                                "user_isLogged": user_status,
                                "action": action,
                            },
                            context_instance=RequestContext(request),
                        )
                    else:
                        editCategory = Topic.objects.get(id=category_id)
                        editCategory.topic_name = clean_data["category_name"]
                        editCategory.topic_body = topic_body = clean_data["category_body"]
                        editCategory.keyword_dict = keyword_dict = clean_data["category_keywords"]
                        try:
                            if editCategory.owner == owner_user:
                                editCategory.save()
                                if not topic_id is None and not thread_id is None:
                                    id_list = (topic_id, thread_id)
                                    redirect_url = r"/forum/?topic_id=%s&thread_id=%s/" % id_list
                                elif not topic_id is None and thread_id is None:
                                    if topic_id == "community":
                                        redirect_url = r"/forum/"
                                    elif topic_id == "view-all":
                                        redirect_url = r"/forum/view-all/"
                                    else:
                                        redirect_url = r"/forum/?cat_id=%s" % (topic_id)
                                elif topic_id is None and thread_id is None:
                                    redirect_url = r"/"
                                request.session["Action"] = "edit-category"
                                return HttpResponseRedirect(redirect_url)
                        except:
                            return render_to_response(
                                "edit_category.html",
                                {
                                    "editTopicForm": editTopicForm,
                                    "dberror": True,
                                    "user_isLogged": user_status,
                                    "action": action,
                                },
                                context_instance=RequestContext(request),
                            )
                except:
                    return HttpResponseRedirect("/")
        # except KeyError:
        #    return HttpResponseRedirect('/login/')*/

        # Select Values From Database
        category_content = Topic.objects.get(id=category_id)
        editTopicForm = edit_category_form(
            initial={
                "category_name": category_content.topic_name,
                "category_body": category_content.topic_body,
                "category_keywords": category_content.keyword_dict,
            }
        )
        return render_to_response(
            "edit_category.html",
            {
                "editTopicForm": editTopicForm,
                "user_isLogged": user_status,
                "category_name": category_content.topic_name,
                "action": action,
            },
            context_instance=RequestContext(request),
        )
    except:
        return HttpResponseRedirect("/")
Example #5
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/")
Example #6
0
def newThread(request, news_id=None, topic_id=None, parent_id=None, is_parent=None, origthread_id=None):
    """
     Inserts a new thread to the database with given relations.
    """
    # Remove action
    user_action = isUserAction(request)
    if user_action == True:
        action = request.session["Action"]
        try:
            del request.session["Action"]
        except:
            pass
    else:
        action = ""

    try:
        user_status = is_userLogged(request)
        if (Topic.objects.get(id=topic_id)).is_locked:
            redirect_url = r"/forum/?cat_id=%s/" % topic_id
            return HttpResponseRedirect(redirect_url)
        else:
            try:
                # Check if a user is logged in. Otherwise redirect to login page.
                logged_user = request.session["User_id"]

                if request.method == "POST":
                    newThreadForm = new_thread_form(request.POST)
                    if newThreadForm.is_valid():
                        clean_data = newThreadForm.cleaned_data
                        if is_parent and not clean_data["thread_name"]:
                            redirect_url = r"/forum/?cat_id=%s/" % topic_id
                            return render_to_response(
                                "new_thread.html",
                                {
                                    "threadForm": newThreadForm,
                                    "emptyThreadName": True,
                                    "user_isLogged": user_status,
                                    "action": action,
                                },
                                context_instance=RequestContext(request),
                            )

                        if not clean_data["thread_body"]:
                            return render_to_response(
                                "new_thread.html",
                                {
                                    "threadForm": newThreadForm,
                                    "emptyThreadBody": True,
                                    "user_isLogged": user_status,
                                    "action": action,
                                },
                                context_instance=RequestContext(request),
                            )

                        try:
                            # Get the user,topic, and thread objects from the database,
                            # for the logged user and given topic and thread ids.
                            owner_user = User.objects.get(id=uuid.UUID(logged_user))
                            # Insert New Category
                            if clean_data["new_category"]:
                                new_topic_name = clean_data["new_category"]
                                category = add_new_category(new_topic_name, owner_user)
                                categ_topic = Topic.objects.get(id=str(category))
                            else:
                                if clean_data["category_id"]:
                                    categ_topic = Topic.objects.get(id=topic_id)
                                else:
                                    return render_to_response(
                                        "new_thread.html",
                                        {
                                            "threadForm": newThreadForm,
                                            "emptyCategory": True,
                                            "user_isLogged": user_status,
                                            "action": action,
                                        },
                                        context_instance=RequestContext(request),
                                    )

                            parentThread = Thread.objects.get(id=parent_id)
                        except User.DoesNotExist:
                            pass
                        except Topic.DoesNotExist:
                            pass
                        except Thread.DoesNotExist:
                            # If no parent_id was passed to the view,
                            # set parentThread to None.
                            parentThread = None

                        # Create a new uuid.
                        new_thread_id = uuid.uuid4()

                        # Create a new thread instance to save to the db.
                        new_thread = Thread(
                            id=new_thread_id,
                            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()
                            allowed_content_list = [
                                "application/pdf",
                                "application/zip",
                                "image/gif",
                                "image/jpeg",
                                "image/pjpeg",
                            ]

                            try:
                                docfile1 = request.FILES["docfile"]
                                c_type = docfile1.content_type
                                if "docfile" in request.FILES and c_type in allowed_content_list:
                                    newdoc = hptb_attachments(
                                        hptable_ID="th", hprecord_id=new_thread_id, docfile=request.FILES["docfile"]
                                    )
                                    newdoc.save()
                            except:
                                pass
                            # If the thread to be saved is a top-level parent thread,
                            # redirect to the new parent thread created. Otherwise
                            # redirect to the same thread page where user was last.
                            if is_parent:
                                id_list = (topic_id, new_thread_id)
                            else:
                                id_list = (topic_id, origthread_id)
                            request.session["Action"] = "add-post"
                            redirect_url = r"/forum/?topic_id=%s&thread_id=%s/" % id_list
                            return HttpResponseRedirect(redirect_url)

                        except:
                            return render_to_response(
                                "new_thread.html",
                                {
                                    "threadForm": newThreadForm,
                                    "dberror": True,
                                    "user_isLogged": user_status,
                                    "action": action,
                                },
                                context_instance=RequestContext(request),
                            )

            except KeyError:
                return HttpResponseRedirect("/login/")

        categ_topic = Topic.objects.get(id=topic_id)
        threadForm = new_thread_form(initial={"category_id": categ_topic})
        return render_to_response(
            "new_thread.html",
            {
                "threadForm": threadForm,
                "user_isLogged": user_status,
                "category_name": categ_topic.topic_name,
                "cat_id": topic_id,
                "action": action,
            },
            context_instance=RequestContext(request),
        )
    except:
        return HttpResponseRedirect("/")
Example #7
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),
    )