Fix search engine URL generation

This commit is contained in:
Théophile Bastian 2018-02-26 10:19:51 +01:00
parent 45ddbff91a
commit 3e5fc2f9b3

View file

@ -185,8 +185,8 @@ class SearchEngine(models.Model):
url = models.URLField() url = models.URLField()
query_pattern = models.CharField(max_length=256) # This field is the query_pattern = models.CharField(max_length=256) # This field is the
# query pattern. It should contain a `{}`, which, when substituted with a # query pattern. It should contain a `{}`, which, when substituted with a
# search term (using `.format()`), must yield a URL that can be resolved to # search term (using `.format()`), must yield a URL tail that can be
# perform the search # concatenated with `url` to perform a search (eg. `?q={}` for ddg).
def __str__(self): def __str__(self):
return self.name return self.name
@ -194,9 +194,10 @@ class SearchEngine(models.Model):
def search_url(self, search_term): def search_url(self, search_term):
''' Obtain a url to search `search_term` with this search engine ''' ''' Obtain a url to search `search_term` with this search engine '''
pattern = str(self.query_pattern) pattern = str(self.query_pattern)
search_term = str(search_term).replace(' ', '+')
if '{}' not in pattern: if '{}' not in pattern:
raise InvalidData("Search engine {}: bad pattern".format(self)) raise InvalidData("Search engine {}: bad pattern".format(self))
return str(self.query_pattern).format(search_term) return self.url + (str(self.query_pattern).format(search_term))
class Interest(models.Model): class Interest(models.Model):