1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # |
3 # |
4 # This file is part of CubicWeb. |
4 # This file is part of CubicWeb. |
5 # |
5 # |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
499 def __init__(self, *regids): |
499 def __init__(self, *regids): |
500 super(adaptable, self).__init__('adapters', *regids) |
500 super(adaptable, self).__init__('adapters', *regids) |
501 |
501 |
502 def __call__(self, cls, req, **kwargs): |
502 def __call__(self, cls, req, **kwargs): |
503 kwargs.setdefault('accept_none', False) |
503 kwargs.setdefault('accept_none', False) |
504 # being adaptable to an interface should takes precedence other is_instance('Any'), |
504 score = super(adaptable, self).__call__(cls, req, **kwargs) |
505 # but not other explicit is_instance('SomeEntityType'), and: |
505 if score == 0 and kwargs.get('rset') and len(kwargs['rset']) > 1 and not 'row' in kwargs: |
|
506 # on rset containing several entity types, each row may be |
|
507 # individually adaptable, while the whole rset won't be if the |
|
508 # same adapter can't be used for each type |
|
509 for row in xrange(len(kwargs['rset'])): |
|
510 kwargs.setdefault('col', 0) |
|
511 _score = super(adaptable, self).__call__(cls, req, row=row, **kwargs) |
|
512 if not _score: |
|
513 return 0 |
|
514 # adjust score per row as expected by default adjust_score |
|
515 # implementation |
|
516 score += self.adjust_score(_score) |
|
517 else: |
|
518 score = self.adjust_score(score) |
|
519 return score |
|
520 |
|
521 @staticmethod |
|
522 def adjust_score(score): |
|
523 # being adaptable to an interface should takes precedence other |
|
524 # is_instance('Any'), but not other explicit |
|
525 # is_instance('SomeEntityType'), and, for **a single entity**: |
506 # * is_instance('Any') score is 1 |
526 # * is_instance('Any') score is 1 |
507 # * is_instance('SomeEntityType') score is at least 2 |
527 # * is_instance('SomeEntityType') score is at least 2 |
508 score = super(adaptable, self).__call__(cls, req, **kwargs) |
|
509 if score >= 2: |
528 if score >= 2: |
510 return score - 0.5 |
529 return score - 0.5 |
511 if score == 1: |
530 if score == 1: |
512 return score + 0.5 |
531 return score + 0.5 |
513 return score |
532 return score |