Skip to content

Commit

Permalink
Merge pull request #55 from eddieferrer/enhanced_sanitize_script
Browse files Browse the repository at this point in the history
Improved db sanitation script
  • Loading branch information
eddieferrer authored Aug 10, 2020
2 parents e4d2c52 + 2b52fb7 commit 8177209
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions backend/scripts/sanitize_dev_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from backend.models import Item, User_Item, User

import urllib.request
import random
import random, string

# Get a random list of names
word_url = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
Expand All @@ -15,6 +15,10 @@
name_words = [word for word in upper_words if not word.isupper() and len(word) > 3 and "'" not in word]
username_words = [word for word in lower_words if len(word) > 5 and "'" not in word]

def randomword(length):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))

def sanitize_dev_database():
# Trim size of database to 1500 uses
users = User.query.all()
Expand All @@ -26,26 +30,29 @@ def sanitize_dev_database():
for user_item in all_user_items:
if user_item.user_id > 1500:
db.session.delete(user_item)
else:
user_item.comments = ' '.join(random.sample(words, random.randint(4,140)))

# Randomize user name, username, emails and provider_id
# Randomize user name, username, emails, password hash, tokens, and provider_id
users = User.query.all()
for user in users:
mailSuffixes = ['@gmail.com', '@outlook.com', '@hotmail.com', '@mail.com']
first_name = name_words[random.randint(0, len(name_words))-1]
last_name = name_words[random.randint(0, len(name_words))-1]
rand_email = str.lower(last_name) + str(random.randint(0,100)) + mailSuffixes[random.randint(0, len(mailSuffixes))-1]
username = username_words[random.randint(0, len(username_words))-1] + str(random.randint(0,100));

user.username = username;
user.email = rand_email;
user.name = ' '.join([first_name, last_name]);
user.token = ''.join([randomword(5), str(random.randint(0,10000000000000000))]);
user.password_hash = ''.join([randomword(30), str(random.randint(0,10000000000000000))]);

if 'sizesquirrel' in user.provider_id:
user.provider_id = 'sizesquirrel$' + rand_email;
user.email = rand_email;
user.name = ' '.join([first_name, last_name]);

if 'facebook' in user.provider_id:
user.provider_id = 'facebook$' + str(random.randint(0,10000000000000000));
user.email = rand_email;
user.name = ' '.join([first_name, last_name]);

db.session.commit();
print('Database sanitized')
Expand Down
Binary file modified sample_dev-database.db
Binary file not shown.

0 comments on commit 8177209

Please sign in to comment.