Пример #1
0
def get_movie(request, pk):
    """
    function to handle request for a particular movie
    GET => uesd to query
    POST w/o id, to insert and return inserted value
    POST w/ id, to update existing record
    :param request: incomming http request
    :param pk: primary key of the movie requested
    :return:
    """
    #check the incomming method
    try:
        if request.method == "GET":
            if pk != '':
                _movie = Movie.objects.get(pk=pk).json()
                response = HttpResponse(_movie, content_type="application/json")
                return response
            else:
                response = search_movie(request)
                return response
                raise Movie.MultipleObjectsReturned()
        elif request.method == 'POST':
            #check if user is authenticated to touch it
            #if pk='', insert, else overwrite
            if pk == '' and has_perm(request, 'IMDB.create_movie'):
                _m = Movie()
            elif pk != '' and has_perm(request, 'IMDB.change_movie'):
                _m = get_object_or_404(Movie, pk=pk)
            else:
                raise PermissionDenied()
            _m.add_using_json(request.body)
            _m.save()
            return HttpResponse(_m.json(), content_type="application/json", status=201)
        elif request.method == 'DELETE':
            if pk != '':
                if has_perm(request, 'delete_movie'):
                    _m = get_object_or_404(Movie, pk=pk)
                    _m.delete()
                    return HttpResponse('delete successful', content_type="application/json", status=200)
                else:
                    raise PermissionDenied()
            else:
                raise Movie.MultipleObjectsReturned()
        else:
            raise Movie.MultipleObjectsReturned()  #avoiding modification to the entire series
    except IntegrityError as ie:
        return HttpResponseBadRequest("{'status':400,'message':'Bad Request -- Integrity violation:" + ie.message + "'}",
                                        content_type="application/json")
    except KeyError as k:
        return HttpResponseBadRequest("{'status':400,'message':'Bad Request -- Key violation:" + k.message + "'}",
                                      content_type="application/json")
    except Movie.MultipleObjectsReturned as e:
        return HttpResponseNotFound(json.dumps({'status': 404, 'message': 'movie not found'}),
                                    content_type="application/json")
    except (Movie.DoesNotExist, Http404):
        return HttpResponseNotFound(json.dumps({'status': 404, 'message': 'movie not found'}),
                                    content_type="application/json")
    except PermissionDenied as p:
        return HttpResponseForbidden(json.dumps({'status': 403, 'message': 'Permission Denied{0:s}'.format(p.message)}),
                                     content_type="application/json")
Пример #2
0
 def test_add_movie_using_json_model(self):
     f = open('single_movie.json', 'r')
     m = Movie()
     m.add_using_json(f.read())
     m.save()
     self.assertEqual(Movie.objects.get(movie_name='three little pigs'),m)