def edit_topic(request, cat, topic): """display process for edit topic :param request: object contain context of request :type request: object :param cat: slug of categorie :type cat: slug :param topic: slug of topic :type topic: slug :var cat: select category by slug :return: HTTPResponse """ logger_info.info(info_load_log_message(request)) try: cat = models.ForumCat.objects.get(slug=cat) wrong_cat = 0 except: wrong_cat = 1 try: models.ForumTopic.objects.get(slug=topic) wrong_topic = 0 except: wrong_topic = 1 if not wrong_cat and not wrong_topic and request.method == "POST": form = TopicForm(request.POST) if form.is_valid(): update = models.ForumTopic.objects.filter(slug=topic).update( Title=form.cleaned_data["Title"], Autor=request.user, slug=slugify(form.cleaned_data["Title"]), Message=form.cleaned_data["Message"], LastModified=timezone.now(), ) if update is not None: topic = ( models.ForumTopic.objects.filter(Title=form.cleaned_data["Title"]) .filter(Autor=request.user) .filter(Message=form.cleaned_data["Message"]) ) return redirect(reverse("topic_cat", kwargs={"cat": cat.slug, "topic": topic[0].slug}), permanent=True) else: context = {"cat": cat, "error": _("thread_fail"), "form": form} else: top = models.ForumTopic.objects.get(slug=topic) form = TopicForm({"Title": top.Title, "Message": top.Message}) context = {"cat": cat, "form": form} elif wrong_cat and wrong_topic: return redirect(reverse("forum"), permanent=True) elif wrong_cat: top = models.ForumTopic.objects.get(slug=topic) return redirect(reverse("edit_topic", kwargs={"cat": top.CatParent.slug, "topic": top.slug}), permanent=True) elif wrong_topic: return redirect(reverse("cat", kwargs={"cat": cat}), permanent=True) else: top = models.ForumTopic.objects.get(slug=topic) topic = {"Title": top.Title, "Message": top.Message} form = TopicForm(topic) context = {"cat": cat, "form": form, "edit": 1} return render(request, "forum/create_topic.html", context)
def create_topic(request, cat): """create new topic :param request: object contain context of request :type request: object :param cat: slug of categorie :type cat: slug :var cat: categorie object selected by slug :var form: form for create topic :var errors: dict of process error :var create: new topic :var topic: get the new topic :var context: define the context for display page :return: HTTPResponse """ logger_info.info(info_load_log_message(request)) cat = models.ForumCat.objects.get(slug=cat) form = TopicForm(request.POST) error = {} if request.method == "POST": if form.is_valid(): try: create = models.ForumTopic( CatParent=cat, Title=form.cleaned_data["Title"], Autor=request.user, Message=form.cleaned_data["Message"], ).save() if create is None: topic = ( models.ForumTopic.objects.filter(Title=form.cleaned_data["Title"]) .filter(Autor=request.user) .filter(Message=form.cleaned_data["Message"])[0] ) return redirect(reverse("topic_cat", kwargs={"cat": cat.slug, "topic": topic.slug}), permanent=True) except: logger_error.error(_("save_topic")) error["save_topic"] = _("topic_fail") else: if not request.POST["Title"]: error["no_title"] = _("topic_must_contain_title") if not request.POST["Message"]: error["no_title"] = _("topic_must_contain_message") context = {"cat": cat, "form": form, "error": error} return render(request, "forum/create_topic.html", context)