Browse Source

Messages

REST
main
Oliver 1 year ago
parent
commit
858c2fe0c3
7 changed files with 81 additions and 2 deletions
  1. 12
    0
      create_notice.http
  2. 6
    0
      delete_notice.http
  3. 1
    0
      mysite/settings.py
  4. 9
    0
      polls/serializers.py
  5. 3
    0
      polls/urls.py
  6. 49
    2
      polls/views.py
  7. 1
    0
      requirements.txt

+ 12
- 0
create_notice.http View File

POST http://127.0.0.1:8000/polls/notices/ HTTP/1.1
Content-Type: application/json

{
"notice_title": "Notice via API",
"notice_text": "This is a notice created via the API",
"pub_start": "2019-01-01T00:00:00Z",
"pub_end": "2119-12-31T23:59:59Z",
"user_id": 1
}

###

+ 6
- 0
delete_notice.http View File

DELETE http://127.0.0.1:8000/polls/notices/15/ HTTP/1.1
Content-Type: application/json

{}

###

+ 1
- 0
mysite/settings.py View File

# Application definition # Application definition


INSTALLED_APPS = [ INSTALLED_APPS = [
'rest_framework',
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',

+ 9
- 0
polls/serializers.py View File

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', 'user_id']



+ 3
- 0
polls/urls.py View File

path('', views.index, name='index'), path('', views.index, name='index'),
path('new', views.new, name='new'), path('new', views.new, name='new'),
path('delete/<int:deleteId>', views.delete, name='delete'), path('delete/<int:deleteId>', views.delete, name='delete'),

path('notices/', views.notice_list, name='notice_list'),
path('notices/<int:id>/', views.notice_detail, name='notice_detail'),
] ]





+ 49
- 2
polls/views.py View File

from django.http import JsonResponse
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from rest_framework.parsers import JSONParser

from .models import Notice from .models import Notice
from django.utils import timezone from django.utils import timezone
from .forms import NoticeForm from .forms import NoticeForm
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt



from django.contrib import messages from django.contrib import messages
from .serializers import NoticeSerializer




@login_required @login_required
notice = Notice.objects.get(pk=deleteId) notice = Notice.objects.get(pk=deleteId)
if request.user.id == notice.user_id or request.user.is_staff: if request.user.id == notice.user_id or request.user.is_staff:
notice.delete() notice.delete()
messages.success(request, f"Nachricht gelöscht")
messages.success(request, f"Nachricht cht")
else: else:
messages.warning(request, f"Keine Berechtigung") messages.warning(request, f"Keine Berechtigung")
except: except:
messages.warning(request, f"Nachricht nicht gefunden") messages.warning(request, f"Nachricht nicht gefunden")
return redirect('index') return redirect('index')



def index(request): def index(request):
notices = Notice.objects.all() notices = Notice.objects.all()
notices = notices.filter(pub_start__lte=timezone.now()) notices = notices.filter(pub_start__lte=timezone.now())
context = {'notices': notices} context = {'notices': notices}
return render(request, 'polls/index.html', context) return render(request, 'polls/index.html', context)




@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)
else:
return JsonResponse({'error': 'GET or POST request required'}, status=400)

@csrf_exempt
def notice_detail(request, id):
try:
notice = Notice.objects.get(pk=id)
except Notice.DoesNotExist:
return JsonResponse({'error': 'Notice not found'}, 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 JsonResponse({'message': 'Notice was deleted successfully!'}, status=204)
else:
return JsonResponse({'error': 'GET, PUT or DELETE request required'}, status=400)

+ 1
- 0
requirements.txt View File

Django>=4.2.6 Django>=4.2.6
djangorestframework>=3.14.0

Loading…
Cancel
Save