Atenção!
Depois de ler o artigo, você pode ter a impressão de que adoro BDSM ou algo parecido, mas só parece para você.
Problemas de loja
Eu trabalho em uma loja de bicicletas regular no centro de Varsóvia. Comercializamos tanto estacionários quanto na Internet Número médio de bicicletas compradas por dia ~ 2 durante todo o ano. Ao mesmo tempo, o pico de vendas cai no verão, e então podemos ter cerca de 17 pedidos online por dia e o mesmo número na loja, e no inverno não podemos vender nada.
Em 2020. Em conexão com a pandemia de COVID, a demanda por bicicletas cresceu a taxas incríveis e nós, como um escritório decente, começamos a expandir.
Isso levou ao fato de que no final da temporada passada, os pedidos apenas para bicicletas inexistentes aumentaram em média até 4 vezes por semana durante os 4 meses mais produtivos. E isso são cerca de 16 inconsistências no site por mês (sem contar as falhas na loja).
Este ano, com o aparecimento de mais um grande armazém, a situação agravou-se e agora, para além das bicicletas que não existiam, foram adicionadas as que não existiam e muitas vezes era impossível levá-las a tempo aos clientes.
, - EXEL, , . , . 2 2-3 .
. , , , – , . .
, , - . . , . :
, .
:
Python API PrestaShop;
, -, . ;
Chrome . , Chrome.
middle-, . .
API PrestaShop
API PrestaShop Python , , PS 5% ( 1.6 ). — prestapyt, . , , .
, , API, PS. , , .
, , ! , .
- , .
, , .
:
reference ;
— ;
, , stock_availables. ;
associations .
, , > 0 — . – , .
XML stock_availables, .
.
, - , .
API
https://domain.com/api. , . id – , «products» . , .
API XML, :
<prestashop>
<api shopName="myshop">
<addresses xlink:href="https://domain.com/api/addresses" get="true" put="false" post="false" delete="false" head="false"></addresses>
</api>
</prestashop>
- . https://domain.com/api/products:
<prestashop>
<products>
<product id="22" xlink:href="https://domain.com/api/products/22"/>
<product id="24" xlink:href="https://domain.com/api/products/24"/>
<product id="265" xlink:href="https://domain.com/api/products/265"/>
<product id="294" xlink:href="https://domain.com/api/products/294"/>
<products />
<prestashop />
.
requests. , Requests — , .
API PS Basic ( ). . requests:
request_url = «https://domain.com/api»
get_combination_xml = requests.get(request_url, auth=(self.api_secret_key, ''))
200, , XML. .
XML
– xml.etree. , , ( ), , Python.
:
# 2
from xml.etree import ElementTree as ET
from xml.etree.ElementTree import ElementTree
# xml.etree
def xml_data_extractor(data, tag):
# data – XML
# tag = products–
try:
xml_content = ET.fromstring(data.content) # ElementTree. . .
general_tag = xml_content[0] # –
tag = general_tag.find(tag) #
tag_inner_link = tag.get('{http://www.w3.org/1999/xlink}href') #
# href
#
product_meta = {'product_link': tag_inner_link}
return product_meta
except:
return None
: https://domain.com/api/products. , .
None except Try. , , , , .
PS
PS . , – SQL . XML , — , .
:
https://domain.com/api/combinations/?filter[reference]=reference
, reference — .
- -, : KRHE1Z26X14M200034.
, . ! Git:
«»
( , ). base64 , , , :
def __init__(self, api_secret_key, request_url=None, **kwargs):
try:
self.api_secret_key = str(api_secret_key) #!API key is necessary!
self.api_secret_key_64 = base64.b64encode((self.api_secret_key + ':').encode())
except:
raise TypeError('The secret_key must be a string!')
# Main path to working directory
self.base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
self.request_url = request_url
self.kwargs = kwargs.items()
#product meta to return
self.name = None
self.total_quantity = None
self.w_from = None
self.w_to = None
self.date = str(datetime.datetime.now().strftime("%d-%m-%Y, %H:%M"))
: _xml_data_extractor(), _wd() _logging(), . -.
— . 11. . , , .
middle-
c Python. , - . WSGI- , !
, , .
, . , Django, — middle-, .
, , - .
! https://palachintosh.com/xxx/xxx/
? ( ) , , . .
/?code=1122334455&token=IUFJ44KPQE342M3M109DNWI
( ):
success – , , , , - . .
, , , , . Django Django REST. Java Retrofit, .
– – . – - :
token = None
with open(‘token.txt’) as file_t:
token = file_t[0]
if token == str(request.GET.get(‘token’)):
//
return JsonResponse({‘Error’: ‘Invalid token’})
, , .
Chrome
, , ..
, . . XMLHttpRequest.
CORS. , Access-Control-Allow-Origin. , OPTIONS.
views.py def options(self, request). GET .
100 , .
- , . . :
, . «Kod roweru» , .
– PrestaRequest . , .
«sku» , ajax . , ajax . .
var interval;
function main_interval() {
clearInterval(interval);
interval = setInterval(function () {
href = window.location.href
if (href.indexOf('https://24.kross.pl/warranty/new') >= 0 ||
href.indexOf('id_product=') >= 0) {
if (href.indexOf('id_product=') >= 0) {
prestaCheck();
clearInterval(interval);
}
if (href.indexOf('https://24.kross.pl/warranty/new') >= 0) {
location.reload();
get_buttons();
}
}
if (href.indexOf('https://24.kross.pl/bike-overview/new') >= 0) {
clearInterval(interval);
check_all();
}
}, 1000);
}
:
// onclick or enter events
function getFormData() {
var getForm = document.forms[0];
if (getForm != null) {
if (getForm.hasChildNodes("sku") && getForm.sku != null){
var code = String(getForm.sku.value);
}
if (getForm.hasChildNodes("bike_model") && getForm.bike_model != null) {
edit_msg = document.querySelector(".message-container > span > h1");
edit_msg.innerText = "Rower " + String(getForm.bike_model.value) + " zostanie usunięty ze stanów!";
}
if (code != null && getForm.serial_number != null) {
sendRequest(code);
}
}
}
bike_model , , . :
var getBodyBlock = document.querySelector('body');
var alert_div = document.createElement('div');
alert_div.innerHTML = '<div class="alert-message"><div class="message-container">\
<span><h1></h1></span>\
<div class="inner-buttons">\
<button id="btnYes" class="ant-btn ant-btn-danger">Potwierdzam!</button>\
<button id="btnReserve" class="ant-btn ant-btn-danger">Zdjąć rezerwację</button>\
<button id="btnNo" class="ant-btn ant-btn-success">Nie teraz</button>\
</div></div></div>';
loader = document.createElement('div');
getBodyBlock.appendChild(alert_div);
: https://github.com/palachintosh/shop_extension
– Android-
, , . . , , , 50 ., .
. , . , .
( )
3 . , :
, .
, , , - , .
, . - . , - . Java .
Retrofit 2. , Python. , , , , , .
Main Activity 3 – onCreate, scanCode, enterCode , :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner fromSpinner = (Spinner) findViewById(R.id.fromSpinner);
Spinner toSpinner = (Spinner) findViewById(R.id.toSpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (
this, android.R.layout.simple_spinner_item, warehouses);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fromSpinner.setAdapter(adapter);
toSpinner.setAdapter(adapter);
}
public void scanCode(View view) {
Intent intent = new Intent(MainActivity.this, Scan.class);
Spinner get_w_from = (Spinner) findViewById(R.id.fromSpinner);
Spinner get_w_to = (Spinner) findViewById(R.id.toSpinner);
EditText editText = (EditText) findViewById(R.id.prodctQuantity);
String quantity_tt = editText.getText().toString();
RequestData requestData = new RequestData(
get_w_from.getSelectedItem().toString(),
get_w_to.getSelectedItem().toString(),
quantity_tt);
intent.putExtra(RequestData.class.getSimpleName(), requestData);
startActivity(intent);
}
“Enter Code” . , . – .
Scan Code Activity co , Barcode Scanner Google.
Send Code – . , . Retrofit . , , – Java Android , .
github: https://github.com/palachintosh/product_control.git
:
– ;
– , , ;
- , .
:
– - , ;
– ;
– .
, 2- -.
, , 5:
.
( , PrestaShop).
"Enter code" .
"Scan code".
– "Send Code".
, , , .
, , , . - - .
: , , , .
, , , .
As pessoas que consultaram os repositórios ou geralmente leram isso ficarão gratas por comentários construtivos e críticas.