Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reserva repetição #97

Merged
merged 6 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions proj_SIGApoio/app/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from .models import TipoRecurso, TipoUsuario, Recurso, Usuario, Emprestimo, Horario, Local, ReservaSemanal, ReservaMensal, ReservaDiaUnico, TipoLocal, Chamado
from .models import TipoRecurso, TipoUsuario, Recurso, Usuario, Emprestimo, Horario, Local, ReservaSemanal, ReservaDiaUnico, TipoLocal, Chamado

admin.site.register(Usuario)
admin.site.register(TipoRecurso)
Expand All @@ -9,7 +9,6 @@
admin.site.register(Horario)
admin.site.register(Local)
admin.site.register(ReservaSemanal)
admin.site.register(ReservaMensal)
admin.site.register(ReservaDiaUnico)
admin.site.register(TipoLocal)
admin.site.register(Chamado)
Expand Down
76 changes: 74 additions & 2 deletions proj_SIGApoio/app/bo/horarios.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,82 @@

from app.models import Horario
from django.db.models import Q
from datetime import datetime, time

def converter_horarios(dias, horarios):
'''Converte 2 vetores de strings, um de dias e outro de horarios, em apenas horarios com seus respectivo dias'''
'''Converte 2 vetores de strings, um de dias e outro de horarios, em apenas horarios com seus respectivos dias'''
horarios_final = []
for dia in dias:
for horario in horarios:
horarios_final.append(dia + horario)
return horarios_final

def converter_horarios_dia(dataInicio, dataFinal):
'''Recebe duas datas e retorna os horarios entre elas.'''
diaHoraInicio = datetime.strptime(dataInicio, '%Y-%m-%dT%H:%M')
diaHoraFinal = datetime.strptime(dataFinal, '%Y-%m-%dT%H:%M')

diaSemanaInicio = diaHoraInicio.weekday() ## 0 - Segunda, 6 - domingo
diaSemanaFinal = diaHoraFinal.weekday()

if diaSemanaInicio == 6:
diaSemanaInicio = 0 ## domingo = 0
else:
diaSemanaInicio = diaSemanaInicio + 1 ## segunda = 1 e assim em diante

if diaSemanaFinal == 6:
diaSemanaFinal = 0
else:
diaSemanaFinal = diaSemanaFinal + 1

if diaSemanaInicio in [6, 0] and diaSemanaFinal in [6, 0]: ## se começar e terminar no fim de semana, retorna None
return None

horarioInicio = getHorarioAt(diaSemanaInicio, f'{diaHoraInicio.hour:02d}:{diaHoraInicio.minute:02d}', True)
horarioFinal = getHorarioAt(diaSemanaFinal, f'{diaHoraFinal.hour:02d}:{diaHoraFinal.minute:02d}', False)

horarios_final = getHorariosBetween(horarioInicio, horarioFinal)
return horarios_final

def getHorarioAt(diaSemana, hora, inicio):
if diaSemana in [6, 0] and inicio: ## se começar no final de semana, pula pra segunda
diaSemana = 1
elif diaSemana in [6, 0] and not inicio: ## se terminar no final de semana, antecipa pra sexta
diaSemana = 5

hora_obj = time.fromisoformat(hora)
horarios = Horario.objects.filter(
dia=diaSemana,
horaInicio__lte=hora_obj,
horaFim__gte=hora_obj
)
horario = horarios.first()
if horario != None:
return horario
else:
horario = Horario.objects.filter(
dia=diaSemana,
horaInicio__gt=hora_obj
).order_by('horaInicio').first()
return horario

def getHorariosBetween(horarioInicio, horarioFinal):
dia_inicio = horarioInicio.dia
dia_fim = horarioFinal.dia
hora_inicio = horarioInicio.horaInicio
hora_fim = horarioFinal.horaFim

if dia_inicio == dia_fim:
horarios = Horario.objects.filter(
dia=dia_inicio,
horaInicio__lte=hora_fim,
horaFim__gte=hora_inicio
)
else:
# Condição para horários entre dias diferentes
horarios = Horario.objects.filter(
(Q(dia=dia_inicio) & Q(horaInicio__gte=hora_inicio)) | # dia = dia inicio e horaInicio >= hora_inicio
(Q(dia__gt=dia_inicio) & Q(dia__lt=dia_fim)) | # dia > dia_inicio e dia < dia_fim
(Q(dia=dia_fim) & Q(horaFim__lte=hora_fim)) # dia = dia_fim e horaFim <= hora_fim
)

return horarios
67 changes: 9 additions & 58 deletions proj_SIGApoio/app/forms.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
from django import forms
from django.db import connection
from .models import Recurso, TipoRecurso, Local, TipoLocal, Usuario, ReservaSemanal, ReservaDiaUnico, ReservaMensal, Chamado
from .models import Recurso, TipoRecurso, Local, TipoLocal, Usuario, ReservaSemanal, ReservaDiaUnico, Chamado


