summaryrefslogtreecommitdiff
path: root/chimere/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'chimere/models.py')
-rw-r--r--chimere/models.py49
1 files changed, 37 insertions, 12 deletions
diff --git a/chimere/models.py b/chimere/models.py
index 4faf42b..07a2967 100644
--- a/chimere/models.py
+++ b/chimere/models.py
@@ -38,7 +38,8 @@ from django import forms
from django.conf import settings
from django.contrib.auth.models import User, Permission, ContentType, Group
from django.contrib.gis.db import models
-from django.contrib.postgres.search import SearchVectorField, SearchVector
+from django.contrib.postgres.search import SearchVectorField, SearchVector, \
+ SearchQuery
from django.core.files import File
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
@@ -264,8 +265,6 @@ class Icon(models.Model):
class SubCategory(models.Model):
- '''Sub-category
- '''
category = models.ForeignKey(Category, verbose_name=_("Category"),
related_name='subcategories')
name = models.CharField(_("Name"), max_length=150)
@@ -316,10 +315,11 @@ class SubCategory(models.Model):
@classmethod
def getAvailable(cls, item_types=None, area_name=None, public=False,
- instance=False):
- '''Get list of tuples with first the category and second the associated
+ instance=False, area=None):
+ """
+ Get list of tuples with first the category and second the associated
subcategories
- '''
+ """
sub_categories = {}
subcategories = cls.objects.filter(category__available=True)
if not item_types:
@@ -331,6 +331,7 @@ class SubCategory(models.Model):
selected_cats = []
if area_name:
area = Area.objects.get(urn=area_name)
+ if area:
# if there some restrictions with categories limit them
if area.subcategories.count():
sub_ids = [sub.id for sub in area.subcategories.all()]
@@ -388,8 +389,9 @@ class SubCategory(models.Model):
return items
def getJSON(self, categories_id=[]):
- '''Return a JSON string - mainly used to get description
- '''
+ """
+ Return a JSON string - mainly used to get description
+ """
json_string = json.dumps(self.getJSONDict())
return json_string
@@ -845,6 +847,29 @@ class GeographicItem(models.Model):
(max_weight - self.weight or 0) /
(float((max_weight - min_weight)) or 1), 5)
+ @classmethod
+ def getGeoJSONs(self, queryset, limit_to_categories=[]):
+ raise NotImplementedError()
+
+ @classmethod
+ def search(cls, query, area=None, get_json=False):
+ """
+ Search items using search_vector
+
+ :param query: terms to search
+ :param area: area limitation (default None)
+ :param get_json: output as json if True else return a queryset
+ :return: items matching the search query and the area
+ """
+ subcats = SubCategory.getAvailable(instance=True, area=area)
+ q = cls.objects.filter(categories__in=subcats,
+ search_vector=SearchQuery(
+ query,
+ config=settings.CHIMERE_SEARCH_LANGUAGE))
+ if get_json:
+ return cls.getGeoJSONs(q)
+ return q
+
def weighted_post_save(sender, **kwargs):
if not kwargs['instance']:
@@ -1147,8 +1172,9 @@ class Polygon(GeographicItem):
verbose_name = _("Polygon")
def getGeoJSON(self, color="#000", inner_color='#0F0'):
- '''Return a GeoJSON string
- '''
+ """
+ Return a GeoJSON string
+ """
try:
geom = json.loads(self.polygon.geojson)
except json.JSONDecodeError:
@@ -1938,8 +1964,7 @@ class Area(models.Model, SimpleArea):
@classmethod
def getAvailable(cls):
- '''Get available areas
- '''
+ # Get available areas
return cls.objects.filter(available=True)
def getWkt(self):