Revision 135:0fb2a13e3760 aai/models.py

b/aai/models.py
41 41
        self.mdtree = parse(filename)
42 42
        self.metadata = self.mdtree.getroot()
43 43

  
44
    def getEntities(self, entity_type = None):
45
        """Returns an IdpList holding all Identity Providers found in the metadata"""
44
    def getEntities(self, entity_type = None, augmented=False):
45
        """Returns an EntityList holding all Entities (of requested type) found 
46
        in the metadata, perhaps augmented in to the corresponding object type
47
        """
46 48
        if entity_type is "idp":
47 49
            entityfilter = "IDPSSODescriptor"
48 50
        elif entity_type is "sp":
49 51
            entityfilter = "SPSSODescriptor"
50
        elif entity_type is "aa":
51
            entityfilter = "AttributeAuthorityDescriptor"
52
        # elif entity_type is "aa":
53
        #     entityfilter = "AttributeAuthorityDescriptor"
52 54
        else:
53 55
            entityfilter = None
54 56

  
......
60 62
            else:
61 63
                return False
62 64

  
63
        # Create an EntityList holding entities of the requested type
64
        return EntityList([ Entity(kot) for kot in
65
                         filter(filtertype,
66
                                self.metadata.xpath("//md:EntityDescriptor",
67
                                                    namespaces=xpath_ns)
68
                                ) ]
69
                       )
65
        kots = filter(filtertype, self.metadata.xpath("//md:EntityDescriptor",
66
                                                     namespaces=xpath_ns))
67

  
68
        if augmented:
69
            return EntityList([ Entity.construct_augmented_entity(kot)
70
                                for kot in kots])
71
        else:
72
            return EntityList([ Entity(kot) for kot in kots])
70 73

  
71 74
    def getIdps(self):
72 75
        """Returns an IdpList holding all Identity Providers found in the metadata"""
......
116 119
            groups.update(x.groups)
117 120
        return groups
118 121

  
119
    def getEntities(self, lang=None, group=None):
122
    def getEntities(self, lang=None, group=None, logosize=tuple()):
120 123
        """Returns a list of entities, where each entity is represented by a 
121 124
        dict carrying the localized name, url, logo and entity ID attributes
122 125

  
......
133 136

  
134 137
        return map(lambda x: {'name': x.getName(lang),
135 138
                              'url': x.getURL(lang),
136
                              'logo': x.getLogo(),
139
                              'logo': x.getLogo(targetsize=logosize),
137 140
                              'id': x.id },
138 141
                   entities)
139 142

  
......
297 300
        if issubclass(cls, entity.__class__) and hasattr(entity, 'el'):
298 301
            return cls(entity.el)
299 302
        else:
300
            raise Exception('Can not instantiate %s from %s' % (cls, str(entity)))
303
            raise Exception('Can not instantiate %s from %s' % (cls,
304
                                                                str(entity)))
305

  
306
    @staticmethod
307
    def construct_augmented_entity(el):
308
        elcls = el.__class__
309
        # should do isinstance() but we do not import whole lxl.objectify
310
        if ("%s.%s" % (elcls.__module__, elcls.__name__)) == \
311
                "lxml.objectify.ObjectifiedElement":
312
            if hasattr(el, "IDPSSODescriptor"):
313
                aug_ent_cls = IdentityProvider
314
            elif hasattr(el, "SPSSODescriptor"):
315
                aug_ent_cls = ServiceProvider
316
            # elif hasattr(el, "AttributeAuthorityDescriptor"):
317
            #     pass
318
            else:
319
                aug_ent_cls = None
320

  
321
            if aug_ent_cls is not None:
322
                return aug_ent_cls(el)
323

  
324
        raise Exception("Can not instantiate augmented Entity from %s" %
325
                             el)
301 326

  
302 327
    def getName(self,lang=None):
303 328
        if not lang:
......
324 349

  
325 350
        return None
326 351

  
327
    def getLogo(self,targetdimensions=tuple()):
352
    def getLogo(self,targetsize=tuple()):
328 353

  
329 354
        if not self.logo:
330 355
            return None
331 356

  
332 357
        # get (x, y) closest to target (a, b)
333
        if isinstance(targetdimensions, tuple) and \
334
                len(targetdimensions) is 2 and \
335
                [isinstance(i, int) for i in targetdimensions]:
358
        if isinstance(targetsize, tuple) and \
359
                len(targetsize) is 2 and \
360
                [isinstance(i, int) for i in targetsize]:
336 361
            dimensions = min(self.logo.keys(),
337
                             key = lambda x: abs(x[0]-targetdimensions[0]) + \
338
                                 abs(x[1]-targetdimensions[1])
362
                             key = lambda x: abs(x[0]-targetsize[0]) + \
363
                                 abs(x[1]-targetsize[1])
339 364
                             )
340 365
        else:
341 366
            dimensions = random.choice(self.logo.keys())
342 367

  
343
        return self.logo[dimensions]
368
        return { 'width': dimensions[0],
369
                 'height': dimensions[1],
370
                 'data': self.logo[dimensions] }
344 371

  
345 372

  
346 373
class IdentityProvider(Entity):

Also available in: Unified diff