Exemplo n.º 1
0
def index(request):
    params = function.json_decode(request.body)
    data = {}
    if not params.get('name'):
        return function.ehcoJson({
            'code': 400,
            'msg': function.ReturnCode[400]
        })
    data['name'] = params.get('name')
    if not params.get('city'):
        return function.ehcoJson({
            'code': 400,
            'msg': function.ReturnCode[400]
        })
    data['city'] = params.get('city')
    if not params.get('email'):
        return function.ehcoJson({
            'code': 400,
            'msg': function.ReturnCode[400]
        })
    data['email'] = params.get('email')
    data['is_valid'] = 1

    publish = Publish(**data)
    publish.save()
    if publish:
        print(publish)
        return function.ehcoJson({
            'code': 200,
            'msg': function.ReturnCode[200]
        })
    return function.ehcoJson({'code': 500, 'msg': function.ReturnCode[500]})
Exemplo n.º 2
0
def index(request):
    params = function.json_decode(request.body)
    if params.get('id') and params.get('name') and params.get('price') and \
            params.get('publish_date') and params.get('publish__id') and params.get('authors') :
        if params.get('is_valid') == 1 or params.get('is_valid') == 0:

            where_dict = {'is_valid': params.get('is_valid')}
            where_dict['name'] = params.get('name')
            where_dict['price'] = params.get('price')
            where_dict['publish_date'] = params.get('publish_date')
            where_dict['publish'] = params.get('publish__id')
            #where_dict['authors']=params.get('authors')
            res = Book.objects.filter(id=params.get('id')).update(**where_dict)
            book = Book.objects.filter(id=params.get('id')).first()
            book.authors.set(params.get('authors'))
            if res and book:
                return function.ehcoJson({
                    'code': 200,
                    'msg': function.ReturnCode[200]
                })
            else:
                return function.ehcoJson({
                    'code': 500,
                    'msg': function.ReturnCode[500]
                })
    return function.ehcoJson({'code': 400, 'msg': function.ReturnCode[400]})
Exemplo n.º 3
0
def index(request):
    params = function.json_decode(request.body)
    data = {}
    data2 = {}
    if not params.get('name') or not params.get('price') or not params.get(
            'publish_date') or not params.get('publish__id') or not params.get(
                'authors'):
        return function.ehcoJson({
            'code': 400,
            'msg': function.ReturnCode[400]
        })
    data['name'] = params.get('name')
    data['price'] = params.get('price')
    data['publish_date'] = params.get('publish_date')
    data['publish_id'] = params.get('publish__id')
    data['is_valid'] = 1

    data2['authors'] = params.get('authors')
    #data2=Author.objects.filter(id__in=params.get('authors'))

    book = Book.objects.create(**data)
    book.authors.add(*data2['authors'])
    if book:
        return function.ehcoJson({
            'code': 200,
            'msg': function.ReturnCode[200]
        })
    return function.ehcoJson({'code': 500, 'msg': function.ReturnCode[500]})
Exemplo n.º 4
0
def index(request):
    params = function.json_decode(request.body)
    if params.get('id') and params.get('name') and params.get('email') and params.get('city'):
        if params.get('is_valid') == 1 or params.get('is_valid') == 0:

            where_dict = {'is_valid': params.get('is_valid')}
            where_dict['name']=params.get('name')
            where_dict['email']=params.get('email')
            where_dict['city']=params.get('city')
            res=Publish.objects.filter(id=params.get('id')).update(**where_dict)
            if res :
                return  function.ehcoJson({'code':200,'msg':function.ReturnCode[200]})
            else:
                return function.ehcoJson({'code': 500, 'msg': function.ReturnCode[500]})
    return function.ehcoJson({'code': 400, 'msg': function.ReturnCode[400]})
