def static(request, **kwargs): customer_id = kwargs.get('id', 0) try: customer = Customer.objects.get(id=customer_id) except Customer.DoesNotExist: raise Http404() user = request.user if not customer or get_customer(user) != customer: raise Http404() name_file = kwargs.get('name_file', 0) files = CustomersFiles.objects.filter(customer=customer, name=name_file) if len(files) > 0: url = resolve_url(f'san_site/static/files_for_loading/{customer_id}/{name_file}') file_path = os.path.join(settings.BASE_DIR, url) if not os.path.exists(file_path): raise Http404() content_type = 'application/vnd.ms-excel' response = HttpResponse(open(file_path, mode='rb'), content_type=content_type) date = files[0].change_date.date().isoformat().replace('-', '_') name_file = date + "_" + name_file response['Content-Disposition'] = f'attachment; filename={name_file}' response['Content-Length'] = os.path.getsize(file_path) return response else: raise Http404()
def order_request(request, **kwargs): order_id = kwargs.get('id', 0) try: order_currently = Order.objects.get(id=order_id) except Order.DoesNotExist: raise Http404() customer = get_customer(request.user) if customer == order_currently.person.customer: if settings.CELERY_NO_CREATE_ORDERS: try: order_currently.request_order() except Order.RequestOrderError: pass else: task_order_request.delay(order_id) return render(request, 'orders/order.html', {'order': order_currently}) else: response = HttpResponseForbidden() log_response( 'Order %s Not Allowed (%s): %s', order_id, request.user, request.path, response=response, request=request, ) return response
def create_files(user): customer = get_customer(user) if not customer: return path_files = os.path.join(settings.BASE_DIR, 'san_site\\static\\files_for_loading') create_directory(path_files) path_files_customer = os.path.join(path_files, str(customer.id)) create_directory(path_files_customer) write_files(path_files_customer, user) url = f'static/{customer.id}/' CustomersFiles.objects.filter(customer=customer).delete() view = 'Данные в формате CSV (значения разделенные точкой с запятой)' CustomersFiles.objects.create(customer=customer, name='goods_b2b_santex.csv', view=view, url=url, type_file='csv') view = 'Данные в формате EXCEL (Microsoft Office Excel)' CustomersFiles.objects.create(customer=customer, name='goods_b2b_santex.xlsx', view=view, url=url, type_file='xlsx')
def for_loading(request): user = request.user customer = get_customer(user) if not customer: files = [] else: files = customer.get_files() return render(request, 'files/files_for_loading.html', {'files': files})
def create_files_customers(): # for all create_files_inventories() # for customers customers_yet = [] users = User.objects.filter(is_active=True) for user in users: customer = get_customer(user) if not customer: continue if customer.id in customers_yet: continue create_files(user) customers_yet.append(customer.id)
def our_api(request): person = get_person(request.user) customer = get_customer(request.user) uid = '' if person: if person.uid in ('','xxx'): person.create_uid() uid = person.uid if not customer: files = [] else: files = customer.get_files() for file in files: type_file = file['type'] file['url'] = f'http://b2b.santex.su/api/v1/outside/?uid={uid}&type={type_file}' return render(request, 'files/files_API.html', {'uid': uid, 'files': files})
def order(request, **kwargs): order_id = kwargs.get('id', 0) try: order_currently = Order.objects.get(id=order_id) except Order.DoesNotExist: raise Http404() customer = get_customer(request.user) if customer == order_currently.person.customer: return render(request, 'orders/order.html', {'order': order_currently}) else: response = HttpResponseForbidden() log_response( 'Order %s Not Allowed (%s): %s', order_id, request.user, request.path, response=response, request=request, ) return response
def get(request): try: uid = request.GET.get('uid') except MultiValueDictKeyError: return Response({'error': 'Не удалось авторизовать пользователя'}, status=HTTP_403_FORBIDDEN) try: user = Token.objects.get(key=uid).user except Token.DoesNotExist: return Response({'error': 'Не удалось авторизовать пользователя'}, status=HTTP_403_FORBIDDEN) customer = get_customer(user=user) if customer is None: return Response({'error': 'Не удалось авторизовать пользователя'}, status=HTTP_403_FORBIDDEN) try: type_file = request.GET.get('type') except MultiValueDictKeyError: type_file = 'csv' name_file = f'goods_b2b_santex.{type_file}' files = CustomersFiles.objects.filter(customer=customer, type_file=type_file) if len(files) > 0: url = resolve_url( f'san_site/static/files_for_loading/{customer.id}/{name_file}') file_path = os.path.join(settings.BASE_DIR, url) if not os.path.exists(file_path): return Response({'error': 'Нет файла'}, status=HTTP_404_NOT_FOUND) content_type = 'application/vnd.ms-excel' response = HttpResponse(open(file_path, mode='rb'), content_type=content_type) response[ 'Content-Disposition'] = f'attachment; filename={name_file}' response['Content-Length'] = os.path.getsize(file_path) return response else: return Response({'error': 'Нет файла'}, status=HTTP_404_NOT_FOUND)
def our_api(request): customer = get_customer(request.user) uid, _ = Token.objects.get_or_create(user=request.user) if not customer: files = [] else: files = customer.get_files() for file in files: type_file = file['type'] file['url'] = f'/api/v1/catalog/?uid={uid}&type={type_file}' brands = ', '.join([ el.name for el in Brand.objects.filter(is_deleted=False).order_by('name') ]) return render(request, 'files_API.html', { 'uid': uid, 'files': files, 'brands': brands })
def get(request): current_customer = get_customer(request.user) current_customer_id = None current_customer_price_id = 0 current_customer_suffix = '' if current_customer: current_customer_id = current_customer.id current_customer_price_id = current_customer.price.id current_customer_suffix = current_customer.suffix if current_customer_id is None: return Response({'error': 'Не удалось авторизовать пользователя'}, status=HTTP_403_FORBIDDEN) current_person = get_person(request.user) current_person_id = 0 if current_person: current_person_id = current_person.id param = [current_customer_id] filter_code = '' for key, value in request.GET.items(): if key.startswith('filter_code'): filter_code += f"{'' if filter_code == '' else ','}{value}" filter_article = '' for key, value in request.GET.items(): if key.startswith('filter_article'): filter_article += f"{'' if filter_article == '' else ','}{value}" filter_barcode = '' for key, value in request.GET.items(): if key.startswith('filter_barcode'): filter_barcode += f"{'' if filter_barcode == '' else ','}{value}" filter_brand = '' for key, value in request.GET.items(): if key.startswith('filter_brand'): filter_brand += f"{'' if filter_brand == '' else ','}{value}" filter_quantity = request.GET.get('filter_quantity', '') str_filter_code = ' TRUE ' if filter_code != '': param += [ list(map(lambda x: x.upper(), filter_code.split(','))), ] str_filter_code = 'UPPER(_product.code::text) = ANY(%s)' str_filter_article = ' TRUE ' if filter_article != '': param += [ list(map(lambda x: x.upper(), filter_article.split(','))), ] str_filter_article = 'UPPER(_product.code_brand::text) = ANY(%s)' str_filter_barcode = ' TRUE ' if filter_barcode != '': param += [ list(map(lambda x: x.upper(), filter_barcode.split(','))), ] str_filter_barcode = 'UPPER(_product.barcode::text) = ANY(%s)' str_filter_brand = ' TRUE ' if filter_brand != '': param += [ list(map(lambda x: x.upper(), filter_brand.split(','))), ] str_filter_brand = 'UPPER(_brand.name::text) = ANY(%s)' str_filter_quantity = ' TRUE ' if filter_quantity != '' and filter_quantity.upper() == 'YES': str_filter_quantity = 'COALESCE(_inventories.quantity, 0) > 0' queryset = Product.objects.raw( f"""WITH result AS ( SELECT _product.id AS id, _product.code AS code_, _product.guid AS guid_, _product.code_brand AS article_, _product.barcode AS barcode_, _product.name AS name_, _product.matrix AS matrix_, _product.image AS image, COALESCE(_brand.name, '') AS brand_name_, COALESCE(_customersprices.discount, COALESCE(_prices.value, 0)) AS price, COALESCE(_customersprices_cur.name, COALESCE(_prices_cur.name, '')) AS currency, COALESCE(_prices.rrp, 0) AS price_rrp, SUM(COALESCE(_inventories.quantity, 0)) AS quantity FROM san_site_product _product LEFT JOIN san_site_prices _prices ON _product.id = _prices.product_id AND _prices.price_id = {current_customer_price_id} LEFT JOIN san_site_currency _prices_cur ON _prices.currency_id = _prices_cur.id LEFT JOIN san_site_customersprices{current_customer_suffix} _customersprices ON _customersprices.customer_id = %s AND _product.id = _customersprices.product_id LEFT JOIN san_site_currency _customersprices_cur ON _customersprices.currency_id = _customersprices_cur.id LEFT JOIN san_site_personstores _personstores ON _personstores.person_id = {current_person_id} LEFT JOIN san_site_inventories _inventories ON _product.id = _inventories.product_id AND _personstores.store_id = _inventories.store_id LEFT JOIN san_site_brand _brand ON _product.brand_id = _brand.id WHERE _product.is_deleted = FALSE AND {str_filter_code} AND {str_filter_article} AND {str_filter_brand} AND {str_filter_barcode} AND {str_filter_quantity} GROUP BY _product.id, _product.code, _product.code_brand, _product.barcode, _product.name, _product.matrix, _product.image, COALESCE(_brand.name, ''), COALESCE(_prices.value, 0), COALESCE(_customersprices.discount, COALESCE(_prices.value, 0)), COALESCE(_customersprices_cur.name, COALESCE(_prices_cur.name, '')), COALESCE(_prices.rrp, 0) ) SELECT * FROM result ORDER BY result.code_;""", param) serializer = ProductSerializer(queryset, many=True) return Response(serializer.data, status=HTTP_200_OK)