def create(self, request): """ Bookmark a Listing for the current user. POST JSON data: { "listing": { "id": 1 }, "folder": "folderName" (optonal) } --- parameters: - name: body required: true paramType: body parameters_strategy: form: replace query: replace omit_serializer: true """ serializer = serializers.UserLibrarySerializer( data=request.data, context={'request': request}) if not serializer.is_valid(): logger.error('{0!s}'.format(serializer.errors)) raise errors.RequestException('{0!s}'.format(serializer.errors)) serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED)
def list(self, request): """ The current user's bookmarked listings --- serializer: ozpcenter.api.library.serializers.UserLibrarySerializer """ queryset = self.get_queryset() serializer = serializers.UserLibrarySerializer( queryset, many=True, context={'request': request}) return Response(serializer.data)
def create_batch(self, request): """ Create Batch """ current_request_username = request.user.username request_data = request.data data = model_access.create_batch_library_entries( current_request_username, request_data) serializer = serializers.UserLibrarySerializer( data, many=True, context={'request': request}) return Response(serializer.data, status=status.HTTP_201_CREATED)
def import_bookmarks(self, request): """ Import Bookmarks """ current_request_username = request.user.username bookmark_notification_id = request.data.get('bookmark_notification_id') error, data = model_access.import_bookmarks(current_request_username, bookmark_notification_id) if error: raise errors.RequestException('{0!s}'.format(error)) else: serializer = serializers.UserLibrarySerializer( data, many=True, context={'request': request}) return Response(serializer.data, status=status.HTTP_201_CREATED)
def import_bookmarks(self, request): """ Import Bookmarks """ current_request_username = request.user.username bookmark_notification_id = request.data.get('bookmark_notification_id') errors, data = model_access.import_bookmarks(current_request_username, bookmark_notification_id) if errors: return Response({'message': '{0}'.format(errors)}, status=status.HTTP_400_BAD_REQUEST) else: serializer = serializers.UserLibrarySerializer( data, many=True, context={'request': request}) return Response(serializer.data, status=status.HTTP_201_CREATED)
def retrieve(self, request, pk=None): """ Retrieve by library id (not listing id) """ queryset = self.get_queryset() library_entry = get_object_or_404(queryset, pk=pk) serializer = serializers.UserLibrarySerializer( library_entry, context={'request': request}) request_username = request.user.username listing_type = self.request.query_params.get('type', None) cache_key = 'library_self-{0}-{1}-{2}'.format(listing_type, request_username, pk) cache_data = cache.get(cache_key) if not cache_data: cache_data = serializer.data cache.set(cache_key, cache_data, timeout=settings.GLOBAL_SECONDS_TO_CACHE_DATA) return Response(cache_data)