我基本上使用django-formtool表单向导来处理表单的保存。
当我尝试保存向导的最后一步时,我遇到了麻烦,
我让我回到第一步。由于存储库存在预期的ValidationError,因为在我的If(string.IsNullOrEmpty(data))
{
Break;
}
上执行views.py
方法。
以下是我目前的相关代码。
views.py
clone_repository()
forms.py
class PlaybookWizard(SessionWizardView):
def process_step(self, form):
if self.steps.current == '0':
form.clone_repository()
if self.steps.current == '1':
form.save()
return super(PlaybookWizard, self).process_step(form)
def done(self, form_list, form_dict, **kwargs):
return HttpResponseRedirect('/playbooks')
models.py
def check_path_exists(repository, host_inventory=None):
if host_inventory:
os.chdir(settings.PLAYBOOK_DIR + repository)
current_dir = os.getcwd()
return os.path.exists(os.path.join(current_dir, host_inventory))
return os.path.exists(os.path.join(settings.PLAYBOOK_DIR, repository))
def get_dir_name(repository):
return os.path.join(settings.PLAYBOOK_DIR, repository)
def get_remote_repo_url(username, repository):
return "https://github.com/{0}/{1}.git".format(
username, repository
)
class AnsibleForm1(ModelForm):
class Meta:
model = Playbook
fields = ['repository', 'username']
def clean_repository(self):
if check_path_exists(self.cleaned_data['repository']):
raise ValidationError("Repository already exists")
return self.cleaned_data['repository']
def clone_repository(self):
repository = self.cleaned_data['repository']
username = self.cleaned_data['username']
dir_name = get_dir_name(repository)
remote_url = get_remote_repo_url(username, repository)
os.mkdir(os.path.join(dir_name))
repo = git.Repo.init(dir_name)
origin = repo.create_remote('origin', remote_url)
origin.fetch()
origin.pull(origin.refs[0].remote_head)
class AnsibleForm2(ModelForm):
class Meta:
model = Playbook
fields = ['inventory', 'user']