Exemplo n.º 1
0
def manage_tags(pid):
	if check_hash():
		role = get_role()
		if  (role == 'user' and ph.get_post(pid)[1] == uh.user_by_hash(session['key'])) or role=='moderator' or role=='superuser':
			form = AddTagForm()
			p_data = None
			all_data = None
			try:
				p_data = ph.get_post_tags(pid)
				all_data = th.filter_tags(None)
			except:
				return render_template('404.html', error='Error while getting info')

			data = [value for value in all_data if value not in p_data]
			choices = []
			for rec in data:
				choices.append((rec[0], rec[0]))
			form.tag.choices = choices	
			if request.method == "GET":
					return render_template('posttags.html', pid=pid, form=form, data=p_data)
					
			if request.method == "POST":
				if form.validate():
					status = ph.add_tag_to_post(pid, request.form['tag'])
					if status != 'ok': return render_template('404.html', error=status)
					
					return redirect(url_for('manage_tags', pid=pid))

				return render_template('404.html', error="Validation violated")

	return render_template('404.html', error = 'You have no rights for this action')
Exemplo n.º 2
0
def view_post(pid):

	role = get_role()
	
	answer_form = None
	tag_form = None
	if check_hash() and (role == 'superuser' or role == 'moderator'):
		answer_form = AnswerForm()

	if (role == 'user' and ph.get_post(pid)[1] == uh.user_by_hash(session['key'])) or role=='moderator' or role=='superuser':
		tag_form = AddTagForm()
		p_data = None
		all_data = None
		try:
			p_data = ph.get_post_tags(pid)
			all_data = th.filter_tags(None)
		except:
			return render_template('404.html', error='Error while getting info')

		data = [value for value in all_data if value not in p_data]
		choices = []
		for rec in data:
			choices.append((rec[0], rec[0]))
		tag_form.tag.choices = choices

	if request.method == "GET":
		data = None
		author = None
		answer = None
		try:
			data = ph.get_post(pid)
			author = uh.get_user(data[1])[2]
			tags = ph.get_post_tags(pid)
			answer = ah.filter_answers(None, None, pid, None)
			if answer:
				answer = answer[0]
				if answer_form:
					answer_form.title.data = answer[3]
					answer_form.text.data = answer[4]
				l = list(answer)
				l[1] = uh.get_user(l[1])[2]
				answer = tuple(l)

		except:
			return render_template('404.html', error='Error while getting info')
		if data[4] == None and role == 'user':
			return render_template('404.html', error = 'Post not found')
		return render_template('post.html', post=data, author=author, tags=tags, answer=answer,
						 answer_form=answer_form, tag_form=tag_form)

	if request.method == "POST":
		if tag_form.validate(): 
			status = ph.add_tag_to_post(pid, request.form['tag'])
			if status != 'ok': return render_template('404.html', error=status)
		else: 
			return render_template('404.html', error="Validation violated")
						
		return redirect(url_for('view_post', _anchor='tag', pid=pid))

	return render_template('404.html', error = 'You have no rights for this action')
Exemplo n.º 3
0
def remove_tag_from_post(pid, tag):
	if check_hash():
		role = get_role()
		if  (role == 'user' and ph.get_post(pid)[1] == uh.user_by_hash(session['key'])) or role=='moderator' or role=='superuser':
			status = ph.delete_tag_from_post(pid, tag)
			if status != 'ok': return render_template('404.html', error = status)
			flash('Tag successfully deleted from post')
			return redirect(url_for('view_post',_anchor='tag', pid=pid))

	return render_template('404.html', error = 'You have no rights for this action')
Exemplo n.º 4
0
def edit_answer(aid):
	role = get_role()
	answer = ah.get_answer(aid)
	if check_hash() and (role == 'superuser' or (role == 'moderator' and \
		answer[1] == uh.user_by_hash(session['key']))):
		form = AnswerForm()
		if form.validate():
			status = ah.edit_answer(aid, request.form['title'], request.form['text'])
			if status != 'ok': return render_template('404.html', error=status)
			return redirect(url_for('view_post', pid=answer[2]))
	return render_template('404.html', error = 'You have no rights for this action')
Exemplo n.º 5
0
def remove_post(pid):
	role = get_role()
	if check_hash():
		if  (role == 'user' and ph.get_post(pid)[1] == uh.user_by_hash(session['key'])) or role=='moderator' or role=='superuser':
			status = ph.hide_post(pid)
			if status!='ok': return render_template('404.html', error=status)
			flash("Post was removed")
			if role=='user':
				return redirect('/')
			return redirect(url_for('manage_posts'))

	return render_template('404.html', error = 'You have no rights for this action')
Exemplo n.º 6
0
def add_answer(pid):
	role = get_role()
	if check_hash() and (role == 'superuser' or role == 'moderator'):
		form = AnswerForm()
		if request.method == "GET":
			try:
				post = ph.get_post(pid)
			except:
				return render_template('404.html', error = 'Error while getting post info')

			return render_template('answerform.html', form=form, data=post)
		if request.method == 'POST':
			status = "ok"
			author = uh.user_by_hash(session['key'])[0]
			status = ah.add_answer(author, pid, request.form['title'], request.form['text'])[1]
			if status != "ok": return render_template('404.html', error = status)
			flash('Answer successfully added')
			return redirect(url_for('view_post', pid=pid))
	return render_template('404.html', error = 'You have no rights for this action')
Exemplo n.º 7
0
def edit_post(pid):
	if check_hash():
		form = PostForm()
		categories = ch.filter_categories(None)
		category_list = []
		for category in categories:
			category_list.append((category[0], category[0]))
		form.category.choices = category_list
		data = ph.get_post(pid)
		if get_role() == 'superuser' or data[0] == uh.user_by_hash(session['key']):
			if request.method == "GET":
				form.title.data = data[2]
				form.text.data = data[3]
				form.category.data = data[6]
				return render_template("postform.html", form=form)
			if request.method == "POST":
				if form.validate():
					status = ph.edit_post(pid, request.form['title'], request.form['text'], request.form['category'])
					if status!='ok': return render_template('404.html', error='Error while editing')
					flash('Post edited successfully')
					return redirect(url_for('view_post', pid=pid))
			return render_template('404.html', error="Validation violated")
	return render_template('404.html', error = 'You have no rights for this action')