combo/combo/apps/notifications/api_views.py

85 lines
3.3 KiB
Python

# combo - content management system
# Copyright (C) 2016 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from rest_framework import serializers, permissions, status
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from .models import Notification
class NotificationSerializer(serializers.Serializer):
summary = serializers.CharField(required=True, allow_blank=False, max_length=140)
id = serializers.CharField(required=False, allow_null=True)
body = serializers.CharField(allow_blank=False, default='')
url = serializers.URLField(allow_blank=True, default='')
origin = serializers.CharField(allow_blank=True, default='')
start_timestamp = serializers.DateTimeField(required=False, allow_null=True)
end_timestamp = serializers.DateTimeField(required=False, allow_null=True)
duration = serializers.IntegerField(required=False, allow_null=True, min_value=0)
class Add(GenericAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = NotificationSerializer
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if not serializer.is_valid():
response = {'err': 1, 'err_desc': serializer.errors}
return Response(response, status.HTTP_400_BAD_REQUEST)
data = serializer.validated_data
try:
notification = Notification.notify(
user=request.user,
summary=data['summary'],
id=data.get('id'),
body=data.get('body'),
url=data.get('url'),
origin=data.get('origin'),
start_timestamp=data.get('start_timestamp'),
end_timestamp=data.get('end_timestamp'),
duration=data.get('duration'),
)
except ValueError as e:
response = {'err': 1, 'err_desc': {'id': [unicode(e)]}}
return Response(response, status.HTTP_400_BAD_REQUEST)
else:
response = {'err': 0, 'data': {'id': notification.public_id}}
return Response(response)
add = Add.as_view()
class Ack(GenericAPIView):
permission_classes = (permissions.IsAuthenticated,)
def get(self, request, notification_id, *args, **kwargs):
Notification.objects.find(request.user, notification_id).ack()
return Response({'err': 0})
ack = Ack.as_view()
class Forget(GenericAPIView):
permission_classes = (permissions.IsAuthenticated,)
def get(self, request, notification_id, *args, **kwargs):
Notification.objects.find(request.user, notification_id).forget()
return Response({'err': 0})
forget = Forget.as_view()