def edit(request): """Edit the given topic.""" try: topic_pk = request.POST["topic"] except: # problem in variable format raise Http404 if "page" in request.POST: try: page = int(request.POST["page"]) except: #problem in variable format raise Http404 else: page = 1 data = request.POST resp = {} g_topic = get_object_or_404(Topic, pk=topic_pk) if "follow" in data: resp["follow"] = follow(g_topic) if "email" in data: resp["email"] = follow_by_email(g_topic) if request.user == g_topic.author \ or request.user.has_perm("forum.change_topic"): if "solved" in data: g_topic.is_solved = not g_topic.is_solved resp["solved"] = g_topic.is_solved if request.user.has_perm("forum.change_topic"): # Staff actions using AJAX TODO: Do not redirect on AJAX requests if "lock" in data: g_topic.is_locked = data["lock"] == "true" messages.success(request, u"Le sujet {0} est désormais vérouillé." .format(g_topic.title)) if "sticky" in data: g_topic.is_sticky = data["sticky"] == "true" messages.success(request, u"Le sujet {0} est désormais épinglé." .format(g_topic.title)) if "move" in data: try: forum_pk = int(request.POST["move_target"]) except: # problem in variable format raise Http404 forum = get_object_or_404(Forum, pk=forum_pk) g_topic.forum = forum g_topic.save() if request.is_ajax(): return HttpResponse(json.dumps(resp)) else: if not g_topic.forum.can_read(request.user): return redirect(reverse("zds.forum.views.index")) else: return redirect(u"{}?page={}".format(g_topic.get_absolute_url(), page))
def profile(): if not session.get('logged_in'): return redirect(url_for('login')) userid = session['userid'] if request.method == 'POST': Name = request.form['Name'] if request.form['butt'] == 'addWishlist': dbHandler.addToWishlist(userid, Name) elif request.form['butt'] == 'deleteWishlist': dbHandler.deleteFromWishlist(userid, Name) elif request.form['butt'] == 'addBook': dbHandler.addToBooklist(userid, Name) elif request.form['butt'] == 'deleteBook': dbHandler.deleteFromBooklist(userid, Name) elif request.form['butt'] == 'addPeople': id2 = dbHandler.getUserid(Name) if id2 != 'User Not Found': dbHandler.follow(userid, id2) else: flash(id2) elif request.form['butt'] == 'deletePeople': id2 = dbHandler.getUserid(Name) dbHandler.unFollow(userid, id2) wish_list = dbHandler.getWishlist(userid) book_list = dbHandler.getBooklist(userid) followers = dbHandler.getFollowers(userid) following = dbHandler.getFollowing(userid) return render_template('profile.html', books=book_list, wish=wish_list, followers=followers, following=following)
def edit(request): ''' Edit the given topic ''' try: topic_pk = request.POST['topic'] except KeyError: raise Http404 try: page = int(request.POST['page']) except KeyError: page = 1 data = request.POST resp = {} g_topic = get_object_or_404(Topic, pk=topic_pk) if 'follow' in data: resp['follow'] = follow(g_topic) if request.user == g_topic.author: # Author actions if 'solved' in data: g_topic.is_solved = not g_topic.is_solved resp['solved'] = g_topic.is_solved if request.user.has_perm('forum.change_topic'): # Staff actions using AJAX # TODO: Do not redirect on AJAX requests if 'lock' in data: g_topic.is_locked = data['lock'] == 'true' if 'sticky' in data: g_topic.is_sticky = data['sticky'] == 'true' if 'move' in data: try: forum_pk = int(request.POST['move_target']) except KeyError: raise Http404 forum = get_object_or_404(Forum, pk=forum_pk) g_topic.forum = forum g_topic.save() if request.is_ajax(): return HttpResponse(json.dumps(resp)) else: return redirect(u'{}?page={}'.format(g_topic.get_absolute_url(), page))
def answer(request): """Adds an answer from a user to a topic.""" try: topic_pk = request.GET["sujet"] except KeyError: raise Http404 # Retrieve current topic. g_topic = get_object_or_404(Topic, pk=topic_pk) if not g_topic.forum.can_read(request.user): raise PermissionDenied # Making sure posting is allowed if g_topic.is_locked: raise PermissionDenied # Check that the user isn't spamming if g_topic.antispam(request.user): raise PermissionDenied last_post_pk = g_topic.last_message.pk # Retrieve 10 last posts of the current topic. posts = \ Post.objects.filter(topic=g_topic) \ .prefetch_related() \ .order_by("-pubdate" )[:10] # User would like preview his post or post a new post on the topic. if request.method == "POST": data = request.POST newpost = last_post_pk != int(data["last_post"]) # Using the « preview button », the « more » button or new post if "preview" in data or newpost: form = PostForm(g_topic, request.user, initial={"text": data["text" ]}) form.helper.form_action = reverse("zds.forum.views.answer") \ + "?sujet=" + str(g_topic.pk) return render_template("forum/post/new.html", { "text": data["text"], "topic": g_topic, "posts": posts, "last_post_pk": last_post_pk, "newpost": newpost, "form": form, }) else: # Saving the message form = PostForm(g_topic, request.user, request.POST) if form.is_valid(): data = form.data post = Post() post.topic = g_topic post.author = request.user post.text = data["text"] post.text_html = emarkdown(data["text"]) post.pubdate = datetime.now() post.position = g_topic.get_post_count() + 1 post.ip_address = get_client_ip(request) post.save() g_topic.last_message = post g_topic.save() #Send mail subject = "ZDS - Notification : " + g_topic.title from_email = "Zeste de Savoir <{0}>".format(settings.MAIL_NOREPLY) followers = g_topic.get_followers_by_email() for follower in followers: receiver = follower.user if receiver == request.user: continue pos = post.position - 1 last_read = TopicRead.objects.filter( topic=g_topic, post__position=pos, user=receiver).count() if last_read > 0: message_html = get_template('email/notification/new.html') \ .render( Context({ 'username': receiver.username, 'title':g_topic.title, 'url': settings.SITE_URL + post.get_absolute_url(), 'author': request.user.username }) ) message_txt = get_template('email/notification/new.txt').render( Context({ 'username': receiver.username, 'title':g_topic.title, 'url': settings.SITE_URL + post.get_absolute_url(), 'author': request.user.username }) ) msg = EmailMultiAlternatives( subject, message_txt, from_email, [ receiver.email]) msg.attach_alternative(message_html, "text/html") msg.send() # Follow topic on answering if not g_topic.is_followed(user=request.user): follow(g_topic) return redirect(post.get_absolute_url()) else: return render_template("forum/post/new.html", { "text": data["text"], "topic": g_topic, "posts": posts, "last_post_pk": last_post_pk, "newpost": newpost, "form": form, }) else: # Actions from the editor render to new.html. text = "" # Using the quote button if "cite" in request.GET: post_cite_pk = request.GET["cite"] post_cite = Post.objects.get(pk=post_cite_pk) if not post_cite.is_visible: raise PermissionDenied for line in post_cite.text.splitlines(): text = text + "> " + line + "\n" text = u"{0}Source:[{1}]({2}{3})".format( text, post_cite.author.username, settings.SITE_URL, post_cite.get_absolute_url()) form = PostForm(g_topic, request.user, initial={"text": text}) form.helper.form_action = reverse("zds.forum.views.answer") \ + "?sujet=" + str(g_topic.pk) return render_template("forum/post/new.html", { "topic": g_topic, "posts": posts, "last_post_pk": last_post_pk, "form": form, })
def new(request): """Creates a new topic in a forum.""" try: forum_pk = request.GET["forum"] except KeyError: raise Http404 forum = get_object_or_404(Forum, pk=forum_pk) if not forum.can_read(request.user): raise PermissionDenied if request.method == "POST": # If the client is using the "preview" button if "preview" in request.POST: form = TopicForm(initial={"title": request.POST["title"], "subtitle": request.POST["subtitle"], "text": request.POST["text"]}) return render_template("forum/topic/new.html", {"forum": forum, "form": form, "text": request.POST["text"]}) form = TopicForm(request.POST) data = form.data if form.is_valid(): # Treat title (tags, title) = get_tag_by_title(data["title"]) # Creating the thread n_topic = Topic() n_topic.forum = forum n_topic.title = title n_topic.subtitle = data["subtitle"] n_topic.pubdate = datetime.now() n_topic.author = request.user n_topic.save() # add tags n_topic.add_tags(tags) n_topic.save() # Adding the first message post = Post() post.topic = n_topic post.author = request.user post.text = data["text"] post.text_html = emarkdown(request.POST["text"]) post.pubdate = datetime.now() post.position = 1 post.ip_address = get_client_ip(request) post.save() n_topic.last_message = post n_topic.save() # Follow the topic follow(n_topic) return redirect(n_topic.get_absolute_url()) else: form = TopicForm() return render_template("forum/topic/new.html", {"forum": forum, "form": form})
def answer(request): """Adds an answer from a user to a topic.""" try: topic_pk = request.GET["sujet"] except: # problem in variable format raise Http404 # Retrieve current topic. g_topic = get_object_or_404(Topic, pk=topic_pk) if not g_topic.forum.can_read(request.user): raise PermissionDenied # Making sure posting is allowed if g_topic.is_locked: raise PermissionDenied # Check that the user isn't spamming if g_topic.antispam(request.user): raise PermissionDenied last_post_pk = g_topic.last_message.pk # Retrieve last posts of the current topic. posts = Post.objects.filter(topic=g_topic) \ .prefetch_related() \ .order_by("-pubdate")[:settings.POSTS_PER_PAGE] # User would like preview his post or post a new post on the topic. if request.method == "POST": data = request.POST newpost = last_post_pk != int(data["last_post"]) # Using the « preview button », the « more » button or new post if "preview" in data or newpost: form = PostForm(g_topic, request.user, initial={"text": data["text"]}) form.helper.form_action = reverse("zds.forum.views.answer") \ + "?sujet=" + str(g_topic.pk) return render_template("forum/post/new.html", { "text": data["text"], "topic": g_topic, "posts": posts, "last_post_pk": last_post_pk, "newpost": newpost, "form": form, }) else: # Saving the message form = PostForm(g_topic, request.user, request.POST) if form.is_valid(): data = form.data post = Post() post.topic = g_topic post.author = request.user post.text = data["text"] post.text_html = emarkdown(data["text"]) post.pubdate = datetime.now() post.position = g_topic.get_post_count() + 1 post.ip_address = get_client_ip(request) post.save() g_topic.last_message = post g_topic.save() #Send mail subject = "ZDS - Notification : " + g_topic.title from_email = "Zeste de Savoir <{0}>".format(settings.MAIL_NOREPLY) followers = g_topic.get_followers_by_email() for follower in followers: receiver = follower.user if receiver == request.user: continue pos = post.position - 1 last_read = TopicRead.objects.filter( topic=g_topic, post__position=pos, user=receiver).count() if last_read > 0: message_html = get_template('email/notification/new.html') \ .render( Context({ 'username': receiver.username, 'title':g_topic.title, 'url': settings.SITE_URL + post.get_absolute_url(), 'author': request.user.username }) ) message_txt = get_template('email/notification/new.txt').render( Context({ 'username': receiver.username, 'title':g_topic.title, 'url': settings.SITE_URL + post.get_absolute_url(), 'author': request.user.username }) ) msg = EmailMultiAlternatives( subject, message_txt, from_email, [ receiver.email]) msg.attach_alternative(message_html, "text/html") msg.send() # Follow topic on answering if not g_topic.is_followed(user=request.user): follow(g_topic) return redirect(post.get_absolute_url()) else: return render_template("forum/post/new.html", { "text": data["text"], "topic": g_topic, "posts": posts, "last_post_pk": last_post_pk, "newpost": newpost, "form": form, }) else: # Actions from the editor render to new.html. text = "" # Using the quote button if "cite" in request.GET: post_cite_pk = request.GET["cite"] post_cite = Post.objects.get(pk=post_cite_pk) if not post_cite.is_visible: raise PermissionDenied for line in post_cite.text.splitlines(): text = text + "> " + line + "\n" text = u"{0}Source:[{1}]({2}{3})".format( text, post_cite.author.username, settings.SITE_URL, post_cite.get_absolute_url()) form = PostForm(g_topic, request.user, initial={"text": text}) form.helper.form_action = reverse("zds.forum.views.answer") \ + "?sujet=" + str(g_topic.pk) return render_template("forum/post/new.html", { "topic": g_topic, "posts": posts, "last_post_pk": last_post_pk, "form": form, })
def new(request): """Creates a new topic in a forum.""" try: forum_pk = request.GET["forum"] except: # problem in variable format raise Http404 forum = get_object_or_404(Forum, pk=forum_pk) if not forum.can_read(request.user): raise PermissionDenied if request.method == "POST": # If the client is using the "preview" button if "preview" in request.POST: form = TopicForm(initial={"title": request.POST["title"], "subtitle": request.POST["subtitle"], "text": request.POST["text"]}) return render_template("forum/topic/new.html", {"forum": forum, "form": form, "text": request.POST["text"]}) form = TopicForm(request.POST) data = form.data if form.is_valid(): # Treat title (tags, title) = get_tag_by_title(data["title"]) # Creating the thread n_topic = Topic() n_topic.forum = forum n_topic.title = title n_topic.subtitle = data["subtitle"] n_topic.pubdate = datetime.now() n_topic.author = request.user n_topic.save() # add tags n_topic.add_tags(tags) n_topic.save() # Adding the first message post = Post() post.topic = n_topic post.author = request.user post.text = data["text"] post.text_html = emarkdown(request.POST["text"]) post.pubdate = datetime.now() post.position = 1 post.ip_address = get_client_ip(request) post.save() n_topic.last_message = post n_topic.save() # Follow the topic follow(n_topic) return redirect(n_topic.get_absolute_url()) else: form = TopicForm() return render_template("forum/topic/new.html", {"forum": forum, "form": form})
def answer(request): ''' Adds an answer from a user to a topic ''' try: topic_pk = request.GET['sujet'] except KeyError: raise Http404 g_topic = get_object_or_404(Topic, pk=topic_pk) if not g_topic.forum.can_read(request.user): raise Http404 posts = Post.objects.filter(topic=g_topic).order_by('-pubdate')[:3] last_post_pk = g_topic.last_message.pk # Making sure posting is allowed if g_topic.is_locked: raise Http404 # Check that the user isn't spamming if g_topic.antispam(request.user): raise Http404 # If we just sent data if request.method == 'POST': data = request.POST newpost = last_post_pk != int(data['last_post']) # Using the « preview button », the « more » button or new post if 'preview' in data or 'more' in data or newpost: return render_template( 'forum/answer.html', { 'text': data['text'], 'topic': g_topic, 'posts': posts, 'last_post_pk': last_post_pk, 'newpost': newpost }) # Saving the message else: form = PostForm(request.POST) if form.is_valid() and data['text'].strip() != '': data = form.data post = Post() post.topic = g_topic post.author = request.user post.text = data['text'] post.text_html = emarkdown(data['text']) post.pubdate = datetime.now() post.position = g_topic.get_post_count() + 1 post.ip_address = get_client_ip(request) post.save() g_topic.last_message = post g_topic.save() # Follow topic on answering if not g_topic.is_followed(): follow(g_topic) return redirect(post.get_absolute_url()) else: raise Http404 else: text = '' # Using the quote button if 'cite' in request.GET: post_cite_pk = request.GET['cite'] post_cite = Post.objects.get(pk=post_cite_pk) for line in post_cite.text.splitlines(): text = text + '> ' + line + '\n' text = u'**{0} a écrit :**\n{1}\n'.format( post_cite.author.username, text) return render_template( 'forum/answer.html', { 'topic': g_topic, 'text': text, 'posts': posts, 'last_post_pk': last_post_pk })
def new(request): ''' Creates a new topic in a forum ''' try: forum_pk = request.GET['forum'] except KeyError: raise Http404 forum = get_object_or_404(Forum, pk=forum_pk) if request.method == 'POST': # If the client is using the "preview" button if 'preview' in request.POST: return render_template( 'forum/new.html', { 'forum': forum, 'title': request.POST['title'], 'subtitle': request.POST['subtitle'], 'text': request.POST['text'], }) form = TopicForm(request.POST) if form.is_valid() and data['text'].strip() != '': data = form.data # Creating the thread n_topic = Topic() n_topic.forum = forum n_topic.title = data['title'] n_topic.subtitle = data['subtitle'] n_topic.pubdate = datetime.now() n_topic.author = request.user n_topic.save() # Adding the first message post = Post() post.topic = n_topic post.author = request.user post.text = data['text'] post.text_html = emarkdown(request.POST['text']) post.pubdate = datetime.now() post.position = 1 post.ip_address = get_client_ip(request) post.save() n_topic.last_message = post n_topic.save() # Follow the topic follow(n_topic) return redirect(n_topic.get_absolute_url()) else: # TODO: add errors to the form and return it raise Http404 else: form = TopicForm() return render_template('forum/new.html', { 'form': form, 'forum': forum })