Improved comments + programed submissions

master
Laurent 6 years ago
parent 23c8a550d5
commit 67a537ec82
  1. BIN
      db.sqlite3
  2. BIN
      news/__pycache__/choices.cpython-37.pyc
  3. BIN
      news/__pycache__/forms.cpython-37.pyc
  4. BIN
      news/__pycache__/models.cpython-37.pyc
  5. BIN
      news/__pycache__/urls.cpython-37.pyc
  6. BIN
      news/__pycache__/views.cpython-37.pyc
  7. 12
      news/forms.py
  8. 30
      news/migrations/0005_auto_20190918_0918.py
  9. BIN
      news/migrations/__pycache__/0005_auto_20190918_0918.cpython-37.pyc
  10. 6
      news/models.py
  11. BIN
      news/static/media/7490e65c-5ca7-4734-882a-8b268b1662d2
  12. BIN
      news/static/media/7c44bcb2-d09e-4a68-839e-3e1dfecd9c63
  13. BIN
      news/static/media/9ebca694-d49c-4f4d-81f5-5d953b5f449b
  14. 4
      news/static/news/css/app.css
  15. 4
      news/templates/base.html
  16. 46
      news/templates/news/post.html
  17. 4
      news/templates/news/submission.html
  18. 3
      news/urls.py
  19. 23
      news/views.py

Binary file not shown.

@ -3,14 +3,22 @@ from django.contrib.auth.models import User
from django import forms from django import forms
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from .choices import * from .choices import *
import time
from datetime import datetime, date, time, timedelta
# from datetime import datetime
class PostForm(forms.Form): 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) 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') # date = forms.DateTimeField('date published')
# state = forms.IntegerField(label='State', default=1) # state = forms.IntegerField(label='State', default=1)
style = forms.ChoiceField(label='Style',choices=STYLE_CHOICES, required=True) 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() image = forms.FileField()
class SigninForm(forms.Form): class SigninForm(forms.Form):

@ -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,
),
]

@ -23,7 +23,7 @@ class Post(models.Model):
return self.title return self.title
def top_comments(self): def top_comments(self):
return self.comment_set.all()[:5] return self.comment_set.all()[:3]
def flat_comments(self, user): def flat_comments(self, user):
flat = [] flat = []
@ -38,10 +38,11 @@ class Post(models.Model):
class Comment(models.Model): class Comment(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, 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) 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) body = models.CharField(max_length=100)
voters = models.ManyToManyField(User, related_name='voted_comments', null=True, blank=True) voters = models.ManyToManyField(User, related_name='voted_comments', null=True, blank=True)
date = models.DateTimeField('date published') date = models.DateTimeField('date published')
deleted = models.BooleanField(default=False)
def __str__(self): def __str__(self):
return self.body return self.body
@ -52,6 +53,7 @@ class Comment(models.Model):
self.level = range(level) self.level = range(level)
self.upvoted = user in self.voters.all() self.upvoted = user in self.voters.all()
self.score = len(self.voters.all()) self.score = len(self.voters.all())
self.is_deletable = (self.deleted == False and user == self.author)
flat = [self] flat = [self]
for comment in self.comment_set.all(): for comment in self.comment_set.all():
flat.extend(comment.flat_children(level + 1, user)) flat.extend(comment.flat_children(level + 1, user))

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 330 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 330 KiB

