Example #1
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 #2
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("/")