diff options
Diffstat (limited to 'website')
-rw-r--r-- | website/forms.py | 2 | ||||
-rw-r--r-- | website/models.py | 40 | ||||
-rw-r--r-- | website/templatetags/count_tags.py | 14 | ||||
-rw-r--r-- | website/templatetags/notify.py | 7 | ||||
-rw-r--r-- | website/templatetags/sidebar_tags.py | 2 | ||||
-rw-r--r-- | website/urls.py | 8 | ||||
-rw-r--r-- | website/views.py | 220 |
7 files changed, 234 insertions, 59 deletions
diff --git a/website/forms.py b/website/forms.py index 3ca48da..f1e29be 100644 --- a/website/forms.py +++ b/website/forms.py @@ -71,6 +71,6 @@ class NewQuestionForm(forms.Form): title = forms.CharField(max_length=200) body = forms.CharField(widget=forms.Textarea()) -class ReplyQuesitionForm(forms.Form): +class AnswerQuesitionForm(forms.Form): question = forms.IntegerField(widget=forms.HiddenInput()) body = forms.CharField(widget=forms.Textarea()) diff --git a/website/models.py b/website/models.py index 52575b8..c280d2d 100644 --- a/website/models.py +++ b/website/models.py @@ -15,19 +15,46 @@ class Question(models.Model): body = models.TextField() date_created = models.DateTimeField(auto_now_add=True) date_modified = models.DateTimeField(auto_now=True) - views = models.IntegerField() + views = models.IntegerField(default=1) + # votes = models.IntegerField(default=0) def user(self): user = User.objects.get(id=self.uid) return user.username +class QuestionVote(models.Model): + uid = models.IntegerField() + question = models.ForeignKey(Question) -class Reply(models.Model): +class QuestionComment(models.Model): + uid = models.IntegerField() + question = models.ForeignKey(Question) + body = models.TextField() + date_created = models.DateTimeField(auto_now_add=True) + date_modified = models.DateTimeField(auto_now=True) + +class Answer(models.Model): uid = models.IntegerField() question = models.ForeignKey(Question) body = models.TextField() date_created = models.DateTimeField(auto_now_add=True) date_modified = models.DateTimeField(auto_now=True) + # votes = models.IntegerField(default=0) + + def user(self): + user = User.objects.get(id=self.uid) + return user.username + +class AnswerVote(models.Model): + uid = models.IntegerField() + answer = models.ForeignKey(Answer) + +class AnswerComment(models.Model): + uid = models.IntegerField() + answer = models.ForeignKey(Answer) + body = models.TextField() + date_created = models.DateTimeField(auto_now_add=True) + date_modified = models.DateTimeField(auto_now=True) def user(self): user = User.objects.get(id=self.uid) @@ -37,8 +64,13 @@ class Notification(models.Model): uid = models.IntegerField() pid = models.IntegerField() qid = models.IntegerField() - rid = models.IntegerField() + aid = models.IntegerField(default=0) + cid = models.IntegerField(default=0) date_created = models.DateTimeField(auto_now_add=True) + + def poster(self): + user = User.objects.get(id=self.pid) + return user.username # CDEEP database created using inspectdb arg of manage.py class TutorialDetails(models.Model): @@ -47,6 +79,7 @@ class TutorialDetails(models.Model): tutorial_name = models.CharField(max_length=600L) tutorial_level = models.CharField(max_length=400L) order_code = models.IntegerField() + class Meta: db_table = 'tutorial_details' @@ -72,5 +105,6 @@ class TutorialResources(models.Model): cvideo_version = models.IntegerField() hit_count = models.BigIntegerField() request_exception = models.TextField() + class Meta: db_table = 'tutorial_resources' diff --git a/website/templatetags/count_tags.py b/website/templatetags/count_tags.py index 33aff7f..735675a 100644 --- a/website/templatetags/count_tags.py +++ b/website/templatetags/count_tags.py @@ -1,6 +1,6 @@ from django import template -from website.models import Question, Reply +from website.models import Question, Answer register = template.Library() @@ -59,3 +59,15 @@ register.filter('mul', mul) def div(value, arg=1): return value / int(arg) register.filter('div', div) + +# retriving total number of questions +def total_question_count(): + count = Question.objects.all().count() + return count +register.simple_tag(total_question_count) + +# retriving total number of answers +def total_answer_count(): + count = Answer.objects.all().count() + return count +register.simple_tag(total_answer_count) diff --git a/website/templatetags/notify.py b/website/templatetags/notify.py index 4c6692a..44465ea 100644 --- a/website/templatetags/notify.py +++ b/website/templatetags/notify.py @@ -1,17 +1,17 @@ from django import template -from website.models import Question, Reply, Notification +from website.models import Question, Answer, Notification register = template.Library() def get_notification(nid): notification = Notification.objects.get(pk=nid) question = Question.objects.get(pk=notification.qid); - reply = Reply.objects.get(pk=notification.rid) + answer = Answer.objects.get(pk=notification.aid) context = { 'notification': notification, 'question': question, - 'reply': reply, + 'answer': answer, } return context register.inclusion_tag('website/templates/notify.html')(get_notification) @@ -20,4 +20,3 @@ def notification_count(user_id): count = Notification.objects.filter(uid=user_id).count() return count register.simple_tag(notification_count) - diff --git a/website/templatetags/sidebar_tags.py b/website/templatetags/sidebar_tags.py index 8b7efb9..ac3d0d8 100644 --- a/website/templatetags/sidebar_tags.py +++ b/website/templatetags/sidebar_tags.py @@ -1,6 +1,6 @@ from django import template -from website.models import Question, Reply +from website.models import Question, Answer register = template.Library() diff --git a/website/urls.py b/website/urls.py index 9288e1e..75a9dbc 100644 --- a/website/urls.py +++ b/website/urls.py @@ -3,7 +3,8 @@ from django.conf.urls import patterns, include, url urlpatterns = patterns('', url(r'^$', 'website.views.home', name='home'), url(r'^question/(?P<question_id>\d+)/$', 'website.views.get_question', name='get_question'), - url(r'^question-reply/$', 'website.views.question_reply', name='question_reply'), + url(r'^question-answer/$', 'website.views.question_answer', name='question_answer'), + url(r'^answer-comment/$', 'website.views.answer_comment', name='answer_comment'), url(r'^filter/(?P<category>[^/]+)/$', 'website.views.filter', name='filter'), url(r'^filter/(?P<category>[^/]+)/(?P<tutorial>[^/]+)/$', 'website.views.filter', name='filter'), url(r'^filter/(?P<category>[^/]+)/(?P<tutorial>[^/]+)/(?P<minute_range>[^/]+)/$', 'website.views.filter', name='filter'), @@ -11,7 +12,7 @@ urlpatterns = patterns('', url(r'^new-question/$', 'website.views.new_question', name='new_question'), url(r'^user/(?P<user_id>\d+)/notifications/$', 'website.views.user_notifications', name='user_notifications'), url(r'^user/(?P<user_id>\d+)/questions/$', 'website.views.user_questions', name='user_questions'), - url(r'^user/(?P<user_id>\d+)/replies/$', 'website.views.user_replies', name='user_replies'), + url(r'^user/(?P<user_id>\d+)/answers/$', 'website.views.user_answers', name='user_answers'), url(r'^clear-notifications/$', 'website.views.clear_notifications', name='clear_notifications'), url(r'^search/$', 'website.views.search', name='search'), @@ -20,7 +21,8 @@ urlpatterns = patterns('', url(r'^ajax-duration/$', 'website.views.ajax_duration', name='ajax_duration'), url(r'^ajax-question-update/$', 'website.views.ajax_question_update', name='ajax_question_update'), url(r'^ajax-details-update/$', 'website.views.ajax_details_update', name='ajax_details_update'), - url(r'^ajax-reply-update/$', 'website.views.ajax_reply_update', name='ajax_reply_update'), + url(r'^ajax-answer-update/$', 'website.views.ajax_answer_update', name='ajax_answer_update'), + url(r'^ajax-answer-comment-update/$', 'website.views.ajax_answer_comment_update', name='ajax_answer_comment_update'), url(r'^ajax-similar-questions/$', 'website.views.ajax_similar_questions', name='ajax_similar_questions'), url(r'^ajax-notification-remove/$', 'website.views.ajax_notification_remove', name='ajax_notification_remove'), url(r'^ajax-keyword-search/$', 'website.views.ajax_keyword_search', name='ajax_keyword_search'), diff --git a/website/views.py b/website/views.py index f8cb04b..81283f7 100644 --- a/website/views.py +++ b/website/views.py @@ -7,10 +7,11 @@ from django.views.decorators.csrf import csrf_exempt from django.contrib.auth.decorators import login_required from django.db.models import Q from django.core.mail import EmailMultiAlternatives - -from website.models import Question, Reply, Notification, TutorialDetails, TutorialResources -from website.forms import NewQuestionForm, ReplyQuesitionForm +from website.models import Question, Answer, Notification, TutorialDetails, TutorialResources, AnswerComment +from website.forms import NewQuestionForm, AnswerQuesitionForm from website.helpers import get_video_info +from django.contrib.auth import get_user_model +User = get_user_model() admins = ( 9, 4376, 4915, 14595, 12329, 22467, 5518 @@ -34,6 +35,14 @@ categories = ( ) def home(request): + questions = Question.objects.all().order_by('date_created').reverse()[:10] + context = { + 'categories': categories, + 'questions': questions + } + return render(request, "website/templates/index.html", context) + +def recent_questions(request): marker = 0 if 'marker' in request.GET: marker = int(request.GET['marker']) @@ -51,11 +60,11 @@ def home(request): def get_question(request, question_id=None): question = get_object_or_404(Question, id=question_id) - replies = question.reply_set.all() - form = ReplyQuesitionForm() + answers = question.answer_set.all() + form = AnswerQuesitionForm() context = { 'question': question, - 'replies': replies, + 'answers': answers, 'form': form } context.update(csrf(request)) @@ -65,27 +74,119 @@ def get_question(request, question_id=None): return render(request, 'website/templates/get-question.html', context) @login_required -def question_reply(request): +def question_answer(request): if request.method == 'POST': - form = ReplyQuesitionForm(request.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) - reply = Reply() - reply.uid = request.user.id - reply.question = question - reply.body = body - reply.save() + answer = Answer() + answer.uid = request.user.id + answer.question = question + answer.body = body + answer.save() if question.uid != request.user.id: notification = Notification() notification.uid = question.uid notification.pid = request.user.id notification.qid = qid - notification.rid = reply.id + notification.aid = answer.id notification.save() - return HttpResponseRedirect('/question/'+str(qid)) + + 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)) + +@login_required +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 + 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 HttpResponse(message) + return HttpResponseRedirect("/question/" + str(answer.question.id) + "#") def filter(request, category=None, tutorial=None, minute_range=None, second_range=None): context = { @@ -94,6 +195,7 @@ def filter(request, category=None, tutorial=None, minute_range=None, second_ran 'minute_range': minute_range, 'second_range': second_range } + if category and tutorial and minute_range and second_range: questions = Question.objects.filter(category=category).filter(tutorial=tutorial).filter(minute_range=minute_range).filter(second_range=second_range) elif tutorial is None: @@ -128,7 +230,7 @@ def new_question(request): question.body = cleaned_data['body'] question.views= 1 question.save() - + # Sending email when a new question is asked subject = 'New Forum Question' message = """ @@ -141,27 +243,28 @@ def new_question(request): question.tutorial, 'http://forums.spoken-tutorial.org/question/'+str(question.id) ) - + email = EmailMultiAlternatives( subject,'', 'forums', ['team@spoken-tutorial.org', 'team@fossee.in'], 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('/') else: form = NewQuestionForm() - + context = { 'form': form } context.update(csrf(request)) return render(request, 'website/templates/new-question.html', context) +# Notification Section @login_required def user_questions(request, user_id): marker = 0 @@ -172,7 +275,7 @@ def user_questions(request, user_id): total = Question.objects.filter(uid=user_id).count() total = int(total - (total % 10 - 10)) questions = Question.objects.filter(uid=user_id).order_by('date_created').reverse()[marker:marker+10] - + context = { 'questions': questions, 'total': total, @@ -182,21 +285,21 @@ def user_questions(request, user_id): return HttpResponse("go away") @login_required -def user_replies(request, user_id): +def user_answers(request, user_id): marker = 0 if 'marker' in request.GET: marker = int(request.GET['marker']) if str(user_id) == str(request.user.id): - total = Reply.objects.filter(uid=user_id).count() + total = Answer.objects.filter(uid=user_id).count() total = int(total - (total % 10 - 10)) - replies =Reply.objects.filter(uid=user_id).order_by('date_created').reverse()[marker:marker+10] + answers =Answer.objects.filter(uid=user_id).order_by('date_created').reverse()[marker:marker+10] context = { - 'replies': replies, + 'answers': answers, 'total': total, 'marker': marker } - return render(request, 'website/templates/user-replies.html', context) + return render(request, 'website/templates/user-answers.html', context) return HttpResponse("go away") @login_required @@ -223,7 +326,7 @@ def search(request): # Ajax Section # All the ajax views go below @csrf_exempt -def ajaX_category(request): +def ajax_category(request): context = { 'categories': categories } @@ -252,12 +355,12 @@ def ajax_duration(request): Q(tutorial_detail_id=video_detail.id), Q(language='English') ) - video_path = '/Sites/spoken_tutorial_org/sites/default/files/{0}'.format( - video_resource.tutorial_video - ) - #video_path = '/home/cheese/test-video.ogv' + #video_path = '/Sites/spoken_tutorial_org/sites/default/files/{0}'.format( + # video_resource.tutorial_video + #) + video_path = '/home/cheese/test-video.ogv' video_info = get_video_info(video_path) - + # convert minutes to 1 if less than 0 # convert seconds to nearest upper 10th number eg(23->30) minutes = video_info['minutes'] @@ -305,25 +408,38 @@ def ajax_details_update(request): return HttpResponse("saved") @csrf_exempt -def ajax_reply_update(request): +def ajax_answer_update(request): if request.method == 'POST': - rid = request.POST['reply_id'] - body = request.POST['reply_body'] - reply= get_object_or_404(Reply, pk=rid) - if reply: - if reply.uid == request.user.id or request.user.id in admins: - reply.body = body - reply.save() + aid = request.POST['answer_id'] + body = request.POST['answer_body'] + answer= get_object_or_404(Answer, pk=aid) + if answer: + if answer.uid == request.user.id or request.user.id in admins: + answer.body = body + answer.save() return HttpResponse("saved") @csrf_exempt +def ajax_answer_comment_update(request): + if request.method == "POST": + comment_id = request.POST["comment_id"] + comment_body = request.POST["comment_body"] + comment = get_object_or_404(AnswerComment, pk=comment_id) + if comment: + if comment.uid == request.user.id or request.user.id in admins: + comment.body = comment_body + comment.save() + return HttpResponse("saved") + + +@csrf_exempt def ajax_similar_questions(request): if request.method == 'POST': category = request.POST['category'] tutorial = request.POST['tutorial'] minute_range = request.POST['minute_range'] second_range = request.POST['second_range'] - + # add more filtering when the forum grows questions = Question.objects.filter(category=category).filter(tutorial=tutorial) context = { @@ -372,18 +488,30 @@ def ajax_time_search(request): if category != 'None': questions = Question.objects.filter(category=category) - if tutorial != 'None': questions = questions.filter(tutorial=tutorial) - if minute_range != 'None': questions = questions.filter(minute_range=minute_range) - if second_range != 'None': questions = questions.filter(second_range=second_range) - + context = { 'questions': questions } - return render(request, 'website/templates/ajax-time-search.html', context) + +@csrf_exempt +def ajax_vote(request): + #for future use + pass + +def forums_mail(to = '', subject='', message=''): + # Start of email send + email = EmailMultiAlternatives( + subject,'', 'forums', + [to], + headers={"Content-type":"text/html;charset=iso-8859-1"} + ) + email.attach_alternative(message, "text/html") + email.send(fail_silently=True) + # End of email send |