Загрузка в БД

This commit is contained in:
iriska-gri committed 2023-11-21 15:20:22 +03:00
1 parent d875dfc1d0
commit 4460ab44bd
6 files changed
+87 -54

No files matched your search

+75
View File
@@ -7,6 +7,9 @@ from openpyxl.worksheet.formula import ArrayFormula
from pandas.api.types import CategoricalDtype
import urllib.request
from datetime import date
from .models import *
from datetime import datetime
from django.db.models import Count
class roundExcel():
def __init__(self, us):
@@ -168,6 +171,7 @@ class roundExcel():
return (width) / PIXELS_PER_CHAR
def excelvacation(self):
print('helooooooooooooooooooooooooooooooooooooo')
# def excroundread(self):
# file = request.data['dailyfiles']
@@ -218,4 +222,75 @@ class roundExcel():
# mess = "Файл загружен"
class loadExcel():
def __init__(self, us, files):
self.file = files
self.dfuser = pd.DataFrame.from_records(us)
def roundfilse(self):
ed = pd.read_excel(self.file)
df = pd.DataFrame(ed)
datas = df.columns[8].split(" ")[1]
df.columns = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"]
df=df.iloc[6:]
df= df[df.b.notna()]
df= df[df.a.notna()]
df['b']=df['b'].str[-3:]
df=df[['b', 'f', 'h', 'j','l']]
df['b'] = df['b'].astype(int)
types = ['f', 'h', 'j','l']
for n in types:
df[n] = df[n].fillna(0)
df[n] = df[n].astype(int)
df3 = pd.merge(df, self.dfuser, left_on= 'b', right_on="id")
df3=df3[['login', 'f', 'h', 'j','l']]
for ind in df3.index:
keys = ''
if df3['h'][ind] == 1:
keys = 4 # В отпуске
elif df3['j'][ind] == 1:
keys = 5 # Болен
elif df3['l'][ind] == 1:
keys = 2 # Удаленка
elif df3['f'][ind] == 1:
keys = 1 # На работе
else:
keys = 6
base=BasePosition.objects.get(pk=keys)
DailyRoundUp.objects.update_or_create(todaysDate=datetime.strptime(datas, '%d.%m.%Y').strftime('%Y-%m-%d'),
user=df3['login'][ind], defaults={'position':base})
pos = DailyRoundUp.objects.values('position__name', 'position').filter(position=6,todaysDate=date.today()).annotate(Count('user'))
return pos
def vacationsExcel(self):
ed = pd.read_excel(self.file, sheet_name='Запланированные отпуска')
df = pd.DataFrame(ed)
df.columns = ["a", "b", "c", "d", "e", "f", "g"]
df=df[['b', 'c', 'd', 'e','f']]
df3 = pd.merge(df, self.dfuser, left_on= 'b', right_on="fio")
df3=df3[['login', 'c', 'd', 'e','f']]
df3.loc[(df3['e'] == 'Осн'), 'e'] = 'os'
df3.loc[(df3['e'] == 'Доп н/д'), 'e'] = 'nd'
df3.loc[(df3['e'] == 'Доп в/л'), 'e'] = 'vl'
df3['d'] = df3['d'].astype(int)
print(df3)
for ind in df3.index:
# print(df3['login'][ind])
#
GeneralVacation.objects.create(login=df3['login'][ind], planned_date=df3['c'][ind], count_days=df3['d'][ind], type_of_vacation=df3['e'][ind], start_work_period=df3['f'][ind])
# DailyRoundUp.objects.update_or_create(todaysDate=datetime.strptime(datas, '%d.%m.%Y').strftime('%Y-%m-%d'),
# user=df3['login'][ind], defaults={'position':base})