[attribute_aggregator] Refine AttributeData and UserAliasInSource classes
* Define Meta and unicode of UserAliasInSource Class * AttributeData have methods that return empty dict, not None.
This commit is contained in:
parent
a9882bf0d8
commit
3a71afbbc2
|
@ -39,6 +39,7 @@ logger = logging.getLogger('attribute_aggregator')
|
|||
|
||||
class AttributeSource(models.Model):
|
||||
name = models.CharField(max_length = 200, unique=True)
|
||||
namespace = models.CharField(max_length = 200, blank=True, null=True)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
|
@ -58,6 +59,13 @@ def get_source_from_name(name):
|
|||
return None
|
||||
|
||||
|
||||
def get_all_sources():
|
||||
try:
|
||||
return AttributeSource.objects.all()
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
class LdapSource(AttributeSource):
|
||||
server = models.CharField(max_length=200, unique=True)
|
||||
user = models.CharField(max_length=200, blank=True, null=True)
|
||||
|
@ -68,13 +76,26 @@ class LdapSource(AttributeSource):
|
|||
certificate = models.TextField(blank=True)
|
||||
is_auth_backend = models.BooleanField(default=False)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(LdapSource, self).__init__(*args, **kwargs)
|
||||
self.namespace = "X500"
|
||||
|
||||
|
||||
class UserAliasInSource(models.Model):
|
||||
name = models.CharField(max_length = 200, unique=True)
|
||||
name = models.CharField(max_length = 200)
|
||||
source = models.ForeignKey(AttributeSource,
|
||||
verbose_name = _('Attribute Source'))
|
||||
user = models.ForeignKey(User, related_name='user_alias_in_source')
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('alias in source')
|
||||
verbose_name_plural = _('aliases in source')
|
||||
unique_together = ("name", "source")
|
||||
|
||||
def __unicode__(self):
|
||||
return "alias %s of user %s in %s" % (self.name, self.user,
|
||||
self.source)
|
||||
|
||||
|
||||
class AttributeData:
|
||||
|
||||
|
@ -129,6 +150,9 @@ class AttributeData:
|
|||
def get_values(self):
|
||||
return self.values
|
||||
|
||||
def get_converted_values(self):
|
||||
return [convert_from_string(self.definition, value) for value in self.values]
|
||||
|
||||
def get_source(self):
|
||||
try:
|
||||
return AttributeSource.objects.get(pk=self.source_id)
|
||||
|
@ -205,7 +229,7 @@ class UserAttributeProfile(models.Model):
|
|||
l = loads(str(self.data))
|
||||
return l
|
||||
except:
|
||||
return None
|
||||
return []
|
||||
|
||||
def get_data_of_definition(self, definition, in_list=None):
|
||||
l = None
|
||||
|
@ -214,40 +238,42 @@ class UserAttributeProfile(models.Model):
|
|||
else:
|
||||
l = self.get_all_data()
|
||||
if not l:
|
||||
return None
|
||||
return []
|
||||
return [d for d in l if d.get_definition() == definition]
|
||||
|
||||
def get_freshest_data_of_definition(self, definition):
|
||||
l = self.get_data_of_definition(definition)
|
||||
if not l:
|
||||
return None
|
||||
return []
|
||||
l.sort(key=lambda x: x.expiration_date, reverse=True)
|
||||
return l[0]
|
||||
|
||||
def get_data_of_source(self, source, in_list=None):
|
||||
l = None
|
||||
l = []
|
||||
if in_list:
|
||||
l = in_list
|
||||
else:
|
||||
l = self.get_all_data()
|
||||
if not l or not isinstance(source, AttributeSource):
|
||||
return None
|
||||
return []
|
||||
return [d for d in l if d.get_source_id() == source.id]
|
||||
|
||||
def get_data_of_source_by_name(self, source):
|
||||
l = self.get_all_data()
|
||||
if not l:
|
||||
return None
|
||||
s = None
|
||||
return []
|
||||
s = []
|
||||
try:
|
||||
s = AttributeSource.objects.get(name=source)
|
||||
except:
|
||||
return None
|
||||
return []
|
||||
return [d for d in l if d.get_source_id() == s.id]
|
||||
|
||||
def get_data_of_definition_and_source(self, definition, source):
|
||||
return self.get_data_of_definition(definition,
|
||||
in_list=self.get_data_of_source(source))
|
||||
in_list = self.get_data_of_source(source)
|
||||
if not in_list:
|
||||
return []
|
||||
return self.get_data_of_definition(definition, in_list=in_list)
|
||||
|
||||
def get_data_of_definition_and_source_by_name(self, definition, source):
|
||||
return self.get_data_of_definition(definition,
|
||||
|
|
Reference in New Issue