diff --git a/db.sqlite3 b/db.sqlite3 index 7acf0aa..f6f97dd 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/news/__pycache__/choices.cpython-37.pyc b/news/__pycache__/choices.cpython-37.pyc index d2e07d7..6271ed9 100644 Binary files a/news/__pycache__/choices.cpython-37.pyc and b/news/__pycache__/choices.cpython-37.pyc differ diff --git a/news/__pycache__/forms.cpython-37.pyc b/news/__pycache__/forms.cpython-37.pyc index a957475..9fc2b5e 100644 Binary files a/news/__pycache__/forms.cpython-37.pyc and b/news/__pycache__/forms.cpython-37.pyc differ diff --git a/news/__pycache__/models.cpython-37.pyc b/news/__pycache__/models.cpython-37.pyc index d7b7439..1307b32 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 e5ecb0f..5009421 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 2d16379..66cb150 100644 Binary files a/news/__pycache__/views.cpython-37.pyc and b/news/__pycache__/views.cpython-37.pyc differ diff --git a/news/forms.py b/news/forms.py index 17e73b8..f4af69c 100644 --- a/news/forms.py +++ b/news/forms.py @@ -3,14 +3,22 @@ from django.contrib.auth.models import User from django import forms from django.core.exceptions import ValidationError from .choices import * +import time +from datetime import datetime, date, time, timedelta +# from datetime import datetime class PostForm(forms.Form): - title = forms.CharField(label='Title', max_length=200) - body = forms.CharField(label='Optional text', max_length=10000) url = forms.CharField(label='URL', max_length=200) + title = forms.CharField(label='Title', max_length=200) + body = forms.CharField(label='Optional text', max_length=10000, required=False) # date = forms.DateTimeField('date published') # state = forms.IntegerField(label='State', default=1) style = forms.ChoiceField(label='Style',choices=STYLE_CHOICES, required=True) + + today = datetime.today() # + datetime.timedelta(days=1) + today_formatted = today.strftime("%Y-%m-%d %H:%M:%S") + + pub_date = forms.DateTimeField(label='Optional publication date, example: ' + today_formatted, required=False) image = forms.FileField() class SigninForm(forms.Form): diff --git a/news/migrations/0005_auto_20190918_0918.py b/news/migrations/0005_auto_20190918_0918.py new file mode 100644 index 0000000..b4fcec0 --- /dev/null +++ b/news/migrations/0005_auto_20190918_0918.py @@ -0,0 +1,30 @@ +# Generated by Django 2.2.5 on 2019-09-18 09:18 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('news', '0004_auto_20190917_1301'), + ] + + operations = [ + migrations.AddField( + model_name='comment', + name='deleted', + field=models.BooleanField(default=False), + ), + migrations.AlterField( + model_name='comment', + name='parent_comment', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='news.Comment'), + ), + migrations.AlterField( + model_name='comment', + name='post', + field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='news.Post'), + preserve_default=False, + ), + ] diff --git a/news/migrations/__pycache__/0005_auto_20190918_0918.cpython-37.pyc b/news/migrations/__pycache__/0005_auto_20190918_0918.cpython-37.pyc new file mode 100644 index 0000000..d815e75 Binary files /dev/null and b/news/migrations/__pycache__/0005_auto_20190918_0918.cpython-37.pyc differ diff --git a/news/models.py b/news/models.py index aee717c..524e02d 100644 --- a/news/models.py +++ b/news/models.py @@ -23,7 +23,7 @@ class Post(models.Model): return self.title def top_comments(self): - return self.comment_set.all()[:5] + return self.comment_set.all()[:3] def flat_comments(self, user): flat = [] @@ -38,10 +38,11 @@ class Post(models.Model): class Comment(models.Model): 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) + parent_comment = models.ForeignKey("self", on_delete=models.SET_NULL, null=True, blank=True) body = models.CharField(max_length=100) voters = models.ManyToManyField(User, related_name='voted_comments', null=True, blank=True) date = models.DateTimeField('date published') + deleted = models.BooleanField(default=False) def __str__(self): return self.body @@ -52,6 +53,7 @@ class Comment(models.Model): self.level = range(level) self.upvoted = user in self.voters.all() self.score = len(self.voters.all()) + self.is_deletable = (self.deleted == False and user == self.author) flat = [self] for comment in self.comment_set.all(): flat.extend(comment.flat_children(level + 1, user)) diff --git a/news/static/media/7490e65c-5ca7-4734-882a-8b268b1662d2 b/news/static/media/7490e65c-5ca7-4734-882a-8b268b1662d2 new file mode 100644 index 0000000..c7f434f Binary files /dev/null and b/news/static/media/7490e65c-5ca7-4734-882a-8b268b1662d2 differ diff --git a/news/static/media/7c44bcb2-d09e-4a68-839e-3e1dfecd9c63 b/news/static/media/7c44bcb2-d09e-4a68-839e-3e1dfecd9c63 new file mode 100644 index 0000000..4c9b220 Binary files /dev/null and b/news/static/media/7c44bcb2-d09e-4a68-839e-3e1dfecd9c63 differ diff --git a/news/static/media/9ebca694-d49c-4f4d-81f5-5d953b5f449b b/news/static/media/9ebca694-d49c-4f4d-81f5-5d953b5f449b new file mode 100644 index 0000000..4c9b220 Binary files /dev/null and b/news/static/media/9ebca694-d49c-4f4d-81f5-5d953b5f449b differ diff --git a/news/static/news/css/app.css b/news/static/news/css/app.css index db2f889..7d8c360 100644 --- a/news/static/news/css/app.css +++ b/news/static/news/css/app.css @@ -143,3 +143,7 @@ header { li a { color: red; } + +.inline { + display: inline-block; +} diff --git a/news/templates/base.html b/news/templates/base.html index 4ed97a0..7d1d37c 100644 --- a/news/templates/base.html +++ b/news/templates/base.html @@ -20,8 +20,8 @@
-