BLOCOS_CHOICES = [('A', 'Bloco A'), ('B', 'Bloco B'), ('C', 'Bloco C'), ('D', 'Bloco D'), ('Aud', 'Auditórios'), ('Lab', 'Laboratórios')]

def get_usuario_choices():
try:
if 'app_usuario' in connection.introspection.table_names():
print('achou usuário')
return [(usuario.matricula, usuario.nome) for usuario in Usuario.objects.all()]
else:
return []
print('nao achou usuário')

except Exception as e:
print('deu erro')
return []



class TipoRecursoForm(forms.Form, forms.ModelForm):
tipo = forms.CharField(
label = 'Tipo',
Expand Down Expand Up @@ -201,63 +204,12 @@ class ReservaDiaForm(forms.ModelForm, forms.Form):
)
)

qtd_pessoas = forms.IntegerField(
max_value=250,
initial=0,
widget=forms.NumberInput(
attrs={
'class': 'form-control gray-back blue-text me-4'
},
)
)

bloco = forms.ChoiceField(
choices=BLOCOS_CHOICES,
repeticao = forms.ChoiceField(
choices=[('unico', 'Única'), ('semana', 'Semanal'), ('mes', 'Mensal')],
widget=forms.Select(
attrs={
'class': 'form-select gray-back blue-text me-4'
}
)
)

matSolicitante = forms.ChoiceField(
label='Solicitante',
choices=get_usuario_choices(),
widget=forms.Select(
attrs={'class': 'form-select blue-text gray-back me-4'}
)
)

class Meta:
model = ReservaDiaUnico
fields = ['descricao', 'diaHoraInicio', 'diaHoraFim', 'local', 'matSolicitante']

class ReservaMensalForm(forms.ModelForm, forms.Form):
descricao = forms.CharField(
label='Descrição',
widget=forms.TextInput(
attrs={'class': 'form-control blue-text gray-back me-4'}
)
)

local = forms.ChoiceField(
label='Local',
widget=forms.Select(
attrs={'class': 'form-select blue-text gray-back me-4'}
)
)

dias = forms.ChoiceField(
label="Dias",
widget=forms.DateInput(
attrs={'class': 'form-control blue-text gray-back me-4', 'type': 'date'}
)
)

repeticoes = forms.IntegerField(
label="Repetições (meses)",
widget=forms.NumberInput(
attrs={'class': 'form-control blue-text gray-back me-4'}
},
)
)

Expand Down Expand Up @@ -291,4 +243,3 @@ class ReservaMensalForm(forms.ModelForm, forms.Form):
class Meta:
model = ReservaDiaUnico
fields = ['descricao', 'diaHoraInicio', 'diaHoraFim', 'local', 'matSolicitante']

2 changes: 1 addition & 1 deletion proj_SIGApoio/app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.7 on 2024-07-23 18:14
# Generated by Django 5.0.6 on 2024-07-30 00:48

import django.db.models.deletion
from django.db import migrations, models
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.0.6 on 2024-07-30 13:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('app', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='reservamensal',
name='horaFim',
field=models.TimeField(default='22:00'),
),
migrations.AddField(
model_name='reservamensal',
name='horaInicio',
field=models.TimeField(default='07:00'),
),
migrations.AddField(
model_name='reservamensal',
name='mesInicial',
field=models.IntegerField(choices=[(1, 'Janeiro'), (2, 'Fevereiro'), (3, 'Março'), (4, 'Abril'), (5, 'Maio'), (6, 'Junho'), (7, 'Julho'), (8, 'Agosto'), (9, 'Setembro'), (10, 'Outubro'), (11, 'Novembro'), (12, 'Dezembro')], default=7),
),
]

This file was deleted.

This file was deleted.

18 changes: 1 addition & 17 deletions proj_SIGApoio/app/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models

from datetime import date
class TipoUsuario(models.Model):
tipo = models.CharField(max_length=50, unique=True, primary_key=True)

Expand Down Expand Up @@ -83,22 +83,6 @@ class ReservaDiaUnico(models.Model):
matSolicitante = models.ForeignKey(Usuario, related_name='%(class)s_usuario', on_delete=models.DO_NOTHING, default='')
diaHoraInicio = models.DateTimeField()
diaHoraFim = models.DateTimeField()

class ReservaMensal(models.Model):
descricao = models.CharField(max_length=100, null=True)
local = models.ForeignKey(Local, on_delete=models.DO_NOTHING)
matResponsavel = models.ForeignKey(Usuario, on_delete=models.DO_NOTHING)
matSolicitante = models.ForeignKey(Usuario, related_name='%(class)s_usuario', on_delete=models.DO_NOTHING, default='')
dias = models.JSONField()
meses = models.IntegerField()

def add_dia(self, value):
if isinstance(value, int):
self.dias.append(value)
self.save()

def __str__(self):
return self.local.nome

class Chamado(models.Model):
id_chamado = models.IntegerField(primary_key=True)
Expand Down
Loading