# 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/gatekeeper/forms.py
# class CollectionForm(forms.Form):
#     def __init__(self, *args, **kwargs):
#         user = kwargs['user']
#         del kwargs['user']
#         super(CollectionForm, self).__init__(*args, **kwargs)
#         self.fields['collections'].queryset = user.collection_set.all()
#     collections = forms.ModelMultipleChoiceField(
#         queryset=Collection.objects.all(),
#         widget=forms.CheckboxSelectMultiple
#     )
# 
# class ManageForm(forms.Form):
#     user = forms.ModelChoiceField(queryset=User.objects.all())
#     collections = forms.ModelMultipleChoiceField(
#         queryset=Collection.objects.all(),
#         widget=forms.CheckboxSelectMultiple,
#         required=False,
#     )
# 
#     class Media(object):
#         js = ('js/jquery.js',)


from django.conf import settings
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from opendiamond.scope import generate_cookie_django
from .forms import CollectionForm, ManageForm
import json
#
#  The OpenDiamond Platform for Interactive Search
#
#  Copyright (c) 2009-2019 Carnegie Mellon University
#  All rights reserved.
#
#  This software is distributed under the terms of the Eclipse Public
#  License, Version 1.0 which can be found in the file named LICENSE.
#  ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS SOFTWARE CONSTITUTES
#  RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT
#





@login_required
def index(request):
    if request.method == 'POST':
        form = CollectionForm(request.POST, user=request.user)

        if form.is_valid():
            cookie = []
            for collection in form.cleaned_data['collections']:
                scope = ["/collection/%s" % collection.gid.replace(':', '')]
                servers = set()
                for server in collection.servers.all():
                    servers.add(server.host)
                cookie.extend(generate_cookie_django(

# 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/gatekeeper/forms.py
# class CollectionForm(forms.Form):
#     def __init__(self, *args, **kwargs):
#         user = kwargs['user']
#         del kwargs['user']
#         super(CollectionForm, self).__init__(*args, **kwargs)
#         self.fields['collections'].queryset = user.collection_set.all()
#     collections = forms.ModelMultipleChoiceField(
#         queryset=Collection.objects.all(),
#         widget=forms.CheckboxSelectMultiple
#     )
# 
# class ManageForm(forms.Form):
#     user = forms.ModelChoiceField(queryset=User.objects.all())
#     collections = forms.ModelMultipleChoiceField(
#         queryset=Collection.objects.all(),
#         widget=forms.CheckboxSelectMultiple,
#         required=False,
#     )
# 
#     class Media(object):
#         js = ('js/jquery.js',)


# Path: opendiamond/scopeserver/gatekeeper/views.py
from django.conf import settings
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from opendiamond.scope import generate_cookie_django
from .forms import CollectionForm, ManageForm
import json

#
#  The OpenDiamond Platform for Interactive Search
#
#  Copyright (c) 2009-2019 Carnegie Mellon University
#  All rights reserved.
#
#  This software is distributed under the terms of the Eclipse Public
#  License, Version 1.0 which can be found in the file named LICENSE.
#  ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS SOFTWARE CONSTITUTES
#  RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT
#





@login_required
def index(request):
    if request.method == 'POST':
        form = CollectionForm(request.POST, user=request.user)

        if form.is_valid():
            cookie = []
            for collection in form.cleaned_data['collections']:
                scope = ["/collection/%s" % collection.gid.replace(':', '')]
                servers = set()
                for server in collection.servers.all():
                    servers.add(server.host)
                cookie.extend(generate_cookie_django(

                    scope, servers,