Advertisement
akonrad

Module 9 create script view functions

Mar 24th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. def build_script(request,problem_id):
  2.     if request.method == "GET":
  3.         user = request.user
  4.         if not user.is_authenticated:
  5.             return redirect("share:login")
  6.         else:
  7.             problem = get_object_or_404(Problem, pk=problem_id)
  8.             return render(request, "share/build_script_form.html", {"user":user, "problem":problem})
  9.  
  10.  
  11. def create_script(request,problem_id):
  12.     if request.method == "POST":
  13.         user = request.user
  14.         if not user.is_authenticated:
  15.             return redirect("share:login")
  16.  
  17.         problem = get_object_or_404(Problem, pk=problem_id)
  18.  
  19.         if not request.POST["title"]:  # this does not work with textarea  form elements
  20.             return render(request, "share/build_script_form.html",
  21.             {"user": user, "problem":problem, "error":"One of the required fields was empty"})
  22.         else:
  23.             coder = user.coder
  24.             title = request.POST["title"]
  25.             description = request.POST["description"]
  26.             code = request.POST["code"]
  27.             url = request.POST["url"]
  28.             input = request.POST["input"]
  29.             output = request.POST["output"]
  30.             working_code = request.POST.get('working_code', False)
  31.             make_public = request.POST.get('make_public', False)
  32.  
  33.             if working_code == 'on':
  34.                 working_code = True
  35.             else:
  36.                 working_code = False
  37.  
  38.             if make_public == 'on':
  39.                 make_public = True
  40.             else:
  41.                 make_public = False
  42.  
  43.         # Validate URL in Script (for future work)
  44.         """
  45.        try:
  46.            URLValidator()(url)
  47.        except:
  48.            return render(request, "share/build_script_form.html", {"error":"URL is not valid"})
  49.        """
  50.  
  51.         try:
  52.             script = Script.objects.create(coder=coder, problem=problem, title=title, description=description, code=code, url=url, input=input, output=output, working_code=working_code, make_public=make_public)
  53.             script.save()
  54.  
  55.             script = get_object_or_404(Script, pk=script.id)
  56.             problem = get_object_or_404(Problem, pk=problem_id)
  57.             return render(request, "share/script.html",{"user":user, "problem":problem, "script": script})
  58.  
  59.         except:
  60.             return render(request, "share/build_script_form.html", {"error":"Can't create the script"})
  61.  
  62.     else:
  63.         # the user enteing    http://127.0.0.1:8000/problem/8/create
  64.         user = request.user
  65.         all_problems = Problem.objects.all()
  66.         return render(request, "share/index.html", {"user":user, "all_problems": all_problems, "error":"Can't create!"})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement