From 7c13ee17d4765f56bc6a1c1525795a35639032f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Oudin?= Date: Mon, 19 Feb 2018 22:56:16 +0100 Subject: [PATCH] Skeleton of history generation --- histories/models.py | 57 +++++++++++++++++++++++++++++++++++++++---- pinocchio/settings.py | 3 +++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/histories/models.py b/histories/models.py index e5f5fe3..df542f3 100644 --- a/histories/models.py +++ b/histories/models.py @@ -3,13 +3,16 @@ entries, which looks like human-based browsing, according to a dedicated user interests, keywords... """ +from random import weibullvariate +from math import floor from django.db import models -from profiles.models import BrowserFingerprint +from profiles.models import Profile +from pinocchio.settings import HISTORY_MIN class HistoryEntry(models.Model): """ A history entry, aka a url, and a timestamp. """ - search = models.CharField(max_length=255) + search = models.URLField(help_text="The url to be searched") timestamp = models.DateTimeField() history = models.ForeignKey( 'History', @@ -17,6 +20,8 @@ class HistoryEntry(models.Model): ) def __str__(self): + """ Returns the string representation of a history entry. + """ return "{} : {}".format(self.timestamp, self.search) @@ -24,14 +29,56 @@ class History(models.Model): """ A history for a user, containing some web connections (http, https). Each history is timed, in a human-behaviour manner. """ - start_ts = models.DateTimeField() + start_ts = models.DateTimeField( + help_text='The starting timestamp of the history. Useful for cron-like ' + 'structure.' + + ) played = models.BooleanField(default=False) - user_agent = models.ForeignKey( - BrowserFingerprint, + user = models.ForeignKey( + Profile, on_delete=models.CASCADE ) + def return_history(self): + """ Returns the history, sorted by increasing timestamps + """ + history_set = self.history_set.order_by('timestamp') + return history_set + def __str__(self): + """ Returns the string representation of a history. + """ history_set = self.history_set.order_by('timestamp') header = "[History]:\n" return header + "\n".join(history_set) + + + 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 + ) diff --git a/pinocchio/settings.py b/pinocchio/settings.py index ed9a7f3..4c24915 100644 --- a/pinocchio/settings.py +++ b/pinocchio/settings.py @@ -13,6 +13,9 @@ https://docs.djangoproject.com/en/2.0/ref/settings/ import os from .settings_local import BASE_DIR, DEBUG, SECRET_KEY, DATABASES + +HISTORY_MIN = 25 + ALLOWED_HOSTS = []