def project_edit(request, project_id): """ edit a project """ valid_method_or_404(request, ['GET', 'POST']) project = get_object_or_404(Project, pk=project_id) if request.method == 'POST': name = escape(request.POST.get('name')) description = escape(request.POST.get('description')) if not name.isalnum: messages.error(request, "name must be alnum") elif not description.isalnum: messages.error(request, "description must be alnum") else : project.name = escape(name) project.description = escape(description) project.save() return redirect(reverse('browser.views.project_detail', args=(project.id, ))) else: # request.method == 'GET' form = ProjectForm(instance=project) return render(request, 'project/new.html', {'form': form, 'project_id': project.id})
def project_edit(request, project_id): """ edit a project """ valid_method_or_404(request, ['GET', 'POST']) project = get_object_or_404(Project, pk=project_id) if request.method == 'POST': name = escape(request.POST.get('name')) description = escape(request.POST.get('description')) if not name.isalnum: messages.error(request, "name must be alnum") elif not description.isalnum: messages.error(request, "description must be alnum") else: project.name = escape(name) project.description = escape(description) project.save() return redirect( reverse('browser.views.project_detail', args=(project.id, ))) else: # request.method == 'GET' form = ProjectForm(instance=project) return render(request, 'project/new.html', { 'form': form, 'project_id': project.id })
def ajax_add_funcgraph_link(request, f, d, x): """ """ valid_method_or_404(request, ["POST",]) dajax = Dajax() try : depth = int(d) except ValueError : depth = -1 xref = x if x is not None else True try : for obj in serializers.deserialize("xml", f): data = obj data.save() break caller_f = data.object if not isinstance(caller_f, Function): return dajax.json() project = caller_f.project if not project.is_parsed: return dajax.json() except xml.sax.SAXParseException: return dajax.json() project = caller_f.project if f: base = settings.CACHED_SVG_FMT % (project.id, caller_f.id, caller_f.id, "up", depth) else : base = settings.CACHED_SVG_FMT % (project.id, caller_f.id, caller_f.id, "down", depth) hashed_basename = hashlib.sha256(base).hexdigest() + ".svg" pic_name = unipath.Path(settings.CACHE_PATH + "/" + hashed_basename) if not pic_name.isfile(): ret, err = generate_graph(pic_name, project, caller_f, xref, depth) if ret==False : return dajax.json() fmt_str = "<tr>" fmt_str+= "<td width=\"40%\">{0}</td>" fmt_str+= "<td width=\"15%\">{1}</td>" fmt_str+= "<td width=\"15%\">{2}</td>" fmt_str+= "<td width=\"30%\"><a href=\"{3}\">{0}</a></td>" fmt_str+= "</tr>" line = fmt_str.format(caller_f.name, xref, depth, reverse('browser.views.get_cache',args=(hashed_basename,))) dajax.assign('#table-graphs', 'innerHTML', line) return dajax.json()
def project_new(request): """ create a new project """ valid_method_or_404(request, ['GET', 'POST']) if request.method == 'POST': form = NewProjectForm(request.POST, request.FILES) if form.is_valid(): if 'file' in request.FILES: file = request.FILES['file'] if is_valid_file(file): ext = get_file_extension(file.name) if handle_uploaded_file(file, form.cleaned_data['name'], ext) is not None: form.cleaned_data['source_path'] = form.cleaned_data[ 'name'] project = Project.create(form.cleaned_data) if project: messages.success(request, "Successfully added") return redirect( reverse('browser.views.project_detail', args=(project.id, ))) else: form.errors['project'] = [ "Failed to create project" ] else: form.errors['extension'] = ["File extension is invalid"] else: form.errors['file'] = ["Error while handling uploaded file"] else: form.errors['file'] = ["File is not valid"] msg = "Invalid form: " msg += ", ".join( ["'%s': %s" % (k, v[0]) for k, v in form.errors.iteritems()]) messages.error(request, msg) return render(request, 'project/new.html', { 'form': form, 'project_id': -1 }) else: # request.method == 'GET' form = NewProjectForm() return render(request, 'project/new.html', { 'form': form, 'project_id': -1 })
def generic_search(request, filelist=None): """ perform a search in all files """ valid_method_or_404(request, ["GET"]) if "q" not in request.GET: return render(request, "search.html", {"pattern": ""}) pattern = request.GET["q"] if filelist is None: filelist = File.objects.all() context = File.search(pattern, filelist) return render(request, "search.html", context)
def project_new(request): """ create a new project """ valid_method_or_404(request, ['GET', 'POST']) if request.method == 'POST': form = NewProjectForm(request.POST, request.FILES) if form.is_valid(): if 'file' in request.FILES: file = request.FILES['file'] if is_valid_file(file): ext = get_file_extension(file.name) if handle_uploaded_file(file, form.cleaned_data['name'], ext) is not None : form.cleaned_data['source_path'] = form.cleaned_data['name'] project = Project.create(form.cleaned_data) if project: messages.success(request, "Successfully added") return redirect(reverse('browser.views.project_detail', args=(project.id, ))) else: form.errors['project']= ["Failed to create project"] else : form.errors['extension']= ["File extension is invalid"] else : form.errors['file']= ["Error while handling uploaded file"] else : form.errors['file'] = ["File is not valid"] msg = "Invalid form: " msg+= ", ".join(["'%s': %s"%(k,v[0]) for k,v in form.errors.iteritems()]) messages.error(request, msg) return render(request, 'project/new.html', {'form': form, 'project_id': -1}) else : # request.method == 'GET' form = NewProjectForm() return render(request, 'project/new.html', {'form': form, 'project_id': -1})
def project_draw(request, project_id): """ """ valid_method_or_404(request, [ "GET", ]) project = get_object_or_404(Project, pk=project_id) if "file" not in request.GET or "function" not in request.GET: messages.error(request, "Missing argument") return redirect( reverse("browser.views.project_detail", args=(project.id, ))) files = project.file_set.filter(name=request.GET["file"]) if len(files) < 1: messages.error(request, "Cannot find %s in project" % request.GET["file"]) return redirect( reverse("browser.views.project_detail", args=(project.id, ))) if not files[0].is_parsed and not project.is_parsed: messages.error(request, "Project must be xref-ed first") return redirect( reverse("browser.views.project_detail", args=(project.id, ))) callers = Function.objects.filter(project=project, name=request.GET["function"], file=files[0]) if callers.count() == 0: messages.error(request, "No function matching criterias") return redirect( reverse("browser.views.project_detail", args=(project.id, ))) elif callers.count() > 1: messages.error(request, "More than one function match criterias") return redirect( reverse("browser.views.project_detail", args=(project.id, ))) caller_f = callers[0] try: depth = int(request.GET.get('depth', -1)) except ValueError: depth = -1 # xref_to = request.GET.get("xref", True) # xref_to = False if request.GET.get('xref')=='1' else True if request.GET.get('xref', '1') == '1': xref_to = True base = settings.CACHED_SVG_FMT % (project.id, caller_f.id, caller_f.id, "up", depth) else: xref_to = True base = settings.CACHED_SVG_FMT % (project.id, caller_f.id, caller_f.id, "down", depth) basename = hashlib.sha256(base).hexdigest() + ".svg" pic_name = unipath.Path(settings.CACHE_PATH + "/" + basename).absolute() if not pic_name.isfile(): # if no file in cache, create it ret, err = generate_graph(pic_name, project, caller_f, xref_to, depth) if ret == False: messages.error(request, "Failed to create png graph: %s" % err) return redirect( reverse("browser.views.project_detail", args=(project.id, ))) return redirect(reverse("browser.views.get_cache", args=(basename, )))
def project_draw(request, project_id): """ """ valid_method_or_404(request, ["GET",]) project = get_object_or_404(Project, pk=project_id) if "file" not in request.GET or "function" not in request.GET: messages.error(request, "Missing argument") return redirect( reverse("browser.views.project_detail", args=(project.id,))) files = project.file_set.filter( name=request.GET["file"] ) if len(files) < 1 : messages.error(request, "Cannot find %s in project" % request.GET["file"]) return redirect( reverse("browser.views.project_detail", args=(project.id,))) if not files[0].is_parsed and not project.is_parsed: messages.error(request, "Project must be xref-ed first") return redirect( reverse("browser.views.project_detail", args=(project.id,))) callers = Function.objects.filter(project=project, name=request.GET["function"], file=files[0]) if callers.count() == 0: messages.error(request, "No function matching criterias") return redirect( reverse("browser.views.project_detail", args=(project.id,))) elif callers.count() > 1: messages.error(request, "More than one function match criterias") return redirect( reverse("browser.views.project_detail", args=(project.id,))) caller_f = callers[0] try : depth = int(request.GET.get('depth', -1)) except ValueError: depth = -1 # xref_to = request.GET.get("xref", True) # xref_to = False if request.GET.get('xref')=='1' else True if request.GET.get('xref', '1')=='1': xref_to = True base = settings.CACHED_SVG_FMT % (project.id, caller_f.id, caller_f.id, "up", depth) else: xref_to = True base = settings.CACHED_SVG_FMT % (project.id, caller_f.id, caller_f.id, "down", depth) basename = hashlib.sha256(base).hexdigest() + ".svg" pic_name = unipath.Path(settings.CACHE_PATH + "/" + basename).absolute() if not pic_name.isfile(): # if no file in cache, create it ret, err = generate_graph(pic_name, project, caller_f, xref_to, depth) if ret==False : messages.error(request, "Failed to create png graph: %s" % err) return redirect( reverse("browser.views.project_detail", args=(project.id,))) return redirect(reverse("browser.views.get_cache", args=(basename,)))
def ajax_add_funcgraph_link(request, f, d, x): """ """ valid_method_or_404(request, [ "POST", ]) dajax = Dajax() try: depth = int(d) except ValueError: depth = -1 xref = x if x is not None else True try: for obj in serializers.deserialize("xml", f): data = obj data.save() break caller_f = data.object if not isinstance(caller_f, Function): return dajax.json() project = caller_f.project if not project.is_parsed: return dajax.json() except xml.sax.SAXParseException: return dajax.json() project = caller_f.project if f: base = settings.CACHED_SVG_FMT % (project.id, caller_f.id, caller_f.id, "up", depth) else: base = settings.CACHED_SVG_FMT % (project.id, caller_f.id, caller_f.id, "down", depth) hashed_basename = hashlib.sha256(base).hexdigest() + ".svg" pic_name = unipath.Path(settings.CACHE_PATH + "/" + hashed_basename) if not pic_name.isfile(): ret, err = generate_graph(pic_name, project, caller_f, xref, depth) if ret == False: return dajax.json() fmt_str = "<tr>" fmt_str += "<td width=\"40%\">{0}</td>" fmt_str += "<td width=\"15%\">{1}</td>" fmt_str += "<td width=\"15%\">{2}</td>" fmt_str += "<td width=\"30%\"><a href=\"{3}\">{0}</a></td>" fmt_str += "</tr>" line = fmt_str.format( caller_f.name, xref, depth, reverse('browser.views.get_cache', args=(hashed_basename, ))) dajax.assign('#table-graphs', 'innerHTML', line) return dajax.json()