def editTutorial(request, id): try: tutorial = models.Tutorial.objects.get(id=id) except ObjectDoesNotExist: mensaje = "Este tutorial no existe" return HttpResponse(json.dumps({"error": mensaje}), status=404, content_type='application/json') if request.method == "POST": form = TutorialForm(request.POST, instance=tutorial) if form.is_valid(): form.save() mensaje = {"mensaje": "Guardado exitoso"} return HttpResponse(json.dumps({"mensaje": mensaje}), status=200, content_type='application/json') erros = form.errors.items() return HttpResponse(json.dumps(erros), status=400, content_type='application/json') mensaje = "Metodo no permitido" return HttpResponse(json.dumps({"mensaje": mensaje}), status=404, content_type='application/json')
def new_tutorial(): if not g.user_id: return redirect(url_for("index")) form = TutorialForm(request.form) if request.method == 'POST' and form.validate(): title = form.title.data tutorial = form.tutorial.data new_tutorial = Tutorial(title=title, tutorial=tutorial) db_session.add(new_tutorial) db_session.commit() return redirect(url_for("list_tutorials")) return render_template('new_tutorial.html', form=form)
def edit_tutorial(id): if not g.user_id: return redirect(url_for("index")) tutorial = db_session.query(Tutorial).get(id) form = TutorialForm(request.form, tutorial) if request.method == "POST" and form.validate(): tutorial.title = form.title.data tutorial.tutorial = form.tutorial.data db_session.add(tutorial) db_session.commit() return redirect(url_for("list_tutorials")) rendered_tutorial = markdown.markdown(tutorial.tutorial, safe_mode='escape') return render_template('tutorial.html', form=form, tutorial=tutorial, tutorial_text=rendered_tutorial)
def addTutorial(request): if request.method == "POST": form = TutorialForm(request.POST) if form.is_valid(): form.save() mensaje = {"mensaje": "Tutorial agregado con Éxito!"} return HttpResponse(json.dumps({"mensaje": mensaje}), status=200, content_type='application/json') erros = form.errors.items() return HttpResponse(json.dumps(erros), status=400, content_type='application/json') mensaje = "Metodo no permitido" return HttpResponse(json.dumps({"error": mensaje}),status=404, content_type='application/json')
def tutorial(): """Tutorial for setting up user correctly""" if g.user and (session[LOGGED_IN_USER] != 'logged in user'): if g.user.fully_set_up: return redirect(f'/hub/{g.user.id}') else: form = TutorialForm() if form.validate_on_submit(): which_user = User.query.get_or_404(g.user.id) which_user.first_name = form.first_name.data which_user.last_name = form.last_name.data which_user.profile_pic_url = form.profile_pic_url.data which_user.location = form.location.data which_user.fully_set_up = True db.session.add(which_user) db.session.commit() return redirect(f'/hub/{g.user.id}') else: return render_template('tutorial.html', form=form) return redirect('/')
def addTutorialView(request): return render(request, 'herramienta/add_tutorial.html', {"form": TutorialForm()})