# 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/cocktail/forms.py
# class CocktailBaseForm(forms.Form):
#     def __init__(self, *args, **kwargs):
#         user = kwargs['user']
#         del kwargs['user']
#         super(CocktailBaseForm, self).__init__(*args, **kwargs)
#         self.fields['bases'].queryset = user.collection_set.all()
#         self.fields['mixers'].queryset = user.cocktailbase_set.all()
# 
#     bases = forms.ModelChoiceField(
#         queryset=Collection.objects.all(),
#         initial=1,
#         widget=forms.RadioSelect
#     )
# 
#     mixers = forms.ModelChoiceField(
#         queryset=CocktailBase.objects.all(),
#         empty_label='None',
#         widget=forms.RadioSelect,
#         required=False,
#     )
# 
#     classes = forms.CharField(label='Classes: ',
#                                help_text='Comma-separated Classes. '
#                                          'All selected if left blank',
#                                required=False)  # no 'strip' in Django 1.3
# 
#     TYPE_CHOICES = (
#         ('d', 'DETERMINISTIC'),
#         ('r', 'RANDOM'),
#     )
# 
#     mixing_type = forms.ChoiceField(label='Type: ',
#     							  widget=forms.Select(),
#                                   choices=TYPE_CHOICES,
#                                   initial='d')
# 
#     seed = forms.IntegerField(label='Seed: ',
#                                  help_text='Integer',
#                                  initial=42)
# 
#     percentage = forms.FloatField(label='Percentage: ',
#                                   help_text='%',
#                                   initial=1,
#                                   min_value=0,
#                                   max_value=100,
#                                   required=False)
# 
# class ManageForm(forms.Form):
#     user = forms.ModelChoiceField(queryset=User.objects.all())
#     bases = forms.ModelMultipleChoiceField(
#         queryset=CocktailBase.objects.all(),
#         widget=forms.CheckboxSelectMultiple,
#         required=False,
#     )
# 
#     class Media(object):
#         js = ('js/jquery.js',)


from future import standard_library
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 CocktailBaseForm, ManageForm
import json
import urllib.request, urllib.parse, urllib.error
            mixers = form.cleaned_data['mixers']
            server_all = set([server.host for server in collection.servers.all()])
            if mixers:
                percentage = form.cleaned_data['percentage']
                if percentage:
                    url_string += '_{}'.format(percentage)
                classes = form.cleaned_data['classes']
                if classes:
                    url_string += '/classes/{}'.format(classes)
                server_all = server_all & set([server.host for server in mixers.servers.all()])
                url_string = "/mixers/{}".format(mixers.dataset.replace(':', '').upper()) + url_string
            servers = server_all
            len_servers = len(servers)
            if mixers and len_servers > 1:
                for idx, server in enumerate(servers):
                    scope = [urllib.parse.quote("/cocktail/base/{}/distrbuted/{}of{}".format(
                    collection.gid.replace(':', '').upper(), (idx+1), len_servers) + url_string)]
                    cookie.extend(generate_cookie_django(
                        scope, [server],
                        blaster=getattr(settings, 'GATEKEEPER_BLASTER', None)))
            else:
                # If no mixing same url for all servers
                scope = [urllib.parse.quote("/cocktail/base/{}".format(
                            collection.gid.replace(':', '').upper()) + url_string)]
                cookie.extend(generate_cookie_django(
                    scope, servers,
                    blaster=getattr(settings, 'GATEKEEPER_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/cocktail/forms.py
# class CocktailBaseForm(forms.Form):
#     def __init__(self, *args, **kwargs):
#         user = kwargs['user']
#         del kwargs['user']
#         super(CocktailBaseForm, self).__init__(*args, **kwargs)
#         self.fields['bases'].queryset = user.collection_set.all()
#         self.fields['mixers'].queryset = user.cocktailbase_set.all()
# 
#     bases = forms.ModelChoiceField(
#         queryset=Collection.objects.all(),
#         initial=1,
#         widget=forms.RadioSelect
#     )
# 
#     mixers = forms.ModelChoiceField(
#         queryset=CocktailBase.objects.all(),
#         empty_label='None',
#         widget=forms.RadioSelect,
#         required=False,
#     )
# 
#     classes = forms.CharField(label='Classes: ',
#                                help_text='Comma-separated Classes. '
#                                          'All selected if left blank',
#                                required=False)  # no 'strip' in Django 1.3
# 
#     TYPE_CHOICES = (
#         ('d', 'DETERMINISTIC'),
#         ('r', 'RANDOM'),
#     )
# 
#     mixing_type = forms.ChoiceField(label='Type: ',
#     							  widget=forms.Select(),
#                                   choices=TYPE_CHOICES,
#                                   initial='d')
# 
#     seed = forms.IntegerField(label='Seed: ',
#                                  help_text='Integer',
#                                  initial=42)
# 
#     percentage = forms.FloatField(label='Percentage: ',
#                                   help_text='%',
#                                   initial=1,
#                                   min_value=0,
#                                   max_value=100,
#                                   required=False)
# 
# class ManageForm(forms.Form):
#     user = forms.ModelChoiceField(queryset=User.objects.all())
#     bases = forms.ModelMultipleChoiceField(
#         queryset=CocktailBase.objects.all(),
#         widget=forms.CheckboxSelectMultiple,
#         required=False,
#     )
# 
#     class Media(object):
#         js = ('js/jquery.js',)


# Path: opendiamond/scopeserver/cocktail/views.py
from future import standard_library
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 CocktailBaseForm, ManageForm
import json
import urllib.request, urllib.parse, urllib.error

            mixers = form.cleaned_data['mixers']
            server_all = set([server.host for server in collection.servers.all()])
            if mixers:
                percentage = form.cleaned_data['percentage']
                if percentage:
                    url_string += '_{}'.format(percentage)
                classes = form.cleaned_data['classes']
                if classes:
                    url_string += '/classes/{}'.format(classes)
                server_all = server_all & set([server.host for server in mixers.servers.all()])
                url_string = "/mixers/{}".format(mixers.dataset.replace(':', '').upper()) + url_string
            servers = server_all
            len_servers = len(servers)
            if mixers and len_servers > 1:
                for idx, server in enumerate(servers):
                    scope = [urllib.parse.quote("/cocktail/base/{}/distrbuted/{}of{}".format(
                    collection.gid.replace(':', '').upper(), (idx+1), len_servers) + url_string)]
                    cookie.extend(generate_cookie_django(
                        scope, [server],
                        blaster=getattr(settings, 'GATEKEEPER_BLASTER', None)))
            else:
                # If no mixing same url for all servers
                scope = [urllib.parse.quote("/cocktail/base/{}".format(
                            collection.gid.replace(':', '').upper()) + url_string)]
                cookie.extend(generate_cookie_django(
                    scope, servers,
                    blaster=getattr(settings, 'GATEKEEPER_BLASTER', None)))

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

        form = CocktailBaseForm(user=request.user)