From 114c8a3d3eedd0871e472cbcc7663419d759450e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Wed, 24 Jan 2018 13:50:16 +0100 Subject: [PATCH] Add model for search engines --- profiles/models.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/profiles/models.py b/profiles/models.py index c75de45..22091dd 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -1,6 +1,18 @@ 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): ''' A browser fingerprint, containing things like a user agent ''' @@ -22,3 +34,24 @@ class BrowserFingerprint(models.Model): def __str__(self): 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)