2018-01-24 16:07:33 +01:00
|
|
|
""" Models for the history. This history should be able to generate history
|
|
|
|
entries, which looks like human-based browsing, according to a dedicated user
|
|
|
|
interests, keywords...
|
|
|
|
"""
|
|
|
|
|
2018-02-19 22:56:16 +01:00
|
|
|
from random import weibullvariate
|
|
|
|
from math import floor
|
2018-01-23 18:12:47 +01:00
|
|
|
from django.db import models
|
2018-02-19 22:56:16 +01:00
|
|
|
from profiles.models import Profile
|
|
|
|
from pinocchio.settings import HISTORY_MIN
|
2018-01-24 16:07:33 +01:00
|
|
|
|
|
|
|
class HistoryEntry(models.Model):
|
|
|
|
""" A history entry, aka a url, and a timestamp.
|
|
|
|
"""
|
2018-02-19 22:56:16 +01:00
|
|
|
search = models.URLField(help_text="The url to be searched")
|
2018-01-24 16:07:33 +01:00
|
|
|
timestamp = models.DateTimeField()
|
|
|
|
history = models.ForeignKey(
|
|
|
|
'History',
|
|
|
|
on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
2018-02-19 22:56:16 +01:00
|
|
|
""" Returns the string representation of a history entry.
|
|
|
|
"""
|
2018-01-24 16:07:33 +01:00
|
|
|
return "{} : {}".format(self.timestamp, self.search)
|
|
|
|
|
|
|
|
|
|
|
|
class History(models.Model):
|
|
|
|
""" A history for a user, containing some web connections (http, https).
|
|
|
|
Each history is timed, in a human-behaviour manner. """
|
|
|
|
|
2018-02-19 22:56:16 +01:00
|
|
|
start_ts = models.DateTimeField(
|
|
|
|
help_text='The starting timestamp of the history. Useful for cron-like '
|
|
|
|
'structure.'
|
|
|
|
|
|
|
|
)
|
2018-01-24 16:07:33 +01:00
|
|
|
played = models.BooleanField(default=False)
|
2018-02-19 22:56:16 +01:00
|
|
|
user = models.ForeignKey(
|
|
|
|
Profile,
|
2018-01-24 16:07:33 +01:00
|
|
|
on_delete=models.CASCADE
|
|
|
|
)
|
2018-01-23 18:12:47 +01:00
|
|
|
|
2018-02-19 22:56:16 +01:00
|
|
|
def return_history(self):
|
|
|
|
""" Returns the history, sorted by increasing timestamps
|
|
|
|
"""
|
|
|
|
history_set = self.history_set.order_by('timestamp')
|
|
|
|
return history_set
|
|
|
|
|
2018-01-24 16:07:33 +01:00
|
|
|
def __str__(self):
|
2018-02-19 22:56:16 +01:00
|
|
|
""" Returns the string representation of a history.
|
|
|
|
"""
|
2018-01-24 16:07:33 +01:00
|
|
|
history_set = self.history_set.order_by('timestamp')
|
2018-02-19 13:58:45 +01:00
|
|
|
header = "[History]:\n"
|
|
|
|
return header + "\n".join(history_set)
|
2018-02-19 22:56:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
def play_history(self):
|
|
|
|
""" Actually plays the history.
|
|
|
|
"""
|
|
|
|
self.played = True
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_history(user, ts_start):
|
|
|
|
""" Generate a new history for the user `user`, starting from timestamp
|
|
|
|
`ts_start`.
|
|
|
|
A few heuristics are used in order to give the impression that the history
|
|
|
|
is actually played by a user.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# let's defin a new history object.
|
|
|
|
history = History(start_ts=ts_start, user=user)
|
|
|
|
length = HISTORY_MIN + floor(10 * weibullvariate(1, 1.5))
|
|
|
|
|
|
|
|
history_line = 0
|
|
|
|
|
|
|
|
while history_line < length:
|
|
|
|
ts_start += random.uniform(1, 10)
|
|
|
|
new_line = HistoryEntry(
|
|
|
|
search="https://google.com:",
|
|
|
|
timestamp=ts_start,
|
|
|
|
history=history
|
|
|
|
)
|