Add model for search engines
This commit is contained in:
parent
225742798b
commit
114c8a3d3e
1 changed files with 33 additions and 0 deletions
|
@ -1,6 +1,18 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidData(Exception):
|
||||||
|
''' Thrown when the DB contains invalid data, and cannot perform
|
||||||
|
something '''
|
||||||
|
|
||||||
|
def __init__(self, what):
|
||||||
|
self.what = what
|
||||||
|
super(InvalidData, self).__init__()
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.what
|
||||||
|
|
||||||
|
|
||||||
class BrowserFingerprint(models.Model):
|
class BrowserFingerprint(models.Model):
|
||||||
''' A browser fingerprint, containing things like a user agent '''
|
''' A browser fingerprint, containing things like a user agent '''
|
||||||
|
|
||||||
|
@ -22,3 +34,24 @@ class BrowserFingerprint(models.Model):
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.description
|
return self.description
|
||||||
|
|
||||||
|
|
||||||
|
class SearchEngine(models.Model):
|
||||||
|
''' A search engine, and all the data needed to use it '''
|
||||||
|
|
||||||
|
name = models.CharField(max_length=256)
|
||||||
|
url = models.URLField()
|
||||||
|
query_pattern = models.CharField(max_length=256) # This field is the
|
||||||
|
# query pattern. It should contain a `{}`, which, when substituted with a
|
||||||
|
# search term (using `.format()`), must yield a URL that can be resolved to
|
||||||
|
# perform the search
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
def search_url(self, search_term):
|
||||||
|
''' Obtain a url to search `search_term` with this search engine '''
|
||||||
|
pattern = str(self.query_pattern)
|
||||||
|
if '{}' not in pattern:
|
||||||
|
raise InvalidData("Search engine {}: bad pattern".format(self))
|
||||||
|
return str(self.query_pattern).format(search_term)
|
||||||
|
|
Loading…
Reference in a new issue