querysets.py 771 B

1234567891011121314151617181920212223
  1. from __future__ import unicode_literals
  2. from django.db.models import Q, QuerySet
  3. class ConfigContextQuerySet(QuerySet):
  4. def get_for_object(self, obj):
  5. """
  6. Return all applicable ConfigContexts for a given object. Only active ConfigContexts will be included.
  7. """
  8. # `device_role` for Device; `role` for VirtualMachine
  9. role = getattr(obj, 'device_role', None) or obj.role
  10. return self.filter(
  11. Q(regions=obj.site.region) | Q(regions=None),
  12. Q(sites=obj.site) | Q(sites=None),
  13. Q(roles=role) | Q(roles=None),
  14. Q(tenants=obj.tenant) | Q(tenants=None),
  15. Q(platforms=obj.platform) | Q(platforms=None),
  16. is_active=True,
  17. ).order_by('weight', 'name')