poker
downtown

- hardcore newsing +

poker
rumble

+ hardcore news


diff --git a/news/templates/news/post.html b/news/templates/news/post.html index 1d733bf..23e3503 100644 --- a/news/templates/news/post.html +++ b/news/templates/news/post.html @@ -19,31 +19,61 @@

{% csrf_token %} -

Add comment

-
+ +

----Comments----

{% for comment in comments %}

{% for n in comment.level %}

{% endfor %}

diff --git a/news/templates/news/submission.html b/news/templates/news/submission.html index ff7a283..548cf35 100644 --- a/news/templates/news/submission.html +++ b/news/templates/news/submission.html @@ -1,9 +1,13 @@ {% extends "base.html" %} +{% load static %} + {% block title %}My amazing submission{% endblock %} {% block content %} +{{ form.media }} + {% if error_message %}

{{ error_message }}

{% endif %} {% if user.is_authenticated %} diff --git a/news/urls.py b/news/urls.py index 644e8e6..9659c7f 100644 --- a/news/urls.py +++ b/news/urls.py @@ -6,10 +6,11 @@ app_name = 'news' 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('comment//delete', views.delete_comment, name='delete_comment'), path('upvote/', views.upvote, name='upvote'), + path('submission', views.submission, name='submission'), 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 e6e864d..fc46155 100644 --- a/news/views.py +++ b/news/views.py @@ -9,6 +9,7 @@ from django.contrib import messages from django.contrib.auth import authenticate, login, logout, update_session_auth_hash from django.contrib.auth.decorators import login_required from django.contrib.auth.forms import PasswordChangeForm +from django.db.models import Q from .models import Post, Comment, PostState from .forms import PostForm, CustomUserCreationForm, SigninForm from datetime import datetime @@ -74,7 +75,11 @@ def password_change(request): #Post def index(request): - latest_post_list = Post.objects.filter(state=1).order_by('-date') + + filter1 = Q(state=1) + filter2 = Q(date__lte=datetime.today()) + + latest_post_list = Post.objects.filter(filter1 & filter2).order_by('-date') paginator = Paginator(latest_post_list, 25) page = request.GET.get('page') @@ -105,6 +110,9 @@ def submission(request): post.image_url = filename if request.user.is_staff: post.state = PostState.PUBLISHED.value + pub_date = form.cleaned_data['pub_date'] + if pub_date is not None: + post.date = pub_date else: post.state = PostState.USER_SUBMITTED.value post.save() @@ -131,13 +139,24 @@ 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.parent_comment = get_object_or_404(Comment, pk=comment_id) comment.body = request.POST['body'] comment.save() comment.voters.add(request.user) comment.save() return HttpResponseRedirect(reverse('news:post', args=(post_id,))) +def delete_comment(request, comment_id): + comment = get_object_or_404(Comment, pk=comment_id) + post = comment.post + if comment.comment_set.all().count() > 0: + comment.body = "[deleted]" + comment.deleted = True + comment.save() + else: + comment.delete() + return render(request, 'news/post.html', {'post': post, 'comments': post.flat_comments(request.user) }) + def upvote(request, comment_id): comment = get_object_or_404(Comment, pk=comment_id) comment.voters.add(request.user)