[U] fix error when post office hour w/o login. returns json error

instead.
This commit is contained in:
Ching 2019-11-15 17:51:17 +08:00
parent 0f7e710889
commit d0c1b65632
2 changed files with 7 additions and 2 deletions

View File

@ -15,7 +15,7 @@ class OfficeHoursForm(forms.ModelForm):
def full_clean(self):
if not self.user.is_authenticated:
raise ValidationError('Invalid User.')
raise forms.ValidationError('Invalid User.')
return
def save(self):

View File

@ -1,10 +1,12 @@
from django.contrib.auth.models import User, Group
import django.forms
from django.shortcuts import render
import django.views.generic
from rest_framework import authentication, permissions, status, viewsets
from rest_framework.generics import CreateAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from django.http import JsonResponse
import timer.serializers
import timer.models
@ -57,8 +59,11 @@ class OfficeHoursFormView(django.views.generic.FormView):
def post(self, request, *args, **kwargs):
form = self.form_class(**self.get_form_kwargs())
if form.is_valid():
try:
form.is_valid()
form.save()
except django.forms.ValidationError as e:
return JsonResponse({'error': e.message}, status=status.HTTP_400_BAD_REQUEST)
return super().post(request, *args, **kwargs)