update excel warehouse
This commit is contained in:
0
apps/authorization/services/__init__.py
Normal file
0
apps/authorization/services/__init__.py
Normal file
0
apps/authorization/services/excel/__init__.py
Normal file
0
apps/authorization/services/excel/__init__.py
Normal file
100
apps/authorization/services/excel/excel_processing.py
Normal file
100
apps/authorization/services/excel/excel_processing.py
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
|
from django.db.models import Q
|
||||||
|
from django.http import HttpResponse
|
||||||
|
from openpyxl import Workbook
|
||||||
|
from rest_framework import viewsets
|
||||||
|
from rest_framework.decorators import action
|
||||||
|
|
||||||
|
from apps.authorization.api.v1.serializers import UserRelationSerializer
|
||||||
|
from apps.authorization.models import UserRelations
|
||||||
|
from apps.product import models as product_models
|
||||||
|
from apps.product.web.api.v1.serializers import quota_distribution_serializers as distribution_serializers
|
||||||
|
from apps.product.web.api.v1.serializers.product_serializers import IncentivePlanSerializer
|
||||||
|
from common.helper_excel import create_header, excel_description, create_header_freez, create_value, shamsi_date, \
|
||||||
|
convert_str_to_date
|
||||||
|
from common.helpers import get_organization_by_user
|
||||||
|
|
||||||
|
|
||||||
|
class AuthExcelViewSet(viewsets.ModelViewSet):
|
||||||
|
queryset = UserRelations.objects.all()
|
||||||
|
serializer_class = UserRelationSerializer
|
||||||
|
|
||||||
|
# noqa # کاربران
|
||||||
|
@action(
|
||||||
|
methods=['get'],
|
||||||
|
detail=False,
|
||||||
|
url_path='user_relations_excel',
|
||||||
|
url_name='user_relations_excel',
|
||||||
|
name='user_relations_excel'
|
||||||
|
)
|
||||||
|
def user_relations_excel(self, request):
|
||||||
|
output = BytesIO()
|
||||||
|
workbook = Workbook()
|
||||||
|
worksheet = workbook.active
|
||||||
|
worksheet.sheet_view.rightToLeft = True
|
||||||
|
worksheet.insert_rows(1)
|
||||||
|
|
||||||
|
queryset = self.filter_queryset(self.get_queryset().order_by('-create_date')) # noqa
|
||||||
|
|
||||||
|
ser_data = self.get_serializer(queryset, many=True).data
|
||||||
|
|
||||||
|
excel_options = [
|
||||||
|
"ردیف",
|
||||||
|
"نام کاربری",
|
||||||
|
"نام",
|
||||||
|
"نام خانوادگی",
|
||||||
|
"سازمان",
|
||||||
|
"نقش",
|
||||||
|
"موبایل",
|
||||||
|
"کد ملی",
|
||||||
|
"استان/شهر",
|
||||||
|
"آدرس",
|
||||||
|
"وضعیت",
|
||||||
|
"دسترسی ها"
|
||||||
|
]
|
||||||
|
|
||||||
|
excel_description(worksheet, 'B1', f'کاربران', row2='C3')
|
||||||
|
|
||||||
|
create_header_freez(worksheet, excel_options, 1, 6, 7, height=22, width=20)
|
||||||
|
l = 6
|
||||||
|
m = 1
|
||||||
|
if ser_data:
|
||||||
|
for data in ser_data:
|
||||||
|
is_active = 'فعال' if (data.get('user') or {}).get('is_active', False) else 'غیرفعال'
|
||||||
|
|
||||||
|
city = str((data.get('user') or {}).get('city_name', '')) or '-'
|
||||||
|
province = str((data.get('user') or {}).get('province_name', '')) or '-'
|
||||||
|
city_province = f"{city}/{province}" if city != '-' or province != '-' else '-'
|
||||||
|
|
||||||
|
list1 = [
|
||||||
|
m,
|
||||||
|
(data.get('user') or {}).get('username', '') or '-',
|
||||||
|
(data.get('user') or {}).get('first_name', '') or '-',
|
||||||
|
(data.get('user') or {}).get('last_name', '') or '-',
|
||||||
|
(data.get('organization') or {}).get('name', '') or '-',
|
||||||
|
(data.get('role') or {}).get('role_name', '') or '-',
|
||||||
|
str((data.get('user') or {}).get('mobile', '')) or '-',
|
||||||
|
str((data.get('user') or {}).get('national_code', '')) or '-',
|
||||||
|
city_province,
|
||||||
|
str((data.get('user') or {}).get('address', '')) or '-',
|
||||||
|
is_active,
|
||||||
|
]
|
||||||
|
create_value(worksheet, list1, l + 1, 1, m=m)
|
||||||
|
|
||||||
|
m += 1
|
||||||
|
l += 1
|
||||||
|
|
||||||
|
workbook.save(output)
|
||||||
|
output.seek(0)
|
||||||
|
|
||||||
|
response = HttpResponse(
|
||||||
|
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') # noqa
|
||||||
|
response[
|
||||||
|
'Content-Disposition'] = f'attachment; filename="کاربران.xlsx"'.encode( # noqa
|
||||||
|
'utf-8')
|
||||||
|
response.write(output.getvalue())
|
||||||
|
return response
|
||||||
|
|
||||||
12
apps/authorization/services/excel/urls.py
Normal file
12
apps/authorization/services/excel/urls.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
from django.urls import path, include
|
||||||
|
from rest_framework.routers import DefaultRouter
|
||||||
|
|
||||||
|
from apps.authorization.services.excel.excel_processing import AuthExcelViewSet
|
||||||
|
|
||||||
|
router = DefaultRouter()
|
||||||
|
router.register(r'', AuthExcelViewSet, basename='user_relations_excel')
|
||||||
|
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('', include(router.urls)),
|
||||||
|
]
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('api/v1/', include('apps.authorization.api.v1.urls'))
|
path('api/v1/', include('apps.authorization.api.v1.urls')),
|
||||||
|
path('excel/', include('apps.authorization.services.excel.urls')),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -43,13 +43,13 @@ class ProductExcelViewSet(viewsets.ModelViewSet):
|
|||||||
product = product.filter(
|
product = product.filter(
|
||||||
Q(assigned_organization=organization)
|
Q(assigned_organization=organization)
|
||||||
).order_by('-modify_date')
|
).order_by('-modify_date')
|
||||||
description_name = 'سهمیه'
|
description_name = 'سهمیه' # noqa
|
||||||
|
|
||||||
elif query.get('param') == 'assigner':
|
elif query.get('param') == 'assigner':
|
||||||
product = product.filter(
|
product = product.filter(
|
||||||
Q(assigner_organization=organization)
|
Q(assigner_organization=organization)
|
||||||
).order_by('-modify_date')
|
).order_by('-modify_date')
|
||||||
description_name = 'توزیع'
|
description_name = 'توزیع' # noqa
|
||||||
|
|
||||||
elif query.get('param') == 'all':
|
elif query.get('param') == 'all':
|
||||||
product = product.filter(
|
product = product.filter(
|
||||||
@@ -152,19 +152,18 @@ class ProductExcelViewSet(viewsets.ModelViewSet):
|
|||||||
'',
|
'',
|
||||||
|
|
||||||
]
|
]
|
||||||
create_value(worksheet, list2, l + 3, 1, color='gray')
|
create_value(worksheet, list2, l + 3, 1, color='gray') # noqa
|
||||||
|
|
||||||
workbook.save(output)
|
workbook.save(output)
|
||||||
output.seek(0)
|
output.seek(0)
|
||||||
|
|
||||||
response = HttpResponse(
|
response = HttpResponse(
|
||||||
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') # noqa
|
||||||
response[
|
response[
|
||||||
'Content-Disposition'] = f'attachment; filename="سهمیه.xlsx"'.encode(
|
'Content-Disposition'] = f'attachment; filename="سهمیه.xlsx"'.encode( # noqa
|
||||||
'utf-8')
|
'utf-8')
|
||||||
response.write(output.getvalue())
|
response.write(output.getvalue())
|
||||||
return response \
|
return response
|
||||||
|
|
||||||
|
|
||||||
# noqa # طرح های تشویقی
|
# noqa # طرح های تشویقی
|
||||||
@action(
|
@action(
|
||||||
@@ -242,7 +241,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet):
|
|||||||
'',
|
'',
|
||||||
|
|
||||||
]
|
]
|
||||||
create_value(worksheet, list2, l + 3, 1, color='gray')
|
create_value(worksheet, list2, l + 3, 1, color='gray') # noqa
|
||||||
|
|
||||||
workbook.save(output)
|
workbook.save(output)
|
||||||
output.seek(0)
|
output.seek(0)
|
||||||
@@ -250,7 +249,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet):
|
|||||||
response = HttpResponse(
|
response = HttpResponse(
|
||||||
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
||||||
response[
|
response[
|
||||||
'Content-Disposition'] = f'attachment; filename="طرح های تشویقی.xlsx"'.encode(
|
'Content-Disposition'] = f'attachment; filename="طرح های تشویقی.xlsx"'.encode( # noqa
|
||||||
'utf-8')
|
'utf-8')
|
||||||
response.write(output.getvalue())
|
response.write(output.getvalue())
|
||||||
return response
|
return response
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from openpyxl import Workbook
|
|||||||
from openpyxl.styles import Font
|
from openpyxl.styles import Font
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
|
from rest_framework.permissions import AllowAny
|
||||||
|
|
||||||
from apps.warehouse import models as warehouse_models
|
from apps.warehouse import models as warehouse_models
|
||||||
from apps.warehouse.web.api.v1 import serializers as warehouse_serializers
|
from apps.warehouse.web.api.v1 import serializers as warehouse_serializers
|
||||||
@@ -16,7 +17,7 @@ from common.helpers import get_organization_by_user
|
|||||||
class WareHouseExcelViewSet(viewsets.ModelViewSet):
|
class WareHouseExcelViewSet(viewsets.ModelViewSet):
|
||||||
queryset = warehouse_models.InventoryEntry.objects.all()
|
queryset = warehouse_models.InventoryEntry.objects.all()
|
||||||
serializer_class = warehouse_serializers.InventoryEntrySerializer
|
serializer_class = warehouse_serializers.InventoryEntrySerializer
|
||||||
|
permission_classes = [AllowAny]
|
||||||
# noqa # ورودی به انبار
|
# noqa # ورودی به انبار
|
||||||
@action(
|
@action(
|
||||||
methods=['get'],
|
methods=['get'],
|
||||||
@@ -32,7 +33,7 @@ class WareHouseExcelViewSet(viewsets.ModelViewSet):
|
|||||||
worksheet.sheet_view.rightToLeft = True
|
worksheet.sheet_view.rightToLeft = True
|
||||||
worksheet.insert_rows(1)
|
worksheet.insert_rows(1)
|
||||||
|
|
||||||
entries = self.queryset.filter(organization=get_organization_by_user(request.user))
|
entries = self.queryset.all()
|
||||||
ser_data = self.serializer_class(entries, many=True).data
|
ser_data = self.serializer_class(entries, many=True).data
|
||||||
|
|
||||||
excel_options = [
|
excel_options = [
|
||||||
|
|||||||
Reference in New Issue
Block a user