# Path: yak/rest_notifications/views.py
# class NotificationSettingViewSet(mixins.UpdateModelMixin,
#                                  mixins.ListModelMixin,
#                                  GenericViewSet):
#     queryset = NotificationSetting.objects.all()
#     serializer_class = NotificationSettingSerializer
#     permission_classes = (IsOwner,)
# 
#     def perform_update(self, serializer):
#         serializer.save(user=self.request.user)
# 
#     def get_queryset(self):
#         return self.queryset.filter(user=self.request.user)
# 
#     def put(self, request, *args, **kwargs):
#         queryset = self.get_queryset()
#         serializer = self.get_serializer(queryset, data=request.data, many=True, partial=True)
# 
#         serializer.is_valid(raise_exception=True)
#         serializer.save()
#         return Response(serializer.data, status=status.HTTP_200_OK)
# 
# class NotificationView(generics.ListAPIView):
#     queryset = Notification.objects.all()
#     serializer_class = NotificationSerializer
#     permission_classes = (IsOwner,)
# 
#     def get_queryset(self):
#         return self.queryset.filter(user=self.request.user)
# 
# class NotificationFollowViewSet(FollowViewSet):
#     def perform_create(self, serializer):
#         obj = serializer.save(user=self.request.user)
#         notification_type = NotificationType.objects.get(slug="follow")
#         create_notification(obj.content_object, obj.user, obj.content_object, notification_type)
# 
# class NotificationLikeViewSet(LikeViewSet):
#     def perform_create(self, serializer):
#         obj = serializer.save(user=self.request.user)
#         notification_type = NotificationType.objects.get(slug="like")
#         create_notification(obj.content_object.user, obj.user, obj.content_object, notification_type)
# 
# class NotificationShareViewSet(ShareViewSet):
#     def perform_create(self, serializer):
#         obj = serializer.save(user=self.request.user)
#         notification_type = NotificationType.objects.get(slug="share")
#         for receiver in obj.shared_with.all():
#             create_notification(receiver, obj.user, obj.content_object, notification_type)
# 
# class NotificationCommentViewSet(CommentViewSet):
#     def perform_create(self, serializer):
#         obj = serializer.save(user=self.request.user)
#         notification_type = NotificationType.objects.get(slug="comment")
#         create_notification(obj.content_object.user, obj.user, obj.content_object, notification_type)
# 
# class PushwooshTokenView(generics.CreateAPIView):
#     queryset = PushwooshToken.objects.all()
#     serializer_class = PushwooshTokenSerializer
#     permission_classes = (IsOwner,)
# 
#     def perform_create(self, serializer):
#         hwid = serializer.validated_data.pop("hwid")
#         language = serializer.validated_data.pop("language")
#         platform = serializer.validated_data.pop("platform")
# 
#         platform_code = constants.PLATFORM_IOS
#         if platform == 'android':
#             platform_code = constants.PLATFORM_ANDROID
# 
#         push_client = client.PushwooshClient()
#         command = RegisterDeviceCommand(yak_settings.PUSHWOOSH_APP_CODE, hwid, platform_code,
#                                         serializer.validated_data["token"], language)
#         response = push_client.invoke(command)
# 
#         if response["status_code"] != 200:
#             raise AuthenticationFailed("Authentication with notification service failed")
# 
#         serializer.save(user=self.request.user)


from django.conf.urls import url, include
from rest_framework import routers
from yak.rest_notifications.views import NotificationSettingViewSet, NotificationView, \
    NotificationFollowViewSet, NotificationLikeViewSet, NotificationShareViewSet, NotificationCommentViewSet, \
    PushwooshTokenView


router = routers.DefaultRouter()

router.register(r'notification_settings', NotificationSettingViewSet, base_name='notification_settings')
router.register(r'follows', NotificationFollowViewSet, base_name='follows')
router.register(r'likes', NotificationLikeViewSet, base_name='likes')
router.register(r'shares', NotificationShareViewSet, base_name='shares')
router.register(r'comments', NotificationCommentViewSet, base_name='comments')

urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^notifications/$', NotificationView.as_view(), name="notifications"),

