def __init__(self, definition, values=None, source=None, expiration_date=None): """ definition can be given by its name, an alias or an oid """ self.definition = None if definition in ATTRIBUTE_MAPPING: self.definition = definition else: d = get_def_name_from_oid(definition) if d: self.definition = d else: self.definition = get_def_name_from_alias(definition) if not self.definition: raise Exception("Definition not found.") self.values = list() if values: for value in values: if convert_from_string(self.definition, value): self.values.append(value.encode("utf-8")) if isinstance(source, AttributeSource): self.source_id = source.id else: self.source_id = -1 """ISO8601""" try: iso8601_to_datetime(expiration_date) self.expiration_date = expiration_date except: self.expiration_date = None
def __init__(self, definition, values=None, source=None, expiration_date=None): ''' definition can be given by its name, an alias or an oid ''' self.definition = None if definition in ATTRIBUTE_MAPPING: self.definition = definition else: d = get_def_name_from_oid(definition) if d: self.definition = d else: self.definition = get_def_name_from_alias(definition) if not self.definition: raise Exception('Definition not found.') self.values = list() if values: for value in values: if convert_from_string(self.definition, value): self.values.append(value.encode('utf-8')) if isinstance(source, AttributeSource): self.source_id = source.id else: self.source_id = -1 '''ISO8601''' try: iso8601_to_datetime(expiration_date) self.expiration_date = expiration_date except: self.expiration_date = None
def set_exptime_in_iso8601(self, expiration_date): try: iso8601_to_datetime(expiration_date) self.expiration_date = expiration_date except: self.expiration_date = None self.save() return self.expiration_date
def load_by_dic(self, dictionnary): """ Dictionnary format: attributes = dict() data_from_source = list() a1 = dict() a1['oid'] = definition_name Or a1['definition'] = definition_name definition may be the definition name like 'gn' or an alias like 'givenName' Or a1['name'] = attribute_name_in_ns a1['namespace'] = ns_name a1['values'] = list_of_values data_from_source.append(a1) ... data_from_source.append(a2) attributes[source_name] = data_from_source First attempt on 'definition' key. Else, definition is searched by 'name' and 'namespece' keys. """ if not dictionnary: logger.info("load_by_dic: empty dictionnary provided") return -1 for source_name in dictionnary: logger.debug("load_by_dic: loading from source with name: %s" % source_name) source = get_source_from_name(source_name) if source: logger.debug("load_by_dic: attributes: %s" % str(dictionnary[source_name])) for attribute in dictionnary[source_name]: if ( not "oid" in attribute and not "definition" in attribute and not ("name" in attribute and "namespace" in attribute) ) or not "values" in attribute: logger.warn( "load_by_dic: \ missing data to treat %s" % str(attribute) ) else: definition = None if "oid" in attribute: definition = get_def_name_from_oid(attribute["oid"]) elif "definition" in attribute: if attribute["definition"] in ATTRIBUTE_MAPPING: definition = attribute["definition"] else: definition = get_def_name_from_alias(attribute["definition"]) else: definition = get_def_name_from_name_and_ns_of_attribute( attribute["name"], attribute["namespace"] ) if not definition: logger.warn( "load_by_dic: \ unable to find definition for %s" % str(attribute) ) else: logger.debug( "load_by_dic: \ definition %s found" % definition ) expiration_date = None if "expiration_date" in attribute: logger.debug("load_by_dic: expire at %s" % attribute["expiration_date"]) try: iso8601_to_datetime(attribute["expiration_date"]) expiration_date = attribute["expiration_date"] logger.debug( "load_by_dic: expiration \ date has the ISO8601 format" ) except: logger.warn( "load_by_dic: expiration \ date has not the ISO8601 format" ) if not expiration_date: expiration_date = datetime.datetime.now().isoformat() values = [value for value in attribute["values"] if convert_from_string(definition, value)] if ( self.add_data( AttributeData( definition, values=values, source=source, expiration_date=expiration_date ) ) == 0 ): logger.debug( "load_by_dic: \ attribute successfully added" ) else: logger.warn( "load_by_dic: \ error addind attribute" ) else: logger.critical( "load_by_dic: \ The source with name %s providing attributes %s \ is unknown of the system" % (str(source_name), str(dictionnary[source_name])) ) return 0
def get_exptime_in_datetime(self): return iso8601_to_datetime(self.expiration_date)
def load_by_dic(self, dictionnary): ''' Dictionnary format: attributes = dict() data_from_source = list() a1 = dict() a1['oid'] = definition_name Or a1['definition'] = definition_name definition may be the definition name like 'gn' or an alias like 'givenName' Or a1['name'] = attribute_name_in_ns a1['namespace'] = ns_name a1['values'] = list_of_values data_from_source.append(a1) ... data_from_source.append(a2) attributes[source_name] = data_from_source First attempt on 'definition' key. Else, definition is searched by 'name' and 'namespece' keys. ''' if not dictionnary: logger.error('load_by_dic: \ Missing profile or dictionnary') return -1 for source_name in dictionnary: logger.debug('load_by_dic: loading from source with name: %s' \ % source_name) source = get_source_from_name(source_name) if source: logger.debug('load_by_dic: attributes: %s' \ % str(dictionnary[source_name])) for attribute in dictionnary[source_name]: if (not 'oid' in attribute \ and not 'definition' in attribute \ and not('name' in attribute \ and 'namespace' in attribute)) \ or not 'values' in attribute: logger.warn('load_by_dic: \ missing data to treat %s' % str(attribute)) else: definition = None if 'oid' in attribute: definition = \ get_def_name_from_oid(attribute['oid']) elif 'definition' in attribute: if attribute['definition'] in ATTRIBUTE_MAPPING: definition = attribute['definition'] else: definition = \ get_def_name_from_alias(attribute['definition']) else: definition = \ get_def_name_from_name_and_ns_of_attribute(\ attribute['name'], attribute['namespace']) if not definition: logger.warn('load_by_dic: \ unable to find definition for %s' \ % str(attribute)) else: logger.debug('load_by_dic: \ definition %s found' % definition) expiration_date = None if 'expiration_date' in attribute: logger.debug('load_by_dic: expire at %s' \ % attribute['expiration_date']) try: iso8601_to_datetime(\ attribute['expiration_date']) expiration_date = \ attribute['expiration_date'] logger.debug('load_by_dic: expiration \ date has the ISO8601 format') except: logger.warn('load_by_dic: expiration \ date has not the ISO8601 format') if not expiration_date: expiration_date = \ datetime.datetime.now().isoformat() values = [value for value in attribute['values'] \ if convert_from_string(definition, value)] if self.add_data(AttributeData(\ definition, values=values, source=source, expiration_date=expiration_date)) == 0: logger.debug('load_by_dic: \ attribute successfully added') else: logger.warn('load_by_dic: \ error addind attribute') else: logger.critical('load_by_dic: \ The source with name %s providing attributes %s \ is unknown of the system' \ % (str(source_name), str(dictionnary[source_name]))) return 0