# Path: accounts/models.py
# class UserProfile(models.Model, StatsFromChallengesMixin):
#     objects = UserProfileQuerySet.as_manager()
# 
#     team = models.ForeignKey('accounts.Team', null=True, blank=True, on_delete=models.CASCADE)
#     user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
#     created_at = models.DateTimeField(auto_now_add=True)
#     image = models.FileField(upload_to='accounts/', default='accounts/user.png')
#     job = models.CharField(max_length=255)
#     gender = models.CharField(max_length=1, choices=(('M', 'Male'), ('F','Female')))
#     website = models.CharField(max_length=255, null=True, blank=True)
#     country = models.ForeignKey(Country, on_delete=models.CASCADE)
#     skills = models.TextField(null=True, blank=True)
# 
#     @property
#     def skill_list(self):
#         return self.skills.split(SKILLS_SEPARATOR) if self.skills else []
# 
#     def __str__(self):
#         return '{}, Team: {}'.format(self.user.username, self.team)
# 
#     @property
#     def failed_challenges(self):
#         return self._all_failed_challenges.exclude(pk__in=self.solved_challenges.all())
# 
# class Team(models.Model, StatsFromChallengesMixin):
#     objects = TeamQuerySet.as_manager()
# 
#     name = models.CharField(max_length=256, unique=True)
#     users = models.ManyToManyField(User, through='accounts.userprofile')
#     created_at = models.DateTimeField(auto_now_add=True, editable=False)
#     created_by = models.ForeignKey(User, related_name='created_team', on_delete=models.CASCADE)
# 
#     @property
#     def num_users(self):
#         return self.users.count()
# 
#     @property
#     def challengesolved_set(self):
#         from challenges.models import ChallengeSolved
#         return ChallengeSolved.objects.filter(user__team=self)
# 
#     @property
#     def solved_challenges(self):
#         from challenges.models import Challenge
#         return Challenge.objects.filter(solved_by__team=self).distinct()
# 
#     @property
#     def failed_challenges(self):
#         from challenges.models import Challenge
#         return Challenge.objects\
#             .filter(failed_by__team=self)\
#             .distinct()\
#             .exclude(pk__in=self.solved_challenges.all())
# 
#     def __str__(self):
#         return self.name
# 
# SKILLS_SEPARATOR = ','
# 
# Path: accounts/fixtures/autofixtures_data.py

# 
# Path: challenges/models.py
# class Challenge(models.Model):
#     objects = ChallengeQuerySet.as_manager()
# 
#     name = models.CharField(max_length=256)
#     category = models.ForeignKey('challenges.Category', related_name='challenges', on_delete=models.CASCADE)
#     description = models.TextField()
#     key = models.CharField(max_length=256)
#     points = models.IntegerField()
#     difficulty = models.CharField(max_length=1, choices=(
#         ('E', 'Easy'),
#         ('M', 'Medium'),
#         ('H', 'Hard')
#     ))
# 
#     solved_by = models.ManyToManyField(RelatedUserModel,
#                                        related_name='solved_challenges',
#                                        through='challenges.ChallengeSolved')
# 
#     failed_by = models.ManyToManyField(RelatedUserModel,
#                                        related_name='_all_failed_challenges',
#                                        through='challenges.ChallengeFail')
# 
#     @property
#     def newest_solved(self):
#         return self.challengesolved_set.order_by('-datetime')
# 
#     def __str__(self):
#         return self.name
# 
# class ChallengeSolved(models.Model):
#     user = models.ForeignKey(RelatedUserModel, on_delete=models.CASCADE)
#     challenge = models.ForeignKey('challenges.Challenge', on_delete=models.CASCADE)
#     datetime = models.DateTimeField(auto_now_add=True)
# 
#     def __str__(self):
#         return 'Challenge: {}, User: {}'.format(self.challenge.name, self.user)


import os
import random
import requests
from datetime import timedelta, datetime
from io import BytesIO
from itertools import repeat, cycle
from autofixture import generators, register, AutoFixture
from django.contrib.auth import get_user_model
from django.utils.timezone import now
from accounts.models import UserProfile, Team, SKILLS_SEPARATOR
from django.contrib.auth.hashers import make_password
from accounts.fixtures.autofixtures_data import user_first_names, user_last_names, team_names, user_usernames, user_profiles_skills
from challenges.models import Challenge, ChallengeSolved
            start = instance.created_at
            solved.datetime = start + (now() - start) * random.random()
            solved.save()


        # random image
        """
        gender = 'men' if instance.gender == 'M' else 'women'
        image_url = 'https://randomuser.me/api/portraits/{}/{}.jpg'.format(gender, instance.id)
        try:
            response = requests.get(image_url)
        except requests.HTTPError as e:
            print(e)
            return instance

        instance.image.save(
            os.path.basename('image_{}.jpg'.format(instance.id)),
            BytesIO(response.content)
        )
        """
        instance.image = 'accounts/' + ('image_{}.jpg'.format(instance.id) if instance.id < 50 else 'user.png')

        if commit:
            instance.save()

        return instance


register(get_user_model(), UserAutoFixture)
register(UserProfile, UserProfileAutoFixture)

