Files
RasadDam_Backend/apps/authorization/services/excel/excel_processing.py
2025-08-02 07:36:21 +03:30

122 lines
4.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
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 '-'
permission_name = [name for name in data['permission']['page_name']]
fa_permissions = [fa_permissions_dict.get(name, name) for name in permission_name]
# اگر لیست خالی بود، نمایش '-'
fa_permission_text = ' - '.join(fa_permissions) if fa_permissions 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,
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