# Path: competition/models.py
# class Competition(models.Model):
#     name = models.CharField('Name', max_length=255, unique=True)
#     slug = models.SlugField(unique=True)
#     url = models.URLField('Competition URL', blank=True)
#     start_time = models.DateTimeField(blank=True, null=True)
#     end_time = models.DateTimeField(blank=True, null=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     __str__ = __unicode__
# 
#     def get_absolute_url(self):
#         return reverse('view_ctf', kwargs={'ctf_slug': self.slug})
# 
# class Challenge(models.Model):
#     NOT_STARTED = 0
#     IN_PROGRESS = 1
#     SOLVED = 2
# 
#     PROGRESS_CHOICES = (
#         (NOT_STARTED, 'Not Started'),
#         (IN_PROGRESS, 'In Progress'),
#         (SOLVED, 'Solved')
#     )
# 
#     name = models.CharField('Name', max_length=255)
#     slug = models.SlugField()
#     progress = models.PositiveSmallIntegerField(choices=PROGRESS_CHOICES)
#     num_progress = models.FloatField('Progress %', default=0)
#     point_value = models.FloatField(default=0)
# 
#     competition = models.ForeignKey(Competition, related_name='challenges')
#     last_viewed = models.DateTimeField(auto_created=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     __str__ = __unicode__
# 
#     def get_absolute_url(self):
#         return reverse('view_challenge', kwargs={'ctf_slug': self.competition.slug, 'chall_slug': self.slug})
# 
#     def last_viewed_display(self):
#         if self.last_viewed == 0:
#             return 'Never'
#         else:
#             return self.last_viewed
# 
#     class Meta:
#         unique_together = ('name', 'competition')
#         ordering = ('progress',)
# 
# Path: tools/misc.py
# class JSONResponse(HttpResponse):
#     def __init__(self, *args, **kwargs):
#         # Content-Type override
#         if 'content_type' not in kwargs:
#             kwargs['content_type'] = 'application/json'
#         super(JSONResponse, self).__init__(*args, **kwargs)


import json
import datetime as dt
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import resolve
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_GET, require_POST
from django.db.models import Sum
from competition.models import Competition, Challenge
from tools.misc import JSONResponse
    from pytz import UTC
    from tools.misc import UTC
        'online': 4,
        'total': 22
    }

    pv_sum = Sum('point_value')
    points = {
        'earned': solved_challenges.aggregate(pv_sum)['point_value__sum'] or 0.001,
        'in_progress': in_progress_challenges.aggregate(pv_sum)['point_value__sum'] or 0.001,
        'total': challenges.aggregate(pv_sum)['point_value__sum'] or 1
    }

    data = {
        'challenges': challenges_data,
        'start_time': start_time,
        'end_time': end_time,
        'users': users,
        'points': points
    }

    return JSONResponse(json.dumps(data))


@login_required
@csrf_exempt
@require_POST
def track_challenge_visit(request):
    resolved = resolve(request.POST['url'])
    if resolved.view_name.endswith('challenge'):
        try:
            kwargs = resolved.kwargs

# Path: competition/models.py
# class Competition(models.Model):
#     name = models.CharField('Name', max_length=255, unique=True)
#     slug = models.SlugField(unique=True)
#     url = models.URLField('Competition URL', blank=True)
#     start_time = models.DateTimeField(blank=True, null=True)
#     end_time = models.DateTimeField(blank=True, null=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     __str__ = __unicode__
# 
#     def get_absolute_url(self):
#         return reverse('view_ctf', kwargs={'ctf_slug': self.slug})
# 
# class Challenge(models.Model):
#     NOT_STARTED = 0
#     IN_PROGRESS = 1
#     SOLVED = 2
# 
#     PROGRESS_CHOICES = (
#         (NOT_STARTED, 'Not Started'),
#         (IN_PROGRESS, 'In Progress'),
#         (SOLVED, 'Solved')
#     )
# 
#     name = models.CharField('Name', max_length=255)
#     slug = models.SlugField()
#     progress = models.PositiveSmallIntegerField(choices=PROGRESS_CHOICES)
#     num_progress = models.FloatField('Progress %', default=0)
#     point_value = models.FloatField(default=0)
# 
#     competition = models.ForeignKey(Competition, related_name='challenges')
#     last_viewed = models.DateTimeField(auto_created=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     __str__ = __unicode__
# 
#     def get_absolute_url(self):
#         return reverse('view_challenge', kwargs={'ctf_slug': self.competition.slug, 'chall_slug': self.slug})
# 
#     def last_viewed_display(self):
#         if self.last_viewed == 0:
#             return 'Never'
#         else:
#             return self.last_viewed
# 
#     class Meta:
#         unique_together = ('name', 'competition')
#         ordering = ('progress',)
# 
# Path: tools/misc.py
# class JSONResponse(HttpResponse):
#     def __init__(self, *args, **kwargs):
#         # Content-Type override
#         if 'content_type' not in kwargs:
#             kwargs['content_type'] = 'application/json'
#         super(JSONResponse, self).__init__(*args, **kwargs)


# Path: competition/ajax.py
import json
import datetime as dt
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import resolve
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_GET, require_POST
from django.db.models import Sum
from competition.models import Competition, Challenge
from tools.misc import JSONResponse
    from pytz import UTC
    from tools.misc import UTC

        'online': 4,
        'total': 22
    }

    pv_sum = Sum('point_value')
    points = {
        'earned': solved_challenges.aggregate(pv_sum)['point_value__sum'] or 0.001,
        'in_progress': in_progress_challenges.aggregate(pv_sum)['point_value__sum'] or 0.001,
        'total': challenges.aggregate(pv_sum)['point_value__sum'] or 1
    }

    data = {
        'challenges': challenges_data,
        'start_time': start_time,
        'end_time': end_time,
        'users': users,
        'points': points
    }

    return JSONResponse(json.dumps(data))


@login_required
@csrf_exempt
@require_POST
def track_challenge_visit(request):
    resolved = resolve(request.POST['url'])
    if resolved.view_name.endswith('challenge'):
        try:
            kwargs = resolved.kwargs

            challenge = Challenge.objects.get(competition__slug=kwargs['ctf_slug'], slug=kwargs['chall_slug'])