"""
Views for generating and serving policy files.

"""

from django.http import HttpResponse

from flashpolicies import policies


def serve(request, policy):
    """
    Given a ``flashpolicies.policies.Policy``, serialize it to XML and
    serve it.

    **Required arguments:**

    ``policy``
        A ``flashpolicies.policies.Policy`` instance.

    **Optional arguments:**

    None.

    """
    return HttpResponse(policy.xml_dom.toprettyxml(encoding='utf-8'),
                        content_type='text/x-cross-domain-policy; charset=utf-8')


def simple(request, domains):
    """
    A simple Flash cross-domain access policy.

    Note that if this is returned from the URL ``/crossdomain.xml`` on
    a domain, it will act as a master policy and will not permit other
    policies to exist on that domain. If you need to set meta-policy
    information and allow other policies, use the view
    :view:`flashpolicies.views.metapolicy` for the master policy instead.

    **Required arguments:**

    ``domains``
        A list of domains from which to allow access. Each value may
        be either a domain name (e.g., ``example.com``) or a wildcard
        (e.g., ``*.example.com``). Due to serious potential security
        issues, it is strongly recommended that you not use wildcard
        domain values.

    **Optional arguments:**

    None.

    """
    return serve(request, policies.Policy(*domains))


def no_access(request):
    """
    A Flash cross-domain access policy which permits no access of any
    kind, via a meta-policy declaration disallowing all policy files.

    Note that this view, if used, must become the master policy for
    the domain, and so must be served from the URL
    ``/crossdomain.xml`` on the domain -- setting meta-policy
    information in other policy files is forbidden by the
    specification.

    **Required arguments:**

    None.

    **Optional arguments:**

    None.

    """
    policy = policies.Policy()
    policy.metapolicy(policies.SITE_CONTROL_NONE)
    return serve(request, policy)


def metapolicy(request, site_control, domains=None):
    """
    A Flash cross-domain policy which allows other policies to exist
    on the same domain.

    Note that this view, if used, must become the master policy for
    the domain, and so must be served from the URL
    ``/crossdomain.xml`` on the domain -- setting meta-policy
    information in other policy files is forbidden by the
    specification.

    **Required arguments:**

    ``site_control``
        A string indicating the extent to which other policies are
        permitted. Acceptable values are defined as constants in
        ``flashpolicies.policies``.

    **Optional arguments:**

    ``domains``
        A list of domains from which to allow access. Each value may
        be either a domain name (e.g., ``example.com``) or a wildcard
        (e.g., ``*.example.com``). Due to serious potential security
        issues, it is strongly recommended that you not use wildcard
        domain values.

    """
    if domains is None:
        domains = []
    policy = policies.Policy(*domains)
    policy.metapolicy(site_control)
    return serve(request, policy)

@@ -206,16 +206,25 @@
 Policy%60%60
+ instance
 , serial
@@ -229,16 +229,20 @@
 alize it
+%0A   
  to XML 
@@ -244,20 +244,16 @@
  XML and
-%0A   
  serve i
@@ -253,16 +253,119 @@
 serve it
+. Internall, this is used by all other views as%0A    the mechanism which actually serves the policy file
 .%0A%0A    *
@@ -411,17 +411,19 @@
         
-A
+The
  %60%60flash
@@ -457,16 +457,25 @@
 instance
+ to serve
 .%0A%0A    *
@@ -1665,25 +1665,24 @@
 , via a meta
--
 policy decla
@@ -1752,36 +1752,32 @@
 if used, must be
-come
  the master poli
@@ -1778,32 +1778,32 @@
 r policy for
-%0A   
  the
+%0A   
  domain, and
@@ -1825,36 +1825,32 @@
 ved from the URL
-%0A   
  %60%60/crossdomain.
@@ -1849,32 +1849,36 @@
 sdomain.xml%60%60 on
+%0A   
  the domain -- s
@@ -1864,35 +1864,33 @@
 n%0A    the domain
- --
+:
  setting meta-po
@@ -1882,35 +1882,30 @@
 setting meta
--
 policy
-%0A   
  information
@@ -1918,32 +1918,36 @@
 her policy files
+%0A   
  is forbidden by
@@ -1942,36 +1942,52 @@
 forbidden by the
-%0A   
+ corss-domain policy
  specification.%0A
@@ -2392,12 +2392,8 @@
 t be
-come
  the
@@ -2410,25 +2410,25 @@
 licy for
+ the
 %0A    
-the 
 domain, 
@@ -2461,20 +2461,16 @@
  the URL
-%0A   
  %60%60/cros
@@ -2485,16 +2485,20 @@
 xml%60%60 on
+%0A   
  the dom
@@ -2504,11 +2504,9 @@
 main
- --
+:
  set
@@ -2510,35 +2510,30 @@
 setting meta
--
 policy
-%0A   
  information
@@ -2554,16 +2554,20 @@
 cy files
+%0A   
  is forb
@@ -2578,20 +2578,36 @@
 n by the
-%0A   
+ cross-domain policy
  specifi
@@ -2756,87 +2756,130 @@
 d. A
-cceptable values are defined as constants in%0A        %60%60flashpolicies.policies%60%60
+ set of constants is available in%0A        %60%60flashpolicies.policies%60%60, defining acceptable values for%0A        this argument
 .%0A%0A 

Update some view docstrings to match the main documentation.