# Path: opendiamond/scope.py
# def generate_cookie_django(scopeurls, servers, proxies=None, blaster=None):
#     '''A variant of generate_cookie() which pulls the more obscure fixed
#     arguments from Django settings.
# 
#     The signing key file comes from settings.SCOPE_KEY_FILE, if present.
# 
#     The expiration time (in seconds) comes from
#     settings.SCOPE_COOKIE_EXPIRATION, if present.'''
# 
#     from django.conf import settings
#     keyfile = getattr(settings, 'SCOPE_KEY_FILE', None)
#     expires = getattr(settings, 'SCOPE_COOKIE_EXPIRATION', None)
#     if expires is not None:
#         expires = timedelta(seconds=expires)
#     return generate_cookie(scopeurls, servers, proxies=proxies,
#                            keyfile=keyfile, expires=expires, blaster=blaster)
# 
# Path: opendiamond/scopeserver/flickr/forms.py
# class FlickrForm(forms.Form):
#     tags = forms.CharField(widget=forms.Textarea,
#                            help_text="Tags to search for (one per line)",
#                            required=False)
#     tag_mode = forms.BooleanField(label="Match all tags", required=False)
#     text = forms.CharField(help_text="Searches title, description, and tags",
#                            required=False)
#     proxied = forms.BooleanField(label="Use proxies", required=False)


from django.contrib.auth.decorators import permission_required
from django.conf import settings
from django.http import QueryDict, HttpResponse
from django.shortcuts import render
from opendiamond.scope import generate_cookie_django
from .forms import FlickrForm
    if request.method == 'POST':
        form = FlickrForm(request.POST)

        if form.is_valid():
            tags = form.cleaned_data.get('tags', '').split('\n')
            tags = [tag.strip() for tag in tags]  # trim whitespace
            tags = [tag for tag in tags if tag]   # skip empty

            tag_mode = 'all' if form.cleaned_data['tag_mode'] else 'any'

            text = form.cleaned_data.get('text', '')

            q = QueryDict('').copy()
            if tags:
                q['tags'] = ','.join(tags)
                q['tag_mode'] = tag_mode
            if text:
                q['text'] = text
            query = q.urlencode()

            scope = ["/flickr/?%s" % query]

            proxies = settings.FLICKR_PROXIES \
                if form.cleaned_data['proxied'] else None
            cookie = generate_cookie_django(
                scope, settings.FLICKR_SERVERS, proxies,
                blaster=getattr(settings, 'FLICKR_BLASTER', None))

            return HttpResponse(cookie, content_type='application/x-diamond-scope')
    else:

# Path: opendiamond/scope.py
# def generate_cookie_django(scopeurls, servers, proxies=None, blaster=None):
#     '''A variant of generate_cookie() which pulls the more obscure fixed
#     arguments from Django settings.
# 
#     The signing key file comes from settings.SCOPE_KEY_FILE, if present.
# 
#     The expiration time (in seconds) comes from
#     settings.SCOPE_COOKIE_EXPIRATION, if present.'''
# 
#     from django.conf import settings
#     keyfile = getattr(settings, 'SCOPE_KEY_FILE', None)
#     expires = getattr(settings, 'SCOPE_COOKIE_EXPIRATION', None)
#     if expires is not None:
#         expires = timedelta(seconds=expires)
#     return generate_cookie(scopeurls, servers, proxies=proxies,
#                            keyfile=keyfile, expires=expires, blaster=blaster)
# 
# Path: opendiamond/scopeserver/flickr/forms.py
# class FlickrForm(forms.Form):
#     tags = forms.CharField(widget=forms.Textarea,
#                            help_text="Tags to search for (one per line)",
#                            required=False)
#     tag_mode = forms.BooleanField(label="Match all tags", required=False)
#     text = forms.CharField(help_text="Searches title, description, and tags",
#                            required=False)
#     proxied = forms.BooleanField(label="Use proxies", required=False)


# Path: opendiamond/scopeserver/flickr/views.py
from django.contrib.auth.decorators import permission_required
from django.conf import settings
from django.http import QueryDict, HttpResponse
from django.shortcuts import render
from opendiamond.scope import generate_cookie_django
from .forms import FlickrForm

    if request.method == 'POST':
        form = FlickrForm(request.POST)

        if form.is_valid():
            tags = form.cleaned_data.get('tags', '').split('\n')
            tags = [tag.strip() for tag in tags]  # trim whitespace
            tags = [tag for tag in tags if tag]   # skip empty

            tag_mode = 'all' if form.cleaned_data['tag_mode'] else 'any'

            text = form.cleaned_data.get('text', '')

            q = QueryDict('').copy()
            if tags:
                q['tags'] = ','.join(tags)
                q['tag_mode'] = tag_mode
            if text:
                q['text'] = text
            query = q.urlencode()

            scope = ["/flickr/?%s" % query]

            proxies = settings.FLICKR_PROXIES \
                if form.cleaned_data['proxied'] else None
            cookie = generate_cookie_django(
                scope, settings.FLICKR_SERVERS, proxies,
                blaster=getattr(settings, 'FLICKR_BLASTER', None))

            return HttpResponse(cookie, content_type='application/x-diamond-scope')
    else:

        form = FlickrForm()