diff --git a/db.sqlite3 b/db.sqlite3 index a79e049..7acf0aa 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/news/__pycache__/models.cpython-37.pyc b/news/__pycache__/models.cpython-37.pyc index 4ff7462..d7b7439 100644 Binary files a/news/__pycache__/models.cpython-37.pyc and b/news/__pycache__/models.cpython-37.pyc differ diff --git a/news/__pycache__/urls.cpython-37.pyc b/news/__pycache__/urls.cpython-37.pyc index e2c8ce1..e5ecb0f 100644 Binary files a/news/__pycache__/urls.cpython-37.pyc and b/news/__pycache__/urls.cpython-37.pyc differ diff --git a/news/__pycache__/views.cpython-37.pyc b/news/__pycache__/views.cpython-37.pyc index ecf9d3c..2d16379 100644 Binary files a/news/__pycache__/views.cpython-37.pyc and b/news/__pycache__/views.cpython-37.pyc differ diff --git a/news/migrations/0002_auto_20190917_1255.py b/news/migrations/0002_auto_20190917_1255.py new file mode 100644 index 0000000..1c0ce04 --- /dev/null +++ b/news/migrations/0002_auto_20190917_1255.py @@ -0,0 +1,20 @@ +# Generated by Django 2.2.5 on 2019-09-17 12:55 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('news', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='comment', + name='author', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/news/migrations/0003_auto_20190917_1258.py b/news/migrations/0003_auto_20190917_1258.py new file mode 100644 index 0000000..b3cc510 --- /dev/null +++ b/news/migrations/0003_auto_20190917_1258.py @@ -0,0 +1,30 @@ +# Generated by Django 2.2.5 on 2019-09-17 12:58 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('news', '0002_auto_20190917_1255'), + ] + + operations = [ + migrations.AlterField( + model_name='comment', + name='author', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + migrations.AlterField( + model_name='comment', + name='parent_comment', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='news.Comment'), + ), + migrations.AlterField( + model_name='comment', + name='voters', + field=models.ManyToManyField(blank=True, null=True, related_name='voted_comments', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/news/migrations/0004_auto_20190917_1301.py b/news/migrations/0004_auto_20190917_1301.py new file mode 100644 index 0000000..fa80a8a --- /dev/null +++ b/news/migrations/0004_auto_20190917_1301.py @@ -0,0 +1,25 @@ +# Generated by Django 2.2.5 on 2019-09-17 13:01 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('news', '0003_auto_20190917_1258'), + ] + + operations = [ + migrations.AlterField( + model_name='comment', + name='author', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + migrations.AlterField( + model_name='comment', + name='post', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='news.Post'), + ), + ] diff --git a/news/migrations/__pycache__/0002_auto_20190917_1255.cpython-37.pyc b/news/migrations/__pycache__/0002_auto_20190917_1255.cpython-37.pyc new file mode 100644 index 0000000..f179e43 Binary files /dev/null and b/news/migrations/__pycache__/0002_auto_20190917_1255.cpython-37.pyc differ diff --git a/news/migrations/__pycache__/0003_auto_20190917_1258.cpython-37.pyc b/news/migrations/__pycache__/0003_auto_20190917_1258.cpython-37.pyc new file mode 100644 index 0000000..ef15007 Binary files /dev/null and b/news/migrations/__pycache__/0003_auto_20190917_1258.cpython-37.pyc differ diff --git a/news/migrations/__pycache__/0004_auto_20190917_1301.cpython-37.pyc b/news/migrations/__pycache__/0004_auto_20190917_1301.cpython-37.pyc new file mode 100644 index 0000000..aa3204c Binary files /dev/null and b/news/migrations/__pycache__/0004_auto_20190917_1301.cpython-37.pyc differ diff --git a/news/models.py b/news/models.py index d73ab29..aee717c 100644 --- a/news/models.py +++ b/news/models.py @@ -24,20 +24,39 @@ class Post(models.Model): def top_comments(self): return self.comment_set.all()[:5] + + def flat_comments(self, user): + flat = [] + for comment in self.comment_set.all(): + # flat.append(comment) + flat.extend(comment.flat_children(0, user)) + return flat # return self.comment_set.annotate(ratings_num=count('voters')).order_by('-ratings_num')[:5] # return self.comment_set.order_by('voters_num').all()[:5] class Comment(models.Model): - author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) - post = models.ForeignKey(Post, on_delete=models.CASCADE, null=True) - parent_comment = models.ForeignKey("self", on_delete=models.CASCADE, null=True) + author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True) + post = models.ForeignKey(Post, on_delete=models.CASCADE) + parent_comment = models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True) body = models.CharField(max_length=100) - voters = models.ManyToManyField(User, related_name='voted_comments') + voters = models.ManyToManyField(User, related_name='voted_comments', null=True, blank=True) date = models.DateTimeField('date published') def __str__(self): return self.body + def upvoted_by(User): + return user in self.voters + + def flat_children(self, level, user): + self.level = range(level) + self.upvoted = user in self.voters.all() + self.score = len(self.voters.all()) + flat = [self] + for comment in self.comment_set.all(): + flat.extend(comment.flat_children(level + 1, user)) + return flat + class Player(models.Model): name = models.CharField(max_length=100) post = models.ForeignKey(Post, on_delete=models.CASCADE) diff --git a/news/templates/news/post.html b/news/templates/news/post.html index 2490c16..1d733bf 100644 --- a/news/templates/news/post.html +++ b/news/templates/news/post.html @@ -30,16 +30,23 @@

