Esempio n. 1
0
    def post(self, request, *args, **kwargs):
        user_url = request.data.get("user", None)
        if user_url:
            try:
                path = urlparse.urlparse(user_url).path
                match = resolve(path)
                user_pk = match.kwargs.get("pk")
            except (Resolver404, User.DoesNotExist) as e:
                raise CustomAPIException(
                    "You need to send the url of a User to create its UserPreference.",
                    status=status.HTTP_400_BAD_REQUEST,
                )
        else:
            raise CustomAPIException(
                "You need to identify a User to create its UserPreference.",
                status=status.HTTP_400_BAD_REQUEST,
            )

        userpreference_obj = UserPreference(user_id=user_pk,
                                            send_email=request.data.get(
                                                "send_email", False))
        userpreference_obj.save()
        d = UserPreferenceSerializer(userpreference_obj,
                                     context={
                                         "request": request
                                     }).data
        return Response(d, status=status.HTTP_201_CREATED)
Esempio n. 2
0
    def test_delete(self):
        userpreference = UserPreference(**self.test_user_preference_data)
        userpreference.save()

        retr_userpreference = UserPreference.objects.get(user_id=self.test_user.id)
        retr_userpreference.delete()

        retr_userpreference = UserPreference.objects.filter(user_id=self.test_user.id)
        self.assertFalse(retr_userpreference.exists())
Esempio n. 3
0
    def test_delete(self):
        userpreference = UserPreference(**self.test_user_preference_data)
        userpreference.save()

        retr_userpreference = UserPreference.objects.get(
            user_id=self.test_user.id)
        retr_userpreference.delete()

        retr_userpreference = UserPreference.objects.filter(
            user_id=self.test_user.id)
        self.assertFalse(retr_userpreference.exists())
Esempio n. 4
0
    def post(self, request, *args, **kwargs):
        user_url = request.data.get('user', None)
        if user_url:
            try:
                path = urlparse.urlparse(user_url).path
                match = resolve(path)
                user_pk = match.kwargs.get('pk')
            except (Resolver404, User.DoesNotExist) as e:
                raise CustomAPIException("You need to send the url of a User to create its UserPreference.", status=status.HTTP_400_BAD_REQUEST)
        else:
            raise CustomAPIException("You need to identify a User to create its UserPreference.", status=status.HTTP_400_BAD_REQUEST)

        userpreference_obj = UserPreference(user_id=user_pk, send_email=request.data.get('send_email', False))
        userpreference_obj.save()
        d = UserPreferenceSerializer(userpreference_obj, context={'request': request}).data
        return Response(d, status=status.HTTP_201_CREATED)