portal = context.getPortalObject()

resource = portal.restrictedTraverse("service_module/slapos_virtual_master_subscription")
customer = context

# create the project
project = portal.project_module.newContent(
  portal_type='Project',
  destination_value=customer,
  title=title
)
project.validate()

currency_value = portal.restrictedTraverse(price_currency)

# create the subscription request, which will lead to the Open Order
try:
  subscription_request = resource.Resource_createSubscriptionRequest(customer, [], project, currency_value=currency_value)
except AssertionError, e:
  if batch:
    raise
  return context.Base_redirect(
    keep_items={
      'portal_status_level': 'error',
      'portal_status_message': str(e)
    },
    abort_transaction=True,
  )


# XXX How to specify the trade condition containing the currency and trade model lines?
specialise_value = subscription_request.getSpecialiseValue(portal_type="Sale Trade Condition")
if ((specialise_value is not None) and
    (specialise_value.getPriceCurrency('') == '') and
    (len(specialise_value.contentValues(portal_type="Trade Model Line")) == 0)):
  specialise_value = specialise_value.getSpecialiseValue(portal_type="Sale Trade Condition")

if specialise_value is None:
  raise ValueError('Could not find a Trade Condition with Trade Model Line')

# and create default assignments for the user
# who can manage compute nodes and create instances
customer.newContent(
  portal_type='Assignment',
  destination_project_value=project,
  function='production/manager'
).open()
customer.newContent(
  portal_type='Assignment',
  destination_project_value=project,
  function='customer'
).open()

# Compute Node trade condition
if is_compute_node_payable:
  source_section_value = subscription_request.getSourceSectionValue(None)
  if source_section_value is None:
    raise AssertionError('No source section found to generate the invoices')
  title = 'Payable Compute Node for: %s' % project.getTitle()
else:
  source_section_value = None
  title = 'Free Compute Node for: %s' % project.getTitle()
sale_trade_condition = portal.sale_trade_condition_module.newContent(
  portal_type="Sale Trade Condition",
  reference=title,
  trade_condition_type="compute_node",
  specialise_value=specialise_value,
  destination_project_value=project,
  source_value=subscription_request.getSourceValue(),
  source_section_value=source_section_value,
  #source_payment_value=seller_bank_account,
  price_currency_value=currency_value
)
sale_trade_condition.validate()

# Instance Tree trade condition
if is_instance_tree_payable:
  source_section_value = subscription_request.getSourceSectionValue(None)
  if source_section_value is None:
    raise AssertionError('No source section found to generate the invoices')
  title = 'Payable Instance Tree for: %s' % project.getTitle()
else:
  source_section_value = None
  title = 'Free Instance Tree for: %s' % project.getTitle()
sale_trade_condition = portal.sale_trade_condition_module.newContent(
  portal_type="Sale Trade Condition",
  reference=title,
  trade_condition_type="instance_tree",
  specialise_value=specialise_value,
  source_project_value=project,
  source_value=subscription_request.getSourceValue(),
  source_section_value=source_section_value,
  #source_payment_value=seller_bank_account,
  price_currency_value=currency_value
)
sale_trade_condition.validate()

if batch:
  return project
return project.Base_redirect()
