<commit_before>'''
Created on Apr 29, 2010

@author: jnaous
'''

from django import forms
from models import OpenFlowAggregate, OpenFlowSliceInfo, OpenFlowConnection
from openflow.plugin.models import OpenFlowInterface, NonOpenFlowConnection
from expedient.common.utils import create_or_update
from django.forms.models import ModelChoiceField

class OpenFlowAggregateForm(forms.ModelForm):
    '''
    A form to create and edit OpenFlow Aggregates.
    '''

    class Meta:
        model = OpenFlowAggregate
        exclude = ['client', 'owner', 'users']

class OpenFlowSliceInfoForm(forms.ModelForm):
    class Meta:
        model = OpenFlowSliceInfo
        exclude = ["slice"]

class OpenFlowConnectionSelectionForm(forms.Form):
    """
    A form to select multiple openflow connections.
    """
    
    of_connections = forms.ModelMultipleChoiceField(
        OpenFlowConnection.objects.all(),
        widget=forms.CheckboxSelectMultiple,
        required=False,
        label="Existing OpenFlow Connections")

    non_of_connections = forms.ModelMultipleChoiceField(
        NonOpenFlowConnection.objects.all(),
        widget=forms.CheckboxSelectMultiple,
        required=False,
        label="Existing Non-OpenFlow Connections")

    def __init__(self, of_cnxn_qs, non_of_cnxn_qs, *args, **kwargs):
        super(OpenFlowConnectionSelectionForm, self).__init__(*args, **kwargs)
        self.fields["of_connections"].queryset = of_cnxn_qs
        self.fields["non_of_connections"].queryset = non_of_cnxn_qs
    
class AsLeafClassModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        if hasattr(obj, "as_leaf_class"):
            return "%s" % obj.as_leaf_class()
    
class OpenFlowStaticConnectionForm(forms.Form):
    """
    A form for selecting a local and a remote interface to create a static
    bi-directional connection.
    """
    
    local_interface = AsLeafClassModelChoiceField(
        OpenFlowInterface.objects.all())
    
    remote_interface = AsLeafClassModelChoiceField(
        OpenFlowInterface.objects.all())
    
    def __init__(self, aggregate, *args, **kwargs):
        super(OpenFlowStaticConnectionForm, self).__init__(*args, **kwargs)

        self.fields["local_interface"].queryset = \
            OpenFlowInterface.objects.filter(aggregate__id=aggregate.id)
    
        self.fields["remote_interface"].queryset = \
            OpenFlowInterface.objects.exclude(aggregate__id=aggregate.id)
        
    def save_connections(self):
        """
        Create two unique unidirectional links between the two interfaces.
        
        @return: tuple of the created connections. If a connection is not
            created, None is returned for it.
        """
        
        cnxn1, created = create_or_update(
            OpenFlowConnection,
            filter_attrs=dict(
                src_iface = self.cleaned_data["local_interface"],
                dst_iface = self.cleaned_data["remote_interface"],
            ),
        )
        if not created: cnxn1 = None
        
        cnxn2, created = create_or_update(
            OpenFlowConnection,
            filter_attrs=dict(
                dst_iface = self.cleaned_data["local_interface"],
                src_iface = self.cleaned_data["remote_interface"],
            ),
        )
        if not created: cnxn2 = None
        
        return (cnxn1, cnxn2)

class NonOpenFlowStaticConnectionForm(forms.ModelForm):
    """
    A form for selecting a local interface and a non-openflow resource to
    create a static connection.
    """
    
    class Meta:
        model = NonOpenFlowConnection
        
    def __init__(self, aggregate, resource_qs, *args, **kwargs):
        super(NonOpenFlowStaticConnectionForm, self).__init__(*args, **kwargs)

        self.fields["of_iface"].queryset = \
            OpenFlowInterface.objects.filter(aggregate__id=aggregate.id)
    
        self.fields["resource"].queryset = resource_qs

<commit_msg>no leaf_name in openflow form<commit_after>'''
Created on Apr 29, 2010

@author: jnaous
'''

