diff --git a/.idea/misc.xml b/.idea/misc.xml
index 08e0dea..79a4365 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -3,5 +3,5 @@
-
+
\ No newline at end of file
diff --git a/.idea/news1.iml b/.idea/news1.iml
index 409e2c8..0af37b3 100644
--- a/.idea/news1.iml
+++ b/.idea/news1.iml
@@ -16,7 +16,7 @@
-
+
diff --git a/news1/routing.py b/news1/routing.py
new file mode 100644
index 0000000..7cd3e7f
--- /dev/null
+++ b/news1/routing.py
@@ -0,0 +1,17 @@
+from channels.routing import ProtocolTypeRouter
+from django.urls import re_path
+from channels.routing import URLRouter
+from channels.http import AsgiHandler
+from channels.auth import AuthMiddlewareStack
+import django_eventstream
+
+urlpatterns = [
+ re_path(r'^events/',
+ AuthMiddlewareStack(URLRouter(django_eventstream.routing.urlpatterns)),
+ {'channels': ['notice']}),
+ re_path(r'', AsgiHandler),
+]
+
+application = ProtocolTypeRouter({
+ 'http' : URLRouter(urlpatterns)
+})
\ No newline at end of file
diff --git a/news1/settings.py b/news1/settings.py
index da82c89..f454aab 100644
--- a/news1/settings.py
+++ b/news1/settings.py
@@ -31,12 +31,17 @@ ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
+ 'posts.apps.PostsConfig',
+ 'rest_framework',
+ 'channels',
+ 'django_eventstream',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
+
]
MIDDLEWARE = [
@@ -47,6 +52,8 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
+ 'django_grip.GripMiddleware',
+ 'django.middleware.security.SecurityMiddleware',
]
ROOT_URLCONF = 'news1.urls'
@@ -69,6 +76,7 @@ TEMPLATES = [
]
WSGI_APPLICATION = 'news1.wsgi.application'
+ASGI_APPLICATION = 'news1.routing.application'
# Database
@@ -104,7 +112,7 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
-LANGUAGE_CODE = 'en-us'
+LANGUAGE_CODE = 'de-de'
TIME_ZONE = 'UTC'
diff --git a/news1/urls.py b/news1/urls.py
index 0b99763..eef7e5d 100644
--- a/news1/urls.py
+++ b/news1/urls.py
@@ -15,8 +15,12 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path,include
+from posts import views
urlpatterns = [
path('admin/', admin.site.urls),
- path('posts/', include('posts.urls'))
+ path('posts/', include('posts.urls')),
+ path('new', views.new, name='new'),
+
+
]
diff --git a/posts/forms.py b/posts/forms.py
new file mode 100644
index 0000000..67712b3
--- /dev/null
+++ b/posts/forms.py
@@ -0,0 +1,14 @@
+from django import forms
+import datetime
+
+
+class NoticeForm(forms.Form):
+ date_formats = ['%d.%m.%Y', '%d.%m.%y']
+ title = forms.CharField(label='Titel', max_length=80)
+ text = forms.CharField(label='Text', max_length=400)
+ start = forms.DateField(label='Von',
+ input_formats=date_formats,
+ initial=datetime.date.today)
+ end = forms.DateField(label='Bis',
+ input_formats=date_formats,
+ initial=datetime.date.today)
\ No newline at end of file
diff --git a/posts/migrations/0001_initial.py b/posts/migrations/0001_initial.py
new file mode 100644
index 0000000..ef0d656
--- /dev/null
+++ b/posts/migrations/0001_initial.py
@@ -0,0 +1,24 @@
+# Generated by Django 2.2.7 on 2019-11-19 13:25
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ initial = True
+
+ dependencies = [
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='Notice',
+ fields=[
+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('notice_title', models.CharField(max_length=80)),
+ ('notice_text', models.CharField(max_length=400)),
+ ('pub_start', models.DateTimeField()),
+ ('pub_end', models.DateTimeField()),
+ ],
+ ),
+ ]
diff --git a/posts/migrations/__init__.py b/posts/migrations/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/posts/models.py b/posts/models.py
index 71a8362..6d2907a 100644
--- a/posts/models.py
+++ b/posts/models.py
@@ -1,3 +1,11 @@
from django.db import models
# Create your models here.
+from django.db import models
+
+
+class Notice(models.Model):
+ notice_title = models.CharField(max_length=80)
+ notice_text = models.CharField(max_length=400)
+ pub_start = models.DateTimeField()
+ pub_end = models.DateTimeField()
diff --git a/posts/serializer.py b/posts/serializer.py
new file mode 100644
index 0000000..0c2abfc
--- /dev/null
+++ b/posts/serializer.py
@@ -0,0 +1,8 @@
+from rest_framework import serializers
+
+from .models import Notice
+
+class NoticeSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = Notice
+ fields = ('id', 'notice_title', 'notice_text', 'pub_start', 'pub_end')
\ No newline at end of file
diff --git a/posts/urls.py b/posts/urls.py
index 9cb15e6..516631c 100644
--- a/posts/urls.py
+++ b/posts/urls.py
@@ -2,5 +2,13 @@ from django.urls import path
from . import views
urlpatterns = [
- path('', views.post, name='post')
+ # path('', views.index, name='post'),
+ # path('delete/', views.delete, name='delete')
+
+ path('', views.index, name='index'),
+ path('new', views.new, name='new'),
+ path('delete/', views.delete, name='delete'),
+
+ path('notices/', views.notice_list),
+ path('notices/', views.notice_detail),
]
diff --git a/posts/views.py b/posts/views.py
index 03b1a9e..39f1289 100644
--- a/posts/views.py
+++ b/posts/views.py
@@ -1,6 +1,12 @@
-from django.shortcuts import render
-
+from django.shortcuts import render, redirect
+from .models import Notice
+from django.utils import timezone
from django.http import HttpResponse
+from .forms import NoticeForm
+from posts.serializer import NoticeSerializer
+from django.http import JsonResponse
+from rest_framework.parsers import JSONParser
+from django.views.decorators.csrf import csrf_exempt
# Create your views here.
@@ -8,3 +14,71 @@ from django.http import HttpResponse
def post(request):
return render(request, "posts/index.html")
+
+def index(request):
+ notices = Notice.objects.all()
+ notices = notices.filter(pub_start__lt = timezone.now())
+ notices = notices.filter(pub_end__gt = timezone.now())
+ context = { "notices" : notices}
+ return render(request, 'posts/index.html', context)
+
+
+def new(request):
+ if request.method == "POST":
+ form = NoticeForm(request.POST)
+ if form.is_valid():
+ newNotice = Notice(notice_title=form.cleaned_data['title'],
+ notice_text=form.cleaned_data['text'],
+ pub_start=form.cleaned_data['start'],
+ pub_end=form.cleaned_data['end'])
+ newNotice.save()
+ #return redirect('index')
+ context = {'form': NoticeForm()}
+ return render(request, 'posts/edit.html', context)
+
+
+def delete(request, deleteId=None):
+ if deleteId != None:
+ delNotice = Notice.objects.get(id=deleteId)
+ if delNotice != None:
+ delNotice.delete()
+ return redirect('index')
+
+@csrf_exempt
+def notice_list(request):
+ if request.method == 'GET':
+ notices = Notice.objects.all()
+ serializer = NoticeSerializer(notices, many=True)
+ return JsonResponse(serializer.data, safe=False)
+
+ elif request.method == 'POST':
+ data = JSONParser().parse(request)
+ serializer = NoticeSerializer(data=data)
+ if serializer.is_valid():
+ serializer.save()
+ return JsonResponse(serializer.data, status=201)
+ return JsonResponse(serializer.errors, status=400)
+
+@csrf_exempt
+def notice_detail(request, id):
+ try:
+ notice = Notice.objects.get(id=id)
+ except Notice.DoesNotExist:
+ return HttpResponse(status=404)
+
+ if request.method == 'GET':
+ serializer = NoticeSerializer(notice)
+ return JsonResponse(serializer.data)
+
+ elif request.method == 'PUT':
+ data = JSONParser().parse(request)
+ serializer=NoticeSerializer(notice, data=data)
+ if serializer.is_valid():
+ serializer.save()
+ return JsonResponse(serializer.data)
+ return JsonResponse(serializer.errors, status=400)
+
+ elif request.method == 'DELETE':
+ notice.delete()
+ return HttpResponse(status=204)
+
diff --git a/templates/posts/edit.html b/templates/posts/edit.html
new file mode 100644
index 0000000..7f076ce
--- /dev/null
+++ b/templates/posts/edit.html
@@ -0,0 +1,10 @@
+{% extends "base.html" %}
+{% block title %}edit.html{% endblock %}
+{% block content %}
+ Neue Nachricht
+
+{% endblock %}
\ No newline at end of file