from django import forms
from django.forms.fields import IntegerField, CharField

import evelink.account


class AddAPIForm(forms.Form):
    key_id = IntegerField()
    v_code = CharField(max_length=64, min_length=1)

    def clean(self):
        super(AddAPIForm, self).clean()
        self._clean()
        return self.cleaned_data

    def _clean(self):
        """Check the access mask and characters of the supplied keypair.

        """
        key_id = self.cleaned_data.get('key_id')
        v_code = self.cleaned_data.get('v_code')
        if not (key_id and v_code):
            return

        api = evelink.api.API(api_key=(key_id, v_code))
        account = evelink.account.Account(api)
        try:
            key_info = account.key_info().result
        except evelink.api.APIError as error:
            self.add_error(None, error.message)
            return

        if key_info['type'] != 'account':
            self.add_error(None, 'The API key should select Character: All')
        if key_info['access_mask'] != 4294967295:
            self.add_error(None, 'The API key should have full access')
        if key_info['expire_ts']:
            self.add_error(None, 'The API key should have no expiry checked')

@@ -1,28 +1,26 @@
-from django import forms
+import evelink.account
 %0Afro
@@ -38,13 +38,13 @@
 rms.
-fi
+mod
 el
-d
 s im
@@ -52,55 +52,55 @@
 ort 
-IntegerField, CharField%0A%0Aimport evelink.account
+ModelForm%0A%0Afrom evexml.models import APIKeyPair
 %0A%0A%0Ac
@@ -115,22 +115,21 @@
 APIForm(
-forms.
+Model
 Form):%0A 
@@ -135,200 +135,73 @@
     
-key_id = IntegerField()%0A    v_code = CharField(max_length=64, min_length=1)%0A%0A    def clean(self):%0A        super(AddAPIForm, self).clean()%0A        self._clean()%0A        return self.cleaned_data
+class Meta:%0A        model = APIKeyPair%0A        fields = '__all__'
 %0A%0A  
@@ -206,16 +206,20 @@
     def 
+full
 _clean(s
@@ -310,16 +310,61 @@
     %22%22%22%0A
+        super(AddAPIForm, self).full_clean()%0A
         
@@ -633,24 +633,29 @@
             
+self.
 key_info = a
@@ -793,35 +793,61 @@
 return%0A%0A        
-if 
+self.save()%0A%0A        if self.
 key_info%5B'type'%5D
@@ -941,32 +941,37 @@
 ll')%0A        if 
+self.
 key_info%5B'access
@@ -1076,16 +1076,21 @@
      if 
+self.
 key_info

Switch to ModelForm