# Path: harnas/contest/forms.py
# class TestCaseForm(forms.ModelForm):
#     in_file = BootstrapFileField()
#     out_file = BootstrapFileField()
# 
#     class Meta:
#         model = TestCase
#         fields = ['name', 'max_memory', 'max_duration']
# 
# Path: harnas/contest/helpers.py
# def save_testcase_file(file, dest_dir):
#     dest_path = os.path.join(dest_dir, file.name)
#     with open(dest_path, 'wb+') as destination:
#         for chunk in file.chunks():
#             destination.write(chunk)
# 
# def get_task_dir(task_id):
#     return os.path.join(settings.TASK_STORAGE_PREFIX, str(task_id))
# 
# 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
# 
# class TestCase(models.Model):
#     QUEUED = "QUE"
#     MEMORY_LIMIT = "MEM"
#     TIME_LIMIT = "TLE"
#     ACCEPTED = "OK"
#     INTERNAL_ERROR = "INT"
#     WRONG_ANSWER = "ANS"
#     COMPILATION_ERROR = "CME"
#     RUNTIME_ERROR = "RTE"
#     STATUS_CHOICES = (
#         (QUEUED, "In checking queue"),
#         (MEMORY_LIMIT, "Memory limit exceeded"),
#         (TIME_LIMIT, "Time limit exceeded"),
#         (ACCEPTED, "Accepted"),
#         (INTERNAL_ERROR, "Internal error"),
#         (WRONG_ANSWER, "Wrong answer"),
#         (COMPILATION_ERROR, "Compilation error"),
#         (RUNTIME_ERROR, "Runtime error"),
#     )
# 
#     task = models.ForeignKey(Task)
#     name = models.CharField(max_length=100, null=True, default=None)
#     max_memory = models.PositiveIntegerField()
#     max_duration = models.PositiveIntegerField()
#     run_order_id = models.PositiveSmallIntegerField(default=0)
#     comparator = models.CharField(max_length=500)
#     executor = models.CharField(max_length=500)
#     in_file_path = models.FilePathField(max_length=500)
#     out_file_path = models.FilePathField(max_length=500)
# 
#     def __str__(self):
#         return self.name


import os
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.views.decorators.http import require_POST
from harnas.contest.forms import TestCaseForm
from harnas.contest.helpers import save_testcase_file, get_task_dir
from harnas.contest.models import Task, TestCase




@require_POST
@login_required
def new(request, task_id):
    task = Task.objects.get(pk=task_id)
    if not request.user.has_perm('contest.edit_task', task):
        raise PermissionDenied
    return handle_testcase_form(request, task, TestCase())


@require_POST
@login_required
def edit(request, task_id, testcase_id):

# Path: harnas/contest/forms.py
# class TestCaseForm(forms.ModelForm):
#     in_file = BootstrapFileField()
#     out_file = BootstrapFileField()
# 
#     class Meta:
#         model = TestCase
#         fields = ['name', 'max_memory', 'max_duration']
# 
# Path: harnas/contest/helpers.py
# def save_testcase_file(file, dest_dir):
#     dest_path = os.path.join(dest_dir, file.name)
#     with open(dest_path, 'wb+') as destination:
#         for chunk in file.chunks():
#             destination.write(chunk)
# 
# def get_task_dir(task_id):
#     return os.path.join(settings.TASK_STORAGE_PREFIX, str(task_id))
# 
# 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
# 
# class TestCase(models.Model):
#     QUEUED = "QUE"
#     MEMORY_LIMIT = "MEM"
#     TIME_LIMIT = "TLE"
#     ACCEPTED = "OK"
#     INTERNAL_ERROR = "INT"
#     WRONG_ANSWER = "ANS"
#     COMPILATION_ERROR = "CME"
#     RUNTIME_ERROR = "RTE"
#     STATUS_CHOICES = (
#         (QUEUED, "In checking queue"),
#         (MEMORY_LIMIT, "Memory limit exceeded"),
#         (TIME_LIMIT, "Time limit exceeded"),
#         (ACCEPTED, "Accepted"),
#         (INTERNAL_ERROR, "Internal error"),
#         (WRONG_ANSWER, "Wrong answer"),
#         (COMPILATION_ERROR, "Compilation error"),
#         (RUNTIME_ERROR, "Runtime error"),
#     )
# 
#     task = models.ForeignKey(Task)
#     name = models.CharField(max_length=100, null=True, default=None)
#     max_memory = models.PositiveIntegerField()
#     max_duration = models.PositiveIntegerField()
#     run_order_id = models.PositiveSmallIntegerField(default=0)
#     comparator = models.CharField(max_length=500)
#     executor = models.CharField(max_length=500)
#     in_file_path = models.FilePathField(max_length=500)
#     out_file_path = models.FilePathField(max_length=500)
# 
#     def __str__(self):
#         return self.name


# Path: harnas/contest/views/testcase.py
import os
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.views.decorators.http import require_POST
from harnas.contest.forms import TestCaseForm
from harnas.contest.helpers import save_testcase_file, get_task_dir
from harnas.contest.models import Task, TestCase





@require_POST
@login_required
def new(request, task_id):
    task = Task.objects.get(pk=task_id)
    if not request.user.has_perm('contest.edit_task', task):
        raise PermissionDenied
    return handle_testcase_form(request, task, TestCase())


@require_POST
@login_required
def edit(request, task_id, testcase_id):

    task = Task.objects.get(pk=task_id)