|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- import logging
-
- from django.shortcuts import render, redirect, render_to_response
- from django.http import HttpResponse, JsonResponse
- from django.utils import timezone
- from django.contrib import messages
- from django.views.decorators.csrf import csrf_exempt
- from django_eventstream import send_event
-
- from rest_framework.renderers import JSONRenderer
- from rest_framework.parsers import JSONParser
-
- from .models import Notice
- from .forms import NoticeForm, EditNoticeForm
- from .serializers import NoticeSerializer
-
- logger = None
-
- def init_loger(logger_type):
- global logger
- if logger == None:
- logger = logging.getLogger()
- logger.setLevel(logging.DEBUG)
- logger.addHandler(logging.StreamHandler())
-
- # Create your views here.
- def index(request):
- init_loger('django.db.backends')
- notices = Notice.objects.all()
- notices = Notice.objects.filter(pub_start__lte=timezone.now(), pub_end__gte=timezone.now())
- message = ""
-
- context = {
- "notices": notices,
- 'message': message,
- }
- return render(request, 'posts/index.html', context)
-
- #@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'])
- newNotice.save()
- send_event('notice', 'message', newNotice.id)
- return redirect('posts')
-
- context = {'form' : NoticeForm()}
- return render(request, 'posts/edit.html', context)
-
- def delete(request, deleteId=None):
- init_loger(__name__)
- logger.info(deleteId)
- message = ""
- if deleteId:
- delNotice = Notice.objects.get(id=deleteId)
- if delNotice:
- delNotice.delete()
- message = "Notice has been deleted."
- send_event('notice', 'message', "Notice has been deleted.")
- return redirect('posts')
-
-
- def edit(request, editId=None):
- init_loger(__name__)
- logger.debug(editId)
- logger.debug(request.method)
- editNotice = None
- if editId:
- editNotice = Notice.objects.get(id=editId)
- logger.debug(editNotice)
- if editNotice:
- form = EditNoticeForm(editNotice)
- #form.title = editNotice.notice_title
- #form.notice_to_form(editNotice)
- logger.info(form)
- #if form.is_valid():
- # return HttpResponse(status=204)
- context = {'form' : form}
- return render(request, 'posts/edit.html', context)
-
- """
- 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()
- send_event('notice', 'message', newNotice.id)
- return redirect('posts')
- """
-
- @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)
|