def handle_solution_form(form, attributes): solution = form.save(commit=False) fill_object(solution, attributes) # TODO modify to .path & test xml = SolutionSpecification.make_xml(solution) config = solution.get_config_filepath() with open(config, "w") as xmlConfigFile: xmlConfigFile.write(xml.toprettyxml()) return HttpResponseRedirect(reverse(index))
def change_agent_mapping(request, solutionId): user = request.user response = 'failure' if request.method == 'POST': params = request.POST if u'json' in params: try: solution = get_solution_or_404(user, id=solutionId) config = solution.get_config_filepath() agents = json.loads(params[u'json']) # TODO Y U forgot to handle this? Validator.validateAgentMapping(agents) # TODO server-side validation goes here # also check IDs!! tree = SolutionSpecification.add_agents_to_xml(config, agents) tree.write(config) response = 'ok' except ValueError, e: response = str(e)
def index_common(request, postSolution=None, postSubEnv=None, others=False): is_post = (request.method == 'POST') user = request.user envUser = get_object_or_404(EnvUser, user=user) userSolutions = Solution.objects.filter(envUser=envUser) subEnvironments = SubEnvironment.objects.get_solved_by_user(user) unsubEnvironments = SubEnvironment.objects.get_unsolved_by_user(user) agentFilter = {} if not user.is_superuser: agentFilter['envUser__user'] = user agentQueryset = EnvAgent.objects.filter(**agentFilter) active_subenv = None active_solution = None allSolutions = [] for subenv_index, subEnvironment in enumerate(subEnvironments): subenvs = SubEnvironment.objects.filter(pk=subEnvironment.pk) SolutionForm = make_special_solution_form(envUser, subenvs, singleSubEnv=True) solutions = userSolutions.filter(subEnvironment__in=subenvs) forms = [] for soln_index, solution in enumerate(solutions): SolutionFormSet = make_single_solution_formset(SolutionForm, solution, extra=0) if is_post and solution == postSolution: formset = SolutionFormSet(request.POST, request.FILES) if formset.is_valid(): return handle_solution_formset( formset, { 'envUser': envUser, 'subEnvironment': subEnvironment, }) else: active_subenv = { 'id': subEnvironment.id, 'index': subenv_index, } active_solution = soln_index else: formset = SolutionFormSet() agentFiles = get_agent_name_list(solution.agents.path) choices = [(filename, filename) for filename in agentFiles] AgentForm = make_custom_agent_form({ 'queryset': agentQueryset, }, { 'choices': choices, }) config = solution.get_config_filepath() aaData = SolutionSpecification.parseAgentMapping(config) rowIds = [row[0][0] for row in aaData] forms.append({ 'form': formset, 'agent_form': AgentForm(), 'obj': solution, 'is_novel': False, 'table': { 'aaData': json.dumps(aaData), 'rowIds': json.dumps(rowIds), }, }) if is_post and subEnvironment == postSubEnv: form = SolutionForm(request.POST, request.FILES) if form.is_valid(): return handle_solution_form(form, { 'envUser': envUser, 'subEnvironment': subEnvironment, }) else: active_subenv = { 'id': subEnvironment.id, 'index': subenv_index, } active_solution = len(solutions) else: form = SolutionForm() forms.append({ 'form': form, 'obj': subEnvironment, 'is_novel': True, }) allSolutions.append({ 'subEnvironment': subEnvironment, 'forms': forms, }) SolutionFormSet = make_solution_formset(envUser, userSolutions, unsubEnvironments) if is_post and others: othersFormset = SolutionFormSet(request.POST, request.FILES) if othersFormset.is_valid(): return handle_solution_formset(othersFormset, { 'envUser': envUser, }, are_novel=True) else: active_subenv = { 'index': len(subEnvironments), } else: othersFormset = SolutionFormSet() AgentForm = make_custom_agent_form({ 'queryset': agentQueryset, }, { 'choices': [], }) return render_to_response('solution/index.html', { 'allSolutions': allSolutions, 'othersFormset': othersFormset, 'agentForm': AgentForm(), 'active_subenv': active_subenv, 'active_solution': active_solution, }, context_instance=RequestContext(request))