{%- import 'lib.jinja2' as lib with context -%}
{%- if preamble -%}
{{- lib.render_preamble(preamble) -}}
from soapfish import soap, xsd{% if use_wsa %}, wsa{% endif -%}
{# [blank line] #}
{# [blank line] #}
BaseHeader = {% if use_wsa %}wsa.Header{% else %}xsd.ComplexType{% endif %}
{# [blank line] #}
{# [blank line] #}
{%- endif -%}
##############################################################################
# Schemas

{% if schemas %}{{ schemas }}{% endif -%}

{%- for service in definitions.services %}
{%- for port in service.ports %}

{%- set binding = get_by_name(definitions.bindings, port.binding) %}

{%- if binding.binding.transport != SOAPTransport %}{% continue %}{% endif %}

{%- if is_server %}
##############################################################################
# Operations
{# [blank line] #}
{%- for operation in binding.operations -%}
{%- set _im = get_message_object(definitions, binding, operation, 'input') %}
{%- set _om = get_message_object(definitions, binding, operation, 'output') %}
{# [blank line] #}
def {{ operation.name }}(request, {{ _im.part.element|remove_namespace }}):
    # TODO: Put your implementation here.
    return {{ _om.part.element|remove_namespace }}
{# [blank line] #}
{# [blank line] #}
{%- endfor %}
{%- endif %}
##############################################################################
# Methods
{# [blank line] #}
{# [blank line] #}
{%- for operation in binding.operations -%}
{%- set _im = get_message_object(definitions, binding, operation, 'input') %}
{%- set _om = get_message_object(definitions, binding, operation, 'output') %}
{%- set _imh = get_message_header(definitions, binding, operation, 'input') %}
{%- set _omh = get_message_header(definitions, binding, operation, 'output') %}
{%- if _imh %}
class {{ _im.name }}Headers(BaseHeader):
    SCHEMA = Schema_{{ schema_name(schema_select(definitions.types.schemas, _imh|map(attribute='element'))) }}
    {%- for part in _imh -%}
    {%- if part.element %}
    {{ part.name }} = SCHEMA.get_element_by_name('{{ part.element|remove_namespace }}')
    {%- else -%}
    {{ part.name }} = xsd.Element({{ part.type|remove_namespace|capitalize }}())
    {%- endif -%}
    {%- endfor %}
{# [blank line] #}
{%- endif -%}
{%- if _omh %}
class {{ _om.name }}Headers(BaseHeader):
    SCHEMA = Schema_{{ schema_name(schema_select(definitions.types.schemas, _omh|map(attribute='element'))) }}
    {%- for part in _omh -%}
    {%- if part.element %}
    {{ part.name }} = SCHEMA.get_element_by_name('{{ part.element|remove_namespace }}')
    {%- else -%}
    {{ part.name }} = xsd.Element({{ part.type|remove_namespace|capitalize }}())
    {%- endif -%}
    {%- endfor %}
{# [blank line] #}
{%- endif %}
{{ operation.name }}_method = xsd.Method(
    {% if is_server %}function={{ operation.name }},
    {% endif -%}
    soapAction='{{ operation.operation.soapAction }}',
    {%- if _im.parts -%}
    {%- if _im.part.element %}
    input='{{ _im.part.element|remove_namespace }}',
    inputPartName='{{ _im.part.name }}',
    {%- else -%}
    input={{ _im.part.type|remove_namespace|capitalize }},
    {%- endif -%}
    {%- endif -%}
    {%- if _imh %}
    input_header={{ _im.name }}Headers,
    {%- endif -%}
    {%- if _om.parts -%}
    {%- if _om.part.element %}
    output='{{ _om.part.element|remove_namespace }}',
    outputPartName='{{ _om.part.name }}',
    {%- else -%}
    output={{ _om.part.type|remove_namespace|capitalize }},
    {%- endif -%}
    {%- endif -%}
    {%- if _omh %}
    output_header={{ _om.name }}Headers,
    {%- endif %}
    operationName='{{ operation.name }}',
    {%- if operation.operation.style %}
    style='{{ operation.operation.style }}',
    {%- endif %}
)
{# [blank line] #}
{# [blank line] #}
{%- endfor %}
##############################################################################
# SOAP Service
{# [blank line] #}
{# [blank line] #}
{{ port.name }}_SERVICE = soap.Service(
    name='{{ port.name }}',
    targetNamespace='{{ definitions.targetNamespace }}',
    location='{{ port.address.location|url_template }}',
    schemas=[{% for s in definitions.types.schemas %}Schema_{{ schema_name(s) }}{% if not loop.last %}, {% endif %}{% endfor %}],
    {%- if soap_version.NAME == 'soap11' %}
    version=soap.SOAPVersion.SOAP11,
    {%- else %}
    version=soap.SOAPVersion.SOAP12,
    {%- endif %}
    methods=[{% for o in binding.operations %}{{ o.name }}_method{% if not loop.last %}, {% endif %}{% endfor %}],
)
{# [blank line] #}
{# [blank line] #}
{%- if is_server %}
##############################################################################
{# [blank line] #}
{# [blank line] #}
# NOTE: Uncomment the following lines to turn on dispatching for Django:
# from soapfish.django_ import django_dispatcher
# dispatcher = django_dispatcher({{ port.name }}_SERVICE)
{# [blank line] #}
{# [blank line] #}
# NOTE: Put these lines in the urls.py for your project or application:
# urlpatterns += patterns('',
#     (r'{{ service.ports.0.address.location|url_regex }}', '<module>.dispatcher'),
# )
{%- else %}
##############################################################################
# SOAP Service Stub
{# [blank line] #}
{# [blank line] #}
class {{ port.name }}ServiceStub(soap.Stub):
    SERVICE = {{ port.name }}_SERVICE
    SCHEME = '{{ port.address.location|url_component('scheme') }}'
    HOST = '{{ port.address.location|url_component('netloc') }}'
{% for operation in binding.operations %}
{%- set _im = get_message_object(definitions, binding, operation, 'input') %}
    def {{ operation.name }}(self, {{ _im.part.element|remove_namespace }}, header=None):
        return self.call('{{ operation.name}}', {{ _im.part.element|remove_namespace }}, header=header)
{% endfor %}
{%- endif %}
{%- endfor %}{# ports #}
{%- endfor %}{# services #}
{#- vim:set et ft=django nowrap sts=4 sw=4 ts=4: -#}
