|
| 1 | +from items.models import Item, CPUType |
| 2 | +from rest_framework.decorators import api_view |
| 3 | +from rest_framework.response import Response |
| 4 | +from .serializers import ItemSerializer, CPUTypeSerializer |
| 5 | +from django.db.models import Q |
| 6 | +from backend.utility import proccessStrParams |
| 7 | + |
| 8 | +from rest_framework import generics |
| 9 | + |
| 10 | + |
| 11 | +# @api_view(['GET']) |
| 12 | +# def items(req, typee, cpu, cpu_type, ram, hdd, gpu): |
| 13 | +# type_list, cpu_list, cpu_type_list, ram_list, hdd_list, gpu_list = proccessStrParams( |
| 14 | +# typee, |
| 15 | +# cpu, |
| 16 | +# cpu_type, |
| 17 | +# ram, |
| 18 | +# hdd, |
| 19 | +# gpu |
| 20 | +# ) |
| 21 | + |
| 22 | +# type_list = Q(model__typee__id__in=type_list) if type_list else Q( |
| 23 | +# model__typee__id__isnull=False) |
| 24 | + |
| 25 | +# cpu_list = Q(cpu_type__name__id__in=cpu_list) if cpu_list else Q( |
| 26 | +# cpu_type__name__id__isnull=False) |
| 27 | + |
| 28 | +# cpu_type_list = Q(cpu_type__id__in=cpu_type_list) if cpu_type_list else Q( |
| 29 | +# cpu_type__id__isnull=False) |
| 30 | + |
| 31 | +# ram_list = Q(ram_type__id__in=ram_list) if ram_list else Q( |
| 32 | +# ram_type__id__isnull=False) |
| 33 | + |
| 34 | +# hdd_list = Q(hdd_type__id__in=hdd_list) if hdd_list else Q( |
| 35 | +# cpu_type__id__isnull=False) |
| 36 | + |
| 37 | +# gpu_list = Q(gpu__id__in=gpu_list) if gpu_list else Q( |
| 38 | +# gpu__id__isnull=False) |
| 39 | + |
| 40 | +# items = Item.objects.filter( |
| 41 | +# type_list & cpu_list & cpu_type_list & ram_list & hdd_list & gpu_list) |
| 42 | + |
| 43 | +# serializer = ItemSerializer(items, many=True) |
| 44 | + |
| 45 | +# return Response(serializer.data) |
| 46 | + |
| 47 | + |
| 48 | +class Items(generics.ListAPIView): |
| 49 | + # queryset = Item.objects.all() |
| 50 | + serializer_class = ItemSerializer |
| 51 | + |
| 52 | + def get_queryset(self): |
| 53 | + type_list, cpu_list, cpu_type_list, ram_list, hdd_list, gpu_list = proccessStrParams( |
| 54 | + self.kwargs['typee'], |
| 55 | + self.kwargs['cpu'], |
| 56 | + self.kwargs['cpu_type'], |
| 57 | + self.kwargs['ram'], |
| 58 | + self.kwargs['hdd'], |
| 59 | + self.kwargs['gpu'] |
| 60 | + ) |
| 61 | + |
| 62 | + type_list = Q(model__typee__id__in=type_list) if type_list else Q( |
| 63 | + model__typee__id__isnull=False) |
| 64 | + |
| 65 | + cpu_list = Q(cpu_type__name__id__in=cpu_list) if cpu_list else Q( |
| 66 | + cpu_type__name__id__isnull=False) |
| 67 | + |
| 68 | + cpu_type_list = Q(cpu_type__id__in=cpu_type_list) if cpu_type_list else Q( |
| 69 | + cpu_type__id__isnull=False) |
| 70 | + |
| 71 | + ram_list = Q(ram_type__id__in=ram_list) if ram_list else Q( |
| 72 | + ram_type__id__isnull=False) |
| 73 | + |
| 74 | + hdd_list = Q(hdd_type__id__in=hdd_list) if hdd_list else Q( |
| 75 | + cpu_type__id__isnull=False) |
| 76 | + |
| 77 | + gpu_list = Q(gpu__id__in=gpu_list) if gpu_list else Q( |
| 78 | + gpu__id__isnull=False) |
| 79 | + |
| 80 | + queryset = Item.objects.filter( |
| 81 | + type_list & cpu_list & cpu_type_list & ram_list & hdd_list & gpu_list) |
| 82 | + |
| 83 | + return queryset |
| 84 | + |
| 85 | + |
| 86 | +@api_view(['GET']) |
| 87 | +def cpuTypes(req, id): |
| 88 | + list, = proccessStrParams(id) |
| 89 | + |
| 90 | + list = Q(name__id__in=list) |
| 91 | + |
| 92 | + cpuTypes = CPUType.objects.filter(list) |
| 93 | + |
| 94 | + serializer = CPUTypeSerializer(cpuTypes, many=True) |
| 95 | + |
| 96 | + return Response(serializer.data) |
0 commit comments