Commit af71c6f7 authored by Georgios Dagkakis's avatar Georgios Dagkakis

erp5_free_subscription: several improvements on interaction the updated Free Subscription

from Free Subscription Request:

- In case of 'subscription', do not create a new Free Subscription
if one already exists
- In case of 'unsubscription' still create a Free Subscription if none exists
and invalidate it at once
- Allow having variation_category that will be passed to the Free Subscription
- Handle definition of effective_date and expiration_date in Free Subscription better, Rule is:
We prioritise effective_date (or expiration_date) if it exists
Then getStopDate result (stop_date or acquiring from start_date) if it is not None
Finally current date
- Have assertions on variation_category and raise in case of unknown type
parent 62731ca6
request = state_change["object"]
portal = request.getPortalObject()
now = DateTime()
request_type = request.getFreeSubscriptionRequestType()
resource = request.getResource()
stop_date = request.getStopDate()
if request.getFreeSubscriptionRequestType() == "unsubscription":
free_subscription = request.getFollowUpValue()
if free_subscription.getValidationState() != "invalidated":
free_subscription.invalidate()
elif request.getFreeSubscriptionRequestType() == "subscription":
from DateTime import DateTime
portal = request.getPortalObject()
# Rule is we prioritise effective_date if it exists,
# then getStopDate result (stop_date or acquiring from start_date) if it is not None
# and finally current date
if request.hasEffectiveDate():
effective_date = request.getEffectiveDate()
else:
effective_date = stop_date or now
free_subscription = request.getFollowUpValue()
if free_subscription is None:
free_subscription = portal.free_subscription_module.newContent(
source=request.getSource(),
destination=request.getDestination(),
resource=request.getResource(),
effective_date=DateTime())
resource=resource,
effective_date=effective_date,
)
request.setFollowUpValue(free_subscription)
variation_category_list = request.getVariationCategoryList()
if request_type == "unsubscription":
assert not variation_category_list, 'unsubscription type of Free Subscription Request with non empty variation_category_list'
validation_state = free_subscription.getValidationState()
if validation_state != "invalidated":
if validation_state == "draft":
# validate, so that we can invalidate in same transaction
free_subscription.validate()
request.setFollowUpValue(free_subscription)
# Rule is we prioritise expiration_date if it exists
# then getStopDate result (stop_date or acquiring from start_date) if it is not None
# and finally current date
if request.hasExpirationDate():
expiration_date = request.getExpirationDate()
else:
expiration_date = stop_date or now
free_subscription.setExpirationDate(expiration_date)
free_subscription.invalidate()
elif request_type == "subscription":
variation_range_category_list = request.getVariationRangeCategoryList()
if not variation_category_list:
assert not request.getVariationRangeCategoryList(), 'subscription type of Free Subscription Request with non empty variation_category_list, while resource has list defined'
for variation_category in variation_category_list:
assert variation_category in variation_range_category_list, 'not allowed variation_category %s' % variation_category
free_subscription.setExpirationDate(None)
if free_subscription.getValidationState() != "validated":
free_subscription.validate()
free_subscription.setEffectiveDate(effective_date)
else:
raise RuntimeError('Unknown request type %s' % request_type)
free_subscription.setVariationCategoryList(variation_category_list)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment