def set_game_duration(delta):
    from datetime import timedelta
    duration = timedelta(
        days=config.GAME_DURATION_DAYS,
        seconds=config.GAME_DURATION_HOURS * 3600 + config.GAME_DURATION_MINS * 60
    )
    new_duration = duration - delta
    config.GAME_DURATION_DAYS = new_duration.days
    config.GAME_DURATION_HOURS = 0
    config.GAME_DURATION_MINS = new_duration.seconds / 60 def send_pause_message():
    consumers.send_message(dict(event='GAME_PAUSE')) def send_start_message():
    consumers.send_message(dict(event='GAME_START')) def send_resume_message():
    consumers.send_message(dict(event='GAME_RESUME')) def send_end_message():
    consumers.send_message(dict(event='GAME_END')) def send_reset_message():
    consumers.send_message(dict(event='GAME_RESET')) 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 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 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) class ChallengeFail(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 json
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import user_passes_test
from django.db.models import Count
from django.shortcuts import render, redirect
from datetime import timedelta
from django.utils import timezone
from SCTF.utils import set_game_duration, send_pause_message, send_start_message, send_resume_message, send_end_message, send_reset_message
from accounts.models import Team, UserProfile
from challenges.models import Challenge, ChallengeSolved, ChallengeFail
from cities_light.models import Country
from constance import config
    from django.contrib.auth import get_user_model

def index(request):
    countries = Country.objects\
        .annotate(num_user=Count('userprofile'))\
        .filter(num_user__gt=0)
    parameters = {
        'users_count': get_user_model().objects.count(),

        'teams_count': Team.objects.count(),