diff --git a/.idea/Rasaddam_Backend.iml b/.idea/Rasaddam_Backend.iml index 168bde0..c5d6090 100644 --- a/.idea/Rasaddam_Backend.iml +++ b/.idea/Rasaddam_Backend.iml @@ -14,7 +14,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 296aa57..29f5506 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/apps/authorization/services/__init__.py b/apps/authorization/services/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/authorization/services/excel/__init__.py b/apps/authorization/services/excel/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/authorization/services/excel/excel_processing.py b/apps/authorization/services/excel/excel_processing.py new file mode 100644 index 0000000..5eb92de --- /dev/null +++ b/apps/authorization/services/excel/excel_processing.py @@ -0,0 +1,120 @@ +from io import BytesIO + +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 common.helper_excel import excel_description, create_header_freez, create_value + + +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=25, width=20) + l = 6 + m = 1 + + fa_permissions_dict = { + "permission_control": "مدیریت دسترسی", + "users": "کاربران", + "organizations": "سازمان ها", + "roles_management": "مدیریت نقش", + "feed_input_products": "محصولات", + "product_categories": "دسته بندی محصولات", + "pricing": "قیمت گذاری", + "incentive_plans": "طرح های تشویقی", + "quota": "سهم بندی", + "quota_distributions": "صفحه توزیع سهمیه", + "inventory": "انبار" + } + + 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 '-' + try: + if data.get('permissions') and isinstance(data['permissions'], list): + permission_names = [] + for perm in data['permissions']: + if isinstance(perm, dict) and 'page_name' in perm: + permission_names.append(perm['page_name']) + + fa_permissions = [fa_permissions_dict.get(name, name) for name in permission_names] + fa_permission_text = ' - '.join(fa_permissions) if fa_permissions else '-' + else: + fa_permission_text = '-' + except (KeyError, TypeError): + fa_permission_text = '-' + + 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, + fa_permission_text + ] + 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 diff --git a/apps/authorization/services/excel/urls.py b/apps/authorization/services/excel/urls.py new file mode 100644 index 0000000..f45faf9 --- /dev/null +++ b/apps/authorization/services/excel/urls.py @@ -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)), +] diff --git a/apps/authorization/urls.py b/apps/authorization/urls.py index dfec62e..5807dfe 100644 --- a/apps/authorization/urls.py +++ b/apps/authorization/urls.py @@ -1,5 +1,6 @@ from django.urls import path, include 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')), ] diff --git a/apps/product/services/excel/excel_processing.py b/apps/product/services/excel/excel_processing.py index cfe98bd..b094122 100644 --- a/apps/product/services/excel/excel_processing.py +++ b/apps/product/services/excel/excel_processing.py @@ -4,9 +4,10 @@ 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 import viewsets, filters from rest_framework.decorators import action +from apps.core.mixins.search_mixin import DynamicSearchMixin 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 @@ -15,9 +16,10 @@ from common.helper_excel import create_header, excel_description, create_header_ from common.helpers import get_organization_by_user -class ProductExcelViewSet(viewsets.ModelViewSet): +class ProductExcelViewSet(viewsets.ModelViewSet, DynamicSearchMixin): queryset = product_models.QuotaDistribution.objects.all() serializer_class = distribution_serializers.QuotaDistributionSerializer + filter_backends = [filters.SearchFilter] # noqa # سهمیه و توزیع @action( @@ -34,22 +36,23 @@ class ProductExcelViewSet(viewsets.ModelViewSet): worksheet.sheet_view.rightToLeft = True worksheet.insert_rows(1) - product = self.queryset - query = request.query_params + product = self.filter_query(self.queryset) # return by search param or all objects organization = get_organization_by_user(request.user) + + query = self.request.query_params description_name = '' if query.get('param') == 'assigned': product = product.filter( Q(assigned_organization=organization) ).order_by('-modify_date') - description_name = 'سهمیه' + description_name = 'سهمیه' # noqa elif query.get('param') == 'assigner': product = product.filter( Q(assigner_organization=organization) ).order_by('-modify_date') - description_name = 'توزیع' + description_name = 'توزیع' # noqa elif query.get('param') == 'all': product = product.filter( @@ -86,11 +89,11 @@ class ProductExcelViewSet(viewsets.ModelViewSet): ] - create_header(worksheet, header_list, 5, 2, height=20, border_style='thin') + create_header(worksheet, header_list, 5, 2, height=25, border_style='thin') excel_description(worksheet, 'B1', f'{description_name}', row2='C3') - create_header_freez(worksheet, excel_options, 1, 6, 7, height=22, width=20) + create_header_freez(worksheet, excel_options, 1, 6, 7, height=25, width=20) l = 6 m = 1 @@ -102,10 +105,10 @@ class ProductExcelViewSet(viewsets.ModelViewSet): str(data['quota']['quota_id']) if data.get('quota') and data['quota'].get('quota_id') else '', str(shamsi_date(convert_str_to_date(data['create_date']), in_value=True)) if data.get( 'create_date') else '', - data[ - 'assigner_organization'].get('organization') or '-', - data[ - 'assigned_organization'].get('organization') or '-', + (data[ + 'assigner_organization'] or {}).get('organization') or '-', + (data[ + 'assigned_organization'] or {}).get('organization') or '-', data.get('weight') or 0, data.get('distributed') or 0, data.get('remaining_weight') or 0, @@ -114,7 +117,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet): data.get('warehouse_entry') or 0, data.get('description') or '-' ] - create_value(worksheet, list1, l + 1, 1, m=m) + create_value(worksheet, list1, l + 1, 1, height=24, m=m) m += 1 l += 1 @@ -152,19 +155,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) output.seek(0) response = HttpResponse( - content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') + content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') # noqa response[ - 'Content-Disposition'] = f'attachment; filename="سهمیه.xlsx"'.encode( + 'Content-Disposition'] = f'attachment; filename="سهمیه.xlsx"'.encode( # noqa 'utf-8') response.write(output.getvalue()) - return response \ - + return response # noqa # طرح های تشویقی @action( @@ -204,7 +206,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet): excel_description(worksheet, 'B1', f'طرح های تشویقی', row2='C3') - create_header_freez(worksheet, excel_options, 1, 6, 7, height=22, width=20) + create_header_freez(worksheet, excel_options, 1, 6, 7, height=25, width=20) l = 6 m = 1 @@ -226,7 +228,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet): str(shamsi_date(convert_str_to_date(data.get('start_date_limit')), in_value=True)), str(shamsi_date(convert_str_to_date(data.get('end_date_limit')), in_value=True)), ] - create_value(worksheet, list1, l + 1, 1, m=m) + create_value(worksheet, list1, l + 1, 1, height=24, m=m) m += 1 l += 1 @@ -242,7 +244,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet): '', ] - create_value(worksheet, list2, l + 3, 1, color='gray') + create_value(worksheet, list2, l + 3, 1, height=24, color='gray') # noqa workbook.save(output) output.seek(0) @@ -250,7 +252,7 @@ class ProductExcelViewSet(viewsets.ModelViewSet): response = HttpResponse( content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') response[ - 'Content-Disposition'] = f'attachment; filename="طرح های تشویقی.xlsx"'.encode( + 'Content-Disposition'] = f'attachment; filename="طرح های تشویقی.xlsx"'.encode( # noqa 'utf-8') response.write(output.getvalue()) return response diff --git a/apps/warehouse/services/excel/excel_processing.py b/apps/warehouse/services/excel/excel_processing.py index b8f9cb2..23e6631 100644 --- a/apps/warehouse/services/excel/excel_processing.py +++ b/apps/warehouse/services/excel/excel_processing.py @@ -51,9 +51,9 @@ class WareHouseExcelViewSet(viewsets.ModelViewSet): ] - create_header(worksheet, header_list, 5, 2, height=20, border_style='thin') + create_header(worksheet, header_list, 5, 2, height=25, border_style='thin') excel_description(worksheet, 'B1', f'ورودی به انبار', row2='C3') - create_header_freez(worksheet, excel_options, 1, 6, 7, height=22, width=20) + create_header_freez(worksheet, excel_options, 1, 6, 7, height=25, width=20) l = 6 m = 1 @@ -74,14 +74,14 @@ class WareHouseExcelViewSet(viewsets.ModelViewSet): str(shamsi_date(convert_str_to_date(data['create_date']), in_value=True)) if data.get( 'create_date') else '', str(data[ - 'distribution'].get('distribution')) or '-', + 'distribution'].get('distribution')) or '-', data.get('weight') or 0, data.get('lading_number') or '-', data.get('delivery_address') or '-', document_value, data.get('notes') or '', ] - create_value(worksheet, list1, l + 1, 1, m=m) + create_value(worksheet, list1, l + 1, 1, height=23, m=m) if document: worksheet.cell(row=l + 1, column=7).font = Font(color="0563C1", underline='single', bold=True) m += 1 @@ -105,7 +105,7 @@ class WareHouseExcelViewSet(viewsets.ModelViewSet): '', '' ] - create_value(worksheet, list2, l + 3, 1, color='gray') + create_value(worksheet, list2, l + 3, 1, color='gray', height=23) workbook.save(output) output.seek(0)