# Path: accounts/models.py
# class UserProfile(models.Model, StatsFromChallengesMixin):
#     objects = UserProfileQuerySet.as_manager()
# 
#     team = models.ForeignKey('accounts.Team', null=True, blank=True, on_delete=models.CASCADE)
#     user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)
#     created_at = models.DateTimeField(auto_now_add=True)
#     image = models.FileField(upload_to='accounts/', default='accounts/user.png')
#     job = models.CharField(max_length=255)
#     gender = models.CharField(max_length=1, choices=(('M', 'Male'), ('F','Female')))
#     website = models.CharField(max_length=255, null=True, blank=True)
#     country = models.ForeignKey(Country, on_delete=models.CASCADE)
#     skills = models.TextField(null=True, blank=True)
# 
#     @property
#     def skill_list(self):
#         return self.skills.split(SKILLS_SEPARATOR) if self.skills else []
# 
#     def __str__(self):
#         return '{}, Team: {}'.format(self.user.username, self.team)
# 
#     @property
#     def failed_challenges(self):
#         return self._all_failed_challenges.exclude(pk__in=self.solved_challenges.all())
# 
# class Team(models.Model, StatsFromChallengesMixin):
#     objects = TeamQuerySet.as_manager()
# 
#     name = models.CharField(max_length=256, unique=True)
#     users = models.ManyToManyField(User, through='accounts.userprofile')
#     created_at = models.DateTimeField(auto_now_add=True, editable=False)
#     created_by = models.ForeignKey(User, related_name='created_team', on_delete=models.CASCADE)
# 
#     @property
#     def num_users(self):
#         return self.users.count()
# 
#     @property
#     def challengesolved_set(self):
#         from challenges.models import ChallengeSolved
#         return ChallengeSolved.objects.filter(user__team=self)
# 
#     @property
#     def solved_challenges(self):
#         from challenges.models import Challenge
#         return Challenge.objects.filter(solved_by__team=self).distinct()
# 
#     @property
#     def failed_challenges(self):
#         from challenges.models import Challenge
#         return Challenge.objects\
#             .filter(failed_by__team=self)\
#             .distinct()\
#             .exclude(pk__in=self.solved_challenges.all())
# 
#     def __str__(self):
#         return self.name
# 
# SKILLS_SEPARATOR = ','
# 
# Path: accounts/fixtures/autofixtures_data.py

# 
# Path: challenges/models.py
# class Challenge(models.Model):
#     objects = ChallengeQuerySet.as_manager()
# 
#     name = models.CharField(max_length=256)
#     category = models.ForeignKey('challenges.Category', related_name='challenges', on_delete=models.CASCADE)
#     description = models.TextField()
#     key = models.CharField(max_length=256)
#     points = models.IntegerField()
#     difficulty = models.CharField(max_length=1, choices=(
#         ('E', 'Easy'),
#         ('M', 'Medium'),
#         ('H', 'Hard')
#     ))
# 
#     solved_by = models.ManyToManyField(RelatedUserModel,
#                                        related_name='solved_challenges',
#                                        through='challenges.ChallengeSolved')
# 
#     failed_by = models.ManyToManyField(RelatedUserModel,
#                                        related_name='_all_failed_challenges',
#                                        through='challenges.ChallengeFail')
# 
#     @property
#     def newest_solved(self):
#         return self.challengesolved_set.order_by('-datetime')
# 
#     def __str__(self):
#         return self.name
# 
# class ChallengeSolved(models.Model):
#     user = models.ForeignKey(RelatedUserModel, on_delete=models.CASCADE)
#     challenge = models.ForeignKey('challenges.Challenge', on_delete=models.CASCADE)
#     datetime = models.DateTimeField(auto_now_add=True)
# 
#     def __str__(self):
#         return 'Challenge: {}, User: {}'.format(self.challenge.name, self.user)


# Path: accounts/fixtures/autofixtures.py
import os
import random
import requests
from datetime import timedelta, datetime
from io import BytesIO
from itertools import repeat, cycle
from autofixture import generators, register, AutoFixture
from django.contrib.auth import get_user_model
from django.utils.timezone import now
from accounts.models import UserProfile, Team, SKILLS_SEPARATOR
from django.contrib.auth.hashers import make_password
from accounts.fixtures.autofixtures_data import user_first_names, user_last_names, team_names, user_usernames, user_profiles_skills
from challenges.models import Challenge, ChallengeSolved

            start = instance.created_at
            solved.datetime = start + (now() - start) * random.random()
            solved.save()


        # random image
        """
        gender = 'men' if instance.gender == 'M' else 'women'
        image_url = 'https://randomuser.me/api/portraits/{}/{}.jpg'.format(gender, instance.id)
        try:
            response = requests.get(image_url)
        except requests.HTTPError as e:
            print(e)
            return instance

        instance.image.save(
            os.path.basename('image_{}.jpg'.format(instance.id)),
            BytesIO(response.content)
        )
        """
        instance.image = 'accounts/' + ('image_{}.jpg'.format(instance.id) if instance.id < 50 else 'user.png')

        if commit:
            instance.save()

        return instance


register(get_user_model(), UserAutoFixture)
register(UserProfile, UserProfileAutoFixture)

register(Team, TeamAutoFixture)