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('/')
Exemple #3
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('/')