from django.core.management.base import NoArgsCommand
from core.models import Game, User, Group
from uuid import uuid4
from faker import Faker
import random
import warnings


class Command(NoArgsCommand):
    help = "Create a bunch of dummy data."

    @staticmethod
    def _create_user(fake, prefix):
        user_dict = {"email": prefix + '-' + fake.email(),
                     "first_name": fake.first_name(),
                     "last_name": fake.first_name(),
                     "date_joined": fake.date_time(),
                     "birthday": fake.date_time_this_year(),
                     "gender": random.choice(["M", "F", "O"])}
        return User(**user_dict)

    @staticmethod
    def _create_group(fake, users):
        name = fake.company()
        g = Group(name=name)
        g.save()
        g.members = users
        g.save()
        return g

    @staticmethod
    def _create_game(fake, prefix, group):
        game_dict = {"name": ' '.join(fake.sentence().split(' ')[:2]).title(),
                     "description": fake.paragraph(),
                     "image": None,
                     "date_published": fake.date_time_this_year(),
                     "group": group,
                     "event_name": fake.word().capitalize()}
        return Game(**game_dict)

    def handle_noargs(self, **options):
        with warnings.catch_warnings():
            # Fake gives us annoying runtime warnings, supress them
            warnings.simplefilter("ignore")
            fake = Faker()
            prefix = uuid4().hex[:8]

            # Make 100 users
            users = []
            for i in range(100):
                users.append(self._create_user(fake, prefix))
            User.objects.bulk_create(users)
            self.stdout.write('Created 100 Users.')

            # Make 20 group
            all_users = User.objects.all()
            for i in range(20):
                users = random.sample(all_users, random.randint(1, 10))
                self._create_group(fake, users)
            self.stdout.write('Created 20 Groups.')

            # Make 100 games
            games = []
            groups = Group.objects.all()
            for i in range(100):
                group = random.choice(groups)
                games.append(self._create_game(fake, prefix, group))
            Game.objects.bulk_create(games)

            self.stdout.write('Created 100 Games.')

@@ -88,16 +88,25 @@
 r, Group
+, GameTag
 %0Afrom uu
@@ -274,27 +274,29 @@
 od%0A    def _
-cre
+gener
 ate_user(fak
@@ -307,16 +307,54 @@
 refix):%0A
+        %22%22%22Generate a user object.%22%22%22%0A
         
@@ -773,24 +773,72 @@
 ke, users):%0A
+        %22%22%22Generate and save a group object.%22%22%22%0A
         name
@@ -993,41 +993,224 @@
 ef _
-create_game(fake, prefix, group):
+generate_gametag(fake):%0A        %22%22%22Generate a tag object.%22%22%22%0A        return GameTag(value=fake.city())%0A%0A    @staticmethod%0A    def _create_game(fake, prefix, group, tags):%0A        %22%22%22Generate and save a tag object.%22%22%22
 %0A   
@@ -1544,30 +1544,27 @@
 ()%7D%0A        
-return
+g =
  Game(**game
@@ -1565,24 +1565,97 @@
 **game_dict)
+%0A        g.save()%0A        g.tags = tags%0A        g.save()%0A        return g
 %0A%0A    def ha
@@ -2017,27 +2017,29 @@
 ppend(self._
-cre
+gener
 ate_user(fak
@@ -2446,40 +2446,302 @@
 ake 
-100 games%0A            games = %5B%5D
+30 tags%0A            tags = %5B%5D%0A            for i in range(30):%0A                tags.append(self._generate_gametag(fake))%0A            GameTag.objects.bulk_create(tags)%0A            self.stdout.write('Created 30 GameTags.')%0A%0A            # Make 100 games%0A            all_tags = GameTag.objects.all()
 %0A   
@@ -2877,106 +2877,122 @@
     
-games.append(self._create_game(fake, prefix, group))%0A            Game.objects.bulk_create(game
+tags = random.sample(all_tags, random.randint(0, 5))%0A                self._create_game(fake, prefix, group, tag
 s)%0A
-%0A
     

Update command to generate tags.