from django import forms
from models import OpenFlowAggregate, OpenFlowSliceInfo, OpenFlowConnection
from openflow.plugin.models import OpenFlowInterface, NonOpenFlowConnection
from expedient.common.utils import create_or_update
from django.forms.models import ModelChoiceField

class OpenFlowAggregateForm(forms.ModelForm):
    '''
    A form to create and edit OpenFlow Aggregates.
    '''

    class Meta:
        model = OpenFlowAggregate
        exclude = ['client', 'owner', 'users', "leaf_name"]

class OpenFlowSliceInfoForm(forms.ModelForm):
    class Meta:
        model = OpenFlowSliceInfo
        exclude = ["slice"]

class OpenFlowConnectionSelectionForm(forms.Form):
    """
    A form to select multiple openflow connections.
    """
    
    of_connections = forms.ModelMultipleChoiceField(
        OpenFlowConnection.objects.all(),
        widget=forms.CheckboxSelectMultiple,
        required=False,
        label="Existing OpenFlow Connections")

    non_of_connections = forms.ModelMultipleChoiceField(
        NonOpenFlowConnection.objects.all(),
        widget=forms.CheckboxSelectMultiple,
        required=False,
        label="Existing Non-OpenFlow Connections")

    def __init__(self, of_cnxn_qs, non_of_cnxn_qs, *args, **kwargs):
        super(OpenFlowConnectionSelectionForm, self).__init__(*args, **kwargs)
        self.fields["of_connections"].queryset = of_cnxn_qs
        self.fields["non_of_connections"].queryset = non_of_cnxn_qs
    
class AsLeafClassModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        if hasattr(obj, "as_leaf_class"):
            return "%s" % obj.as_leaf_class()
    
class OpenFlowStaticConnectionForm(forms.Form):
    """
    A form for selecting a local and a remote interface to create a static
    bi-directional connection.
    """
    
    local_interface = AsLeafClassModelChoiceField(
        OpenFlowInterface.objects.all())
    
    remote_interface = AsLeafClassModelChoiceField(
        OpenFlowInterface.objects.all())
    
    def __init__(self, aggregate, *args, **kwargs):
        super(OpenFlowStaticConnectionForm, self).__init__(*args, **kwargs)

        self.fields["local_interface"].queryset = \
            OpenFlowInterface.objects.filter(aggregate__id=aggregate.id)
    
        self.fields["remote_interface"].queryset = \
            OpenFlowInterface.objects.exclude(aggregate__id=aggregate.id)
        
    def save_connections(self):
        """
        Create two unique unidirectional links between the two interfaces.
        
        @return: tuple of the created connections. If a connection is not
            created, None is returned for it.
        """
        
        cnxn1, created = create_or_update(
            OpenFlowConnection,
            filter_attrs=dict(
                src_iface = self.cleaned_data["local_interface"],
                dst_iface = self.cleaned_data["remote_interface"],
            ),
        )
        if not created: cnxn1 = None
        
        cnxn2, created = create_or_update(
            OpenFlowConnection,
            filter_attrs=dict(
                dst_iface = self.cleaned_data["local_interface"],
                src_iface = self.cleaned_data["remote_interface"],
            ),
        )
        if not created: cnxn2 = None
        
        return (cnxn1, cnxn2)

class NonOpenFlowStaticConnectionForm(forms.ModelForm):
    """
    A form for selecting a local interface and a non-openflow resource to
    create a static connection.
    """
    
    class Meta:
        model = NonOpenFlowConnection
        
    def __init__(self, aggregate, resource_qs, *args, **kwargs):
        super(NonOpenFlowStaticConnectionForm, self).__init__(*args, **kwargs)

        self.fields["of_iface"].queryset = \
            OpenFlowInterface.objects.filter(aggregate__id=aggregate.id)
    
        self.fields["resource"].queryset = resource_qs


{'fraction_non_alphanumeric': 0.08862981070577912, 'fraction_numerical': 0.003008649868371568, 'mean_word_length': 2.817224880382775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6183677', 'n_tokens_mistral': 2235, 'n_tokens_neox': 2193, 'n_words': 477}