massive product load implementation, not tested
This commit is contained in:
@@ -21,6 +21,7 @@ from django.conf import settings
|
||||
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView, TokenVerifyView
|
||||
|
||||
from core import views as core_views
|
||||
from products import views as product_views
|
||||
from .routers import router
|
||||
|
||||
|
||||
@@ -31,6 +32,7 @@ urlpatterns = [
|
||||
path('api/v1/token/verify/', TokenVerifyView.as_view(), name='token_verify'),
|
||||
path('api/v1/user/change_password/<int:pk>/', core_views.ChangeUserPasswordView.as_view(), name="change-password"),
|
||||
path('api/v1/user/update/<int:pk>/', core_views.UpdateUserView.as_view(), name="update-user"),
|
||||
path('api/v1/load_coops/', core_views.load_coop_managers, name='csv-loader'),
|
||||
path('api/v1/load_coops/', core_views.load_coop_managers, name='coop-loader'),
|
||||
path('api/v1/load_products/', product_views.load_coop_products, name='product-loader'),
|
||||
path('api/v1/', include(router.urls)),
|
||||
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
|
||||
@@ -129,3 +129,4 @@ def load_coop_managers(request):
|
||||
return Response()
|
||||
except Exception as e:
|
||||
return Response({"errors": {"details": str(type(e))}})
|
||||
|
||||
|
||||
1
datasets/test_products.csv
Normal file
1
datasets/test_products.csv
Normal file
@@ -0,0 +1 @@
|
||||
id,nombre-producto,descripcion,imagen,url,precio,gastos-envio,cond-envio,descuento,stock,tags,categoria,identificadores
|
||||
|
@@ -1,8 +1,15 @@
|
||||
import logging
|
||||
import csv
|
||||
|
||||
from django.shortcuts import render
|
||||
from django.conf import settings
|
||||
|
||||
# Create your views here.
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.permissions import IsAuthenticatedOrReadOnly
|
||||
from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAdminUser
|
||||
from rest_framework.decorators import api_view, permission_classes
|
||||
|
||||
import requests
|
||||
|
||||
from products.models import Product
|
||||
from products.serializers import ProductSerializer
|
||||
@@ -10,7 +17,78 @@ from products.serializers import ProductSerializer
|
||||
from back_latienda.permissions import IsCreator
|
||||
|
||||
|
||||
logging.basicConfig(
|
||||
filename='logs/product-load.log',
|
||||
filemode='w',
|
||||
format='%(levelname)s:%(message)s',
|
||||
level=logging.INFO,
|
||||
)
|
||||
|
||||
|
||||
class ProductViewSet(viewsets.ModelViewSet):
|
||||
queryset = Product.objects.all()
|
||||
serializer_class = ProductSerializer
|
||||
permission_classes = [IsAuthenticatedOrReadOnly, IsCreator]
|
||||
|
||||
|
||||
@api_view(['POST',])
|
||||
@permission_classes([IsAdminUser,])
|
||||
def load_coop_products(request):
|
||||
"""Read CSV file being received
|
||||
Parse it to create products for related Company
|
||||
"""
|
||||
try:
|
||||
csv_file = request.FILES['csv_file']
|
||||
if csv_file.name.endswith('.csv') is not True:
|
||||
logging.error(f"File {csv_file.name} is not a CSV file")
|
||||
return Response({"errors":{"details": "File is not CSV type"}})
|
||||
|
||||
logging.info(f"Reading contents of {csv_file.name}")
|
||||
decoded_file = csv_file.read().decode('utf-8').splitlines()
|
||||
csv_reader = csv.DictReader(decoded_file, delimiter=',')
|
||||
counter = 0
|
||||
for row in csv_reader:
|
||||
if '' in (row['nombre-producto'], row['descripcion'], row['precio']):
|
||||
logging.error(f"Required data missing: {row}")
|
||||
continue
|
||||
try:
|
||||
# download image references in csv
|
||||
image_url = row['image'].strip()
|
||||
response = requests.get(image_url, stream=True)
|
||||
if response.status_code == 200:
|
||||
path = f"{setting.BASE_DIR}media/{row['nombre-producto'].strip()}.{image.url.split('/')[-1]}"
|
||||
logging.info(f"Saving product image to: {path}")
|
||||
new_image = open(path, 'wb')
|
||||
for chunk in response:
|
||||
new_image.write(chunk)
|
||||
new_image.close()
|
||||
else:
|
||||
logging.warninig(f"Image URL did not work: {image_url}")
|
||||
new_image = None
|
||||
# assemble instance data
|
||||
product_data = {
|
||||
'id': None if row['id'].strip()=='' else row['id'].strip(),
|
||||
'name': row['nombre-coop'].strip(),
|
||||
'short_name': row['nombre-producto'].strip(),
|
||||
'description': row['descripcion'].strip(),
|
||||
'image': new_image,
|
||||
'url': row['url'].strip(),
|
||||
'precio': row['precio'].strip(),
|
||||
'shipping_cost': row['gastos-envio'].strip(),
|
||||
'shipping_terms': row['cond-envio'].strip(),
|
||||
'discount': row['descuento'].strip(),
|
||||
'stock': row['stock'].strip(),
|
||||
'tags': row['tags'].strip(),
|
||||
'category': row['categoria'].strip(),
|
||||
'identifiers': row['identificadores'].strip(),
|
||||
}
|
||||
Product.objects.create(**product_data)
|
||||
logging.info(f"Created Product: {product_data}")
|
||||
counter += 1
|
||||
except Exception as e:
|
||||
logging.error(f"Could not parse {row}")
|
||||
return Response()
|
||||
except Exception as e:
|
||||
return Response({"errors": {"details": str(type(e))}})
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user