Browse Source

p8

master
khaloufam70043 5 years ago
parent
commit
8a364f60e5
7 changed files with 56 additions and 3 deletions
  1. BIN
      db.sqlite3
  2. 1
    0
      news/settings.py
  3. 1
    0
      posts/models.py
  4. 7
    0
      posts/serializers.py
  5. 2
    0
      posts/urls.py
  6. 44
    2
      posts/views.py
  7. 1
    1
      venv/pip-selfcheck.json

BIN
db.sqlite3 View File


+ 1
- 0
news/settings.py View File

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

MIDDLEWARE = [

+ 1
- 0
posts/models.py View File

@@ -1,5 +1,6 @@
from django.db import models


# Create your models here.
class Notice(models.Model):
notice_title = models.CharField(max_length=80)

+ 7
- 0
posts/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', 'notice_text', 'pub_start', 'pub_end')

+ 2
- 0
posts/urls.py View File

@@ -5,4 +5,6 @@ urlpatterns = [
path('', views.index, name='index'),
path('new', views.new, name='new'),
path('delete/<int:deleteId>', views.delete, name='delete'),
path('notices/', views.notice_list),
path('notices/<int:id>', views.notice_detail),
]

+ 44
- 2
posts/views.py View File

@@ -1,7 +1,12 @@
from django.shortcuts import render
from django.shortcuts import render, redirect
from .models import Notice
from django.utils import timezone
from .forms import NoticeForm
from .serializers import NoticeSerializer
from django.http import JsonResponse, HttpResponse
from rest_framework.parsers import JSONParser
from django.views.decorators.csrf import csrf_exempt


def index(request):
notices = Notice.objects.all()
@@ -29,4 +34,41 @@ def delete(request, deleteId=None):
delNotice = Notice.objects.get(id=deleteId)
if delNotice != None:
delNotice.delete()
return redirect('index')
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)

+ 1
- 1
venv/pip-selfcheck.json View File

@@ -1 +1 @@
{"last_check":"2019-11-19T13:10:17Z","pypi_version":"19.3.1"}
{"last_check":"2019-12-03T13:13:19Z","pypi_version":"19.3.1"}

Loading…
Cancel
Save