def get_specifications(self, request, CategoryId=None): if request.method == 'GET': data = productSpecifications.objects.filter(CategoryId=CategoryId, ShowInFilter=True) Serializer = SpecificationsResource(data, many=True) return JsonResponse(encrypt(Serializer.data), safe=False)
def get_searched_products(self, request, search='null'): if request.method == 'GET': List = [] if search != 'null' or search != None: products = getHomeProducts.objects.all().filter( ProductName__icontains=search) categoryIds = [ product for product in products.values_list('CategoryId', flat=True) ] Categories = categories.objects.all().filter( id__in=categoryIds) productNames = [(product.CategoryId.id, product.ProductName) for product in products] categoryNames = [(category.id, category.NameL) for category in Categories] for p in set(productNames): for c in set(categoryNames): if p[0] == c[0]: List.append({ 'ProductName': p[1], 'CategoryName': c[1] }) return JsonResponse(encrypt(List[:10]), safe=False)
def get_max_page(self, request, specValue='null', search='null', categoryId=0): if request.method == 'GET': homeProducts = getHomeProducts.objects.all().order_by('id') if search != 'null': homeProducts = homeProducts.filter( ProductName__contains=search) if specValue != 'null': selectedValues = specValue.split(',') productIds = productSpecifications.objects.all().filter( SpecificationValue__in=selectedValues) ids = [ product for product in productIds.values_list('ProductId', flat=True) ] homeProducts = homeProducts.filter(ProductId__in=ids) if categoryId != 0: homeProducts = homeProducts.filter(CategoryId=categoryId) maxPageNumber = len(homeProducts) / 8 maxPageNumber = math.ceil(maxPageNumber) List = [] for page in range(1, maxPageNumber + 1): List.append(page) return JsonResponse(encrypt(List), safe=False)
def get_home_products(self, request, page=1, specValue='null', search='null', categoryId=0): if request.method == 'GET': homeProducts = getHomeProducts.objects.all().order_by('id') if search != 'null': homeProducts = homeProducts.filter( ProductName__contains=search) if specValue != 'null': selectedValues = specValue.split(',') productIds = productSpecifications.objects.all().filter( SpecificationValue__in=selectedValues) ids = [ product for product in productIds.values_list('ProductId', flat=True) ] homeProducts = homeProducts.filter(ProductId__in=ids) if categoryId != 0: homeProducts = homeProducts.filter(CategoryId=categoryId) products = homeProducts[(page - 1) * 8:page * 8] serializer = getHomeProductsResource(products, many=True) return JsonResponse(encrypt(serializer.data), safe=False)
def update(self, request, id): if request.method == "POST": data = json.loads(request.body) password = data['Password'] with open('key.json') as json_file: key = json.load(json_file)['key'] token = encrypt(password, 'RSA', key, False) data['Password'] = token['token'] users.objects.filter(id=id).update(**data) return HttpResponse(request)
def get_user(self, request, UserId): if request.method == 'GET': data = users.objects.filter(id=UserId) Serializer = usersResource(data, many=True) data = {k: v for item in Serializer.data for k, v in item.items()} data = encrypt(data) return JsonResponse({ 'token': data['token'], 'key': data['key'] }, safe=False)
def all(self, request, model): if request.method == 'GET': result = Model.__getattribute__(models, model)() resource = Model.__getattribute__(resources, model + 'Resource') Serializer = resource(type(result).objects.all(), many=True) data = encrypt(Serializer.data) return JsonResponse({ 'token': data['token'], 'key': data['key'] }, safe=False)
def data(self, request, model, id): if request.method == 'GET': result = Model.__getattribute__(models, model)() resource = Model.__getattribute__(resources, model + 'Resource') Serializer = resource(type(result).objects.filter(id=id), many=True) data = {k: v for item in Serializer.data for k, v in item.items()} data = encrypt(data) return JsonResponse({ 'token': data['token'], 'key': data['key'] }, safe=False)
def register_user(self, request): data = False if request.method == "POST": result = json.loads(request.body) Users = users.objects.filter(Email=result['Email']) if Users.count() > 0: data = False else: password = result['Password'] with open('key.json') as json_file: key = json.load(json_file)['key'] token = encrypt(password, 'RSA', key, False) result['Password'] = token['token'] user = users.objects.create(**result) user = users.objects.filter(id=user.id) Serializer = usersResource(user, many=True) data = { k: v for item in Serializer.data for k, v in item.items() } return JsonResponse(data, safe=False)
def authenticate_user(self, request): if request.method == 'POST': data = False result = json.loads(request.body) count = users.objects.filter(Email=result['Email']).values_list( 'Password', flat=True) for user in count: with open('key.json', "r") as json_file: key = json.load(json_file)['key'] password = decrypt(user, key, 'RSA') if result['Password'] == password: data = users.objects.filter(Email=result['Email']) Serializer = usersResource(data, many=True) data = { k: v for item in Serializer.data for k, v in item.items() } data = encrypt(data) return JsonResponse({ 'token': data['token'], 'key': data['key'] }, safe=False)
def get_sub_categories(self, request): if request.method == 'GET': data = categories.objects.filter(Level=2) Serializer = categoriesResource(data, many=True) return JsonResponse(encrypt(Serializer.data), safe=False)
def login_as_guest(self, request): if request.method == 'GET': data = users.objects.filter(NameL='Guest') Serializer = usersResource(data, many=True) data = {k: v for item in Serializer.data for k, v in item.items()} return JsonResponse(encrypt(data), safe=False)