-
Notifications
You must be signed in to change notification settings - Fork 2
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
✨(backend) add start
and end
date on OrderGroup
model
#1032
Open
jonathanreveille
wants to merge
6
commits into
main
Choose a base branch
from
feat/add_start_and_end_date_to_ordergroup_model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aa17b56
✨(backend) add start and end date on order groups model
jonathanreveille 2671e51
fixup! ✨(backend) add start and end date on order groups model
jonathanreveille 3dfe704
fixup! ✨(backend) add start and end date on order groups model
jonathanreveille 0b824b8
fixup! ✨(backend) add start and end date on order groups model
jonathanreveille 514d25b
fixup! ✨(backend) add start and end date on order groups model
jonathanreveille 371e71a
fixup! ✨(backend) add start and end date on order groups model
jonathanreveille File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/backend/joanie/core/migrations/0055_alter_ordergroup_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Generated by Django 4.2.18 on 2025-02-06 10:16 | ||
|
||
import django.core.validators | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0054_discount_discount_discount_rate_or_amount_required_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='ordergroup', | ||
name='end', | ||
field=models.DateTimeField(blank=True, help_text='order group’s end date and time', null=True, verbose_name='order group end datetime'), | ||
), | ||
migrations.AddField( | ||
model_name='ordergroup', | ||
name='start', | ||
field=models.DateTimeField(blank=True, help_text='order group’s start date and time', null=True, verbose_name='order group start datetime'), | ||
), | ||
migrations.AlterField( | ||
model_name='ordergroup', | ||
name='nb_seats', | ||
field=models.PositiveSmallIntegerField(blank=True, default=None, help_text='The maximum number of orders that can be validated for a given order group', null=True, verbose_name='Number of seats'), | ||
), | ||
migrations.AddConstraint( | ||
model_name='ordergroup', | ||
constraint=models.CheckConstraint(check=models.Q(('start__lte', models.F('end'))), name='check_start_before_end', violation_error_message='Start date cannot be greater than end date'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -366,11 +366,13 @@ class OrderGroup(BaseModel): | |||||
"""Order group to enforce a maximum number of seats for a product.""" | ||||||
|
||||||
nb_seats = models.PositiveSmallIntegerField( | ||||||
default=0, | ||||||
default=None, | ||||||
verbose_name=_("Number of seats"), | ||||||
help_text=_( | ||||||
"The maximum number of orders that can be validated for a given order group" | ||||||
), | ||||||
null=True, | ||||||
blank=True, | ||||||
) | ||||||
course_product_relation = models.ForeignKey( | ||||||
to=CourseProductRelation, | ||||||
|
@@ -379,6 +381,28 @@ class OrderGroup(BaseModel): | |||||
on_delete=models.CASCADE, | ||||||
) | ||||||
is_active = models.BooleanField(_("is active"), default=True) | ||||||
# Available start to end period of activation of the OrderGroup | ||||||
start = models.DateTimeField( | ||||||
help_text=_("order group’s start date and time"), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe something like this ?
Suggested change
|
||||||
verbose_name=_("order group start datetime"), | ||||||
blank=True, | ||||||
null=True, | ||||||
) | ||||||
end = models.DateTimeField( | ||||||
help_text=_("order group’s end date and time"), | ||||||
verbose_name=_("order group end datetime"), | ||||||
blank=True, | ||||||
null=True, | ||||||
) | ||||||
|
||||||
class Meta: | ||||||
constraints = [ | ||||||
models.CheckConstraint( | ||||||
check=models.Q(start__lte=models.F("end")), | ||||||
name="check_start_before_end", | ||||||
violation_error_message=_("Start date cannot be greater than end date"), | ||||||
), | ||||||
] | ||||||
|
||||||
def get_nb_binding_orders(self): | ||||||
"""Query the number of binding orders related to this order group.""" | ||||||
|
@@ -397,6 +421,58 @@ def can_edit(self): | |||||
"""Return True if the order group can be edited.""" | ||||||
return not self.orders.exists() | ||||||
|
||||||
@property | ||||||
def available_seats(self) -> int | None: | ||||||
"""Return the number of available seats on the order group.""" | ||||||
if self.nb_seats is None: | ||||||
return None | ||||||
return self.nb_seats - self.get_nb_binding_orders() | ||||||
|
||||||
# ruff: noqa: PLR0911 | ||||||
# pylint: disable=too-many-return-statements | ||||||
@property | ||||||
def is_enabled(self): | ||||||
""" | ||||||
Returns boolean whether the order group is active based on its status, availability, | ||||||
and time constraints. | ||||||
When a start or end date is setted, we need to verify if we are in the period | ||||||
of early bird, or last minute depending on the course run enrollment start or | ||||||
enrollment end datetimes. When both dates are setted, we need to make sure that the | ||||||
current day is between those. | ||||||
""" | ||||||
if not self.is_active: | ||||||
return False | ||||||
# Unlimited spots | ||||||
if self.nb_seats is None: | ||||||
return True | ||||||
if self.nb_seats - self.get_nb_binding_orders() == 0: | ||||||
return False | ||||||
if not self.start and not self.end: | ||||||
return True | ||||||
|
||||||
now = timezone.now() | ||||||
if self.start and self.end: | ||||||
return self.start <= now <= self.end | ||||||
|
||||||
if self.course_product_relation.product.type == enums.PRODUCT_TYPE_CERTIFICATE: # pylint: disable=no-member | ||||||
course_run_dates = ( | ||||||
self.course_product_relation.course.get_equivalent_course_run_dates( # pylint: disable=no-member | ||||||
ignore_archived=True | ||||||
) | ||||||
) | ||||||
else: | ||||||
course_run_dates = ( | ||||||
self.course_product_relation.product.get_equivalent_course_run_dates( # pylint: disable=no-member | ||||||
ignore_archived=True | ||||||
) | ||||||
) | ||||||
if self.end: # Early birds | ||||||
return course_run_dates["enrollment_start"] <= now <= self.end | ||||||
if self.start: # Last minutes | ||||||
return self.start <= now <= course_run_dates["enrollment_end"] | ||||||
|
||||||
return False | ||||||
|
||||||
|
||||||
class OrderManager(models.Manager): | ||||||
"""Custom manager for the Order model.""" | ||||||
|
@@ -806,10 +882,23 @@ def clean(self): | |||||
) | ||||||
else: | ||||||
nb_seats = self.order_group.nb_seats | ||||||
if 0 < nb_seats <= self.order_group.get_nb_binding_orders(): | ||||||
error_dict["order_group"].append( | ||||||
f"Maximum number of orders reached for product {product_title:s}" | ||||||
) | ||||||
# When nb_seats is None, the order group has unlimited available seats | ||||||
if nb_seats is not None: | ||||||
if 0 < nb_seats <= self.order_group.get_nb_binding_orders(): | ||||||
error_dict["order_group"].append( | ||||||
f"Maximum number of orders reached for product {product_title:s}" | ||||||
) | ||||||
# For order group that does not have a fix nb of seats and is not active | ||||||
# We should block the order creation for that group | ||||||
if ( | ||||||
course_product_relation | ||||||
and self.order_group | ||||||
and not self.order_group.is_active | ||||||
and self.order_group.nb_seats is None | ||||||
): | ||||||
error_dict["order_group"].append( | ||||||
f"This order group does not accept anymore orders for {product_title:s}" | ||||||
) | ||||||
|
||||||
if error_dict: | ||||||
raise ValidationError(error_dict) | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.