# Path: harnas/checker/models.py
# class TestEnvironment(models.Model):
#     template_name = models.CharField(max_length=250)
#     summary = models.CharField(max_length=250)
#     description = models.TextField()
#     maintainer = models.ForeignKey(User)
# 
#     class Meta:
#         permissions = (
#             ('view_test_environment', 'Can view test environment'),
#             ('edit_test_environment', 'Can edit test environment'))
# 
#     def __str__(self):
#         return self.template_name
# 
# Path: harnas/checker/utils.py
# def get_templates_list():
#     templates = cache.get('templates_list')
#     if templates is None:
#         templates = Template.list(settings.HERA_AUTH)
#         cache.set('templates_list', templates, 60 * 60 * 24)
#     return templates
# 
# Path: harnas/contest/models.py
# class Task(models.Model):
#     name = models.CharField(max_length=250)
#     short_name = models.CharField(max_length=3)
#     description = models.TextField()
#     test_environment = models.ForeignKey('checker.TestEnvironment')
#     author = models.ForeignKey(User)
#     parent = models.ForeignKey('self', null=True, default=None)
#     contest = models.ForeignKey(Contest, null=True, default=None)
#     max_solution_size = models.PositiveIntegerField()
# 
#     # Default timestamps values for task. Can be overridden individually in
#     # groups. See GroupsTaskDetails.
#     open = models.DateTimeField(null=True, default=None)
#     deadline = models.DateTimeField(null=True, default=None)
#     close = models.DateTimeField(null=True, default=None)
# 
#     class Meta:
#         permissions = (
#             ('view_task', 'Can view task'),
#             ('submit_solution', 'Can submit solution'),
#             ('edit_task', 'Can edit task'))
# 
#     def __str__(self):
#         return self.name
# 
#     @property
#     def display_name(self):
#         if self.contest is not None and self.contest.name.strip() != "":
#             return "%s (%s)" % (self.name, self.contest.name)
#         else:
#             return self.name


from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _
from harnas.checker.models import TestEnvironment
from harnas.checker.utils import get_templates_list
from harnas.contest.models import Task


class TestEnvironmentForm(ModelForm):

    template_name = forms.ChoiceField(
        choices=lambda: [(template.name, template.name)
                         for template in get_templates_list()])
    maintainer = forms.ModelChoiceField(
        queryset=User.objects.exclude(username='AnonymousUser'),
        empty_label=None)

    class Meta:
        model = TestEnvironment
        fields = ['summary', 'description']


class SubmitForm(forms.Form):
    solution = forms.FileField(label="Upload your solution")
    task = forms.ModelChoiceField(queryset=None, empty_label=None)

    def __init__(self, *args, **kwargs):
        contest = kwargs.pop('contest')
        super(SubmitForm, self).__init__(*args, **kwargs)

# Path: harnas/checker/models.py
# class TestEnvironment(models.Model):
#     template_name = models.CharField(max_length=250)
#     summary = models.CharField(max_length=250)
#     description = models.TextField()
#     maintainer = models.ForeignKey(User)
# 
#     class Meta:
#         permissions = (
#             ('view_test_environment', 'Can view test environment'),
#             ('edit_test_environment', 'Can edit test environment'))
# 
#     def __str__(self):
#         return self.template_name
# 
# Path: harnas/checker/utils.py
# def get_templates_list():
#     templates = cache.get('templates_list')
#     if templates is None:
#         templates = Template.list(settings.HERA_AUTH)
#         cache.set('templates_list', templates, 60 * 60 * 24)
#     return templates
# 
# Path: harnas/contest/models.py
# class Task(models.Model):
#     name = models.CharField(max_length=250)
#     short_name = models.CharField(max_length=3)
#     description = models.TextField()
#     test_environment = models.ForeignKey('checker.TestEnvironment')
#     author = models.ForeignKey(User)
#     parent = models.ForeignKey('self', null=True, default=None)
#     contest = models.ForeignKey(Contest, null=True, default=None)
#     max_solution_size = models.PositiveIntegerField()
# 
#     # Default timestamps values for task. Can be overridden individually in
#     # groups. See GroupsTaskDetails.
#     open = models.DateTimeField(null=True, default=None)
#     deadline = models.DateTimeField(null=True, default=None)
#     close = models.DateTimeField(null=True, default=None)
# 
#     class Meta:
#         permissions = (
#             ('view_task', 'Can view task'),
#             ('submit_solution', 'Can submit solution'),
#             ('edit_task', 'Can edit task'))
# 
#     def __str__(self):
#         return self.name
# 
#     @property
#     def display_name(self):
#         if self.contest is not None and self.contest.name.strip() != "":
#             return "%s (%s)" % (self.name, self.contest.name)
#         else:
#             return self.name


# Path: harnas/checker/forms.py
from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _
from harnas.checker.models import TestEnvironment
from harnas.checker.utils import get_templates_list
from harnas.contest.models import Task



class TestEnvironmentForm(ModelForm):

    template_name = forms.ChoiceField(
        choices=lambda: [(template.name, template.name)
                         for template in get_templates_list()])
    maintainer = forms.ModelChoiceField(
        queryset=User.objects.exclude(username='AnonymousUser'),
        empty_label=None)

    class Meta:
        model = TestEnvironment
        fields = ['summary', 'description']


class SubmitForm(forms.Form):
    solution = forms.FileField(label="Upload your solution")
    task = forms.ModelChoiceField(queryset=None, empty_label=None)

    def __init__(self, *args, **kwargs):
        contest = kwargs.pop('contest')
        super(SubmitForm, self).__init__(*args, **kwargs)

        self.fields['task'].queryset = Task.objects.filter(contest=contest)