Exemplo n.º 1
0
def get_paginated_queryset_response(query_set, request):
    paginator = PageNumberPagination()
    paginator.page_size = 20
    paginated_qs = paginator.paginate_queryset(query_set, request)
    serializer = NameSerializer(paginated_qs,
                                many=True,
                                context={'request': request})
    return paginator.get_paginated_response(serializer.data)
Exemplo n.º 2
0
def like_name(request, pk):
    name = Name.objects.get(pk=pk)
    if request.method == 'POST':
        name = Name.objects.get(id=pk)
        name.likes += 1
        name.save()
        name_serializer = NameSerializer(name)
        return JsonResponse(name_serializer.data, status=status.HTTP_200_OK)
Exemplo n.º 3
0
def name_detail(request, pk):
    if request.user.is_authenticated:

        # Tìm các names theo pk (id)
        try:
            name = Name.objects.get(pk=pk)

            #Tìm một name theo id
            if request.method == 'GET':
                name_serializer = NameSerializer(name)
                return JsonResponse(name_serializer.data)

            #Update một name theo id
            elif request.method == 'PUT':
                name_data = JSONParser().parse(request)
                name_serializer = NameSerializer(name, data=name_data)
                if name_serializer.is_valid():
                    name_serializer.save()
                    return JsonResponse(name_serializer.data,
                                        status=status.HTTP_200_OK)
                return JsonResponse(name_serializer.errors,
                                    status=status.HTTP_400_BAD_REQUEST)

            #Xoá một name theo id
            elif request.method == 'DELETE':
                name.delete()
                return JsonResponse(
                    {'message': 'Name was deleted successfully!'},
                    status=status.HTTP_204_NO_CONTENT)

        except Name.DoesNotExist:
            return JsonResponse({'message': 'The name does not exist'},
                                status=status.HTTP_404_NOT_FOUND)
    return redirect('login/')
Exemplo n.º 4
0
def view_name_list(request):
    names = Name.objects.filter(
        lastname__icontains=request.GET.get('lastname', '')).filter(
            menh__icontains=request.GET.get('menh', '')).filter(
                Q(gioitinh=request.GET.get('gioitinh', ''))
                | Q(gioitinh='Chung')).order_by('lastname')
    print(request.GET)
    if request.method == 'GET':
        names_serializer = NameSerializer(names, many=True)
        return JsonResponse(names_serializer.data, safe=False)
Exemplo n.º 5
0
def name_list(request):
    # GET danh sách các name,
    if request.method == 'GET':
        names = Name.objects.all()
        # print(names)
        lastname = request.GET.get('lastname', None)
        if lastname is not None:
            print(request.GET)
            names = names.filter(
                lastname__icontains=lastname).order_by('lastname')

        names_serializer = NameSerializer(names, many=True)
        return JsonResponse(names_serializer.data, safe=False)
        # return get_paginated_queryset_response(names, request)

    # POST một name mới
    elif request.method == 'POST':
        name_data = JSONParser().parse(request)
        user = Name.objects.create(id=request.data.get('id'),
                                   fullname=request.data.get('fullname'),
                                   menh=request.data.get('menh'),
                                   van=request.data.get('van'),
                                   gioitinh=request.data.get('gioitinh'),
                                   lastname=request.data.get('lastname'),
                                   meaning=request.data.get('meaning'),
                                   likes=request.data.get('likes'))
        print(user.fullname)
        name_serializer = NameSerializer(data=name_data)
        if name_serializer.is_valid():
            name_serializer.save()
            return JsonResponse(name_serializer.data,
                                status=status.HTTP_201_CREATED)
        return JsonResponse(name_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 6
0
def view_name_list_2(request):
    tencha = request.GET.get('tencha', '')
    tenme = request.GET.get('tenme', '')
    menhCha = Name.objects.filter(
        lastname__icontains=tencha).values('menh').first()
    menhCha = menhCha['menh']
    if tencha != "":
        names = Name.objects.filter(menh__icontains=menhCha).filter(
            Q(gioitinh=request.GET.get('gioitinh', ''))
            | Q(gioitinh='Chung')).order_by('lastname')
    elif tencha == "":
        menhMe = Name.objects.filter(lastname__icontains=request.GET.get(
            'tenme', '')).values('menh').first()
        menhMe = menhMe['menh']
        names = Name.objects.filter(menh__icontains=menhMe).filter(
            Q(gioitinh=request.GET.get('gioitinh', ''))
            | Q(gioitinh='Chung')).order_by('lastname')
    else:
        random_idx = random.randint(0, Name.objects.count() - 1)
        names = Name.objects.all()[random_idx]
    if request.method == 'GET':
        names_serializer = NameSerializer(names, many=True)
        return JsonResponse(names_serializer.data, safe=False)
Exemplo n.º 7
0
def top_like(request):
    names = Name.objects.order_by('-likes').filter()[:10]
    print(request.GET)
    if request.method == 'GET':
        names_serializer = NameSerializer(names, many=True)
        return JsonResponse(names_serializer.data, safe=False)