@ -143,3 +143,7 @@ header {
li a { li a {
color: red; color: red;
} }
.inline {
display: inline-block;
}

@ -20,8 +20,8 @@
<div class="grid-x"> <div class="grid-x">
<!-- Header --> <!-- Header -->
<div class="cell large-2 header"> <div class="cell large-2 header">
<h1><a href="{% url 'news:index' %}">poker<br/>downtown</a></h1> <h1><a href="{% url 'news:index' %}">poker<br/>rumble</a></h1>
<span>hardcore newsing</span> <span>hardcore news</span>
<p> <p>
<br/> <br/>

@ -19,31 +19,61 @@
<form action="{% url 'news:comment' post.id %}" method="post"> <form action="{% url 'news:comment' post.id %}" method="post">
{% csrf_token %} {% csrf_token %}
<p>Add comment</p> <p>Add comment</p>
<p> <p>
<textarea name="body" rows="8" cols="80"></textarea> <textarea name="body" rows="8" cols="80"></textarea>
</p> </p>
<input type="submit" value="Submit"> <input type="submit" value="Submit">
</form> </form>
<script type="text/javascript">
function myFunction(divID) {
var x = document.getElementById(divID);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
<br/> <br/>
<p>----Comments----</p> <p>----Comments----</p>
{% for comment in comments %} {% for comment in comments %}
<p> <p>
{% for n in comment.level %}<ul>{% endfor %} {% for n in comment.level %}<ul>{% endfor %}
{% if comment.upvoted == False %}
<form action="{% url 'news:upvote' comment.id %}" method="post">
{% csrf_token %}
<input type="submit" value="+1">
</form>
{% endif %}
<span class="info">{{ comment.author.username }} - {{ comment.date }} - score = {{ comment.score }}</span> <span class="info">{{ comment.author.username }} - {{ comment.date }} - score = {{ comment.score }}</span>
<br/> <br/>
{{ comment.body }} {{ comment.body }}
<p>
{% if comment.upvoted == False %}
<form action="{% url 'news:upvote' comment.id %}" method="post" class="inline">
{% csrf_token %}
<input type="submit" value="+1">
</form>
{% endif %}
{% if comment.is_deletable %}
<form action="{% url 'news:delete_comment' comment.id %}" method="post" class="inline">
{% csrf_token %}
<input type="submit" value="delete">
</form>
{% endif %}
<button onclick="myFunction('div_{{ comment.id }}')" class="inline">reply</button>
<div id="div_{{ comment.id }}" class="inline" style="display:none">
<form action="{% url 'news:comment_with_parent' post.id comment.id %}" method="post">
{% csrf_token %}
<textarea name="body" rows="8" cols="80"></textarea>
<input type="submit" value="Submit">
</form>
</div>
</p>
{% for n in comment.level %}</ul>{% endfor %} {% for n in comment.level %}</ul>{% endfor %}
</p> </p>

@ -1,9 +1,13 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load static %}
{% block title %}My amazing submission{% endblock %} {% block title %}My amazing submission{% endblock %}
{% block content %} {% block content %}
{{ form.media }}
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
{% if user.is_authenticated %} {% if user.is_authenticated %}

@ -6,10 +6,11 @@ app_name = 'news'
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', views.index, name='index'),
path('<int:post_id>', views.post, name='post'), path('<int:post_id>', views.post, name='post'),
path('submission', views.submission, name='submission'),
path('<int:post_id>/comment/<int:comment_id>', views.comment_with_parent, name='comment_with_parent'), path('<int:post_id>/comment/<int:comment_id>', views.comment_with_parent, name='comment_with_parent'),
path('<int:post_id>/comment', views.comment, name='comment'), path('<int:post_id>/comment', views.comment, name='comment'),
path('comment/<int:comment_id>/delete', views.delete_comment, name='delete_comment'),
path('upvote/<int:comment_id>', views.upvote, name='upvote'), path('upvote/<int:comment_id>', views.upvote, name='upvote'),
path('submission', views.submission, name='submission'),
path('submitted', views.submitted, name='submitted'), path('submitted', views.submitted, name='submitted'),
path('account', views.account, name='account'), path('account', views.account, name='account'),
path('signin', views.signin, name='signin'), path('signin', views.signin, name='signin'),

@ -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 import authenticate, login, logout, update_session_auth_hash
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import PasswordChangeForm from django.contrib.auth.forms import PasswordChangeForm
from django.db.models import Q
from .models import Post, Comment, PostState from .models import Post, Comment, PostState
from .forms import PostForm, CustomUserCreationForm, SigninForm from .forms import PostForm, CustomUserCreationForm, SigninForm
from datetime import datetime from datetime import datetime
@ -74,7 +75,11 @@ def password_change(request):
#Post #Post
def index(request): 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) paginator = Paginator(latest_post_list, 25)
page = request.GET.get('page') page = request.GET.get('page')
@ -105,6 +110,9 @@ def submission(request):
post.image_url = filename post.image_url = filename
if request.user.is_staff: if request.user.is_staff:
post.state = PostState.PUBLISHED.value post.state = PostState.PUBLISHED.value
pub_date = form.cleaned_data['pub_date']
if pub_date is not None:
post.date = pub_date
else: else:
post.state = PostState.USER_SUBMITTED.value post.state = PostState.USER_SUBMITTED.value
post.save() post.save()
@ -131,13 +139,24 @@ def comment_with_parent(request, post_id, comment_id):
comment = Comment(author=request.user,date=datetime.today()) comment = Comment(author=request.user,date=datetime.today())
comment.post = get_object_or_404(Post, pk=post_id) comment.post = get_object_or_404(Post, pk=post_id)
if comment_id is not None: 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.body = request.POST['body']
comment.save() comment.save()
comment.voters.add(request.user) comment.voters.add(request.user)
comment.save() comment.save()
return HttpResponseRedirect(reverse('news:post', args=(post_id,))) 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): def upvote(request, comment_id):
comment = get_object_or_404(Comment, pk=comment_id) comment = get_object_or_404(Comment, pk=comment_id)
comment.voters.add(request.user) comment.voters.add(request.user)

Loading…
Cancel
Save