Revision dbdc30ec flowspec/forms.py

b/flowspec/forms.py
102 102
        then = self.cleaned_data.get('then', None)
103 103
        destination = self.cleaned_data.get('destination', None)
104 104
        destinationports = self.cleaned_data.get('destinationport', None)
105
        protocols = self.cleaned_data.get('protocol', None)
105 106
        user = self.cleaned_data.get('applier', None)
106 107
        peer = user.get_profile().peer
107 108
        networks = peer.networks.all()
......
126 127
            raise forms.ValidationError('Fill at least a Rule Match Condition')
127 128
        if not user.is_superuser and then[0].action not in settings.UI_USER_THEN_ACTIONS:
128 129
            raise forms.ValidationError('This action "%s" is not permitted' %(then[0].action))
129
        existing_routes = Route.objects.exclude(status='EXPIRED').exclude(status='PENDING').exclude(status='ERROR').exclude(status='ADMININACTIVE')
130
        existing_routes = Route.objects.exclude(status='EXPIRED').exclude(status='ERROR').exclude(status='ADMININACTIVE')
130 131
        existing_routes = existing_routes.filter(applier__userprofile__peer=peer)
131 132
        if source:
132 133
            source = IPNetwork(source).compressed
133 134
            existing_routes = existing_routes.filter(source=source)
134 135
        else:
135 136
            existing_routes = existing_routes.filter(source=None)
137
        if protocols:
138
            route_pk_list=get_matchingprotocol_route_pks(protocols, existing_routes)
139
            if route_pk_list:
140
                existing_routes = existing_routes.filter(pk__in=route_pk_list)
141
            else:
142
                existing_routes = existing_routes.filter(protocol=None)
143
        else:
144
            existing_routes = existing_routes.filter(protocol=None)
136 145
        if sourceports:
137 146
            route_pk_list=get_matchingport_route_pks(sourceports, existing_routes)
138 147
            if route_pk_list:
......
151 160
                existing_routes = existing_routes.filter(pk__in=route_pk_list)              
152 161
        else:
153 162
            existing_routes = existing_routes.filter(port=None)
154
        
155 163
        for route in existing_routes:
156 164
            if name != route.name:
157 165
                existing_url = reverse('edit-route', args=[route.name])
......
214 222
        rsp = value_list_to_list(route.destinationport.all().values_list('port').order_by('port'))
215 223
        if rsp and rsp == ports_value_list:
216 224
            route_pk_list.append(route.pk)
225
    return route_pk_list
226

  
227
def get_matchingprotocol_route_pks(protocolist, routes):
228
    route_pk_list = []
229
    protocols_value_list = value_list_to_list(protocolist.values_list('protocol').order_by('protocol'))
230
    for route in routes:
231
        rsp = value_list_to_list(route.protocol.all().values_list('protocol').order_by('protocol'))
232
        if rsp and rsp == protocols_value_list:
233
            route_pk_list.append(route.pk)
217 234
    return route_pk_list

Also available in: Unified diff