ceate excel for herd and livestock
This commit is contained in:
2
.idea/Rasaddam_Backend.iml
generated
2
.idea/Rasaddam_Backend.iml
generated
@@ -14,7 +14,7 @@
|
|||||||
</component>
|
</component>
|
||||||
<component name="NewModuleRootManager">
|
<component name="NewModuleRootManager">
|
||||||
<content url="file://$MODULE_DIR$" />
|
<content url="file://$MODULE_DIR$" />
|
||||||
<orderEntry type="jdk" jdkName="Python 3.10 (env)" jdkType="Python SDK" />
|
<orderEntry type="jdk" jdkName="Python 3.10 (bazrasienv)" jdkType="Python SDK" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PyDocumentationSettings">
|
<component name="PyDocumentationSettings">
|
||||||
|
|||||||
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (env)" project-jdk-type="Python SDK" />
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (bazrasienv)" project-jdk-type="Python SDK" />
|
||||||
</project>
|
</project>
|
||||||
0
apps/herd/services/__init__.py
Normal file
0
apps/herd/services/__init__.py
Normal file
0
apps/herd/services/excel/__init__.py
Normal file
0
apps/herd/services/excel/__init__.py
Normal file
234
apps/herd/services/excel/excel_processing.py
Normal file
234
apps/herd/services/excel/excel_processing.py
Normal file
@@ -0,0 +1,234 @@
|
|||||||
|
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.core.mixins.search_mixin import ExcelDynamicSearchMixin
|
||||||
|
from apps.herd.models import Herd, Rancher
|
||||||
|
from apps.herd.web.api.v1.serializers import HerdSerializer, RancherSerializer
|
||||||
|
from common.helper_excel import create_header, excel_description, create_header_freez, create_value
|
||||||
|
|
||||||
|
|
||||||
|
class HerdExcelViewSet(viewsets.ModelViewSet, ExcelDynamicSearchMixin):
|
||||||
|
queryset = Herd.objects.all()
|
||||||
|
serializer_class = HerdSerializer
|
||||||
|
|
||||||
|
# noqa # دامداران
|
||||||
|
@action(
|
||||||
|
methods=['get'],
|
||||||
|
detail=False,
|
||||||
|
url_path='rancher_excel',
|
||||||
|
url_name='rancher_excel',
|
||||||
|
name='rancher_excel'
|
||||||
|
)
|
||||||
|
def rancher_excel(self, request):
|
||||||
|
output = BytesIO()
|
||||||
|
workbook = Workbook()
|
||||||
|
worksheet = workbook.active
|
||||||
|
worksheet.sheet_view.rightToLeft = True
|
||||||
|
worksheet.insert_rows(1)
|
||||||
|
ranchers = Rancher.objects.all().order_by('-modify_date')
|
||||||
|
rancher_search_fields = [
|
||||||
|
"ranching_farm",
|
||||||
|
"first_name",
|
||||||
|
"last_name",
|
||||||
|
"mobile",
|
||||||
|
"national_code",
|
||||||
|
"birthdate",
|
||||||
|
"nationality",
|
||||||
|
"address",
|
||||||
|
"province__name",
|
||||||
|
"city__name",
|
||||||
|
]
|
||||||
|
query = self.filter_query(ranchers, search_list=rancher_search_fields)
|
||||||
|
|
||||||
|
ser_data = RancherSerializer(query, many=True).data
|
||||||
|
|
||||||
|
excel_options = [
|
||||||
|
"ردیف",
|
||||||
|
"نام دامداری",
|
||||||
|
"نام",
|
||||||
|
"نام خانوادگی",
|
||||||
|
"کد ملی",
|
||||||
|
"موبایل",
|
||||||
|
"استان",
|
||||||
|
"شهر",
|
||||||
|
"آدرس",
|
||||||
|
"وضعیت",
|
||||||
|
]
|
||||||
|
|
||||||
|
header_list = [
|
||||||
|
"تعداد دامداران",
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
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=25, width=20)
|
||||||
|
|
||||||
|
l = 6
|
||||||
|
m = 1
|
||||||
|
if ser_data:
|
||||||
|
for data in ser_data:
|
||||||
|
without_herd = 'بدون دام' if data.get('without_herd') == True else 'دام عادی'
|
||||||
|
|
||||||
|
list1 = [
|
||||||
|
m,
|
||||||
|
data.get('ranching_farm') or '-',
|
||||||
|
data.get('first_name') or '-',
|
||||||
|
data.get('last_name') or '-',
|
||||||
|
data.get('national_code') or '-',
|
||||||
|
data.get('mobile') or '-',
|
||||||
|
(data.get('province') or {}).get('name') or '-',
|
||||||
|
(data.get('city') or {}).get('name') or '-',
|
||||||
|
data.get('address') or '-',
|
||||||
|
without_herd,
|
||||||
|
|
||||||
|
]
|
||||||
|
create_value(worksheet, list1, l + 1, 1, height=23, m=m)
|
||||||
|
m += 1
|
||||||
|
l += 1
|
||||||
|
value_list = [
|
||||||
|
len(query)
|
||||||
|
]
|
||||||
|
|
||||||
|
create_value(worksheet, value_list, 3, 5, border_style='thin')
|
||||||
|
workbook.save(output)
|
||||||
|
output.seek(0)
|
||||||
|
|
||||||
|
response = HttpResponse(
|
||||||
|
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
||||||
|
response[
|
||||||
|
'Content-Disposition'] = f'attachment; filename="دامداران.xlsx"'.encode(
|
||||||
|
'utf-8')
|
||||||
|
response.write(output.getvalue())
|
||||||
|
return response
|
||||||
|
|
||||||
|
# noqa # دامداران
|
||||||
|
|
||||||
|
@action(
|
||||||
|
methods=['get'],
|
||||||
|
detail=False,
|
||||||
|
url_path='herd_excel',
|
||||||
|
url_name='herd_excel',
|
||||||
|
name='herd_excel'
|
||||||
|
)
|
||||||
|
def herd_excel(self, request):
|
||||||
|
output = BytesIO()
|
||||||
|
workbook = Workbook()
|
||||||
|
worksheet = workbook.active
|
||||||
|
worksheet.sheet_view.rightToLeft = True
|
||||||
|
worksheet.insert_rows(1)
|
||||||
|
|
||||||
|
query = self.filter_query(self.queryset)
|
||||||
|
|
||||||
|
ser_data = self.serializer_class(query, many=True).data
|
||||||
|
|
||||||
|
excel_options = [
|
||||||
|
"ردیف",
|
||||||
|
"شناسه یکتا",
|
||||||
|
"نام گله",
|
||||||
|
"نام تعاونی",
|
||||||
|
"نام شرکت پیمانکار",
|
||||||
|
"ظرفیت",
|
||||||
|
"نوع فعالیت",
|
||||||
|
"کد اپیدمیولوژیک",
|
||||||
|
"حجم دام سبک",
|
||||||
|
"حجم دام سنگین",
|
||||||
|
"استان",
|
||||||
|
"شهر",
|
||||||
|
"مجوز فعالیت",
|
||||||
|
"وضعیت فعالیت",
|
||||||
|
"کد پستی",
|
||||||
|
]
|
||||||
|
|
||||||
|
header_list = [
|
||||||
|
"تعداد گله ها",
|
||||||
|
"ظرفیت کل",
|
||||||
|
"مجموع دام سبک",
|
||||||
|
"مجموع دام سنگین",
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
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=25, width=20)
|
||||||
|
|
||||||
|
l = 6
|
||||||
|
m = 1
|
||||||
|
if ser_data:
|
||||||
|
for data in ser_data:
|
||||||
|
if data.get('activity') == 'I':
|
||||||
|
activity = 'صنعتی'
|
||||||
|
elif data.get('activity') == 'V':
|
||||||
|
activity = 'روستایی'
|
||||||
|
else:
|
||||||
|
activity = 'عشایری'
|
||||||
|
operating_license_state='ندارد' if data.get('operating_license_state') == False else 'دارد'
|
||||||
|
activity_state='ندارد' if data.get('activity_state') == False else 'دارد'
|
||||||
|
|
||||||
|
list1 = [
|
||||||
|
m,
|
||||||
|
data.get('unit_unique_id') or '-',
|
||||||
|
data.get('name') or '-',
|
||||||
|
(data.get('cooperative') or {}).get('name') or '-',
|
||||||
|
(data.get('contractor') or {}).get('name') or '-',
|
||||||
|
data.get('capacity') or 0,
|
||||||
|
activity,
|
||||||
|
data.get('epidemiologic') or '-',
|
||||||
|
data.get('light_livestock_number') or 0,
|
||||||
|
data.get('heavy_livestock_number') or 0,
|
||||||
|
(data.get('province') or {}).get('name') or '-',
|
||||||
|
(data.get('city') or {}).get('name') or '-',
|
||||||
|
operating_license_state,
|
||||||
|
activity_state,
|
||||||
|
data.get('postal') or '-',
|
||||||
|
|
||||||
|
]
|
||||||
|
create_value(worksheet, list1, l + 1, 1, height=23, m=m)
|
||||||
|
m += 1
|
||||||
|
l += 1
|
||||||
|
|
||||||
|
capacity = sum((data['capacity'] or 0) for data in ser_data)
|
||||||
|
light_livestock_number = sum((data['light_livestock_number'] or 0) for data in ser_data)
|
||||||
|
heavy_livestock_number = sum((data['heavy_livestock_number'] or 0) for data in ser_data)
|
||||||
|
|
||||||
|
value_list = [
|
||||||
|
len(query),
|
||||||
|
capacity,
|
||||||
|
light_livestock_number,
|
||||||
|
heavy_livestock_number,
|
||||||
|
]
|
||||||
|
|
||||||
|
create_value(worksheet, value_list, 3, 5, border_style='thin')
|
||||||
|
|
||||||
|
list2 = [
|
||||||
|
'مجموع==>',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
capacity,
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
light_livestock_number,
|
||||||
|
heavy_livestock_number,
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
''
|
||||||
|
]
|
||||||
|
create_value(worksheet, list2, l + 3, 1, color='gray', height=23)
|
||||||
|
workbook.save(output)
|
||||||
|
output.seek(0)
|
||||||
|
|
||||||
|
response = HttpResponse(
|
||||||
|
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
||||||
|
response[
|
||||||
|
'Content-Disposition'] = f'attachment; filename="گله ها.xlsx"'.encode(
|
||||||
|
'utf-8')
|
||||||
|
response.write(output.getvalue())
|
||||||
|
return response
|
||||||
11
apps/herd/services/excel/urls.py
Normal file
11
apps/herd/services/excel/urls.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
from django.urls import path, include
|
||||||
|
from rest_framework.routers import DefaultRouter
|
||||||
|
|
||||||
|
from apps.herd.services.excel.excel_processing import HerdExcelViewSet
|
||||||
|
|
||||||
|
router = DefaultRouter()
|
||||||
|
router.register(r'', HerdExcelViewSet, basename='herd_excel')
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('', include(router.urls)),
|
||||||
|
]
|
||||||
@@ -4,5 +4,6 @@ from django.urls import path, include
|
|||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('web/', include('apps.herd.web.api.v1.urls'))
|
path('web/', include('apps.herd.web.api.v1.urls')),
|
||||||
|
path('excel/', include('apps.herd.services.excel.urls')),
|
||||||
]
|
]
|
||||||
|
|||||||
0
apps/livestock/services/__init__.py
Normal file
0
apps/livestock/services/__init__.py
Normal file
0
apps/livestock/services/excel/__init__.py
Normal file
0
apps/livestock/services/excel/__init__.py
Normal file
98
apps/livestock/services/excel/excel_processing.py
Normal file
98
apps/livestock/services/excel/excel_processing.py
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
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.core.mixins.search_mixin import ExcelDynamicSearchMixin
|
||||||
|
from apps.livestock.models import LiveStock
|
||||||
|
from apps.livestock.web.api.v1.serializers import LiveStockSerializer
|
||||||
|
from common.helper_excel import create_header, excel_description, create_header_freez, create_value, shamsi_date, \
|
||||||
|
convert_str_to_date
|
||||||
|
|
||||||
|
|
||||||
|
class LiveStockExcelViewSet(viewsets.ModelViewSet, ExcelDynamicSearchMixin):
|
||||||
|
queryset = LiveStock.objects.all()
|
||||||
|
serializer_class = LiveStockSerializer
|
||||||
|
|
||||||
|
# noqa # دامداران
|
||||||
|
@action(
|
||||||
|
methods=['get'],
|
||||||
|
detail=False,
|
||||||
|
url_path='livestock_excel',
|
||||||
|
url_name='livestock_excel',
|
||||||
|
name='livestock_excel'
|
||||||
|
)
|
||||||
|
def livestock_excel(self, request):
|
||||||
|
output = BytesIO()
|
||||||
|
workbook = Workbook()
|
||||||
|
worksheet = workbook.active
|
||||||
|
worksheet.sheet_view.rightToLeft = True
|
||||||
|
worksheet.insert_rows(1)
|
||||||
|
query = self.filter_query(self.queryset)
|
||||||
|
|
||||||
|
ser_data = self.serializer_class(query, many=True).data
|
||||||
|
|
||||||
|
excel_options = [
|
||||||
|
"ردیف",
|
||||||
|
"دام",
|
||||||
|
"نوع دام",
|
||||||
|
"تاریخ تولد",
|
||||||
|
"جنسیت",
|
||||||
|
"گونه",
|
||||||
|
"نوع وزن",
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
header_list = [
|
||||||
|
"تعداد دام",
|
||||||
|
|
||||||
|
]
|
||||||
|
|
||||||
|
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=25, width=20)
|
||||||
|
|
||||||
|
l = 6
|
||||||
|
m = 1
|
||||||
|
if ser_data:
|
||||||
|
for data in ser_data:
|
||||||
|
gender = 'نر' if data.get('gender') == 1 else 'ماده'
|
||||||
|
weight_type = 'سنگین' if data.get('weight_type') == 'H' else 'سبک'
|
||||||
|
date = data.get('birthdate') or None
|
||||||
|
if date:
|
||||||
|
sh_date = shamsi_date(convert_str_to_date(date))
|
||||||
|
else:
|
||||||
|
sh_date = '-'
|
||||||
|
|
||||||
|
list1 = [
|
||||||
|
m,
|
||||||
|
(data.get('type') or {}).get('name') or '-',
|
||||||
|
(data.get('use_type') or {}).get('name') or '-',
|
||||||
|
sh_date,
|
||||||
|
gender,
|
||||||
|
(data.get('species') or {}).get('name') or '-',
|
||||||
|
weight_type,
|
||||||
|
|
||||||
|
]
|
||||||
|
create_value(worksheet, list1, l + 1, 1, height=23, m=m)
|
||||||
|
m += 1
|
||||||
|
l += 1
|
||||||
|
value_list = [
|
||||||
|
len(query)
|
||||||
|
]
|
||||||
|
|
||||||
|
create_value(worksheet, value_list, 3, 5, border_style='thin')
|
||||||
|
workbook.save(output)
|
||||||
|
output.seek(0)
|
||||||
|
|
||||||
|
response = HttpResponse(
|
||||||
|
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
||||||
|
response[
|
||||||
|
'Content-Disposition'] = f'attachment; filename="دام ها.xlsx"'.encode(
|
||||||
|
'utf-8')
|
||||||
|
response.write(output.getvalue())
|
||||||
|
return response
|
||||||
|
|
||||||
|
# noqa # دامداران
|
||||||
11
apps/livestock/services/excel/urls.py
Normal file
11
apps/livestock/services/excel/urls.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
from django.urls import path, include
|
||||||
|
from rest_framework.routers import DefaultRouter
|
||||||
|
|
||||||
|
from apps.livestock.services.excel.excel_processing import LiveStockExcelViewSet
|
||||||
|
|
||||||
|
router = DefaultRouter()
|
||||||
|
router.register(r'', LiveStockExcelViewSet, basename='livestock_excel')
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path('', include(router.urls)),
|
||||||
|
]
|
||||||
@@ -3,6 +3,7 @@ from django.urls import path, include
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('web/api/', include('apps.livestock.web.api.v1.urls')),
|
path('web/api/', include('apps.livestock.web.api.v1.urls')),
|
||||||
|
path('excel/', include('apps.livestock.services.excel.urls')),
|
||||||
# path('app/api/', include('apps.livestock.mobile.api.v1.urls')),
|
# path('app/api/', include('apps.livestock.mobile.api.v1.urls')),
|
||||||
# path('pos/api/', include('apps.livestock.pos.api.v1.urls'))
|
# path('pos/api/', include('apps.livestock.pos.api.v1.urls'))
|
||||||
]
|
]
|
||||||
@@ -161,7 +161,7 @@ def create_value(worksheet, list, l, num, border_style=None, m=None, height=None
|
|||||||
|
|
||||||
cell.font = Font(size=10, bold=True)
|
cell.font = Font(size=10, bold=True)
|
||||||
|
|
||||||
if m is not None and m % 2 != 0:
|
if m is not None and m % 2 == 0:
|
||||||
if m_color:
|
if m_color:
|
||||||
cell.fill = PatternFill(start_color=m_color, fill_type="solid")
|
cell.fill = PatternFill(start_color=m_color, fill_type="solid")
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user