def _add_subnet_id_query(self, current_q, op, item): try: item = parse_integer(item) except ValueError: raise ValidationError("Subnet ID must be numeric.") else: current_q = op(current_q, Q(id=item)) return current_q
def _add_vid_query(self, current_q, op, item): if item.lower() == "untagged": vid = 0 else: vid = parse_integer(item) validate_vid(vid) current_q = op(current_q, Q(vid=vid)) return current_q
def _add_vlan_vid_query(self, current_q, op, item): """Query for a related VLAN with a specified VID (vlan__vid). Even though this is a rather specific query, it was placed in orm.py since it is shared by multiple subclasses. (It will not be used unless referred to by the specifier_types dictionary passed into get_specifiers_q() by the subclass.) """ if item.lower() == 'untagged': vid = 0 else: vid = parse_integer(item) if vid < 0 or vid >= 0xfff: raise ValidationError("VLAN tag (VID) out of range " "(0-4094; 0 for untagged.)") current_q = op(current_q, Q(vlan__vid=vid)) return current_q
def get_object_id(self, name, prefix=None): """ Given the specified name and prefix, attempts to derive an object ID. By default (if a prefix is not supplied), uses the lowercase version of the current model object name as a prefix. For example, if the current model object name is "Fabric", and a string such as 'fabric-10' is supplied, int(10) will be returned. If an incorrect prefix is supplied, None will be returned. If an integer is supplied, the integer will be returned. If a string is supplied, that string will be parsed as an integer and returned (before trying to match against 'prefix-<int>'). :param name: str :param prefix: str :return: int """ if name is None: return None if isinstance(name, int): return name try: object_id = parse_integer(name) return object_id except ValueError: # Move on to check if this is a "name" like "object-10". pass if prefix is None: prefix = get_model_object_name(self).lower() name = name.strip() match = re.match(r'%s-(\d+)$' % prefix, name) if match is not None: (object_id, ) = match.groups() object_id = int(object_id) return object_id else: return None
def _add_default_query(self, current_q, op, item): """If the item we're matching is an integer, first try to locate the object by its ID. Otherwise, search by name. """ try: # Check if the user passed in a VID. vid = parse_integer(item) except ValueError: vid = None pass if item == "untagged": vid = 0 if vid is not None: # We could do something like this here, if you actually need to # look up the VLAN by its ID: # if isinstance(item, unicode) and item.strip().startswith('vlan-') # ... but it's better to use VID, since that means something to # the user (and you always need to qualify a VLAN with its fabric # anyway). validate_vid(vid) return op(current_q, Q(vid=vid)) else: return op(current_q, Q(name=item))