def login(request): if request.session.get('creds'): return redirect('client:index') if request.method == 'POST': form = LoginForm(request.POST) value_next = request.POST.get('next', '') if form.is_valid(): email = form.cleaned_data['email'] password = form.cleaned_data['password'] try: dw = datawiz.DW(email, password) creds = dw.generate_secret(email, password) except InvalidGrantError: return render(request, 'client/login.html', {'form': form, 'error_msg': 'No such email and password.'}) creds['email'] = email creds['password'] = password if "CLIENT_ID" not in creds: creds = test_creds creds['name'] = dw.get_client_info().get('name', 'Empty Name') request.session['creds'] = creds if value_next: return HttpResponseRedirect(value_next) return redirect('client:index') else: form = LoginForm() value_next = request.GET.get('next', '') return render(request, 'client/login.html', {'form': form, 'next': value_next})
def get(self, request): try: storage = get_messages(request) auth = None for message in storage: auth = message break user = auth.message.split(' ') dw = datawiz.DW(user[0], user[1]) date_from = datetime.date(2015, 11, 17) date_to = datetime.date(2015, 11, 18) turnover = dw.get_products_sale(by='turnover', date_from = date_from, date_to = date_to) quantity = dw.get_products_sale(by='qty', date_from = date_from, date_to = date_to) ids = list(turnover) quantity_from = list(quantity.iloc[0]) quantity_to = list(quantity.iloc[1]) turnover_from = list(turnover.iloc[0]) turnover_to = list(turnover.iloc[1]) data = [[dw.get_product(products=ids[i])['product_name'], round(quantity_to[i] - quantity_from[i], 2), round(turnover_to[i] - turnover_from[i], 2)] for i in range(len(ids)) if quantity_from[i] > quantity_to[i]] messages.add_message(request, messages.INFO, auth) return render(request, 'info\down.html', context={'data': data}) except: return redirect('/login/')
def sale_list(request): dw = datawiz.DW(request.user.username, Client.objects.get(user_name=request.user).pass_user) client = dw.get_client_info() sales = dw.get_categories_sale(categories=["sum"], by=['turnover', 'qty', 'receipts_qty'], date_from="2018-03-01", date_to="2018-03-18", interval='days', view_type='raw').drop( ['name', 'category', 'date'], axis='columns') sales['average_qty'] = sales['turnover'] / sales['qty'] sales1 = sales.T.reset_index().rename(columns={ 0: "2018-03-01", 1: "2018-03-18" }) sales1['dif_per'] = sales1["2018-03-18"] / sales1["2018-03-01"] sales1['dif_min'] = sales1["2018-03-18"] - sales1["2018-03-01"] product1 = dw.get_products_sale(by=['turnover', 'qty'], date_from="2018-03-01", date_to="2018-03-18", interval='days', show="name", view_type='raw').drop(['date', 'product'], axis='columns') product2 = dw.get_products_sale(by=['turnover', 'qty'], date_from="2018-03-01", date_to="2018-03-18", interval='days', show='name', view_type='raw').drop(['date', 'product'], axis='columns') product1['differ_qty'] = product2['qty'] - product1['qty'] product1['differ_turnover'] = product2['turnover'] - product1['turnover'] product = product1.drop(['turnover', 'qty'], axis='columns') return render( request, 'test_site/sale_list.html', { 'client': client, 'sales': sales1.to_html(index=False, classes="table"), 'products1': product.sort_values(by='differ_qty', ascending=False).to_html( index=False, classes="table table-striped table-bordered", table_id='differ_max'), 'products2': product.sort_values(by='differ_qty').to_html( index=False, classes="table table-striped table-bordered", table_id='differ_min') })
def wrapper(*args, **kwargs): request = args[0] if not request.session.get('creds'): return redirect(reverse('client:login') + "?next=" + request.get_full_path()) creds = request.session['creds'] try: dw = datawiz.DW(creds['email'], creds['password']) except InvalidGrantError: return render(request, 'client/error.html', {'error_msg': 'Provided credentials were not accepted by DataWiz.'}) kwargs['dw'] = dw return view_func(*args, **kwargs)
def __init__(self): self.USER = datawiz.DW() self.SETTINGS = { 'shops': shops, 'products': products, 'categories': categories } client_info = self.USER.get_client_info() self.SETTINGS['date_from'] = client_info['date_from'] self.SETTINGS['date_to'] = client_info['date_to']
def get(self, request): try: storage = get_messages(request) auth = None for message in storage: auth = message break user = auth.message.split(' ') dw = datawiz.DW(user[0], user[1]) data = dw.get_client_info() messages.add_message(request, messages.INFO, auth) return render(request, 'info\home.html', context={'data': data}) except: return redirect('/login/')
def __init__(self, client_id, client_secret): self.client_id = client_id self.client_secret = client_secret self.dw = datawiz.DW(client_id, client_secret)
from dwapi import datawiz dw = datawiz.DW("*****@*****.**", "1qaz") print(dw.get_shops()) from dwapi import datawiz_auth dw = datawiz_auth.Auth() a = dw.generate_secret("*****@*****.**", "1qaz") print(a)
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization # https://docs.djangoproject.com/en/1.9/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.9/howto/static-files/ STATIC_URL = '/static/' STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'), ) from dwapi import datawiz DW = datawiz.DW('*****@*****.**', 'test2@cbe47a5c9466fe6df05f04264349f32b')
def login(request): if request.method == 'POST': API_KEY = request.POST.get("login") API_SECRET = request.POST.get("password") # API_KEY = "*****@*****.**" # API_SECRET = "1qaz" try: dw = datawiz.DW(API_KEY, API_SECRET) client_data = dw.get_client_info() client_info = {} client_info['name'] = client_data['name'] client_info['shops'] = client_data['shops'] client_info['date_from'] = client_data['date_from'].strftime( "%d. %B %Y") client_info['date_to'] = client_data['date_to'].strftime( "%d. %B %Y") df_turnover = dw.get_products_sale(by=["turnover"], date_from = datetime.date(2015, 11, 17),\ date_to = datetime.date(2015, 11, 18)) turnover_from = round(df_turnover.sum(axis=1)[0], 2) turnover_to = round(df_turnover.sum(axis=1)[1], 2) turnover_difference = round(turnover_to - turnover_from, 2) turnover_percentage = round( turnover_difference / max(turnover_to, turnover_from) * 100, 2) data_turnover = {"turnover_from": turnover_from, "turnover_to": turnover_to, "turnover_difference": \ turnover_difference, "turnover_percentage": turnover_percentage} df_qty = dw.get_products_sale(by=["qty"], date_from=datetime.date(2015, 11, 17), \ date_to=datetime.date(2015, 11, 18)) qty_from = round(df_qty.sum(axis=1)[0], 2) qty_to = round(df_qty.sum(axis=1)[1], 2) qty_difference = round(qty_to - qty_from, 2) qty_percentage = round( qty_difference / max(qty_to, qty_from) * 100, 2) data_qty = {"qty_from": qty_from, "qty_to": qty_to, "qty_difference": \ qty_difference, "qty_percentage": qty_percentage} df_receipts_qty = dw.get_products_sale(by=["receipts_qty"], date_from=datetime.date(2015, 11, 17), \ date_to=datetime.date(2015, 11, 18)) receipts_qty_from = round(df_receipts_qty.sum(axis=1)[0], 2) receipts_qty_to = round(df_receipts_qty.sum(axis=1)[1], 2) receipts_qty_difference = round( receipts_qty_to - receipts_qty_from, 2) receipts_qty_percentage = round( receipts_qty_difference / max(receipts_qty_to, receipts_qty_from) * 100, 2) data_receipts_qty = {"receipts_qty_from": receipts_qty_from, "receipts_qty_to": receipts_qty_to, \ "receipts_qty_difference": receipts_qty_difference, "receipts_qty_percentage": receipts_qty_percentage} average_check_from = round(turnover_from / receipts_qty_from, 2) average_check_to = round(turnover_to / receipts_qty_to, 2) average_check_difference = round( average_check_to - average_check_from, 2) average_check_percentage = round( average_check_difference / max(average_check_to, average_check_from) * 100, 2) data_average_check = {"average_check_from": average_check_from, "average_check_to": average_check_to, \ "average_check_difference": average_check_difference, "average_check_percentage": average_check_percentage} data_indicator = {"data_turnover": data_turnover, "data_qty": data_qty, "data_receipts_qty": data_receipts_qty,\ "data_average_check": data_average_check} list_id_product = list(df_turnover) list_qty_from = list(df_qty.iloc[0]) list_qty_to = list(df_qty.iloc[1]) list_turnover_from = list(df_turnover.iloc[0]) list_turnover_to = list(df_turnover.iloc[1]) grow_sales = [(list_id_product[i], round(list_qty_to[i] - list_qty_from[i], 2), round(list_turnover_to[i] - \ list_turnover_from[i], 2)) for i in range(len(list_qty_from)) if list_qty_from[i] < list_qty_to[i]] fell_sales = [(list_id_product[i], round(list_qty_to[i] - list_qty_from[i], 2), round(list_turnover_to[i] - \ list_turnover_from[i], 2)) for i in range(len(list_qty_from)) if list_qty_from[i] > list_qty_to[i]] data = { "data_indicator": data_indicator, "grow_sales": grow_sales, "fell_sales": fell_sales } return render(request, "index.html", context={ "client_info": client_info, "data": data }) except: return render(request, "login.html") return render(request, "login.html")
''' Приклад роботи с datawiz API ''' import datetime from dwapi import datawiz date_from = datetime.date(2015, 8, 9) date_to = datetime.date(2015, 9, 12) # Створення классу для вибору данних: # datawiz.DW(API_KEY, API_SECRET) # якщо де API_KEY, API_SECRET - ключ і підпис користувача, якщо вони не задані то запускаємо для тестового користувача. dw = datawiz.DW() print dw.get_products_sale(products=[2855561, 2861880], by='turnover', shops=[601, 641, 595], date_from=date_from, date_to=date_to, interval=datawiz.WEEKS)
def get_client(request): email = request.user.username password = request.user.password dw = datawiz.DW() return dw
def get(self, request): try: storage = get_messages(request) auth = None for message in storage: auth = message break user = auth.message.split(' ') dw = datawiz.DW(user[0], user[1]) date_from = datetime.date(2015, 11, 17) date_to = datetime.date(2015, 11, 18) data = { 'date_from': date_from, 'date_to': date_to, } turnover = dw.get_products_sale(by='turnover', date_from=date_from, date_to=date_to) data['turnover'] = { 'from': round(turnover.sum(axis=1)[0], 2), 'to': round(turnover.sum(axis=1)[1], 2), 'percent': round((round(turnover.sum(axis=1)[1], 2) - round(turnover.sum(axis=1)[0], 2)) / max(round(turnover.sum(axis=1)[1], 2), round(turnover.sum(axis=1)[0], 2)) * 100, 2), 'diff': round( round(turnover.sum(axis=1)[1], 2) - round(turnover.sum(axis=1)[0], 2), 2), } quantity = dw.get_products_sale(by='qty', date_from=date_from, date_to=date_to) data['quantity'] = { 'from': round(quantity.sum(axis=1)[0], 2), 'to': round(quantity.sum(axis=1)[1], 2), 'percent': round((round(quantity.sum(axis=1)[1], 2) - round(quantity.sum(axis=1)[0], 2)) / max(round(quantity.sum(axis=1)[1], 2), round(quantity.sum(axis=1)[0], 2)) * 100, 2), 'diff': round( round(quantity.sum(axis=1)[1], 2) - round(quantity.sum(axis=1)[0], 2), 2), } receipts = dw.get_products_sale(by='receipts_qty', date_from=date_from, date_to=date_to) data['receipts'] = { 'from': round(receipts.sum(axis=1)[0], 2), 'to': round(receipts.sum(axis=1)[1], 2), 'percent': round((round(receipts.sum(axis=1)[1], 2) - round(receipts.sum(axis=1)[0], 2)) / max(round(receipts.sum(axis=1)[1], 2), round(receipts.sum(axis=1)[0], 2)) * 100, 2), 'diff': round( round(receipts.sum(axis=1)[1], 2) - round(receipts.sum(axis=1)[0], 2), 2), } data['average'] = { 'from': round(data['turnover']['from'] / data['receipts']['from'], 2), 'to': round(data['turnover']['to'] / data['receipts']['to'], 2), } data['average'] = { 'from': data['average']['from'], 'to': data['average']['to'], 'percent': round( (data['average']['to'] - data['average']['from']) / max(data['average']['to'], data['average']['from']) * 100, 2), 'diff': round((data['average']['to'] - data['average']['from']), 2), } messages.add_message(request, messages.INFO, auth) return render(request, 'info\general.html', context={'data': data}) except: return redirect('/login/')