forms.py 2.69 KB
from django import forms
from django.utils.translation import ugettext_lazy as _
import datetime

from django.forms.formsets import BaseFormSet


class BaseResourceAndTagFormset(BaseFormSet):
    def clean(self):
        """
        Adds validation to check that no two links have the same anchor or URL
        and that all links have both an anchor and URL.
        """
        print(self.errors)
        if any(self.errors):
            return

        for form in self.forms:
        	pass

class ResourceAndTagForm(forms.Form):

	resource = forms.ChoiceField(label=_("Kind Of Resource"), required=True)
	tag  = forms.ChoiceField(label=_('Tag'))

	def __init__(self, *args, **kwargs):
		super(ResourceAndTagForm, self).__init__(*args, **kwargs)
		if kwargs.get('initial'):
			initial = kwargs['initial']
			self.fields['resource'].choices = [(classes.__name__.lower(), classes.__name__.lower()) for classes in initial['class_name']]
			self.fields['tag'].choices = [(tag.id, tag.name) for tag in initial['tag']]

		

class CreateInteractionReportForm(forms.Form):
	topic = forms.ChoiceField( label= _("Topics"), required=True)
	init_date = forms.DateField(required=True, label= _("Initial Date"))
	end_date = forms.DateField(required=True, label= _("Final Date"))

	from_mural = forms.BooleanField(required=False, label=_("From Mural"))
	from_messages = forms.BooleanField(required=False, label=_("Messages"))

	class Meta:
		fields = ('topic', 'init_date', 'end_date', 'from_mural' , 'from_messages')

	def __init__(self, *args, **kwargs):
		super(CreateInteractionReportForm, self).__init__(*args, **kwargs)
		initial = kwargs['initial']
		topics = list(initial['topic'])
		self.subject = initial['subject'] #so we can check date cleaned data
		self.fields['topic'].choices = [(topic.id, topic.name) for topic in topics]
		self.fields['topic'].choices.append((_("All"), _("All")))


	def clean(self):
		cleaned_data = super(CreateInteractionReportForm, self).clean()
		init_date = cleaned_data.get("init_date")
		end_date = cleaned_data.get("end_date")
		if init_date and end_date:
			if init_date > end_date:
				raise forms.ValidationError(_("The initial date can't be after the end one."))

	def clean_init_date(self):
		init_date = self.cleaned_data['init_date']
		if init_date < self.subject.init_date:
			self._errors['init_date'] = [_('This date should be right or after ' + str(self.subject.init_date) + ', which is when the subject started. ')]
		return init_date

	def clean_end_date(self):
		end_date = self.cleaned_data['end_date']
		if end_date > self.subject.end_date:
			self._errors['end_date'] = [_('This date should be right or before ' + str(self.subject.end_date) + ', which is when the subject finishes. ')]
		return end_date