from zExceptions import Unauthorized
if REQUEST is not None:
  raise Unauthorized

if context.getSimulationState() not in ["draft", "planned"]:
  # Don't modify it anymore
  return

if subscription_condition_reference is not None:
  # It would be better use some clever API here.
  subscription_condition = context.portal_catalog.getResultValue(
    portal_type="Subscription Condition",
    reference=subscription_condition_reference,
    validation_state="published") # XXX Wechat workaround, validated?
else:
  subscription_condition = context.getSpecialiseValue()

if subscription_condition is None:
  raise ValueError(
    "It was not possible to find the appropriate Condition %s for this Subscription" \
      % subscription_condition_reference)

instance_xml = subscription_condition.SubscriptionCondition_renderParameter(
  amount=int(context.getQuantity()))
# Get Subscription condition for this Subscription Request

email = context.getDestinationSectionValue().getDefaultEmailText()
now = DateTime()

context.edit(
  title="Subscription %s for %s" % (subscription_condition.getTitle(), email),
  url_string=subscription_condition.getUrlString(),
  text_content=instance_xml,
  sla_xml=subscription_condition.getSlaXml(),
  start_date=now,
  root_slave=subscription_condition.getRootSlave(),
  specialise_value=subscription_condition,
  price=subscription_condition.getPrice(),
  price_currency="currency_module/CNY", # For test!!! subscription_condition.getPriceCurrency()
)

context.setSourceReference(subscription_condition.getSourceReference())
