From 5a6ddce8ade4c17ac194115bb0fd5d3ecf77c51a Mon Sep 17 00:00:00 2001 From: Stanislaw-Golebiewski Date: Mon, 20 Jan 2020 17:02:33 +0100 Subject: [PATCH] add photo integration --- bk_api/bk_api/urls.py | 10 +- bk_api/core/views.py | 2 +- .../subscreens/AddMetadata/AddMetadata.jsx | 96 ++++++++++++++----- .../subscreens/ProductsList/ProductsList.jsx | 13 ++- .../Digest/subscreens/Receipts/Receipts.jsx | 2 +- 5 files changed, 90 insertions(+), 33 deletions(-) diff --git a/bk_api/bk_api/urls.py b/bk_api/bk_api/urls.py index 26da053..a5e7236 100644 --- a/bk_api/bk_api/urls.py +++ b/bk_api/bk_api/urls.py @@ -17,20 +17,16 @@ from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.contrib import admin from django.urls import include, path from django.conf.urls import url - - - from knox import views as knox_views + from .views import LoginView -# from rest_framework.authtoken import views from bills import endpoints +import core urlpatterns = [ - # path('bills/', include('core.urls')), + path('api/photo/', include('core.urls')), path('api/admin/', admin.site.urls), url('^api/', include(endpoints)), - # url(r'^api/auth/', views.obtain_auth_token) - # url(r'api/auth/', include('knox.urls')) url(r'api/auth/login/', LoginView.as_view(), name='knox_login'), url(r'api/auth/logout/', knox_views.LogoutView.as_view(), name='knox_logout'), url(r'api/auth/logoutall/', knox_views.LogoutAllView.as_view(), name='knox_logoutall'), diff --git a/bk_api/core/views.py b/bk_api/core/views.py index 5275e11..8c324e8 100644 --- a/bk_api/core/views.py +++ b/bk_api/core/views.py @@ -28,7 +28,7 @@ def upload_bill(request): product_list = img_to_products(img) products = [{'name': prod, 'price': pri} for _, prod, pri in product_list] response = JsonResponse({'products': products}) - return HttpResponse(response) + return response @require_http_methods(["POST"]) diff --git a/bk_webapp/src/screens/AddReceipt/subscreens/AddMetadata/AddMetadata.jsx b/bk_webapp/src/screens/AddReceipt/subscreens/AddMetadata/AddMetadata.jsx index bf78cf8..8d7ab2e 100644 --- a/bk_webapp/src/screens/AddReceipt/subscreens/AddMetadata/AddMetadata.jsx +++ b/bk_webapp/src/screens/AddReceipt/subscreens/AddMetadata/AddMetadata.jsx @@ -12,7 +12,7 @@ import { } from "semantic-ui-react"; import { DateInput } from "semantic-ui-calendar-react"; import { useHistory } from "react-router-dom"; -import axios from "axios"; +import axios, { post } from "axios"; import useGlobal from "../../../../utils/global_state"; import config from "../../../../config"; @@ -27,9 +27,52 @@ function AddMetadata() { const [shopsLoading, setShopsLoading] = useState(true); const [globalState, globalActions] = useGlobal(); const [pickedShop, changePickedShop] = useState(""); + const [photoIsLoading, setPhotoIsLoading] = useState(false); + const [productsFromPhoto, setProductsFromPhoto] = useState(null); const uploadPhoto = event => { - setSelectedFile(URL.createObjectURL(event.target.files[0])); + setPhotoIsLoading(true); + // setSelectedFile(URL.createObjectURL(event.target.files[0])); + fileUploadRequest(event.target.files[0], true) + .then(resp => { + const reader = new FileReader(); + reader.addEventListener("loadend", e => { + const text = e.srcElement.result; + setProductsFromPhoto(JSON.parse(text).products); + }); + reader.readAsText(resp.data); + }) + .catch(error => { + console.log(error); + }); + + fileUploadRequest(event.target.files[0]) + .then(resp => { + setSelectedFile(URL.createObjectURL(resp.data)); + setPhotoIsLoading(false); + }) + .catch(error => { + console.log(error); + setPhotoIsLoading(false); + }); + }; + + const fileUploadRequest = (file, get_products = false) => { + let url = `${config.api_url}/api/photo/`; + if (!get_products) { + url += "debug_img/"; + } + + const formData = new FormData(); + formData.append("image", file); + const cnf = { + headers: { + "content-type": "multipart/form-data", + Authorization: `Token ${globalState.auth_token}` + }, + responseType: "blob" + }; + return post(url, formData, cnf); }; //set date to current day @@ -104,9 +147,7 @@ function AddMetadata() { id="mypic" accept="image/*" ref={fileInputRef} - onChange={stuff => { - uploadPhoto(stuff); - }} + onChange={uploadPhoto} /> - {pickedDate !== "" && pickedShop !== "" && ( - + + + + {productsFromPhoto && ( + Rozpoznane produkty: {productsFromPhoto.length} + )} + + {selectedFile && } + {photoIsLoading && ( + + Loading + )} - {selectedFile && } ); diff --git a/bk_webapp/src/screens/AddReceipt/subscreens/ProductsList/ProductsList.jsx b/bk_webapp/src/screens/AddReceipt/subscreens/ProductsList/ProductsList.jsx index f74ec25..4e90a35 100644 --- a/bk_webapp/src/screens/AddReceipt/subscreens/ProductsList/ProductsList.jsx +++ b/bk_webapp/src/screens/AddReceipt/subscreens/ProductsList/ProductsList.jsx @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useState, useEffect } from "react"; import { Button, Container, Icon, Menu } from "semantic-ui-react"; import { useHistory } from "react-router-dom"; @@ -21,8 +21,17 @@ function ProductsList() { const [pickedProduct, setPickedProduct] = useState(-1); const history = useHistory(); + useEffect(() => { + const meta_products = history.location.state.products; + console.log(meta_products); + const new_products = []; + for (let i = 0; i < meta_products.length; i++) { + new_products.push({ name: meta_products[i].name, price: 0.0, tags: [] }); + } + console.log(new_products); + setProducts(new_products); + }, []); const meta_info = history.location.state; - const addItem = item => { setProducts([...products, item]); }; diff --git a/bk_webapp/src/screens/Digest/subscreens/Receipts/Receipts.jsx b/bk_webapp/src/screens/Digest/subscreens/Receipts/Receipts.jsx index fc5cc99..53e6df6 100644 --- a/bk_webapp/src/screens/Digest/subscreens/Receipts/Receipts.jsx +++ b/bk_webapp/src/screens/Digest/subscreens/Receipts/Receipts.jsx @@ -81,7 +81,7 @@ function Receipts() { const processResponse = data => { setReceipts(data.results); setReceiptsLoading(false); - setMaxPage(Math.floor(data.count / 10)); + setMaxPage(Math.ceil(data.count / 10)); }; return (