2011-08-20 17:08:00 +00:00
|
|
|
"""Models used by django-watson."""
|
|
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2011-08-20 17:27:57 +00:00
|
|
|
from django.contrib.contenttypes import generic
|
2011-09-06 16:42:06 +00:00
|
|
|
from django.utils import simplejson as json
|
2011-08-20 17:08:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def has_int_pk(model):
|
|
|
|
|
"""Tests whether the given model has an integer primary key."""
|
2011-10-02 17:46:19 +00:00
|
|
|
pk = model._meta.pk
|
2011-08-20 17:08:00 +00:00
|
|
|
return (
|
2011-10-02 17:46:19 +00:00
|
|
|
(
|
|
|
|
|
isinstance(pk, (models.IntegerField, models.AutoField)) and
|
|
|
|
|
not isinstance(pk, models.BigIntegerField)
|
|
|
|
|
) or (
|
|
|
|
|
isinstance(pk, models.ForeignKey) and has_int_pk(pk.rel.to)
|
|
|
|
|
)
|
2011-08-20 17:08:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
META_CACHE_KEY = "_meta_cache"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SearchEntry(models.Model):
|
|
|
|
|
|
|
|
|
|
"""An entry in the search index."""
|
2011-08-23 16:12:35 +00:00
|
|
|
|
|
|
|
|
engine_slug = models.CharField(
|
|
|
|
|
max_length = 200,
|
|
|
|
|
db_index = True,
|
2011-09-01 12:53:22 +00:00
|
|
|
default = "default",
|
2011-08-23 16:12:35 +00:00
|
|
|
)
|
|
|
|
|
|
2011-08-20 17:08:00 +00:00
|
|
|
content_type = models.ForeignKey(
|
|
|
|
|
ContentType,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
object_id = models.TextField()
|
|
|
|
|
|
|
|
|
|
object_id_int = models.IntegerField(
|
|
|
|
|
blank = True,
|
|
|
|
|
null = True,
|
|
|
|
|
db_index = True,
|
|
|
|
|
)
|
|
|
|
|
|
2011-08-20 17:27:57 +00:00
|
|
|
object = generic.GenericForeignKey()
|
2011-08-20 17:08:00 +00:00
|
|
|
|
2011-08-23 16:12:35 +00:00
|
|
|
title = models.CharField(
|
|
|
|
|
max_length = 1000,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
description = models.TextField(
|
|
|
|
|
blank = True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
content = models.TextField(
|
|
|
|
|
blank = True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
url = models.CharField(
|
|
|
|
|
max_length = 1000,
|
|
|
|
|
blank = True,
|
|
|
|
|
)
|
|
|
|
|
|
2011-08-20 17:08:00 +00:00
|
|
|
meta_encoded = models.TextField()
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def meta(self):
|
|
|
|
|
"""Returns the meta information stored with the search entry."""
|
|
|
|
|
# Attempt to use the cached value.
|
|
|
|
|
if hasattr(self, META_CACHE_KEY):
|
|
|
|
|
return getattr(self, META_CACHE_KEY)
|
|
|
|
|
# Decode the meta.
|
2011-09-06 16:42:06 +00:00
|
|
|
meta_value = json.loads(self.meta_encoded)
|
2011-08-20 17:08:00 +00:00
|
|
|
setattr(self, META_CACHE_KEY, meta_value)
|
|
|
|
|
return meta_value
|
|
|
|
|
|
2011-08-23 16:12:35 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
|
"""Returns the URL of the referenced object."""
|
|
|
|
|
return self.url
|
|
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
|
"""Returns a unicode representation."""
|
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
verbose_name_plural = "search entries"
|