Commit 48676050 authored by Falk, Dennis Nikolas's avatar Falk, Dennis Nikolas
Browse files

WYSIWYG-Editor hinzugefügt

# Conflicts:
#	blog/models.py
#	djangoblog.sqlite3
parents 13c4256e 0d825f19
......@@ -2,5 +2,7 @@ from django.contrib import admin
# Register your models here.
from .models import Blog # importing Blog class
from .models import Comment
admin.site.register(Blog)
\ No newline at end of file
admin.site.register(Blog)
admin.site.register(Comment)
\ No newline at end of file
from django.forms import ModelForm, Textarea, HiddenInput
from blog.models import Comment
class CommentForm(ModelForm):
"""Form to save comments for a blog entry"""
class Meta:
model = Comment
# which fields should be edited?
fields = ['author', 'text', 'related_blog']
# labels for the fields
labels = {'author': 'Name'}
# how should the fields be shown?
widgets = {
'author': Textarea(attrs={'rows': 1}),
'text': Textarea(attrs={'rows': 5}),
'related_blog': HiddenInput()
}
# Generated by Django 3.0.7 on 2020-06-12 20:42
import datetime
from django.db import migrations, models
import django.db.models.deletion
import uuid
class Migration(migrations.Migration):
dependencies = [
('blog', '0010_auto_20200611_1257'),
]
operations = [
migrations.AlterField(
model_name='blog',
name='identifier',
field=models.CharField(default=uuid.UUID('c243d1b6-0415-4836-ba1b-531367c635af'), max_length=500, unique=True),
),
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('author', models.TextField(max_length=150)),
('text', models.TextField(max_length=1000)),
('date', models.DateTimeField(default=datetime.datetime.now)),
('related_blog', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='blog.Blog')),
],
),
]
from time import timezone
import uuid
from ckeditor.fields import RichTextField
......@@ -6,22 +5,47 @@ from ckeditor_uploader.fields import RichTextUploadingField
from django.db import models
from datetime import datetime
ARTICLE_STATE_HIDE_FROM_LIST = 1
BLOG_STATE_HIDE_FROM_LIST = 1
BLOG_STATE_CAN_HAVE_COMMENTS = 2
# Create your models here.
class Blog(models.Model):
identifier = models.CharField(max_length=500, unique=True, default=uuid.uuid4())
title = models.CharField(default="Titel", max_length=200)
# description = models.TextField(blank=True)
description = RichTextUploadingField(blank=True)
date = models.DateTimeField(default=datetime.now, blank=True)
# need to install pillow for images
image = models.ImageField(upload_to='images/', blank=True)
created = models.DateField(auto_now_add=True)
edited = models.DateField(auto_now=True)
status = models.IntegerField(default=1)
#TODO URL-Field is currently not used
url = models.URLField(max_length=300, blank=True)
def __str__(self):
return self.identifier + " (" + self.title + ")"
\ No newline at end of file
""" Blog class
One blog is a single block of content which can be shown on our page
"""
identifier = models.CharField(max_length=500, unique=True, default=uuid.uuid4())
title = models.CharField(default="Titel", max_length=200)
description = RichTextUploadingField(blank=True)
date = models.DateTimeField(default=datetime.now, blank=True)
# need to install pillow for images
image = models.ImageField(upload_to='images/', blank=True)
created = models.DateField(auto_now_add=True)
edited = models.DateField(auto_now=True)
status = models.IntegerField(default=1)
# TODO URL-Field is currently not used
url = models.URLField(max_length=300, blank=True)
def __str__(self):
return self.identifier + " (" + self.title + ")"
def is_hidden_in_blog_list(self):
""" Returns true or false if this blog is open for comments """
return self.status & BLOG_STATE_HIDE_FROM_LIST == BLOG_STATE_HIDE_FROM_LIST
def are_comments_allowed(self):
""" Returns true or false if this blog is open for comments """
return self.status & BLOG_STATE_CAN_HAVE_COMMENTS == BLOG_STATE_CAN_HAVE_COMMENTS
class Comment(models.Model):
""" Comment class
Linked to a blog, one blog can have many comments from external users
"""
author = models.TextField(max_length=150)
text = models.TextField(max_length=1000)
date = models.DateTimeField(default=datetime.now)
related_blog = models.ForeignKey(Blog, on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.related_blog.identifier + ': ' + self.text
......@@ -10,7 +10,7 @@ function goToDetails(event) {
var targetclassName = card.className.toLowerCase();
if (targetclassName != 'card-deck'){
let id = card.parentNode.children.item(3).innerHTML
let id = card.parentNode.children.item(3).innerHTML;
openDetails(id);
}
}
......
.comment-area {
border-top: 1px solid #0B9ED9;
border-radius: 0;
}
.comment-area article {
background-color: #eeeeee;
padding: 1em;
border-radius: 3px;
margin-bottom: 1em;
}
.comment-area .author {
font-weight: bold;
margin-bottom: 0.5em;
}
.comment-area .author:after {
content: ":"
}
.comment-area blockquote{
background-color: #ffffff;
padding: 0.3em;
}
.comment-area blockquote:before {
content: "„"
}
.comment-area blockquote:after {
content: "“"
}
.comment-area .no-comment{
font-style: italic;
}
.comment-area label:after{
content: ":"
}
\ No newline at end of file
......@@ -24,7 +24,6 @@ body {
margin-top: 1em;
margin-bottom: 1em;
border-radius: 5px;
box-shadow: 3px 3px 3px #010748;
padding: 1em;
}
......
......@@ -7,6 +7,11 @@
{% bootstrap_css %}
<link rel="stylesheet" href="{% static 'blog/styles.css' %}">
<script src="{% static 'blog/all_blogs.js' %}"></script>
<style>
.card-body {
cursor: pointer;
}
</style>
<meta charset="UTF-8">
<title>Title</title>
</head>
......@@ -24,7 +29,7 @@
<div class="card-deck">
{% for blog in blogs %}
<div class="card float-left mt-3" style="width: 18rem; min-width: 18rem; max-width: 18rem">
{% if blog.image.url %}
{% if blog.image and blog.image.url %}
<img class="card-img-top" src="{{ blog.image.url }}" alt="Card image cap">
{% endif %}
<div class="card-body">
......@@ -45,6 +50,12 @@
</div>
{% endfor %}
</div>
<footer class="fixed-bottom bg-light">
<ul class="nav justify-content-end">
<li class="nav-item"><a class="nav-link" href="{% url 'blog:detail' blog_id=2 %}">Impressum</a></li>
<li class="nav-item"><a class="nav-link" href="{% url 'blog:detail' blog_id=3 %}">Kontakt</a></li>
</ul>
</footer>
</div>
......
......@@ -4,6 +4,7 @@
<html lang="en">
<head>
<link rel="stylesheet" href="{% static 'blog/styles.css' %}">
<link rel="stylesheet" href="{% static 'blog/comment-area.css' %}">
<meta charset="UTF-8">
<title>Title</title>
{% bootstrap_javascript %}
......@@ -18,7 +19,7 @@
</div>
<div class="container">
{% bootstrap_messages %}
<div class="text-right minidate" >{{ blog.date }}</div>
{% autoescape off %}
......@@ -27,11 +28,20 @@
<a href="{% url 'blog:all_blogs' %}" class="btn btn-primary">Back to Blogs</a>
<br>
{% if blog.image.url %}
<div class="container thirtypercent float-right">
{% if blog.image and blog.image.url %}
<div class="container thirtypercent">
<img class="card-img-top" src="{{ blog.image.url }}" alt="Card image cap">
</div>
{% endif %}
</div>
{% block comments %}
<!-- Comments will go in here -->
{% endblock %}
<footer class="fixed-bottom bg-light">
<ul class="nav justify-content-end">
<li class="nav-item"><a class="nav-link" href="{% url 'blog:detail' blog_id=2 %}">Impressum</a></li>
<li class="nav-item"><a class="nav-link" href="{% url 'blog:detail' blog_id=3 %}">Kontakt</a></li>
</ul>
</footer>
</body>
</html>
\ No newline at end of file
{% extends 'blog/detail.html' %}
{% load bootstrap4 %}
{% block comments %}
<div class="container comment-area">
<h4>Kommentare</h4>
<!-- Show existing comments -->
{% for comment in comments %}
<article>
<div class="row">
<div class="col author">{{comment.author}}</div>
<div class="col text-right minidate">{{comment.date}}</div>
</div>
<blockquote class="col">{{comment.text}}</blockquote>
</article>
{% endfor %}
{% if comments.count == 0 %}
<div class="row">
<div class="col no-comment">Es wurden noch keine Kommentare für diesen Artikel abgegeben.</div>
</div>
{% endif %}
<!-- Add new comments -->
<h5>Schreibe einen Kommentar</h5>
<form action="{% url 'blog:save_comment' %}" method="POST">
{% csrf_token %}
{% bootstrap_form comment_form %}
<input type="submit" class="btn btn-primary">
</form>
</div>
{% endblock %}
\ No newline at end of file
......@@ -24,4 +24,5 @@ app_name = 'blog'
urlpatterns = [
path('', views.all_blogs, name='all_blogs'),
path('<int:blog_id>/', views.detail, name='detail'),
path('comment/save', views.save_comment, name='save_comment')
]
\ No newline at end of file
from django.shortcuts import render, get_object_or_404
from .models import Blog
from django.http import HttpResponseNotAllowed
from django.contrib import messages
from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse_lazy
from .forms import CommentForm
from .models import *
# Create your views here.
def all_blogs(request):
blogs = Blog.objects.all()
return render(request, 'blog/all_blogs.html',{'blogs':blogs})
# Exclude those with status hidden from the overall list
query = f'status & {BLOG_STATE_HIDE_FROM_LIST} <> {BLOG_STATE_HIDE_FROM_LIST}'
blogs = Blog.objects.extra(where=[query]).order_by('created')
return render(request, 'blog/all_blogs.html', {'blogs': blogs})
def detail(request, blog_id):
blog = get_object_or_404(Blog, pk=blog_id)
return render(request, 'blog/detail.html', {'blog':blog})
if blog.are_comments_allowed():
# load existing comments
comments = Comment.objects.select_related().filter(related_blog=blog.id)
# dialog for new comments
form = CommentForm(initial={'related_blog': blog.id})
# render with comments
return render(request, 'blog/detail_with_comments.html',
{'blog': blog, 'comments': comments, 'comment_form': form})
return render(request, 'blog/detail.html', {'blog': blog})
def save_comment(request):
if request.method != 'POST':
# no form submit - return 405
return HttpResponseNotAllowed(['POST'])
form = CommentForm(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Danke! Dein Kommentar wurde übermittelt.')
else:
messages.error(request, 'Ups! Da ist etwas schief gelaufen. Wir konnten deinen Kommentar nicht speichern!')
print(form.data.get('related_blog'))
blog = get_object_or_404(Blog, pk=form.data.get('related_blog'))
# back to the details page
# todo after merge: Redirect to url from blog article
return redirect(reverse_lazy('blog:detail', kwargs={'blog_id':blog.id}))
No preview for this file type
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment