diff options
Diffstat (limited to 'chimere/models.py')
-rw-r--r-- | chimere/models.py | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/chimere/models.py b/chimere/models.py index d9b388d..ddeeefd 100644 --- a/chimere/models.py +++ b/chimere/models.py @@ -38,7 +38,7 @@ from django.contrib.gis.db import models from django.core.files import File from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import reverse -from django.db.models import Q +from django.db.models import Q, Count from django.db.models.signals import post_save, pre_save, m2m_changed from django.template import defaultfilters from django.utils.translation import ugettext_lazy as _ @@ -577,11 +577,11 @@ class GeographicItem(models.Model): return return property - def getProperties(self): + def getProperties(self, area_name=None): """Get all the property availables """ properties = [] - for pm in PropertyModel.objects.filter(available=True): + for pm in PropertyModel.getAvailable(area_name=area_name): property = self.getProperty(pm) if property: properties.append(property) @@ -1851,6 +1851,17 @@ class PropertyModel(models.Model): ''' return 'property_%d_%d' % (self.order, self.id) + @classmethod + def getAvailable(cls, area_name=None): + q = cls.objects.filter(available=True) + if not area_name: + return q.annotate(Count('areas')).filter(areas__count=0) + # areas__count__gt=0 necessary to prevent Django bug + return q.annotate(Count('areas'))\ + .filter( + (Q(areas__urn=area_name) & Q(areas__count__gt=0)) | + Q(areas__count=0)) + class PropertyModelChoice(models.Model): '''Choices for property model |