Skip to content

Commit

Permalink
Merge pull request #1865 from FJNR-inc/develop
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
RignonNoel authored Mar 29, 2023
2 parents a1d8233 + e1d9522 commit 1929dce
Show file tree
Hide file tree
Showing 12 changed files with 1,095 additions and 78 deletions.
50 changes: 50 additions & 0 deletions blitz_api/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
OrderLineBaseProduct,
Coupon,
)
from workplace.models import (
TimeSlot,
Reservation as TimeSlotReservation,
Workplace,
Period,
)
from faker import Faker

User = get_user_model()
Expand Down Expand Up @@ -182,3 +188,47 @@ class Meta:
end_time = "2020-01-06T15:11:06-05:00"
max_use = 100
max_use_per_user = 2


class WorkplaceFactory(DjangoModelFactory):
class Meta:
model = Workplace

name = factory.Sequence(lambda n: f'Blitz {n}')
seats = factory.fuzzy.FuzzyInteger(0)
details = "short_description"
address_line1 = "123 random street"
postal_code = "123 456"
state_province = "Random state"
country = "Random country"


class PeriodFactory(DjangoModelFactory):
class Meta:
model = Period

name = factory.Sequence(lambda n: f'Period {n}')
workplace = factory.SubFactory(WorkplaceFactory)
start_date = datetime(2130, 1, 1, 1)
end_date = datetime(2130, 12, 12, 12)
price = factory.fuzzy.FuzzyDecimal(0, 1000, 2)
is_active = True


class TimeSlotFactory(DjangoModelFactory):
class Meta:
model = TimeSlot

period = factory.SubFactory(PeriodFactory)
price = factory.fuzzy.FuzzyDecimal(0, 1000, 2)
start_time = datetime(2130, 1, 15, 8)
end_time = datetime(2130, 1, 15, 12)


class TimeSlotReservationFactory(DjangoModelFactory):
class Meta:
model = TimeSlotReservation

user = factory.SubFactory(UserFactory)
timeslot = factory.SubFactory(TimeSlotFactory)
is_active = True
96 changes: 35 additions & 61 deletions blitz_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,98 +202,72 @@ def send_new_activation_email(self):
)

def get_number_of_past_tomatoes(self):
timeslots = self.get_nb_tomatoes_timeslot()
virtual_retreats = self.get_nb_tomatoes_virtual_retreat()
physical_retreats = self.get_nb_tomatoes_physical_retreat()

past_count = timeslots['past'] + \
virtual_retreats['past'] + \
physical_retreats['past']

custom_tomatoes = Tomato.objects.filter(
today = timezone.now()
nb_tomatoes = Tomato.objects.filter(
user=self,
).aggregate(
Sum('number_of_tomato'),
)

if custom_tomatoes['number_of_tomato__sum'] is not None:
past_count += custom_tomatoes['number_of_tomato__sum']
acquisition_date__lte=today
).aggregate(Sum('number_of_tomato'))['number_of_tomato__sum']
nb_tomatoes = nb_tomatoes if nb_tomatoes else 0

return past_count
return nb_tomatoes

def get_number_of_future_tomatoes(self):
timeslots = self.get_nb_tomatoes_timeslot()
virtual_retreats = self.get_nb_tomatoes_virtual_retreat()
physical_retreats = self.get_nb_tomatoes_physical_retreat()
retreats = self.get_nb_tomatoes_retreat()

future_count = timeslots['future'] + \
virtual_retreats['future'] + \
physical_retreats['future']
future_count = timeslots['future'] + retreats['future']

return future_count

def get_nb_tomatoes_timeslot(self):
from workplace.models import Reservation as TimeslotReservation
today = timezone.now()
nb_tomatoes = Tomato.objects.filter(
user=self,
source=Tomato.TOMATO_SOURCE_TIMESLOT,
acquisition_date__lte=today
).aggregate(Sum('number_of_tomato'))['number_of_tomato__sum']
past_count = nb_tomatoes if nb_tomatoes else 0

reservations = TimeslotReservation.objects.filter(
user=self,
is_active=True,
)

past_count = 0
future_count = 0

for reservation in reservations:
if reservation.timeslot.end_time < timezone.now():
past_count += settings.NUMBER_OF_TOMATOES_TIMESLOT
else:
future_count += settings.NUMBER_OF_TOMATOES_TIMESLOT
if reservation.timeslot.end_time >= timezone.now():
future_count += reservation.timeslot.number_of_tomatoes

return {
'past': past_count,
'future': future_count,
}

def get_nb_tomatoes_virtual_retreat(self):
from retirement.models import Reservation as RetreatReservation

reservations = RetreatReservation.objects.filter(
user=self,
is_active=True,
retreat__type__is_virtual=True,
def get_nb_tomatoes_retreat(self):
from retirement.models import (
Reservation as RetreatReservation,
RetreatDate,
)

past_count = 0
future_count = 0

for reservation in reservations:
if reservation.retreat.end_time < timezone.now():
past_count += reservation.retreat.get_number_of_tomatoes()
else:
future_count += reservation.retreat.get_number_of_tomatoes()

return {
'past': past_count,
'future': future_count,
}

def get_nb_tomatoes_physical_retreat(self):
from retirement.models import Reservation as RetreatReservation
today = timezone.now()
nb_tomatoes = Tomato.objects.filter(
user=self,
source=Tomato.TOMATO_SOURCE_RETREAT,
acquisition_date__lte=today
).aggregate(Sum('number_of_tomato'))['number_of_tomato__sum']
past_count = nb_tomatoes if nb_tomatoes else 0

reservations = RetreatReservation.objects.filter(
user=self,
is_active=True,
retreat__type__is_virtual=False,
)

past_count = 0
future_dates = RetreatDate.objects.filter(
end_time__gte=today,
tomatoes_assigned=False,
retreat__reservations__in=reservations,
)
future_count = 0

for reservation in reservations:
if reservation.retreat.end_time < timezone.now():
past_count += reservation.retreat.get_number_of_tomatoes()
else:
future_count += reservation.retreat.get_number_of_tomatoes()
for date in future_dates:
future_count += date.number_of_tomatoes

return {
'past': past_count,
Expand Down
Loading

0 comments on commit 1929dce

Please sign in to comment.