diff --git a/Interviewbook/admin.py b/Interviewbook/admin.py index 5984657..f8858cc 100644 --- a/Interviewbook/admin.py +++ b/Interviewbook/admin.py @@ -2,6 +2,15 @@ from .models import * # Register your models here. +class CommentAdmin(admin.ModelAdmin): + list_display = ('username', 'body', 'response', 'created_on', 'active') + list_filter = ('active', 'created_on') + search_fields = ('username', 'body') + actions = ['disapprove_comment'] + def disapprove_comment(self, request, queryset): + queryset.update(active=False) + admin.site.register(InterviewResponse) admin.site.register(Company) +admin.site.register(Comment) diff --git a/Interviewbook/forms.py b/Interviewbook/forms.py index 3de6b0c..d7fc5d2 100644 --- a/Interviewbook/forms.py +++ b/Interviewbook/forms.py @@ -1,3 +1,4 @@ +from django import forms from django.forms import ModelForm, Textarea from .models import * from django.contrib.auth.models import User @@ -21,3 +22,9 @@ class CompanyForm(ModelForm): class Meta: model = Company exclude =() + +class CommentForm(forms.ModelForm): + + class Meta: + model = Comment + fields = ('body', ) \ No newline at end of file diff --git a/Interviewbook/models.py b/Interviewbook/models.py index bb82dd9..e7ad7ba 100644 --- a/Interviewbook/models.py +++ b/Interviewbook/models.py @@ -26,3 +26,18 @@ def __str__(self): def increase(self): self.hits += 1 self.save() + +class Comment(models.Model): + response = models.ForeignKey(InterviewResponse, on_delete=models.CASCADE, related_name='comments') + username = models.CharField(max_length=200) + body = models.TextField(null=True) + created_on = models.DateTimeField(auto_now_add=True) + active = models.BooleanField(default=True) + replies = models + + class Meta: + ordering = ['created_on'] + + def __str__(self): + return 'Comment {} by {}'.format(self.body, self.username) + diff --git a/Interviewbook/templates/Interviewbook/response.html b/Interviewbook/templates/Interviewbook/response.html index 0d71f2a..0fba6be 100644 --- a/Interviewbook/templates/Interviewbook/response.html +++ b/Interviewbook/templates/Interviewbook/response.html @@ -31,7 +31,46 @@ No of rounds : {{response.rounds}} +
+
+ +

{{ comments.count }} comments

+ + {% for comment in comments %} +
+

+ {{ comment.username }} + + {{ comment.created_on }} + +

+ {{ comment.body | linebreaks }} +
+ {% endfor %} +
+
+
+
+ {% if not user.is_authenicated %} + Login or Signup to comment + {% endif %} + {% if new_comment %} + + {% else %} +

Leave a comment

+
+ {{ comment_form.as_p }} + {% csrf_token %} + +
+ {% endif %} +
+
+ + {% endblock %} diff --git a/Interviewbook/views.py b/Interviewbook/views.py index b3efd41..30e57cd 100644 --- a/Interviewbook/views.py +++ b/Interviewbook/views.py @@ -67,9 +67,26 @@ def ListResponsesbyCompany(request): def viewResponse(request, response_id): - response = get_object_or_404(InterviewResponse, id=response_id) - response.increase() - return render(request, 'Interviewbook/response.html', {'response': response}) + # response = get_object_or_404(InterviewResponse, id=response_id) + #response.increase() + # return render(request, 'Interviewbook/response.html', {'response': response}) + response = get_object_or_404(InterviewResponse, id=response_id) + comments = response.comments.filter(active=True) + comment_form = CommentForm() + new_comment = None + new_reply = None + if request.method == 'POST': + comment_form = CommentForm(data=request.POST) + if comment_form.is_valid(): + if not (request.user.is_authenticated): + return render(request, 'Interviewbook/login.html') + new_comment = comment_form.save(commit=False) + new_comment.response = response + new_comment.username = request.user.username + new_comment.created_on = timezone.now() + new_comment.active = True + new_comment = comment_form.save() + return render(request, 'Interviewbook/response.html', { 'response': response, 'comments': comments,'new_comment':new_comment, 'comment_form':comment_form }) @login_required(login_url='login') def updateResponse(request, response_id):