----Comments----

-{% for comment in post.comment_set.all %} -

-

- {% csrf_token %} - -
- {{ comment.author.username }} - {{ comment.date }} -
- {{ comment.body }} -

+{% for comment in comments %} +

+ {% for n in comment.level %}

{% endfor %} +

+ {% endfor %} {% endblock content %} diff --git a/news/urls.py b/news/urls.py index 8b4710e..644e8e6 100644 --- a/news/urls.py +++ b/news/urls.py @@ -7,6 +7,7 @@ urlpatterns = [ path('', views.index, name='index'), path('', views.post, name='post'), path('submission', views.submission, name='submission'), + path('/comment/', views.comment_with_parent, name='comment_with_parent'), path('/comment', views.comment, name='comment'), path('upvote/', views.upvote, name='upvote'), path('submitted', views.submitted, name='submitted'), diff --git a/news/views.py b/news/views.py index e364eba..e6e864d 100644 --- a/news/views.py +++ b/news/views.py @@ -85,7 +85,7 @@ def index(request): def post(request, post_id): post = get_object_or_404(Post, pk=post_id) - return render(request, 'news/post.html', {'post': post}) + return render(request, 'news/post.html', {'post': post, 'comments': post.flat_comments(request.user) }) @login_required def submission(request): @@ -125,9 +125,15 @@ def submitted(request): return render(request, 'news/submitted.html', {}) def comment(request, post_id): + return comment_with_parent(request, post_id, None) + +def comment_with_parent(request, post_id, comment_id): comment = Comment(author=request.user,date=datetime.today()) comment.post = get_object_or_404(Post, pk=post_id) + if comment_id is not None: + comment.parent_comment = get_object_or_404(Comment, comment_id) comment.body = request.POST['body'] + comment.save() comment.voters.add(request.user) comment.save() return HttpResponseRedirect(reverse('news:post', args=(post_id,))) @@ -136,4 +142,5 @@ def upvote(request, comment_id): comment = get_object_or_404(Comment, pk=comment_id) comment.voters.add(request.user) comment.save() - return render(request, 'news/post.html', {'post': comment.post}) + post = comment.post + return render(request, 'news/post.html', {'post': post, 'comments': post.flat_comments(request.user) })