def get_shifts(self, user, journal): def shift_event(shift, is_owned): return { 'title': '{} смена'.format(shift.order), 'start': shift.start_time, 'url': '/{}/{}/{}/'.format(shift.journal.plant.name, shift.journal.name, shift.id), 'color': '#169F85' if is_owned else '#2A3F54' } result = [] owned_shifts = user.employee.shift_set.all() from_date = current_date() - timedelta(days=30) to_date = current_date() if journal.type == 'shift': shifts = Shift.objects.select_related('journal', 'journal__plant').only('order'). \ filter(date__range=[from_date, to_date + timedelta(days=1)], journal=journal).cache() shifts_dict = defaultdict(list) for shift in shifts: shifts_dict[str(shift.date)].append(shift) for shifts in shifts_dict.values(): for shift in shifts: is_owned = shift in owned_shifts result.append(shift_event(shift, is_owned)) return JsonResponse(result, safe=False) else: raise TypeError('Attempt to get shifts for non-shift journal')
def get(self, request): def shift_desc(shift): return { 'id': shift.id, 'start': shift.start_time, } result = [] plantName = request.GET.get("plant", None) journalName = request.GET.get("journal", None) days = int(request.GET.get("days", 30)) print(journalName, plantName) if not journalName or not plantName: raise TypeError('Journal or Plant parameters have not been passed') plant = Plant.objects.get(name=plantName) journal = Journal.objects.get(plant=plant, name=journalName) from_date = current_date() - timedelta(days=days) to_date = current_date() if journal.type == 'shift': shifts = Shift.objects.select_related('journal', 'journal__plant').only('order').\ filter(date__range=[from_date, to_date + timedelta(days=1)], journal=journal).cache() result = [shift_desc(shift) for shift in shifts] return JsonResponse(result, safe=False) else: raise TypeError('Attempt to get shifts for non-shift journal')
def add_groups(journal, shifts_num=None): def date_range(start_date, end_date): for n in range(int((end_date - start_date).days)): yield start_date + timedelta(n) now_date = current_date() if journal.type == 'shift': for shift_date in date_range(now_date - timedelta(days=7), now_date + timedelta(days=7)): for shift_order in range(1, shifts_num + 1): shift, created = Shift.objects.get_or_create( journal=journal, order=shift_order, date=shift_date) elif journal.type == 'year': for year in range(2017, current_date().year + 4): year, created = Year.objects.get_or_create(year_date=year, journal=journal) elif journal.type == 'month': for year in range(2017, current_date().year + 4): for ind, month in enumerate([ 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь' ], 1): month, created = Month.objects.get_or_create( year_date=year, month_date=month, month_order=ind, journal=journal) elif journal.type == 'equipment': for equipment in EquipmentDict.objects.filter(plant=journal.plant): Equipment.objects.get_or_create(name=equipment.name, journal=journal)
def get_months(self, user, journal): def month_event(month): return { 'id': month.id, 'year': f'{month.year_date}-й год', 'month': f'{month.month_date}', 'url': '/{}/{}/{}/'.format(month.journal.plant.name, month.journal.name, month.id), } result = [] if journal.type == 'month': months = Month.objects.select_related('journal', 'journal__plant'). \ filter(journal=journal, year_date__lte=current_date().year).cache() \ .order_by('-year_date') months_dict = defaultdict(list) for month in months: months_dict[str(month.month)].append(month) for months in months_dict.values(): for month in months: result.append(month_event(month)) return JsonResponse(result, safe=False) else: raise TypeError('Attempt to get months for non-month journal')
def get_years(self, user, journal): def year_event(year): return { 'id': year.id, 'title': '{}-й год'.format(year.year_date), 'url': '/{}/{}/{}/'.format(year.journal.plant.name, year.journal.name, year.id), } result = [] if journal.type == 'year': years = Year.objects.select_related('journal', 'journal__plant').only('year_date'). \ filter(journal=journal, year_date__lte=current_date().year). \ cache().order_by('-year_date') years_dict = defaultdict(list) for year in years: years_dict[str(year.year)].append(year) for years in years_dict.values(): for year in years: result.append(year_event(year)) return JsonResponse(result, safe=False) else: raise TypeError('Attempt to get years for non-year journal')
def _get_current_group(journal): if journal.type == 'shift': shifts = Shift.objects.filter(journal=journal, date__lte=current_date()).order_by('-date', 'order') print(timezone.localtime()) for shift in shifts: print(shift.is_active(timezone.localtime())) if shift.is_active(timezone.localtime()): return shift elif journal.type == 'year': year = Year.objects.get(journal=journal, year_date=current_date().year) return year elif journal.type == 'month': month = Month.objects.get(journal=journal, month_order=current_date().month, year_date=current_date().year) return month else: return Equipment.objects.filter(journal=journal).first()
def create_moths_and_years(): git = VersionControl() for journal in Journal.objects.all(): if journal.type == 'year': for year in range(2017, current_date().year + 4): y, created = Year.objects.get_or_create(year_date=year, journal=journal) if created: y.version = git.version_of(journal) y.save() elif journal.type == 'month': for year in range(2017, current_date().year + 4): for ind, month in enumerate(['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'], 1): m, created = Month.objects.get_or_create(year_date=year, month_date=month, month_order=ind, journal=journal) if created: m.version = git.version_of(journal) m.save()
def create_shifts(): git = VersionControl() now_date = current_date() for journal in Journal.objects.all(): if journal.type == 'shift': number_of_shifts = Shift.get_number_of_shifts(journal) for shift_date in date_range(now_date, now_date + timedelta(days=3)): for shift_order in range(1, number_of_shifts + 1): shift, created = Shift.objects.get_or_create(journal=journal, order=shift_order, date=shift_date) if created: shift.version = git.version_of(journal) shift.save()
def get_shift(journal, pid=None) -> 'Shift': shift = None if pid: shift = Shift.objects.get(id=pid) else: number_of_shifts = Shift.get_number_of_shifts(journal) assert number_of_shifts > 0, "<= 0 number of shifts" # create shifts for today and return current shift for shift_order in range(1, number_of_shifts + 1): shift = Shift.objects.get_or_create(journal=journal, order=shift_order, date=current_date())[0] if shift.is_active(timezone.localtime()): break return shift
def check_blank_shifts(): for shift in filter(lambda s: s.is_active(timezone.localtime()), list(Shift.objects.filter( date__range=[current_date() - timedelta(days=1), current_date()]))): if not shift.is_active(time=timezone.localtime() + timedelta(minutes=1)): check_for_no_cells(shift)