コード例 #1
0
 def post(self, request, *args, **kwargs):
     if request.content_type == 'text/plain;charset=UTF-8':
         data = json.loads(request.body.decode('utf-8'))
     else:
         data = request.data
     user = get_user_from_request(request)
     # todo: validation throuth forms
     title = data['title']
     authors = data['authors']
     genre = data['genre']
     existing_books = Book.objects.filter(title__contains=title, authors__contains=authors)
     if existing_books:
         book = existing_books[0]
     else:
         book = Book(title=title, authors=authors, genre=genre)
         book.save()
     book_item = BookItem.objects.create(book=book, owner=user)
     if data.get('image'):
         path = 'books/{}/{}.jpg'.format(user.id, book_item.id)
         if data.get('image').find('data:image') != -1:
             image = data['image']
             upload_file(path, image)
             book_item.image = MEDIA_URL + path
             if not book.image:
                 book.image = MEDIA_URL + path
                 book.save()
             book_item.save()
         else:
             book_item.image = data['image']
             if not book.image:
                 book.image = MEDIA_URL + path
                 book.save()
             book_item.save()
     if data.get('isbn'):
         book_item.isbn = data['isbn']
         book_item.save()
     serializer = self.get_serializer(book_item)
     return Response(serializer.data)
コード例 #2
0
def handle_suggestion(request):
    if request.method == "POST":
        confirmation = request.POST['confirmation']
        suggestion = Suggestion.objects.get(slug=request.POST['book-slug'])

        if confirmation == "confirm":
            book = Book(title=suggestion.title,
                        author=suggestion.author,
                        description=suggestion.description,
                        url=suggestion.url,
                        slug=slugify(suggestion.title))
            if suggestion.image:
                book.image = suggestion.image
            suggestion.delete()
            book.save()

            return HttpResponse(json.dumps(["success"]))
        elif confirmation == "decline":
            suggestion.delete()

            return HttpResponse(json.dumps(["success"]))
    else:
        return Http404()