from django.utils.translation import ugettext_lazy as _
from django.views.generic.edit import UpdateView
from django.core.urlresolvers import reverse
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, ButtonHolder, Submit, Field, HTML

from devilry.apps.core.models import Assignment
from devilry_examiner.views.add_deadline import DevilryDatetimeFormField


class AssignmentUpdateForm(forms.ModelForm):
    publishing_time = DevilryDatetimeFormField(
        label=_('Publishing time'),
        help_text=_('The time when the assignment is to be published (visible to students and examiners). Format: "YYYY-MM-DD hh:mm".'),
    )

    class Meta:
        model = Assignment
        fields = ['long_name', 'short_name', 'publishing_time', 'anonymous', 'deadline_handling']


    def __init__(self, *args, **kwargs):
        super(AssignmentUpdateForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Field('long_name', css_class='input-lg'),
            'publishing_time',
            'anonymous',
            'deadline_handling',
            HTML('<div class="alert alert-warning">'),
            'short_name',
            HTML('</div>'),
            HTML('<hr>'),
            ButtonHolder(
                Submit('submit', _('Save'), css_class='btn-lg')
            )
        )


class AssignmentUpdateView(UpdateView):
    model = Assignment
    template_name = 'devilry_subjectadmin/assignment_update_form.django.html'
    form_class = AssignmentUpdateForm
    pk_url_kwarg = 'id'
    context_object_name = 'assignment'

    def get_success_url(self):
        return reverse('devilry_subjectadmin_assignment', kwargs=self.kwargs)

    def get_queryset(self):
        return Assignment.where_is_admin_or_superadmin(self.request.user)
@@ -284,16 +284,26 @@
 ld, HTML
+, Fieldset
 %0A%0Afrom d
@@ -690,16 +690,353 @@
 ),%0A    )
+%0A    students_can_not_create_groups_after = DevilryDatetimeFormField(%0A        required=False,%0A        label=_('Students can not create project groups after'),%0A        help_text=_('Students can not create project groups after this time. Ignored if %22Students can create project groups%22 is not selected. Format: %22YYYY-MM-DD hh:mm%22.'),%0A    )
 %0A%0A    cl
@@ -1135,16 +1135,28 @@
 g_time',
+%0A           
  'anonym
@@ -1180,16 +1180,111 @@
 andling'
+,%0A            'students_can_create_groups', 'students_can_not_create_groups_after'%0A            
 %5D%0A%0A%0A    
@@ -1460,16 +1460,70 @@
 Layout(%0A
+            Fieldset(%0A                _('Stuff'),%0A    
         
@@ -1564,24 +1564,58 @@
 input-lg'),%0A
+                'short_name',%0A    
             
@@ -1637,32 +1637,36 @@
 e',%0A            
+    
 'anonymous',%0A   
@@ -1674,16 +1674,20 @@
         
+ 
+   
 'deadlin
@@ -1701,118 +1701,208 @@
 ing'
-,%0A            HTML('%3Cdiv class=%22alert alert-warning%22%3E'),%0A            'short_name',%0A            HTML('%3C/div%3E'),
+%0A            ),%0A%0A            Fieldset(%0A                _('Allow students to form project groups?'),%0A                'students_can_create_groups',%0A                'students_can_not_create_groups_after'
 %0A   
@@ -1914,22 +1914,12 @@
     
-HTML('%3Chr%3E'
 ),%0A
+%0A
     

Edit project group fields.