Skip to content

Commit

Permalink
Merge pull request #191 from farziengineer/master
Browse files Browse the repository at this point in the history
Simple Comments section on the issues page
  • Loading branch information
Sean Auriti committed Jun 5, 2017
2 parents b553c68 + 3bf3924 commit 525f927
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 2 deletions.
1 change: 1 addition & 0 deletions bugheist/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
url(r'^stats/$', StatsDetailView.as_view()),
url(r'^favicon\.ico$', favicon_view),
url(r'^sendgrid_webhook/$', csrf_exempt(InboundParseWebhookView.as_view()), name='inbound_event_webhook_callback'),
url(r'^post/(?P<pk>\d+)/comment/$',website.views.add_comment_to_post, name='add_comment_to_post'),

) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

9 changes: 8 additions & 1 deletion website/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django import forms
from .models import Issue, InviteFriend, UserProfile
from .models import Issue, InviteFriend, UserProfile, Comment


class IssueEditForm(forms.ModelForm):
Expand All @@ -24,3 +24,10 @@ class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('user_avatar',)


class CommentForm(forms.ModelForm):

class Meta:
model = Comment
fields = ('text',)
35 changes: 35 additions & 0 deletions website/migrations/0025_auto_20170605_1909.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2017-06-05 19:09
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import website.models


class Migration(migrations.Migration):

dependencies = [
('website', '0024_userprofile'),
]

operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('author', models.CharField(max_length=200)),
('author_url', models.CharField(max_length=200)),
('text', models.TextField()),
('created_date', models.DateTimeField(default=django.utils.timezone.now)),
('approved_comment', models.BooleanField(default=False)),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='website.Issue')),
],
),
migrations.AlterField(
model_name='userprofile',
name='user_avatar',
field=models.ImageField(blank=True, null=True, upload_to=website.models.user_images_path),
),
]
19 changes: 19 additions & 0 deletions website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from PIL import Image
from django.db.models import Count
from colorthief import ColorThief
from django.utils import timezone


class Domain(models.Model):
Expand Down Expand Up @@ -307,3 +308,21 @@ def create_profile(sender, **kwargs):
profile.save()

post_save.connect(create_profile, sender=User)


class Comment(models.Model):
post = models.ForeignKey('Issue', related_name='comments')
author = models.CharField(max_length=200)
author_url = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)

def approve(self):
self.approved_comment = True
self.save()

def __str__(self):
return self.text


7 changes: 7 additions & 0 deletions website/templates/add_comment_to_post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% block content %}
<h1>New comment</h1>
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Send</button>
</form>
{% endblock %}
16 changes: 16 additions & 0 deletions website/templates/issue.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,20 @@ <h2 class="page-header"> {{object.description}} <span class="pull-right"><i clas
<hr>
</div>
</div>

<h3>Comments</h3>

{% for comment in all_comment %}
<hr>
<div class="comment">
<div class="date">{{ comment.created_date }}</div>
<strong><a href="{{ comment.author_url }}">{{ comment.author }}</a></strong>
<p>{{ comment.text|linebreaks }}</p>
</div>
{% empty %}
<p>Be the first to comment.</p>
{% endfor %}
<a class="btn btn-default" href="{% url 'add_comment_to_post' pk=issue.pk %}">Add comment</a>


{% endblock %}
17 changes: 16 additions & 1 deletion website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import os
import json
from user_agents import parse
from .forms import IssueEditForm, FormInviteFriend, UserProfileForm
from .forms import IssueEditForm, FormInviteFriend, UserProfileForm, CommentForm
import random
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
Expand Down Expand Up @@ -434,6 +434,7 @@ def get_context_data(self, **kwargs):
context['os_version'] = user_agent.os.version_string
context['users_score'] = Points.objects.filter(user=self.object.user).aggregate(total_score=Sum('score')).values()[0]
context['issue_count'] = Issue.objects.filter(url__contains=self.object.domain_name).count()
context['all_comment'] = self.object.comments.all
return context


Expand Down Expand Up @@ -644,3 +645,17 @@ def form_valid(self, form):
return HttpResponseRedirect(self.success_url)


def add_comment_to_post(request, pk):
post = get_object_or_404(Issue, pk=pk)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.author = request.user.username
comment.author_url = os.path.join('/profile/',request.user.username)
comment.post = post
comment.save()
return redirect(os.path.join('/issue',str(pk)))
else:
form = CommentForm()
return render(request, 'add_comment_to_post.html', {'form': form})

0 comments on commit 525f927

Please sign in to comment.