当前位置: 首页>>代码示例>>Python>>正文


Python Movie.add_using_json方法代码示例

本文整理汇总了Python中models.Movie.add_using_json方法的典型用法代码示例。如果您正苦于以下问题:Python Movie.add_using_json方法的具体用法?Python Movie.add_using_json怎么用?Python Movie.add_using_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.Movie的用法示例。


在下文中一共展示了Movie.add_using_json方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_movie

# 需要导入模块: from models import Movie [as 别名]
# 或者: from models.Movie import add_using_json [as 别名]
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")
开发者ID:jiteshpunjabi,项目名称:movie-rest-api,代码行数:62,代码来源:views.py

示例2: test_add_movie_using_json_model

# 需要导入模块: from models import Movie [as 别名]
# 或者: from models.Movie import add_using_json [as 别名]
 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)
开发者ID:jiteshpunjabi,项目名称:movie-rest-api,代码行数:8,代码来源:tests.py


注:本文中的models.Movie.add_using_json方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。