add product
This commit is contained in:
1 parent
423d4cf40e
commit
c8d45dd07c
58 files changed
+8309
-674
No files matched your search
@@ -5,45 +5,34 @@ from apps.shop.models import Product as ProductModel, ProductCategory as Product
|
||||
from apps.workspace.models import Workspace as WorkspaceModel, City as CityModel
|
||||
from apps.customer.models import Customer as CustomerModel
|
||||
from apps.customer.models import ResponsibleGroup as ResponsibleGroupModel
|
||||
import os
|
||||
from django.core.exceptions import ValidationError as DjangoValidationError
|
||||
from django.db import IntegrityError, transaction
|
||||
from rest_framework import exceptions
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from utils import custom_permissions
|
||||
from utils.get_user_workspace import get_user_workspace
|
||||
from utils.validations.mobile import normalize_mobile
|
||||
|
||||
|
||||
class ImportExcelView(APIView):
|
||||
permission_classes = (IsAuthenticated, custom_permissions.WorkspaceAdminPermission)
|
||||
|
||||
def get(self, request):
|
||||
|
||||
df = pd.read_excel('pe.xlsx')
|
||||
for index, row in df.iterrows():
|
||||
cate_name = row['cate_name']
|
||||
#order_cat = row['order_cat']
|
||||
acc_code = row['acc_code']
|
||||
prd_name = row['name']
|
||||
order_prd = row['size']
|
||||
vahed = row['vahed']
|
||||
|
||||
workspace = WorkspaceModel.objects.get(id=2)
|
||||
try:
|
||||
prd_ct = ProductCategoryModel.objects.get(name=cate_name, sys_ws=workspace)
|
||||
except ProductCategoryModel.DoesNotExist as exp:
|
||||
prd_ct = ProductCategoryModel.objects.create(name=cate_name, sys_ws=workspace)
|
||||
|
||||
if ProductModel.objects.filter(acc_code=acc_code).exists() == False:
|
||||
new_product = ProductModel()
|
||||
new_product.name = prd_name
|
||||
new_product.price = 0
|
||||
new_product.category = ProductCategoryModel.objects.get(id=prd_ct.id)
|
||||
new_product.acc_code = acc_code
|
||||
new_product.order_prd = order_prd
|
||||
new_product.view_only = True
|
||||
new_product.attr = {"acc_vahed": vahed}
|
||||
new_product.sys_ws = workspace
|
||||
new_product.save()
|
||||
else:
|
||||
up_product = ProductModel.objects.get(acc_code=acc_code)
|
||||
up_product.name = prd_name
|
||||
up_product.save()
|
||||
|
||||
return Response(data={"result": 'ok'})
|
||||
# Keep the legacy URL and filename, but use the same validated importer as Admin.
|
||||
from django.core.files import File
|
||||
from apps.shop.product_import import parse_workbook, import_products
|
||||
try:
|
||||
with open('pe.xlsx', 'rb') as source:
|
||||
rows = parse_workbook(File(source, name='pe.xlsx'))
|
||||
for row in rows:
|
||||
row['values'].setdefault('view_only', 'true')
|
||||
result = import_products(get_user_workspace(request), rows,
|
||||
{'mode': 'upsert', 'create_categories': True, 'errors': 'atomic', 'ordering': 'preserve'})
|
||||
except (OSError, DjangoValidationError) as error:
|
||||
raise exceptions.ValidationError(getattr(error, 'messages', ['فایل اکسل محصولات خوانده نشد.']))
|
||||
if not result['committed']:
|
||||
raise exceptions.ValidationError(result)
|
||||
return Response(data={'result': 'ok', 'imported': result['created'] + result['updated']})
|
||||
|
||||
|
||||
class ImportExcelCitysView(APIView):
|
||||
@@ -72,43 +61,46 @@ class ImportExcelCitysView(APIView):
|
||||
return Response(data={"result": 'ok'})
|
||||
|
||||
|
||||
def customer_from_excel(row, workspace):
|
||||
mobile = row['mobile']
|
||||
if pd.isna(mobile) or pd.isna(row['name']):
|
||||
raise exceptions.ValidationError('نام و شماره موبایل مشتری الزامی است.')
|
||||
if isinstance(mobile, float) and mobile.is_integer():
|
||||
mobile = int(mobile)
|
||||
city = CityModel.objects.filter(name=row['shar'], ostan__name=row['ostan'], ostan__is_ostan=True).first()
|
||||
city = city or CityModel.objects.filter(pk=1229).first()
|
||||
if not city:
|
||||
raise exceptions.ValidationError('شهر مشتری در فایل پیدا نشد.')
|
||||
group = ResponsibleGroupModel.objects.filter(pk=row['id_mantaghe'], sys_ws=workspace).first()
|
||||
if not group:
|
||||
raise exceptions.ValidationError('گروه مسئول متعلق به این فضای کاری نیست.')
|
||||
customer = CustomerModel(name_family=row['name'], mobile=normalize_mobile(mobile), city=city,
|
||||
responsible_group=group, sys_ws=workspace,
|
||||
phone=None if pd.isna(row['phone']) else row['phone'],
|
||||
address=None if pd.isna(row['address']) else row['address'])
|
||||
customer.full_clean()
|
||||
customer.save()
|
||||
|
||||
|
||||
class ImportExcelCustomersView(APIView):
|
||||
permission_classes = (IsAuthenticated, custom_permissions.WorkspaceAdminPermission)
|
||||
|
||||
@transaction.atomic
|
||||
def get(self, request):
|
||||
|
||||
df = pd.read_excel('customersData.xlsx')
|
||||
for index, row in df.iterrows():
|
||||
name = row['name']
|
||||
mobile = row['mobile']
|
||||
|
||||
if name and mobile:
|
||||
|
||||
phone = row['phone']
|
||||
address = row['address']
|
||||
ostan = row['ostan']
|
||||
shahr = row['shar']
|
||||
mantaghe = row['id_mantaghe']
|
||||
|
||||
try:
|
||||
ostan_obj = CityModel.objects.get(name=ostan, is_ostan=True)
|
||||
shahr_obj = CityModel.objects.get(name=shahr, ostan=ostan_obj)
|
||||
except Exception as exp:
|
||||
shahr_obj = CityModel.objects.get(id=1229)
|
||||
|
||||
try:
|
||||
cs = CustomerModel()
|
||||
cs.name_family = name
|
||||
cs.mobile = mobile
|
||||
if phone:
|
||||
cs.phone = phone
|
||||
|
||||
cs.city = shahr_obj
|
||||
if address:
|
||||
cs.address = address
|
||||
cs.responsible_group = ResponsibleGroupModel.objects.get(id=mantaghe)
|
||||
cs.sys_ws = WorkspaceModel.objects.get(id=2)
|
||||
cs.save()
|
||||
except Exception as exp:
|
||||
print(name)
|
||||
|
||||
return Response(data={"result": 'ok'})
|
||||
workspace = get_user_workspace(request)
|
||||
try:
|
||||
data = pd.read_excel('customersData.xlsx')
|
||||
except (OSError, ValueError):
|
||||
raise exceptions.ValidationError('فایل اکسل مشتریان خوانده نشد.')
|
||||
for index, row in data.iterrows():
|
||||
try:
|
||||
customer_from_excel(row, workspace)
|
||||
except DjangoValidationError as error:
|
||||
raise exceptions.ValidationError({'row': int(index)+2, 'errors': error.message_dict})
|
||||
except IntegrityError:
|
||||
raise exceptions.ValidationError({'row': int(index)+2, 'mobile': 'شماره موبایل مشتری تکراری است.'})
|
||||
except exceptions.ValidationError as error:
|
||||
raise exceptions.ValidationError({'row': int(index)+2, 'errors': error.detail})
|
||||
except (KeyError, TypeError, ValueError):
|
||||
raise exceptions.ValidationError({'row': int(index)+2, 'error': 'اطلاعات این ردیف معتبر نیست.'})
|
||||
return Response(data={'result': 'ok', 'imported': len(data)})
|
||||
Reference in new issue
Block a user