diff --git a/news/venv/lib/python3.7/site-packages/django/conf/locale/az/__init__.py b/news/venv/lib/python3.7/site-packages/django/conf/locale/az/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/news1/db.sqlite3 b/news1/db.sqlite3 index 89bcc51..769ea99 100644 Binary files a/news1/db.sqlite3 and b/news1/db.sqlite3 differ diff --git a/news1/news1/settings.py b/news1/news1/settings.py index a6f749a..c8deda9 100644 --- a/news1/news1/settings.py +++ b/news1/news1/settings.py @@ -38,6 +38,7 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'posts.apps.PostsConfig', + 'rest_framework', ] MIDDLEWARE = [ diff --git a/news1/posts/serializers.py b/news1/posts/serializers.py new file mode 100644 index 0000000..4ad5466 --- /dev/null +++ b/news1/posts/serializers.py @@ -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','notice_text', 'pub_start', 'pub_end') diff --git a/news1/posts/urls.py b/news1/posts/urls.py index 530c1ec..f88a15c 100644 --- a/news1/posts/urls.py +++ b/news1/posts/urls.py @@ -5,4 +5,7 @@ urlpatterns = [ 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/news1/posts/views.py b/news1/posts/views.py index 800d9f3..1436d08 100644 --- a/news1/posts/views.py +++ b/news1/posts/views.py @@ -1,9 +1,13 @@ from django.shortcuts import render, redirect +from django.views.decorators.csrf import csrf_exempt +from rest_framework.parsers import JSONParser from posts.forms import NoticeForm +from posts.serializers import NoticeSerializer from .models import Notice from django.utils import timezone -from django.http import HttpResponse +from django.http import HttpResponse, JsonResponse + def index(request): notices = Notice.objects.all() @@ -31,4 +35,37 @@ def delete(request, deleteId=None): delNotice = Notice.objects.get(id=deleteId) if delNotice != None: delNotice.delete() - return redirect('index') \ No newline at end of file + 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)