Browse Source

PR8

master
Laura 5 years ago
parent
commit
b9dfad7a72
7 changed files with 96 additions and 9 deletions
  1. 1
    0
      PR/settings.py
  2. 24
    0
      polls/migrations/0001_initial.py
  3. 0
    0
      polls/migrations/__init__.py
  4. 3
    1
      polls/models.py
  5. 7
    0
      polls/serializers.py
  6. 6
    1
      polls/urls.py
  7. 55
    7
      polls/views.py

+ 1
- 0
PR/settings.py View File

@@ -38,6 +38,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
]

MIDDLEWARE = [

+ 24
- 0
polls/migrations/0001_initial.py View File

@@ -0,0 +1,24 @@
# Generated by Django 2.2.7 on 2019-11-30 16:27

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

+ 0
- 0
polls/migrations/__init__.py View File


+ 3
- 1
polls/models.py View File

@@ -5,4 +5,6 @@ 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()
pub_end = models.DateTimeField()

#neues model python migration

+ 7
- 0
polls/serializers.py View File

@@ -0,0 +1,7 @@
from rest_framework import serializers
from .models import Notice

class NoticeSerializer(serializers.ModelSerializer):
class Meta:
model = Notice
fields = ('id', 'notice_title', 'pub_start', 'pub_end')

+ 6
- 1
polls/urls.py View File

@@ -26,6 +26,11 @@ urlpatterns = [
path('welcome', views.welcome_seite),
path('home', views.welcome_seite),
path('about', views.about_seite),
path('delete/<int:deleteId>', views.delete, name ='delete')
path('delete/<int:deleteId>', views.delete, name ='delete'),
#url(r'^new', views.new, name='new'),
path('notices/', views.notice_list),
path('notices/<int:id>/', views.notice_detail),



]

+ 55
- 7
polls/views.py View File

@@ -1,9 +1,17 @@
from django.http import HttpResponse
from django.shortcuts import render,redirect
from .models import Notice
from django.utils import timezone
import logging

#Pr8
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse,JsonResponse
from polls.forms import NoticeForm
from polls.models import Notice
from polls.serializers import NoticeSerializer
from rest_framework.parsers import JSONParser
#XXXX


# Create your views here.
logger = None
@@ -28,21 +36,23 @@ def index(request):
#context = {"notices" : notices}
return render(request, 'polls/notice.html',{"notices" : notices})

#def new(request):
# return render(request, 'polls/edit.html')
#login_required
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'])
notice_text=form.cleaned_data['text'],
#pub_start=form.cleaned_data['start'],
pub_start= timezone.now(),
pub_end=form.cleaned_data['end'])
newNotice.save()
return redirect('index')
context = {'form': NoticeForm()}
return render(request, 'polls/edit.html', context)

#staff_member_required
def delete(request, deleteId = None):
if deleteId != None:
delNotice = Notice.objects.get(id=deleteId)
@@ -54,4 +64,42 @@ def welcome_seite(request):
return render(request, 'polls/index.html')

def about_seite(request):
return render(request, 'polls/about.html')
return render(request, 'polls/about.html')

#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().parser(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)

Loading…
Cancel
Save