server/ammico/views.py
changeset 86 2deead259283
parent 72 ed2ee692ff6f
child 90 faf2cdb47813
equal deleted inserted replaced
85:5c8d95d96c58 86:2deead259283
    75 
    75 
    76     def get(self, request):
    76     def get(self, request):
    77         """
    77         """
    78         Return a list of all Books.
    78         Return a list of all Books.
    79         """
    79         """
    80         books = Book.objects.all()
    80         books = Book.objects.filter(user = request.user.id)
    81         serializer = BookSerializer(books, many=True)
    81         serializer = BookSerializer(books, many=True)
    82         return Response(serializer.data)
    82         return Response(serializer.data)
    83     
    83     
    84     def post(self, request):
    84     def post(self, request):
    85         """
    85         """
    90             "user":request.user.id,
    90             "user":request.user.id,
    91         }
    91         }
    92         
    92         
    93         if ('idParent' in request.data):
    93         if ('idParent' in request.data):
    94             try:
    94             try:
    95                 book = Book.objects.get(id=request.data['idParent'])
    95                 book = Book.objects.get(user = request.user.id, id=request.data['idParent'])
    96             except Book.DoesNotExist:
    96             except Book.DoesNotExist:
    97                 return HttpResponse(status=404)
    97                 return HttpResponse(status=404)
    98             new_book = deepcopy(book)
    98             new_book = deepcopy(book)
    99             new_book.title = request.data['title']
    99             new_book.title = request.data['title']
   100             new_book.idArticle = None
   100             new_book.idArticle = None
   128     def get(self, request, idBook):
   128     def get(self, request, idBook):
   129         """
   129         """
   130         Get information from a book
   130         Get information from a book
   131         """
   131         """
   132         try:
   132         try:
   133             book = Book.objects.get(id=idBook)
   133             book = Book.objects.get(user = request.user.id, id=idBook)
   134         except Book.DoesNotExist:
   134         except Book.DoesNotExist:
   135             return HttpResponse(status=404)
   135             return HttpResponse(status=404)
   136         
   136         
   137         serializer = BookSerializer(book)
   137         serializer = BookSerializer(book)
   138         return Response(serializer.data)
   138         return Response(serializer.data)
   139     
   139     
   140     def delete(self, request, idBook):
   140     def delete(self, request, idBook):
   141         """
   141         """
   142         delete a book
   142         delete a book
   143         """
   143         """
   144         book = Book.objects.get(id = idBook)
   144         book = Book.objects.get(user = request.user.id, id = idBook)
   145         book.delete()
   145         book.delete()
   146         return Response(status=status.HTTP_204_NO_CONTENT)
   146         return Response(status=status.HTTP_204_NO_CONTENT)
   147     
   147     
   148 class BookSlides(APIView):
   148 class BookSlides(APIView):
   149     """
   149     """
   152     authentication_classes = (TokenAuthentication,)
   152     authentication_classes = (TokenAuthentication,)
   153     permission_classes = (IsAuthenticated,)
   153     permission_classes = (IsAuthenticated,)
   154 
   154 
   155     def get(self, request, idBook):
   155     def get(self, request, idBook):
   156         try:
   156         try:
   157             book = Book.objects.get(id=idBook)
   157             book = Book.objects.get(user = request.user.id, id=idBook)
   158         except Book.DoesNotExist:
   158         except Book.DoesNotExist:
   159             return HttpResponse(status=404)
   159             return HttpResponse(status=404)
   160         
   160         
   161         serializer = SlideSerializer(book.slides.all(), many=True)
   161         serializer = SlideSerializer(book.slides.all(), many=True)
   162         return Response(serializer.data)
   162         return Response(serializer.data)
   173     def get(self, request, idBook):
   173     def get(self, request, idBook):
   174         """
   174         """
   175         Get slides order
   175         Get slides order
   176         """
   176         """
   177         try:
   177         try:
   178             book = Book.objects.get(id=idBook)
   178             book = Book.objects.get(user = request.user.id, id=idBook)
   179         except Book.DoesNotExist:
   179         except Book.DoesNotExist:
   180             return HttpResponse(status=404)
   180             return HttpResponse(status=404)
   181         response = {}
   181         response = {}
   182         response["order"] = book.get_slide_order()
   182         response["order"] = book.get_slide_order()
   183         return Response(response)
   183         return Response(response)
   185     def post(self, request, idBook):
   185     def post(self, request, idBook):
   186         """
   186         """
   187         Set Slides order
   187         Set Slides order
   188         """
   188         """
   189         try:
   189         try:
   190             book = Book.objects.get(id=idBook)
   190             book = Book.objects.get(user = request.user.id, id=idBook)
   191         except Book.DoesNotExist:
   191         except Book.DoesNotExist:
   192             return HttpResponse(status=404)
   192             return HttpResponse(status=404)
   193         
   193         
   194         book.set_slide_order(request.data['order'])
   194         book.set_slide_order(request.data['order'])
   195         return Response(status=status.HTTP_200_OK)
   195         return Response(status=status.HTTP_200_OK)
   204 
   204 
   205     def get(self, request):
   205     def get(self, request):
   206         """
   206         """
   207         Return a list of slide
   207         Return a list of slide
   208         """               
   208         """               
   209         slides = Slide.objects.filter()
   209         slides = Slide.objects.filter(book__user = request.user.id)
   210         serializer = SlideSerializer(slides, many=True)
   210         serializer = SlideSerializer(slides, many=True)
   211         return Response(serializer.data)
   211         return Response(serializer.data)
   212     
   212     
   213     def post(self, request):
   213     def post(self, request):
   214         """
   214         """
   231     def get(self, request, idSlide):
   231     def get(self, request, idSlide):
   232         """
   232         """
   233         Get information from a slide
   233         Get information from a slide
   234         """
   234         """
   235         try:
   235         try:
   236             slide = Slide.objects.get(id=idSlide)
   236             slide = Slide.objects.get(book__user = request.user.id, id=idSlide)
   237         except Slide.DoesNotExist:
   237         except Slide.DoesNotExist:
   238             return HttpResponse(status=404)
   238             return HttpResponse(status=404)
   239         
   239         
   240         serializer = SlideSerializer(slide)
   240         serializer = SlideSerializer(slide)
   241         return Response(serializer.data)
   241         return Response(serializer.data)
   243     def post(self, request, idSlide):
   243     def post(self, request, idSlide):
   244         """
   244         """
   245         Update slide information
   245         Update slide information
   246         """
   246         """
   247         try:
   247         try:
   248             slide = Slide.objects.get(id=idSlide)
   248             slide = Slide.objects.get(book__user = request.user.id, id=idSlide)
   249         except Slide.DoesNotExist:
   249         except Slide.DoesNotExist:
   250             return HttpResponse(status=404)
   250             return HttpResponse(status=404)
   251         serializer = SlideSerializer(slide, data=request.data)
   251         serializer = SlideSerializer(slide, data=request.data)
   252         if serializer.is_valid():
   252         if serializer.is_valid():
   253             serializer.save()
   253             serializer.save()
   257     def delete(self, request, idSlide):
   257     def delete(self, request, idSlide):
   258         """
   258         """
   259         Delete a slide
   259         Delete a slide
   260         """
   260         """
   261         try:
   261         try:
   262             slide = Slide.objects.get(id = idSlide)
   262             slide = Slide.objects.get(book__user = request.user.id, id = idSlide)
   263         except Slide.DoesNotExist:
   263         except Slide.DoesNotExist:
   264             return HttpResponse(status=404)
   264             return HttpResponse(status=404)
   265         
   265         
   266         slide.delete()
   266         slide.delete()
   267         return Response(status=status.HTTP_204_NO_CONTENT)
   267         return Response(status=status.HTTP_204_NO_CONTENT)