Created
July 15, 2013 06:05
-
-
Save arnab/5997795 to your computer and use it in GitHub Desktop.
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
# python manage.py shell | |
from polls.models import Poll, Choice | |
p = Poll.objects.all()[0] | |
import datettime | |
import random | |
def pick_random(choices): | |
rand_index = random.randint(0, len(choices) - 1) | |
return str(choices[rand_index]) | |
def pad_zero_if_needed(string): | |
return int(string) < 10 and '0{0}'.format(string) or string | |
def generate_date(): | |
year = pick_random(range(2007, 2014)) | |
month = pad_zero_if_needed(pick_random(range(1, 12))) | |
day = pad_zero_if_needed(pick_random(range(1, 31))) | |
hour = pad_zero_if_needed(pick_random(range(0, 23))) | |
minute = pad_zero_if_needed(pick_random(range(0, 59))) | |
date_str = '-'.join([year, month, day]) + ' ' + ':'.join([hour, minute]) + ' UTC' | |
try: | |
date = datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M %Z") | |
except ValueError, e: | |
date = datetime.datetime.now() | |
finally: | |
return date | |
for i in range(1, 201): | |
question = 'Question {0}'.format(i) | |
date = generate_date() | |
Poll.objects.create(question=question, pub_date=date) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment