Url generation method, for more genericity

This commit is contained in:
Rémi Oudin 2018-02-21 11:37:44 +01:00
parent 5539f57139
commit 8f4458b009

View file

@ -30,6 +30,11 @@ class Keyword(models.Model):
def __str__(self):
return self.text
def generate_url(self, user):
""" Generates the url for a keyword, based on the user search engine.
"""
return user.search_engine.search_url(self)
class Webpage(models.Model):
''' A webpage url '''
@ -50,6 +55,22 @@ class Website(models.Model):
def __str__(self):
return self.name
def generate_url(self, user):
""" Generates the url in case the interest chosen is a website.
"""
rand = random.random()
if user.uses_url:
url = self.url
elif rand <= 0.1:
url = random.choice(self.notable_pages).url
elif rand <= 0.8:
search_term_text = self.name + " " + \
random.choice(self.keywords)
url = user.search_engine.search_url(search_term_text)
else:
url = user.search_engine.search_url(self.name)
return url
class Place(models.Model):
''' A real-life place '''
@ -62,6 +83,16 @@ class Place(models.Model):
def __str__(self):
return self.name
def generate_url(self, user):
""" Generates the url for a place.
"""
rand = random.random()
if rand < 1/2:
url = user.search_engine.search_url(self.name)
else:
url = user.search_engine.search_url(self.address)
return url
class Event(models.Model):
''' A real-life event (protests, meeting, ...) '''
@ -73,6 +104,16 @@ class Event(models.Model):
def __str__(self):
return self.name
def generate_url(self, user):
""" generate the url for an event object.
"""
possibilities = random.sample(
[self.name, self.date, self.place],
3
)
return user.search_engine.search_url(" ".join(possibilities))
class BrowserFingerprint(models.Model):
''' A browser fingerprint, containing things like a user agent '''