Exemple #1
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("/")
Exemple #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),
    )