from django import forms
from django.conf import settings
from django.forms import ModelForm
from ansible.models import Playbook

class AnsibleForm1(ModelForm):
    class Meta:
        model = Playbook
        fields = ['repository', 'username']


class AnsibleForm2(ModelForm):
    class Meta:
        model = Playbook
        fields = ['inventory', 'user']


class LoginForm(forms.Form):
    username = forms.CharField(label='Username', max_length=100)
    password = forms.CharField(label='Password', max_length=100)


class PlaybookFileForm(forms.Form):
    filename = forms.CharField(label='Filename', max_length=100)
    playbook = forms.CharField(widget=forms.Textarea(attrs={'rows':30,'cols':80}))

@@ -18,16 +18,67 @@
 t forms%0A
+from django.core.validators import ValidationError%0A
 from dja
@@ -102,16 +102,16 @@
 ettings%0A
-
 from dja
@@ -172,16 +172,66 @@
 Playbook
+%0Aimport utils.playbook as playbook_utils%0Aimport os
 %0A%0Aclass 
@@ -713,24 +713,24 @@
 length=100)%0A
-
     playbook
@@ -800,8 +800,554 @@
 ':80%7D))%0A
+%0A    def __init__(self, *args, **kwargs):%0A        self.pk = kwargs.pop('pk', None)%0A        super(PlaybookFileForm, self).__init__(*args, **kwargs)%0A%0A    def clean_filename(self):%0A        data = playbook_utils.append_extension(self.cleaned_data%5B'filename'%5D)%0A        playbook = Playbook.query_set.get(pk=self.pk)%0A        playbook_dir = playbook.directory%0A        playbook_file_path = os.path.join(playbook_dir, data)%0A        if os.path.exists(playbook_file_path):%0A            raise forms.ValidationError(%22Filename already used%22)%0A        return data%0A

Use clean_filename to validate if filename is already used