# Path: yak/rest_notifications/views.py
# class NotificationSettingViewSet(mixins.UpdateModelMixin,
#                                  mixins.ListModelMixin,
#                                  GenericViewSet):
#     queryset = NotificationSetting.objects.all()
#     serializer_class = NotificationSettingSerializer
#     permission_classes = (IsOwner,)
# 
#     def perform_update(self, serializer):
#         serializer.save(user=self.request.user)
# 
#     def get_queryset(self):
#         return self.queryset.filter(user=self.request.user)
# 
#     def put(self, request, *args, **kwargs):
#         queryset = self.get_queryset()
#         serializer = self.get_serializer(queryset, data=request.data, many=True, partial=True)
# 
#         serializer.is_valid(raise_exception=True)
#         serializer.save()
#         return Response(serializer.data, status=status.HTTP_200_OK)
# 
# class NotificationView(generics.ListAPIView):
#     queryset = Notification.objects.all()
#     serializer_class = NotificationSerializer
#     permission_classes = (IsOwner,)
# 
#     def get_queryset(self):
#         return self.queryset.filter(user=self.request.user)
# 
# class NotificationFollowViewSet(FollowViewSet):
#     def perform_create(self, serializer):
#         obj = serializer.save(user=self.request.user)
#         notification_type = NotificationType.objects.get(slug="follow")
#         create_notification(obj.content_object, obj.user, obj.content_object, notification_type)
# 
# class NotificationLikeViewSet(LikeViewSet):
#     def perform_create(self, serializer):
#         obj = serializer.save(user=self.request.user)
#         notification_type = NotificationType.objects.get(slug="like")
#         create_notification(obj.content_object.user, obj.user, obj.content_object, notification_type)
# 
# class NotificationShareViewSet(ShareViewSet):
#     def perform_create(self, serializer):
#         obj = serializer.save(user=self.request.user)
#         notification_type = NotificationType.objects.get(slug="share")
#         for receiver in obj.shared_with.all():
#             create_notification(receiver, obj.user, obj.content_object, notification_type)
# 
# class NotificationCommentViewSet(CommentViewSet):
#     def perform_create(self, serializer):
#         obj = serializer.save(user=self.request.user)
#         notification_type = NotificationType.objects.get(slug="comment")
#         create_notification(obj.content_object.user, obj.user, obj.content_object, notification_type)
# 
# class PushwooshTokenView(generics.CreateAPIView):
#     queryset = PushwooshToken.objects.all()
#     serializer_class = PushwooshTokenSerializer
#     permission_classes = (IsOwner,)
# 
#     def perform_create(self, serializer):
#         hwid = serializer.validated_data.pop("hwid")
#         language = serializer.validated_data.pop("language")
#         platform = serializer.validated_data.pop("platform")
# 
#         platform_code = constants.PLATFORM_IOS
#         if platform == 'android':
#             platform_code = constants.PLATFORM_ANDROID
# 
#         push_client = client.PushwooshClient()
#         command = RegisterDeviceCommand(yak_settings.PUSHWOOSH_APP_CODE, hwid, platform_code,
#                                         serializer.validated_data["token"], language)
#         response = push_client.invoke(command)
# 
#         if response["status_code"] != 200:
#             raise AuthenticationFailed("Authentication with notification service failed")
# 
#         serializer.save(user=self.request.user)


# Path: yak/rest_notifications/urls.py
from django.conf.urls import url, include
from rest_framework import routers
from yak.rest_notifications.views import NotificationSettingViewSet, NotificationView, \
    NotificationFollowViewSet, NotificationLikeViewSet, NotificationShareViewSet, NotificationCommentViewSet, \
    PushwooshTokenView



router = routers.DefaultRouter()

router.register(r'notification_settings', NotificationSettingViewSet, base_name='notification_settings')
router.register(r'follows', NotificationFollowViewSet, base_name='follows')
router.register(r'likes', NotificationLikeViewSet, base_name='likes')
router.register(r'shares', NotificationShareViewSet, base_name='shares')
router.register(r'comments', NotificationCommentViewSet, base_name='comments')

urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^notifications/$', NotificationView.as_view(), name="notifications"),

    url(r'^pushwoosh_token/$', PushwooshTokenView.as_view(), name="pushwoosh_token"),