예제 #1
0
def like_and_dislike(request, is_like):
    main_value = 1 if is_like else 0
    second_value = 0 if is_like else 1

    user = request.user.custom_user
    definition_id = request.POST.get('def_id', None)
    defin = get_object_or_404(Definition, pk=definition_id)

    if defin.estimates.filter(user=user, estimate=main_value).exists():
        # user has already liked this definition
        # remove like/user
        defin.estimates.get(user=user).delete()
    elif defin.estimates.filter(user=user, estimate=second_value).exists():
        defin.estimates.get(user=user).delete()
        Rating(definition=defin, user=user, estimate=main_value).save()
        if defin.author.id != user.id:
            Notification(date_creation=datetime.now(), action_type=ACTION_TYPES[main_value][0], user=defin.author,
                         models_id="%s%s %s%s" % (USER, user.id, DEF, defin.id)).save()
    else:
        # add a new like for a company
        Rating(definition=defin, user=user, estimate=main_value).save()
        if defin.author.id != user.id:
            Notification(date_creation=datetime.now(), action_type=ACTION_TYPES[main_value][0], user=defin.author,
                         models_id="%s%s %s%s" % (USER, user.id, DEF, defin.id)).save()

    return {'likes_count': defin.get_likes(), 'dislikes_count': defin.get_dislikes(), 'is_liked': is_like}
예제 #2
0
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('/')
예제 #3
0
def notification_new_or_edit(request, pk=None):
    template = "website/notification_{0}.html"
    if pk:
        template = str.format(template, "edit")
        notification = get_object_or_404(Notification, pk=pk)
        if notification.user != request.user:
            return HttpResponseForbidden()
    else:
        template = str.format(template, "new")
        notification = Notification(user=request.user)

    if request.method == "POST":
        form = NotificationForm(request.user, request.POST, instance=notification)
        if form.is_valid():
            coordinates = GeoCoder.get_coordinates_from_address(
                Address(form.cleaned_data["address"],
                        form.cleaned_data["city"],
                        form.cleaned_data["state"],
                        form.cleaned_data["zipcode"]
                        )
            )
            notification = form.save(commit=False)
            notification.latitude = str(coordinates.latitude)
            notification.longitude = str(coordinates.longitude)
            notification.user = request.user
            notification.save()

            for group in form.cleaned_data['groups']:
                notification.groups.add(group)
            notification.save()

            notification.notify()
            return redirect('website.views.notification_detail', pk=notification.pk)
    else:
        form = NotificationForm(request.user, instance=notification)
    return render(request, template, {'form': form})
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('/') 
예제 #5
0
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) + "#")
예제 #7
0
class NotificationTest(TestCase):
    USERNAME_1 = "notificationUser1"
    EMAIL_ADDRESS_1 = "*****@*****.**"
    USERNAME_2 = "notificationUser2"
    EMAIL_ADDRESS_2 = "*****@*****.**"

    GROUP_NAME_1 = "My Group 1"
    GROUP_NAME_2 = "My Group 2"

    ZONE_NAME_1 = "My Notification Zone 1"
    ZONE_NAME_2 = "My Notification Zone 2"
    RADIUS = "1.0"

    TITLE = "My Notification"
    ADDRESS = "6220 Culebra Rd"
    CITY = "San Antonio"
    STATE = "TX"
    ZIPCODE = "78238"
    DATE = "1970-01-01"
    TIME = "12:00 AM"
    LATITUDE = 29.4522704
    LONGITUDE = -98.6101913

    notification = Notification()
    group_1 = Group()
    group_2 = Group()
    user_1 = User()
    user_2 = User()

    def test_notification_creation(self):
        self.given_groups_with_users()
        self.when_the_notification_is_created()
        self.then_the_notification_has_expected_values()

    def test_notify(self):
        self.given_groups_with_users()
        self.when_the_notification_is_created()
        self.when_the_notification_is_sent()
        self.then_the_notifications_are_sent()

    def given_users(self):
        self.user_1 = User.objects.create(username=self.USERNAME_1, password="******", email=self.EMAIL_ADDRESS_1)
        self.user_2 = User.objects.create(username=self.USERNAME_2, password="******", email=self.EMAIL_ADDRESS_2)
        NotificationZone.objects.create(
            user=self.user_1,
            name=self.ZONE_NAME_1,
            address=self.ADDRESS,
            city=self.CITY,
            state=self.STATE,
            zipcode=self.ZIPCODE,
            latitude=self.LATITUDE,
            longitude=self.LONGITUDE,
            radius=self.RADIUS
        )
        NotificationZone.objects.create(
            user=self.user_2,
            name=self.ZONE_NAME_2,
            address=self.ADDRESS,
            city=self.CITY,
            state=self.STATE,
            zipcode=self.ZIPCODE,
            latitude=self.LATITUDE,
            longitude=self.LONGITUDE,
            radius=self.RADIUS
        )

    def given_groups_with_users(self):
        self.given_users()
        self.group_1 = Group.objects.create(name=self.GROUP_NAME_1)
        self.group_2 = Group.objects.create(name=self.GROUP_NAME_2)
        self.group_1.users.add(self.user_1, self.user_2)

    def when_the_notification_is_created(self):
        self.notification = Notification.objects.create(
            user=self.user_1,
            title=self.TITLE,
            address=self.ADDRESS,
            city=self.CITY,
            state=self.STATE,
            zipcode=self.ZIPCODE,
            date=self.DATE,
            time=self.TIME,
            latitude=self.LATITUDE,
            longitude=self.LONGITUDE
        )
        self.notification.groups.add(self.group_1, self.group_2)

    def when_the_notification_is_sent(self):
        self.notification.notify()

    def then_the_notification_has_expected_values(self):
        notification_groups = self.notification.groups.all()
        self.assertTrue(isinstance(self.notification, Notification))
        self.assertEqual(self.notification.user.username, self.USERNAME_1)
        self.assertEqual(notification_groups[0].name, self.GROUP_NAME_1)
        self.assertEqual(notification_groups[1].name, self.GROUP_NAME_2)
        self.assertEqual(self.notification.title, self.TITLE)
        self.assertEqual(self.notification.address, self.ADDRESS)
        self.assertEqual(self.notification.city, self.CITY)
        self.assertEqual(self.notification.state, self.STATE)
        self.assertEqual(self.notification.zipcode, self.ZIPCODE)
        self.assertEqual(self.notification.date, self.DATE)
        self.assertEqual(self.notification.time, self.TIME)

    def then_the_notifications_are_sent(self):
        self.assertEqual(len(mail.outbox), 2)

        # Verify that the subject of the first message is correct.
        self.assertEqual(mail.outbox[0].to[0], self.user_1.email)
        self.assertEqual(mail.outbox[1].to[0], self.user_2.email)
예제 #8
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('/') 
예제 #9
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)