diff --git a/db.sqlite3 b/db.sqlite3 index 153ca49..a79e049 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 96f9289..4ff7462 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 a5938bb..e2c8ce1 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 ada0fde..ecf9d3c 100644 Binary files a/news/__pycache__/views.cpython-37.pyc and b/news/__pycache__/views.cpython-37.pyc differ diff --git a/news/migrations/0001_initial.py b/news/migrations/0001_initial.py index 0ce5620..e7f999e 100644 --- a/news/migrations/0001_initial.py +++ b/news/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.5 on 2019-09-10 14:19 +# Generated by Django 2.2.5 on 2019-09-13 13:56 from django.conf import settings from django.db import migrations, models @@ -23,6 +23,7 @@ class Migration(migrations.Migration): ('url', models.CharField(max_length=200)), ('date', models.DateTimeField(verbose_name='date published')), ('state', models.IntegerField(default=0)), + ('style', models.IntegerField(default=0)), ('image_url', models.CharField(max_length=100)), ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), ], @@ -49,11 +50,11 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('body', models.CharField(max_length=100)), - ('votes', models.IntegerField(default=0)), ('date', models.DateTimeField(verbose_name='date published')), ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), ('parent_comment', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='news.Comment')), ('post', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='news.Post')), + ('voters', models.ManyToManyField(related_name='voted_comments', to=settings.AUTH_USER_MODEL)), ], ), ] diff --git a/news/migrations/0002_post_style.py b/news/migrations/0002_post_style.py deleted file mode 100644 index ecc8e2f..0000000 --- a/news/migrations/0002_post_style.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 2.2.5 on 2019-09-11 14:37 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('news', '0001_initial'), - ] - - operations = [ - migrations.AddField( - model_name='post', - name='style', - field=models.IntegerField(default=0), - ), - ] diff --git a/news/migrations/__pycache__/0001_initial.cpython-37.pyc b/news/migrations/__pycache__/0001_initial.cpython-37.pyc index 012c39e..ba6cfaa 100644 Binary files a/news/migrations/__pycache__/0001_initial.cpython-37.pyc and b/news/migrations/__pycache__/0001_initial.cpython-37.pyc differ diff --git a/news/migrations/__pycache__/0002_post_style.cpython-37.pyc b/news/migrations/__pycache__/0002_post_style.cpython-37.pyc deleted file mode 100644 index c27697a..0000000 Binary files a/news/migrations/__pycache__/0002_post_style.cpython-37.pyc and /dev/null differ diff --git a/news/migrations/__pycache__/__init__.cpython-37.pyc b/news/migrations/__pycache__/__init__.cpython-37.pyc index d19198b..6521513 100644 Binary files a/news/migrations/__pycache__/__init__.cpython-37.pyc and b/news/migrations/__pycache__/__init__.cpython-37.pyc differ diff --git a/news/models.py b/news/models.py index 185946d..d73ab29 100644 --- a/news/models.py +++ b/news/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.contrib.auth.models import User from django.conf import settings from enum import Enum @@ -22,17 +23,20 @@ class Post(models.Model): return self.title def top_comments(self): - return self.comment_set.all()[:3] + return self.comment_set.all()[:5] + # 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) body = models.CharField(max_length=100) - votes = models.IntegerField(default=0) + voters = models.ManyToManyField(User, related_name='voted_comments') date = models.DateTimeField('date published') def __str__(self): - return self.content + return self.body class Player(models.Model): name = models.CharField(max_length=100) diff --git a/news/static/media/8154d123-7ef7-4e0d-851e-1ee46e5f295d b/news/static/media/8154d123-7ef7-4e0d-851e-1ee46e5f295d new file mode 100644 index 0000000..6e0bf94 Binary files /dev/null and b/news/static/media/8154d123-7ef7-4e0d-851e-1ee46e5f295d differ diff --git a/news/static/media/96d8ba65-c5f8-48e6-8ad4-e7befef9373a b/news/static/media/96d8ba65-c5f8-48e6-8ad4-e7befef9373a new file mode 100644 index 0000000..6e0bf94 Binary files /dev/null and b/news/static/media/96d8ba65-c5f8-48e6-8ad4-e7befef9373a differ diff --git a/news/templates/news/post.html b/news/templates/news/post.html index 4e56466..2490c16 100644 --- a/news/templates/news/post.html +++ b/news/templates/news/post.html @@ -17,7 +17,7 @@
-
+ {% csrf_token %}

Add comment

@@ -32,6 +32,10 @@

----Comments----

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

+ + {% csrf_token %} + +

{{ comment.author.username }} - {{ comment.date }}
{{ comment.body }} diff --git a/news/urls.py b/news/urls.py index b1376b1..8b4710e 100644 --- a/news/urls.py +++ b/news/urls.py @@ -8,6 +8,7 @@ urlpatterns = [ path('', views.post, name='post'), path('submission', views.submission, name='submission'), path('/comment', views.comment, name='comment'), + path('upvote/', views.upvote, name='upvote'), path('submitted', views.submitted, name='submitted'), path('account', views.account, name='account'), path('signin', views.signin, name='signin'), diff --git a/news/views.py b/news/views.py index 3aca033..e364eba 100644 --- a/news/views.py +++ b/news/views.py @@ -22,7 +22,7 @@ def register(request): if f.is_valid(): f.save() messages.success(request, 'Account created successfully') - return redirect('register') + return HttpResponseRedirect(reverse('news:index')) else: f = CustomUserCreationForm() @@ -104,9 +104,9 @@ def submission(request): post.image_url = filename if request.user.is_staff: - post.state = PostState.PUBLISHED + post.state = PostState.PUBLISHED.value else: - post.state = PostState.USER_SUBMITTED + post.state = PostState.USER_SUBMITTED.value post.save() return HttpResponseRedirect(reverse('news:submitted')) @@ -128,5 +128,12 @@ def comment(request, post_id): comment = Comment(author=request.user,date=datetime.today()) comment.post = get_object_or_404(Post, pk=post_id) comment.body = request.POST['body'] + comment.voters.add(request.user) comment.save() return HttpResponseRedirect(reverse('news:post', args=(post_id,))) + +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})