def add_new_sketch_view(): template = 'new_sketch_form.html' context = {'sketches_dir': SKETCHBOOK_DIR.resolve()} if request.method == 'POST': sketch_name = slugify(request.form.get('sketch_name', '').strip(), separator='_') if not sketch_name: context['error'] = "You have to input a sketch name to proceed." else: try: # web client only supports pyodide mode for now # TODO: improve post payload to accept a select files = commands.new_sketch(sketch_name, interpreter=PYODIDE_INTERPRETER) template = 'new_sketch_success.html' context.update({ 'files': files, 'sketch_url': f'/sketch/{sketch_name}/', }) except SketchDirAlreadyExistException: path = SKETCHBOOK_DIR.joinpath(sketch_name) context['error'] = f"The sketch {path} already exists." return render_template(template, **context)
def add_new_sketch_view(): template = 'new_sketch_form.html' context = { 'sketches_dir': SKETCHBOOK_DIR.resolve(), 'pyodide_interpreter': PYODIDE_INTERPRETER, 'transcrypt_interpreter': TRANSCRYPT_INTERPRETER, } if request.method == 'POST': sketch_name = slugify(request.form.get('sketch_name', '').strip(), separator='_') interpreter = request.form.get('interpreter', PYODIDE_INTERPRETER) if not sketch_name: context['error'] = "You have to input a sketch name to proceed." elif interpreter not in AVAILABLE_INTERPRETERS: context[ 'error'] = f"The interpreter {interpreter} is not valid. Please, select a valid one." else: try: files = commands.new_sketch(sketch_name, interpreter=interpreter) template = 'new_sketch_success.html' context.update({ 'files': files, 'sketch_url': f'/sketch/{sketch_name}/', }) except SketchDirAlreadyExistException: path = SKETCHBOOK_DIR.joinpath(sketch_name) context['error'] = f"The sketch {path} already exists." return render_template(template, **context)
def sketches_list_view(): sketches = [] for sketch_dir in (p for p in SKETCHBOOK_DIR.iterdir() if p.is_dir()): name = sketch_dir.name sketch_files = SketchFiles(name) if sketch_files.has_all_files: sketches.append({'name': name, 'url': f'/sketch/{name}'}) return render_template('index.html', sketches=sketches, sketches_dir=SKETCHBOOK_DIR.resolve())
def add_new_sketch_view(): template = 'new_sketch_form.html' context = {'sketches_dir': SKETCHBOOK_DIR.resolve()} if request.method == 'POST': sketch_name = slugify(request.form.get('sketch_name', '').strip(), separator='_') if not sketch_name: context['error'] = "You have to input a sketch name to proceed." try: files = commands.new_sketch(sketch_name) template = 'new_sketch_success.html' context.update({ 'files': files, 'sketch_url': f'/sketch/{sketch_name}/', }) except SketchDirAlreadyExistException: path = SKETCHBOOK_DIR.joinpath(sketch_name) context['error'] = f"The sketch {path} already exists." return render_template(template, **context)
def test_get_new_sketch_renders_new_sketch_form_template(self): self.client.get(self.route) self.assert_template_used('new_sketch_form.html') self.assert_context('sketches_dir', SKETCHBOOK_DIR.resolve()) self.assert_context('pyodide_interpreter', PYODIDE_INTERPRETER) self.assert_context('transcrypt_interpreter', TRANSCRYPT_INTERPRETER)