144 lines
5.5 KiB
Python
144 lines
5.5 KiB
Python
import os
|
|
from openpyxl import load_workbook
|
|
import openpyxl
|
|
from rest_framework.response import Response
|
|
from datetime import date
|
|
from .serializers import *
|
|
from .models import *
|
|
import pandas as pd
|
|
from rest_framework.views import APIView
|
|
from userauth.models import *
|
|
from django.shortcuts import render
|
|
from pandas.io.excel import ExcelWriter
|
|
import openpyxl as oxl
|
|
from openpyxl import Workbook
|
|
from openpyxl.styles import Font, Color
|
|
from openpyxl.styles import Alignment
|
|
|
|
|
|
class GeneralVacationAPIView(APIView):
|
|
def get(self, request):
|
|
general = GeneralVacation.objects.all()
|
|
plan = PlanCalendar.objects.all()
|
|
|
|
# Сериализуем извлечённый набор записей
|
|
serializer_for_basecrit = GeneralVacationSerializer(
|
|
instance=general, # Передаём набор записей
|
|
many=True # Указываем, что на вход подаётся именно набор записей
|
|
)
|
|
|
|
serializer_for_plan = PlanCalendarSerializer(
|
|
instance=plan, # Передаём набор записей
|
|
many=True # Указываем, что на вход подаётся именно набор записей
|
|
)
|
|
return Response({"Отпуск": serializer_for_basecrit.data, "План-график": serializer_for_plan.data})
|
|
|
|
class DailyRoundUpAPIView(APIView):
|
|
|
|
def convert_width(self, width):
|
|
"""
|
|
Конвертируем ширину столбца Excel в ширину столбца openpyxl
|
|
"""
|
|
PIXELS_PER_CHAR = 7
|
|
return (width) / PIXELS_PER_CHAR
|
|
|
|
def get(self, request):
|
|
user = DictEmployee.objects.using('user_directory').filter(deleted=0).values('login', 'department', 'familyname', 'name', 'parentname')
|
|
df = pd.DataFrame.from_records(user)
|
|
df['tabel'] = df['login'].str.split('-', expand = True)[1]+df['login'].str.split('-', expand = True)[2]
|
|
file_path = 'media/DailyRoundUp.xlsx'
|
|
# l = pd.DataFrame(df[(df.department=='Руководство')][['tabel', 'familyname', 'name', 'parentname']])
|
|
# l.to_excel(file_path, sheet_name="Список", index= False, header= False, startcol=2, startrow=8)
|
|
wb = Workbook()
|
|
ws = wb.active
|
|
ws.title = "Список"
|
|
ws['B2'] = 'Список сотрудников'
|
|
ws['B3'] = 'МИ ФНС России по ЦОД № 2'
|
|
ws['B4'] = 'по подразделениям'
|
|
|
|
|
|
ws['I1'] = 'На '+ date.today().strftime("%d.%m.%Y")
|
|
|
|
|
|
|
|
column_widths = [29, 53, 96, 96, 112, 28, 222, 29, 148, 28, 145, 33]
|
|
for i, width in enumerate(column_widths):
|
|
column_letter = openpyxl.utils.get_column_letter(i+1)
|
|
ws.column_dimensions[column_letter].width = self.convert_width(width)
|
|
|
|
for col in ws.iter_cols(min_row=2, max_row=5, min_col=2, max_col=5):
|
|
for cell in col:
|
|
cell.font = Font(name='Times New Roman', size=12, bold=True)
|
|
cell.alignment = Alignment(horizontal='center')
|
|
ws['I1'].font = Font(name='Times New Roman', size=14, bold=True)
|
|
ws['I1'].alignment = Alignment(horizontal='left')
|
|
|
|
|
|
for x in range(3):
|
|
ws.merge_cells(start_row=2+x, start_column=2, end_row=2+x, end_column=6)
|
|
#
|
|
kl = ['Руководство']
|
|
|
|
k=7
|
|
for i in df['department'].unique():
|
|
ws.cell(row = k, column = 2).value = i
|
|
print(k, '-------------------1')
|
|
l = pd.DataFrame(df[(df.department==i)][['tabel', 'familyname', 'name', 'parentname']])
|
|
# l.to_excel(file_path, sheet_name="Список", index= False, header= False, startcol=2, startrow=8)
|
|
|
|
|
|
for x in range(len(l)):
|
|
k=k+1
|
|
print(k, '-------------------1')
|
|
ws.cell(row = k, column = 2).value = l.iloc[x]['tabel']
|
|
ws.cell(row = k, column = 3).value = l.iloc[x]['familyname']
|
|
ws.cell(row = k, column = 4).value = l.iloc[x]['name']
|
|
ws.cell(row = k, column = 5).value = l.iloc[x]['parentname']
|
|
|
|
print(l.iloc[x]['name'])
|
|
k=k+1
|
|
print(k, '-------------------1')
|
|
|
|
|
|
wb.save(file_path)
|
|
|
|
# file_path = 'media/DailyRoundUp.xlsx'
|
|
|
|
|
|
return Response('')
|
|
|
|
# with ExcelWriter(file_path, mode="a" if os.path.exists(file_path) else "w" , if_sheet_exists='replace') as writer:
|
|
# #
|
|
# for i in df['department'].unique():
|
|
# for i in df['department'].unique():
|
|
# k = pd.DataFrame(df[(df.department==i)][['tabel', 'familyname', 'name', 'parentname']])
|
|
# # k.to_excel(writer,header=None,index=None)
|
|
# print(i)
|
|
# print(k)
|
|
# return Response('')
|
|
#
|
|
#
|
|
# df.to_excel(table)
|
|
|
|
|
|
# def all_books(request):
|
|
# file_path = 'media/DailyRoundUp.xlsx'
|
|
# workbook = load_workbook(filename=file_path)
|
|
# worksheet = workbook.active
|
|
|
|
# books = []
|
|
# skip_first_row = True
|
|
# for row in worksheet.iter_rows(values_only=True, min_row=2 if skip_first_row else 1):
|
|
# book = {
|
|
# 'book_id': int(row[0]),
|
|
# 'title': row[1],
|
|
# 'author': row[2],
|
|
# 'genre': row[3],
|
|
# 'year': int(row[4]),
|
|
# 'description': row[5],
|
|
# 'cover': row[6]
|
|
# }
|
|
# books.append(book)
|
|
|
|
# return render(request, 'books.html', {'books': books})
|