diff --git a/data/.gitignore b/data/.gitignore new file mode 100644 index 0000000..9886019 --- /dev/null +++ b/data/.gitignore @@ -0,0 +1 @@ +nicknames_dict diff --git a/profiles/models.py b/profiles/models.py index ab44199..7c3190a 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -12,12 +12,36 @@ from django.db import models BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -NICKNAMES = open("/usr/share/dict/american-english").read().splitlines() +NICKNAMES = None LASTNAMES = open(BASE_DIR + "/data/lastnames.txt").read().splitlines() FIRSTNAMES = open(BASE_DIR + "/data/firstnames.txt").read().splitlines() EMAIL_DOMAINS = open(BASE_DIR + "/data/email_domains.txt").read().splitlines() +def require_nicknames(fct): + def read_file(path): + global NICKNAMES + print("Trying {}".format(path)) + with open(path, 'r') as handle: + NICKNAMES = handle.read().splitlines() + + nicknames_files = [ + os.path.join(BASE_DIR, 'data/nicknames_dict'), + "/usr/share/dict/american-english", + ] + if NICKNAMES is None: + for nick_file in nicknames_files: + try: + read_file(nick_file) + break + except FileNotFoundError: + pass + if NICKNAMES is None: + raise FileNotFoundError + + return fct + + class InvalidData(Exception): ''' Thrown when the DB contains invalid data, and cannot perform something ''' @@ -72,7 +96,7 @@ class Website(models.Model): elif rand <= 0.1: url = random.choice(self.notable_pages).url elif rand <= 0.8: - search_term_text = self.name + " " + \ + search_term_text = self.name + " " + \ random.choice(self.keywords) url = user.search_engine.search_url(search_term_text) else: @@ -122,7 +146,6 @@ class Event(models.Model): return user.search_engine.search_url(" ".join(possibilities)) - class BrowserFingerprint(models.Model): ''' A browser fingerprint, containing things like a user agent ''' @@ -147,11 +170,11 @@ class BrowserFingerprint(models.Model): def serialize_headers(self): return { - "Description" : str(self.description), - "User-Agent" : str(self.useragent), - "Accept-Encoding" : str(self.accept_encoding), - "Accept" : str(self.accept_default), - "Accept-Language" : str(self.accept_lang), + "Description": str(self.description), + "User-Agent": str(self.useragent), + "Accept-Encoding": str(self.accept_encoding), + "Accept": str(self.accept_default), + "Accept-Language": str(self.accept_lang), } @@ -214,11 +237,13 @@ def generate_email(nick, first_name, last_name): if random.random() < 0.3: email = first_name + "." + last_name + "@" + domain else: - email = nick + "@" + domain + email = nick + "@" + domain return email + +@require_nicknames def create_profile(nick=None): - nick = "".join(random.sample(NICKNAMES, random.randrange(2,5))) + nick = "".join(random.sample(NICKNAMES, random.randrange(2, 5))) first_name = random.choice(FIRSTNAMES) last_name = random.choice(LASTNAMES) email = generate_email(nick, first_name, last_name) @@ -231,3 +256,7 @@ def create_profile(nick=None): ) profile.search_engine = random.choice(SearchEngine.objects.all()) profile.browser_fingerprint = random.choice(BrowserFingerprint.objects.all()) + + profile.full_clean() + profile.save() + return profile