# Path: harnas/contest/forms.py
# class GroupForm(forms.ModelForm):
#     class Meta:
#         model = Group
#         fields = ['name']
# 
# class TaskDetailsForm(forms.ModelForm):
#     class Meta:
#         model = GroupTaskDetails
#         fields = ['open', 'deadline', 'close']
# 
# Path: harnas/contest/models.py
# class Contest(models.Model):
#     name = models.CharField(max_length=250, unique=True, null=False)
#     slug = models.SlugField()
#     description = models.TextField()
#     created_at = models.DateTimeField(auto_now_add=True)
#     modified_at = models.DateTimeField(auto_now=True)
#     creator = models.ForeignKey(User)
# 
#     class Meta:
#         permissions = (
#             ('view_contest', 'Can view'),
#             ('participate_in_contest', 'Can participate'),
#             ('manage_contest', 'Can manage contest'))
# 
#     def __str__(self):
#         return self.name
# 
# class GroupTaskDetails(models.Model):
#     group = models.ForeignKey(Group, null=False)
#     task = models.ForeignKey(Task, null=False)
# 
#     open = models.DateTimeField()
#     deadline = models.DateTimeField()
#     close = models.DateTimeField()
# 
#     class Meta:
#         index_together = ['group', 'task']
# 
# 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.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import Group
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views.decorators.http import (require_GET, require_http_methods,
                                          require_POST)
from guardian.shortcuts import assign_perm
from harnas.contest.forms import GroupForm, TaskDetailsForm
from harnas.contest.models import Contest, GroupTaskDetails, Task
@require_http_methods(['POST', 'GET'])
def edit_task_details(request, contest_id, group_id, task_id):
    if not request.user.has_perm('manage_contest',
                                 Contest.objects.get(pk=contest_id)):
        raise PermissionDenied
        return HttpResponseRedirect(
            reverse('contest_details',
                    args=[contest_id]) + '?current_tab=groups')
    else:
        if request.method == 'POST':
            form = TaskDetailsForm(request.POST)
            if form.is_valid():
                details = GroupTaskDetails.objects.get(group=group_id,
                                                       task=task_id)
                details.open = form.cleaned_data['open']
                details.deadline = form.cleaned_data['deadline']
                details.close = form.cleaned_data['close']
                details.save()
                messages.add_message(request,
                                     messages.SUCCESS,
                                     "Timestamps have been updated.")
                return HttpResponseRedirect(reverse('contest_group_details',
                                                    args=[contest_id,
                                                          group_id]))
        else:
            details = GroupTaskDetails.objects.get(task=task_id,
                                                   group=group_id)
            return render(request, 'contest/task_details_edit.html', {
                'contest_id': contest_id,
                'group_id': group_id,

# Path: harnas/contest/forms.py
# class GroupForm(forms.ModelForm):
#     class Meta:
#         model = Group
#         fields = ['name']
# 
# class TaskDetailsForm(forms.ModelForm):
#     class Meta:
#         model = GroupTaskDetails
#         fields = ['open', 'deadline', 'close']
# 
# Path: harnas/contest/models.py
# class Contest(models.Model):
#     name = models.CharField(max_length=250, unique=True, null=False)
#     slug = models.SlugField()
#     description = models.TextField()
#     created_at = models.DateTimeField(auto_now_add=True)
#     modified_at = models.DateTimeField(auto_now=True)
#     creator = models.ForeignKey(User)
# 
#     class Meta:
#         permissions = (
#             ('view_contest', 'Can view'),
#             ('participate_in_contest', 'Can participate'),
#             ('manage_contest', 'Can manage contest'))
# 
#     def __str__(self):
#         return self.name
# 
# class GroupTaskDetails(models.Model):
#     group = models.ForeignKey(Group, null=False)
#     task = models.ForeignKey(Task, null=False)
# 
#     open = models.DateTimeField()
#     deadline = models.DateTimeField()
#     close = models.DateTimeField()
# 
#     class Meta:
#         index_together = ['group', 'task']
# 
# 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/contest/views/groups.py
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import Group
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views.decorators.http import (require_GET, require_http_methods,
                                          require_POST)
from guardian.shortcuts import assign_perm
from harnas.contest.forms import GroupForm, TaskDetailsForm
from harnas.contest.models import Contest, GroupTaskDetails, Task

@require_http_methods(['POST', 'GET'])
def edit_task_details(request, contest_id, group_id, task_id):
    if not request.user.has_perm('manage_contest',
                                 Contest.objects.get(pk=contest_id)):
        raise PermissionDenied
        return HttpResponseRedirect(
            reverse('contest_details',
                    args=[contest_id]) + '?current_tab=groups')
    else:
        if request.method == 'POST':
            form = TaskDetailsForm(request.POST)
            if form.is_valid():
                details = GroupTaskDetails.objects.get(group=group_id,
                                                       task=task_id)
                details.open = form.cleaned_data['open']
                details.deadline = form.cleaned_data['deadline']
                details.close = form.cleaned_data['close']
                details.save()
                messages.add_message(request,
                                     messages.SUCCESS,
                                     "Timestamps have been updated.")
                return HttpResponseRedirect(reverse('contest_group_details',
                                                    args=[contest_id,
                                                          group_id]))
        else:
            details = GroupTaskDetails.objects.get(task=task_id,
                                                   group=group_id)
            return render(request, 'contest/task_details_edit.html', {
                'contest_id': contest_id,
                'group_id': group_id,

                'task': Task.objects.get(pk=task_id),