|
@@ -20,12 +20,34 @@
|
|
|
|
|
|
|
|
|
# Django imports.
|
|
|
from django.core.exceptions import ValidationError
|
|
|
from django.core.urlresolvers import reverse
|
|
|
from django.db import models
|
|
|
from django.db.models.query_utils import Q
|
|
|
|
|
|
|
|
|
class SearchManager(models.Manager):
|
|
|
"""
|
|
|
Custom model manager that implements search for model instances that contain
|
|
|
a specific string (search term) in fields "name" or "description".
|
|
|
"""
|
|
|
|
|
|
def search(self, search_term):
|
|
|
"""
|
|
|
Performs a search for model instances that contain the provided search
|
|
|
term in fields "name" or "description". The search is case-insensitive.
|
|
|
|
|
|
Arguments:
|
|
|
search_term - String to search the name and description for.
|
|
|
|
|
|
Returns:
|
|
|
Query set with model instances that matched the search.
|
|
|
"""
|
|
|
|
|
|
return self.filter(Q(name__icontains=search_term) | Q(description__icontains=search_term))
|
|
|
|
|
|
|
|
|
class Project(models.Model):
|
|
|
"""
|
|
|
Implements a model with information about a project. A project has some
|
|
|
basic settings, and mainly serves the purpose of grouping entities for
|
|
@@ -36,12 +58,13 @@ class Project(models.Model):
|
|
|
name - String denoting the project name.
|
|
|
description - Free-form description of the project.
|
|
|
"""
|
|
|
|
|
|
name = models.CharField(max_length=100, unique=True)
|
|
|
description = models.TextField(blank=True)
|
|
|
objects = SearchManager()
|
|
|
|
|
|
class Meta:
|
|
|
permissions = (("view", "Can view information"),)
|
|
|
|
|
|
def __unicode__(self):
|
|
|
"""
|
|
@@ -116,12 +139,13 @@ class Entity(models.Model):
|
|
|
"""
|
|
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
description = models.TextField(blank=True)
|
|
|
project = models.ForeignKey(Project)
|
|
|
location = models.ForeignKey(Location)
|
|
|
objects = SearchManager()
|
|
|
|
|
|
class Meta:
|
|
|
# Fix the plural form used by Django.
|
|
|
verbose_name_plural = 'entities'
|
|
|
# Enforce uniqueness of entity name in a project.
|
|
|
unique_together = ("name", "project")
|