Exemplo n.º 5
0
def index(request):
    params = function.json_decode(request.body)
    page = params.get('page') if params.get('page') else 1
    limit = params.get('limit') if params.get('limit') else 10
    offset1 = (page - 1) * limit
    offset2 = page * limit
    if params.get('is_valid') == 1 or params.get('is_valid') == 0:
        where_dict = {'is_valid': params.get('is_valid')}
    else:
        where_dict = {}
    if params.get('name'):
        where_dict['name__icontains'] = params.get('name')
    if params.get('phone') and params.get('phone').isdigit():
        where_dict['author_detail__phone'] = int(params.get('phone'))
    #基于双下划线的关联查询
    res = Author.objects.filter(**where_dict).values(
        'id', 'name', 'age', 'is_valid', 'author_detail__phone',
        'author_detail__birthday',
        'author_detail__addr').order_by('-id')[offset1:offset2]
    data = []
    for i in res:
        i['phone'] = i['author_detail__phone']
        i['birthday'] = i['author_detail__birthday']
        i['addr'] = i['author_detail__addr']
        data.append(i)
    return function.ehcoJson({
        'code': 200,
        'msg': function.ReturnCode[200],
        'data': data,
        'totalpage': len(data)
    })
Exemplo n.º 6
0
def index(request):
    params = function.json_decode(request.body)
    page = params.get('page') if params.get('page') else 1
    limit = params.get('limit') if params.get('limit') else 10
    offset1 = (page - 1) * limit
    offset2 = page * limit
    if params.get('is_valid') == 1 or params.get('is_valid') == 0:
        where_dict = {'is_valid': params.get('is_valid')}
    else:
        where_dict = {}
    if params.get('name'):
        where_dict['name__icontains'] = params.get('name')
    res = Publish.objects.filter(**where_dict).order_by('-id')[offset1:offset2]
    data = []
    i = 0
    for li in res:
        data.append({
            'id': li.id,
            'name': li.name,
            'email': li.email,
            'city': li.city,
            'is_valid': li.is_valid
        })
        i += 1
    return function.ehcoJson({
        'code': 200,
        'msg': function.ReturnCode[200],
        'data': data,
        'totalpage': i
    })
Exemplo n.º 7
0
def index(request):
    params =function.json_decode(request.body)
    data={}
    data2 = {}
    if not params.get('name') or not params.get('age') or not params.get('phone') or not params.get('birthday') or not params.get('addr'):
        return function.ehcoJson({'code':400,'msg':function.ReturnCode[400]})
    data['name']=params.get('name')
    data['age'] = params.get('age')
    data['is_valid'] = 1

    data2['phone'] = params.get('phone')
    data2['birthday'] = params.get('birthday')
    data2['addr'] = params.get('addr')
    authorDetail=AuthorDetail.objects.create(**data2)
    author=Author(**data,author_detail=authorDetail)
    author.save()
    if author:
        print(author)
        return function.ehcoJson({'code':200,'msg':function.ReturnCode[200]})
    return function.ehcoJson({'code': 500, 'msg': function.ReturnCode[500]})
Exemplo n.º 8
0
def index(request):
    params = function.json_decode(request.body)
    page = params.get('page') if params.get('page') else 1
    limit = params.get('limit') if params.get('limit') else 10
    offset1 = (page - 1) * limit
    offset2 = page * limit
    if params.get('is_valid') == 1 or params.get('is_valid') == 0:
        where_dict = {'is_valid': params.get('is_valid')}
    else:
        where_dict = {}

    if params.get('book_name'):
        where_dict['name__icontains'] = params.get('book_name')
    if params.get('author_name'):
        where_dict['authors__name__icontains'] = params.get('author_name')
    if params.get('publish_name'):
        where_dict['publish__name__icontains'] = params.get('publish_name')
    #基于双下划线的关联查询
    res = Book.objects.filter(**where_dict).values(
        'id', 'name', 'price', 'publish_date', 'is_valid', 'authors__id',
        'authors__name', 'publish__id',
        'publish__name').order_by('-id')[offset1:offset2]
    data = {}
    for i in list(res):
        i['authors'] = data[i['id']]['authors'] if data.get(i['id']) else []
        i['authors'].append(i['authors__id'])

        i['author_arr_name'] = data[i['id']]['author_arr_name'] if data.get(
            i['id']) else []
        i['author_arr_name'].append(i['authors__name'])
        i['authors__name_str'] = ','.join(i['author_arr_name'])
        data[i['id']] = i
    res_data = []
    print(connection.queries)
    for index in data:
        res_data.append(data[index])
    return function.ehcoJson({
        'code': 200,
        'msg': function.ReturnCode[200],
        'data': res_data,
        'totalpage': len(res_data)
    })