from datetime import datetime

from django import forms
from django.conf import settings
from django.forms.extras.widgets import SelectDateWidget

from hr.app_defines import *
from hr.models import Application, Audit
from eve_api.models import EVEPlayerCharacter, EVEPlayerCorporation


def CreateRecommendationForm(user):
    """ Generate a Recommendation form based on the user's permissions """

    characters = EVEPlayerCharacter.objects.filter(eveaccount__user=user)
    applications = Application.objects.filter(status=APPLICATION_STATUS_NOTSUBMITTED)

    class RecommendationForm(forms.Form):
        """ Service Account Form """

        character = forms.ModelChoiceField(queryset=characters, required=True, empty_label=None)
        application = forms.ModelChoiceField(queryset=applications, required=True, empty_label=None)

    return RecommendationForm


def CreateApplicationForm(user):
    """ Generate a Application form based on the user's permissions """

    characters = EVEPlayerCharacter.objects.filter(eveaccount__user=user)
    corporations = EVEPlayerCorporation.objects.filter(application_config__is_accepting=True)

    class ApplicationForm(forms.Form):
        character = forms.ModelChoiceField(queryset=characters, required=True, empty_label=None)
        corporation = forms.ModelChoiceField(queryset=corporations, required=True, empty_label=None)

        def clean_character(self):
 
            if not 'character' in self.cleaned_data or not self.cleaned_data['character']:
                raise forms.ValidationError("Please select a character to apply with")

            if Application.objects.filter(character=self.cleaned_data['character']).exclude(status__in=[APPLICATION_STATUS_COMPLETED, APPLICATION_STATUS_REJECTED]).count():
                raise forms.ValidationError("This character already has a open application")

            return self.cleaned_data['character']


        def clean(self):
            if self.cleaned_data['character'].corporation == self.cleaned_data['corporation']:
                raise forms.ValidationError("%s is already a member of %s" % (self.cleaned_data['character'], self.cleaned_data['corporation']))

            if not self.cleaned_data['character'].account.api_keytype >= self.cleaned_data['corporation'].application_config.api_required:
                raise forms.ValidationError("%s requires a %s API key for this application" % (self.cleaned_data['corporation'], self.cleaned_data['corporation'].application_config.get_api_required_display()))

            return self.cleaned_data

    return ApplicationForm


class NoteForm(forms.ModelForm):
    class Meta:
        model = Audit
        exclude = ('application', 'user', 'event')


class BlacklistUserForm(forms.Form):
    """ A form to capture the reasons for blacklisting a user
        and the related expiry date """

    reason = forms.CharField(required=True, widget=forms.widgets.Textarea())
    expiry_date = forms.DateTimeField(required=False, widget=SelectDateWidget())
    disable = forms.BooleanField(required=False)

@@ -1937,16 +1937,17 @@
 (self):%0A
+%0A
         
@@ -1950,18 +1950,22 @@
         
-if
+char =
  self.cl
@@ -1966,33 +1966,37 @@
 elf.cleaned_data
-%5B
+.get(
 'character'%5D.cor
@@ -1994,24 +1994,28 @@
 ter'
-%5D.corporation
+)%0A            corp
  =
-=
  sel
@@ -2020,33 +2020,37 @@
 elf.cleaned_data
-%5B
+.get(
 'corporation'%5D:%0A
@@ -2050,262 +2050,236 @@
 ion'
-%5D:%0A                raise forms.ValidationError(%22%25s is already a member of %25s%22 %25 (self.cleaned_data%5B'character'%5D, self.cleaned_data%5B'corporation'%5D))%0A%0A            if not self.cleaned_data%5B'character'%5D.account.api_keytype %3E= self.cleaned_data%5B'corporation'%5D
+)%0A%0A            if char and corp:%0A                if char.corporation == corp:%0A                    raise forms.ValidationError(%22%25s is already a member of %25s%22 %25 (char, corp))%0A                if not char.account.api_keytype %3E= corp
 .app
@@ -2308,16 +2308,20 @@
 quired:%0A
+    
         
@@ -2411,74 +2411,18 @@
  %25 (
-self.cleaned_data%5B'corporation'%5D, self.cleaned_data%5B'corporation'%5D
+corp, corp
 .app

Check we have a valid char/corp before doing other checks, fixes bug #64