from django.core.management.base import BaseCommand
from django.db import connection
from noveltorpedo.models import *
from datetime import datetime
import pytz
import random

# Truncate the tables.
cursor = connection.cursor()
cursor.execute('TRUNCATE TABLE noveltorpedo_author CASCADE')
cursor.execute('TRUNCATE TABLE noveltorpedo_story CASCADE')

# Seed data.
first_names = ['Noah', 'Emma', 'Liam', 'Olivia', 'Mason', 'Sophia']
last_names = ['Jones', 'Taylor', 'Williams', 'Brown']

nouns = ['dog', 'cat', 'human', 'robot']
verbs = ['killed', 'jumped', 'exploded', 'devoured']
adverbs = ['majestically', 'ruthlessly', 'relentlessly']


def random_name():
    return random.choice(first_names) + ' ' + random.choice(last_names)


def random_date():
    tz = pytz.timezone('US/Pacific-New')
    year = random.randint(1999, 2009)
    month = random.randint(1, 12)
    day = random.randint(1, 28)
    hour = random.randint(0, 23)
    minute = random.randint(0, 59)
    second = random.randint(0, 59)
    return tz.localize(datetime(year, month, day, hour, minute, second))


def random_sentence():
    return (
        'The ' + random.choice(nouns) + ' ' + random.choice(verbs) + ' ' +
        random.choice(adverbs) + '.'
    )


def random_paragraph():
    paragraph = ''
    for _ in range(random.randint(5, 10)):
        paragraph += random_sentence() + ' '
    return paragraph


def random_chapter():
    chapter = ''
    for _ in range(random.randint(1, 10)):
        chapter += random_paragraph()
    return chapter


class Command(BaseCommand):

    def handle(self, *args, **options):
        # Create 20 random authors.
        authors = []
        for _ in range(20):
            author = Author()
            author.name = random_name()
            author.save()
            authors.append(author)

        # Create 20 random stories.
        for _ in range(20):
            story = Story()
            story.title = random_sentence()
            story.save()

            # Add 1 to 3 random authors.
            for __ in range(random.randint(1, 3)):
                story.authors.add(random.choice(authors))

            # Add 1 to 5 random segments.
            for __ in range(random.randint(1, 5)):
                segment = StorySegment()
                segment.story = story
                segment.contents = random_chapter()
                segment.published = random_date()
                segment.save()

@@ -173,182 +173,8 @@
 om%0A%0A
-# Truncate the tables.%0Acursor = connection.cursor()%0Acursor.execute('TRUNCATE TABLE noveltorpedo_author CASCADE')%0Acursor.execute('TRUNCATE TABLE noveltorpedo_story CASCADE')%0A%0A
 # Se
@@ -1413,16 +1413,222 @@
 tions):%0A
+        # Truncate the tables.%0A        cursor = connection.cursor()%0A        cursor.execute('TRUNCATE TABLE noveltorpedo_author CASCADE')%0A        cursor.execute('TRUNCATE TABLE noveltorpedo_story CASCADE')%0A%0A
         

Move table truncation to handle method