def question_answer(request):
    if request.method == 'POST':
        form = AnswerQuesitionForm(request.POST)
        if form.is_valid():
            cleaned_data = form.cleaned_data
            qid = cleaned_data['question']
            body = cleaned_data['body']
            question = get_object_or_404(Question, id=qid)
            answer = Answer()
            answer.uid = request.user.id
            answer.question = question
            answer.body = body.encode('unicode_escape')
            answer.save()
            if question.uid != request.user.id:
                notification = Notification()
                notification.uid = question.uid
                notification.pid = request.user.id
                notification.qid = qid
                notification.aid = answer.id
                notification.save()

                user = User.objects.get(id=question.uid)
                # Sending email when an answer is posted
                subject = 'Question has been answered'
                message = """
                    Dear {0}<br><br>
                    Your question titled <b>"{1}"</b> has been answered.<br>
                    Link: {2}<br><br>
                    Regards,<br>
                    Spoken Tutorial Forums
                """.format(
                    user.username,
                    question.title,
                    'http://forums.spoken-tutorial.org/question/' + str(question.id) + "#answer" + str(answer.id)
                )

                email = EmailMultiAlternatives(
                    subject, '', 'forums',
                    [user.email],
                    headers={"Content-type": "text/html;charset=iso-8859-1"}
                )

                email.attach_alternative(message, "text/html")
                email.send(fail_silently=True)
                # End of email send
        return HttpResponseRedirect('/question/' + str(qid) + "#answer" + str(answer.id))
    return HttpResponseRedirect('/')
def question_answer(request):
    if request.method == 'POST':
        form = AnswerQuesitionForm(request.POST)
        if form.is_valid():
            cleaned_data = form.cleaned_data
            qid = cleaned_data['question']
            body = cleaned_data['body']
            question = get_object_or_404(Question, id=qid)
            answer = Answer()
            answer.uid = request.user.id
            answer.question = question
            answer.body = body.encode('unicode_escape')
            answer.save()
            if question.uid != request.user.id:
                notification = Notification()
                notification.uid = question.uid
                notification.pid = request.user.id
                notification.qid = qid
                notification.aid = answer.id
                notification.save()
                
                user = User.objects.get(id=question.uid)
                # Sending email when an answer is posted
                subject = 'Question has been answered'
                message = """
                    Dear {0}<br><br>
                    Your question titled <b>"{1}"</b> has been answered.<br>
                    Link: {2}<br><br>
                    Regards,<br>
                    Spoken Tutorial Forums
                """.format(
                    user.username, 
                    question.title, 
                    'http://forums.spoken-tutorial.org/question/' + str(question.id) + "#answer" + str(answer.id)
                )
                
                email = EmailMultiAlternatives(
                    subject,'', 'forums', 
                    [user.email],
                    headers={"Content-type":"text/html;charset=iso-8859-1"}
                )
                
                email.attach_alternative(message, "text/html")
                email.send(fail_silently=True)
                # End of email send
        return HttpResponseRedirect('/question/'+ str(qid) + "#answer" + str(answer.id)) 
    return HttpResponseRedirect('/') 
def answer_comment(request):
    if request.method == 'POST':
        answer_id = request.POST['answer_id']
        body = request.POST['body']
        answer = get_object_or_404(Answer, pk=answer_id)
        comment = AnswerComment()
        comment.uid = request.user.id
        comment.answer = answer
        comment.body = body.encode('unicode_escape')
        comment.save()

        # notifying the answer owner
        if answer.uid != request.user.id:
            notification = Notification()
            notification.uid = answer.uid
            notification.pid = request.user.id
            notification.qid = answer.question.id
            notification.aid = answer.id
            notification.cid = comment.id
            notification.save()

            user = User.objects.get(id=answer.uid)
            subject = 'Comment for your answer'
            message = """
                Dear {0}<br><br>
                A comment has been posted on your answer.<br>
                Link: {1}<br><br>
                Regards,<br>
                Spoken Tutorial Forums
            """.format(
                user.username,
                "http://forums.spoken-tutorial.org/question/" + str(answer.question.id) + "#answer" + str(answer.id)
            )
            forums_mail(user.email, subject, message)

        # notifying other users in the comment thread
        uids = answer.answercomment_set.filter(answer=answer).values_list('uid', flat=True)
        # getting distinct uids
        uids = set(uids)
        uids.remove(request.user.id)
        for uid in uids:
            notification = Notification()
            notification.uid = uid
            notification.pid = request.user.id
            notification.qid = answer.question.id
            notification.aid = answer.id
            notification.cid = comment.id
            notification.save()

            user = User.objects.get(id=uid)
            subject = 'Comment has a reply'
            message = """
                Dear {0}<br><br>
                A reply has been posted on your comment.<br>
                Link: {1}<br><br>
                Regards,<br>
                Spoken Tutorial Forums
            """.format(
                user.username,
                "http://forums.spoken-tutorial.org/question/" + str(answer.question.id) + "#answer" + str(answer.id)
            )
            forums_mail(user.email, subject, message)
    return HttpResponseRedirect("/question/" + str(answer.question.id) + "#")
def answer_comment(request):
    if request.method == 'POST':
        answer_id = request.POST['answer_id'];
        body = request.POST['body']
        answer = Answer.objects.get(pk=answer_id)
        comment = AnswerComment()
        comment.uid = request.user.id
        comment.answer = answer
        comment.body = body.encode('unicode_escape')
        comment.save()
        
        # notifying the answer owner
        if answer.uid != request.user.id:
            notification = Notification()
            notification.uid = answer.uid
            notification.pid = request.user.id
            notification.qid = answer.question.id
            notification.aid = answer.id
            notification.cid = comment.id
            notification.save()
            
            user = User.objects.get(id=answer.uid)
            subject = 'Comment for your answer'
            message = """
                Dear {0}<br><br>
                A comment has been posted on your answer.<br>
                Link: {1}<br><br>
                Regards,<br>
                Spoken Tutorial Forums
            """.format(
                user.username,
                "http://forums.spoken-tutorial.org/question/" + str(answer.question.id) + "#answer" + str(answer.id)
            )
            forums_mail(user.email, subject, message)

        # notifying other users in the comment thread
        uids = answer.answercomment_set.filter(answer=answer).values_list('uid', flat=True)
        #getting distinct uids
        uids = set(uids) 
        uids.remove(request.user.id)
        for uid in uids:
            notification = Notification()
            notification.uid = uid
            notification.pid = request.user.id
            notification.qid = answer.question.id
            notification.aid = answer.id
            notification.cid = comment.id
            notification.save()
            
            user = User.objects.get(id=uid)
            subject = 'Comment has a reply'
            message = """
                Dear {0}<br><br>
                A reply has been posted on your comment.<br>
                Link: {1}<br><br>
                Regards,<br>
                Spoken Tutorial Forums
            """.format(
                user.username,
                "http://forums.spoken-tutorial.org/question/" + str(answer.question.id) + "#answer" + str(answer.id)
            )
            forums_mail(user.email, subject, message)
    return HttpResponseRedirect("/question/" + str(answer.question.id) + "#")
Beispiel #5
0
def question_answer(request,qid):
   
    dict_context = {}
   
    if request.method == 'POST':
    	
        form = AnswerQuestionForm(request.POST)
	question = get_object_or_404(Question, id=qid)
        answers = question.answer_set.all()
        answer = Answer()
        
        answer.uid = request.user.id
        if form.is_valid():
            cleaned_data = form.cleaned_data
            qid = cleaned_data['question']
            body = cleaned_data['body']
            answer.question = question
            answer.body = body.encode('unicode_escape')
            answer.save()
            # if user_id of question not matches to user_id of answer that
            # question , no
            if question.user_id != request.user.id:
                notification = Notification()
                notification.uid = question.user_id
                notification.pid = request.user.id
                notification.qid = qid
                notification.aid = answer.id
                notification.save()
                
                user = User.objects.get(id=question.user_id)
                # Sending email when an answer is posted
                subject = 'Question has been answered'
                message = """
                    Dear {0}<br><br>
                    Your question titled <b>"{1}"</b> has been answered.<br>
                    Link: {2}<br><br>
                    Regards,<br>
                    Fossee Forums
                """.format(
                    user.username, 
                    question.title, 
                    'http://forums.fossee.in/question/' + str(question.id) + "#answer" + str(answer.id)
                )
                
                email = EmailMultiAlternatives(
                    subject,'', 'forums', 
                    [user.email],
                    headers={"Content-type":"text/html;charset=iso-8859-1"}
                )
                
                email.attach_alternative(message, "text/html")
                email.send(fail_silently=True)
                # End of email send
	    return HttpResponseRedirect('/question/'+ str(qid) + "#answer" + str(answer.id)) 
	else:
		dict_context  = {
			'question':question,
			'answers': answers,
			'form': form
		}
		
	    	return render(request, 'website/templates/get-question.html', dict_context)
	
    return HttpResponseRedirect('/') 
Beispiel #6
0
def answer_comment(request):
	if request.method == 'POST':
		answer_id = request.POST['answer_id'];
		answer = Answer.objects.get(pk=answer_id)
		answers = answer.question.answer_set.all()
		form = AnswerCommentForm(request.POST)
		if form.is_valid():
			body = request.POST['body']
			comment = AnswerComment()
			comment.uid = request.user.id
			comment.answer = answer
			comment.body = body.encode('unicode_escape')
			
			comment.save()
		  	# notifying the answer owner
		  	
			if answer.uid != request.user.id:
			    notification = Notification()
			    notification.uid = answer.uid
			    notification.pid = request.user.id
			    notification.qid = answer.question.id
			    notification.aid = answer.id
			    notification.cid = comment.id
			    notification.save()
			    
			    user = User.objects.get(id=answer.uid)
			    subject = 'Comment for your answer'
			    message = """
				Dear {0}<br><br>
				A comment has been posted on your answer.<br>
				Link: {1}<br><br>
				Regards,<br>
				FOSSEE Forums
			    """.format(
				user.username,
				"http://forums.fossee.in/question/" + str(answer.question.id) + "#answer" + str(answer.id)
			    )
			    forums_mail(user.email, subject, message)
		  	# notifying other users in the comment thread
			uids = answer.answercomment_set.filter(answer=answer).values_list('uid', flat=True)
			#getting distinct uids
			uids = set(uids) 
			uids.remove(request.user.id)
			for uid in uids:
			    notification = Notification()
			    notification.uid = uid
			    notification.pid = request.user.id
			    notification.qid = answer.question.id
			    notification.aid = answer.id
			    notification.cid = comment.id
			    notification.save()
			    
			    user = User.objects.get(id=uid)
			    subject = 'Comment has a reply'
			    message = """
				Dear {0}<br><br>
				A reply has been posted on your comment.<br>
				Link: {1}<br><br>
				Regards,<br>
				FOSSEE Forums
			    """.format(
				user.username,
				"http://forums.fossee.in/question/" + str(answer.question.id) + "#answer" + str(answer.id)
			    )
			    forums_mail(user.email, subject, message)

    			return HttpResponseRedirect("/question/" + str(answer.question.id))
	context = {}
    	context.update(csrf(request))
    	context.update({'form':form,
	'question':answer.question,
	'answers':answers})
        return render(request, 'website/templates/get-question.html', context)