Created
November 10, 2019 13:15
-
-
Save d-demirci/1503f905e9561ca7b4e68778b6c36079 to your computer and use it in GitHub Desktop.
django generate random users
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| __author__ = '[email protected]' | |
| #edited by d3n1z to conform with python3 and django >2 | |
| """ | |
| Generates random users, profiles and game entries for testing. | |
| """ | |
| import string | |
| import datetime | |
| import os | |
| import sys | |
| import random | |
| # django environment initialization - replace MYAPP w/ your application identifier | |
| if __name__ == "__main__": | |
| # sys.path.append('/home/n00b/django-adminlte2/') | |
| os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_adminlte3.settings") | |
| import django | |
| django.setup() | |
| from django.contrib.auth.models import User | |
| def get_random_string(length, stringset=string.ascii_letters): | |
| return ''.join(random.choices(string.ascii_uppercase + string.digits, k=length)) | |
| #return ''.join([stringset[i % len(stringset)] for i in [ord(str((x%127))) for x in os.urandom(length)]]) | |
| def generate_users(n): | |
| # print "Generating %s user(s)..." % n | |
| # print "%s\t%s\t%s\t%s\t%s\t%s\t%s" % ("#", "username", "firstname", "lastname", "title", "company", "work_email") | |
| for user_index in range(n): | |
| # create user | |
| new_user = User.objects.create( | |
| username=get_random_string(16), | |
| first_name=get_random_string(16), | |
| last_name=get_random_string(16), | |
| ) | |
| new_user.save() | |
| # create profile for user | |
| # p = Profile.objects.create(user=new_user) | |
| # p.company = get_random_string(16) | |
| # p.title = get_random_string(16) | |
| # p.work_email = "%[email protected]" % get_random_string(5) | |
| # p.save() | |
| # t = "#%s\t%s\t%s\t%s\t%s\t%s\t%s" | |
| # print t % ( | |
| # user_index+1, | |
| # new_user.username, new_user.first_name, new_user.last_name, | |
| # p.title, p.company, p.work_email | |
| # ) | |
| # # for instance, in a game environment, there can be random entries as well | |
| # for entry_index in xrange(random.randint(1, 100)): | |
| # sys.stdout.write('.') | |
| # sys.stdout.flush() | |
| # g = GameEntry.objects.create(user=new_user) | |
| # g.entry_type = "some_type" | |
| # g.which = "source" | |
| # g.when = datetime.datetime.now() | |
| # print "" | |
| def main(argv): | |
| if argv.__len__() < 2: | |
| print( "Usage: %s <count>" % argv[0]) | |
| sys.exit(1) | |
| if not argv[1].isdigit(): | |
| print( "Invalid argument: ""%s"" " % argv[1]) | |
| print( "Usage: %s <count>" % argv[0]) | |
| sys.exit(1) | |
| generate_users(int(argv[1])) | |
| if __name__ == "__main__": | |
| main(sys.argv[0:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment