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 %} --
- {{ comment.author.username }} - {{ comment.date }} -+ {% for n in comment.level %}