def pastie_save(req, nosave=False): """ retrieve shell from the form, save or display """ if req.method == 'POST': slug = req.POST.get('slug', None) if slug: pastieinstance = get_object_or_404(Pastie,slug=slug) pastieform = PastieForm(req.POST, instance=pastieinstance) else: pastieform = PastieForm(req.POST) shellform = ShellForm(req.POST) if shellform.is_valid(): shell = shellform.save(commit=False) if nosave: " return the pastie page only " return pastie_show(req, None, shell) if pastieform.is_valid(): pastie = pastieform.save(commit=False) " create slug from random string" # at the moment no versioning - just saving with version 0 if not pastie.slug: allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789' from random import choice pastie.slug = ''.join([choice(allowed_chars) for i in range(params.SLUG_LENGTH)]) #here some random stuff pastie.save() shell.pastie = pastie shell.save() " return json with pastie url " return HttpResponse(simplejson.dumps( {'pastie_url': ''.join([settings.WEB_SERVER, shell.get_absolute_url()])} ),mimetype='application/javascript' ) else: error = "Pastie form does not validate %s" % pastieform['slug'].errors else: error = "Shell form does not validate" else: error = 'Please use POST request' return HttpResponse(simplejson.dumps({'error':error}), mimetype='application/javascript' )
def pastie_save(req, nosave=False, skin=None): """ retrieve shell from the form, save or display Fix dependency """ if req.method == "POST": slug = req.POST.get("slug", None) if slug: " UPDATE - get the instance if slug provided " pastie = get_object_or_404(Pastie, slug=slug) # pastieform = PastieForm(req.POST, instance=pastieinstance) else: " CREATE " pastie = Pastie() if not nosave: if req.user.is_authenticated(): pastie.author = req.user pastie.save() shellform = ShellForm(req.POST) if shellform.is_valid(): " Instantiate shell data from the form " shell = shellform.save(commit=False) " Base64 decode " shell.code_js = base64.b64decode(shell.code_js) shell.code_html = base64.b64decode(shell.code_html) shell.code_css = base64.b64decode(shell.code_css) " Connect shell with pastie " shell.pastie = pastie # get javascript dependencies dependency_ids = [int(dep[1]) for dep in req.POST.items() if dep[0].startswith("js_dependency")] dependencies = [] for dep_id in dependency_ids: dep = JSDependency.objects.get(id=dep_id) dependencies.append(dep) # append external resources external_resources = [] ext_ids = req.POST.get("add_external_resources", "").split(",") for ext_id in ext_ids: try: external_resources.append(ExternalResource.objects.get(id=int(ext_id))) except: pass if nosave: " return the pastie page only " # no need to connect with pastie return pastie_display( req, None, shell, dependencies=dependencies, resources=external_resources, skin=skin ) " add user to shell if anyone logged in " if req.user.is_authenticated(): shell.author = req.user shell.save() # add saved dependencies for dep in dependencies: shell.js_dependency.add(dep) # add saved external resources for ext in external_resources: shell.external_resources.add(ext) " return json with pastie url " return HttpResponse( simplejson.dumps({"pastie_url_relative": shell.get_absolute_url()}), mimetype="application/javascript" ) else: error = "Shell form does not validate" for s in shellform: if hasattr(s, "errors") and s.errors: error = error + str(s.__dict__) else: error = "Please use POST request" # Report errors return HttpResponse(simplejson.dumps({"error": error}), mimetype="application/javascript")
def pastie_save(req, nosave=False, skin=None): """ retrieve shell from the form, save or display Fix dependency """ if req.method == 'POST': slug = req.POST.get('slug', None) if slug: " UPDATE - get the instance if slug provided " pastie = get_object_or_404(Pastie,slug=slug) #pastieform = PastieForm(req.POST, instance=pastieinstance) else: " CREATE " pastie = Pastie() if not nosave: " create slug from random string if needed" pastie.set_slug() if req.user.is_authenticated(): pastie.author = req.user pastie.save() shellform = ShellForm(req.POST) if shellform.is_valid(): " Instantiate shell data from the form " shell = shellform.save(commit=False) " Connect shell with pastie " shell.pastie = pastie # add js_dependency dependency_ids = [int(dep[1]) for dep in req.POST.items() if dep[0].startswith('js_dependency')] dependencies = [] for dep_id in dependency_ids: dep = JSDependency.objects.get(id=dep_id) dependencies.append(dep) if nosave: " return the pastie page only " # no need to connect with pastie return pastie_display(req, None, shell, dependencies, skin) " add user to shell if anyone logged in " if req.user.is_authenticated(): shell.author = req.user if slug: shell.set_next_version() shell.save() for dep in dependencies: shell.js_dependency.add(dep) " return json with pastie url " return HttpResponse(simplejson.dumps({ #'pastie_url': ''.join(['http://',req.META['SERVER_NAME'], shell.get_absolute_url()]), 'pastie_url_relative': shell.get_absolute_url() }),mimetype='application/javascript' ) else: error = "Shell form does not validate" for s in shellform: if hasattr(s, 'errors') and s.errors: error = error + str(s.__dict__) else: error = 'Please use POST request' # Report errors return HttpResponse(simplejson.dumps({'error':error}), mimetype='application/javascript' )
def pastie_save(req, nosave=False, skin=None): """ retrieve shell from the form, save or display Fix dependency """ if req.method == 'POST': slug = req.POST.get('slug', None) if slug: " UPDATE - get the instance if slug provided " pastie = get_object_or_404(Pastie,slug=slug) #pastieform = PastieForm(req.POST, instance=pastieinstance) else: " CREATE " pastie = Pastie() if not nosave: if req.user.is_authenticated(): pastie.author = req.user pastie.save() draftonly=req.POST.get('draftonly', False) shellform = ShellForm(req.POST) if shellform.is_valid(): " Instantiate shell data from the form " shell = shellform.save(commit=False) if not shell.panel_html: # XXX: A hack to pass the form shell.panel_html = 0 " Base64 decode " try: shell.code_js = base64.b64decode(shell.code_js) except: pass try: shell.code_html = base64.b64decode(shell.code_html) except: pass try: shell.code_css = base64.b64decode(shell.code_css) except: pass " Connect shell with pastie " shell.pastie = pastie # get javascript dependencies dependency_ids = [int(dep[1]) for dep in req.POST.items() \ if dep[0].startswith('js_dependency')] dependencies = [] for dep_id in dependency_ids: dep = JSDependency.objects.get(id=dep_id) dependencies.append(dep) dependencies = sorted( dependencies, key=lambda d: d.ord, reverse=True) # append external resources external_resources = [] ext_ids = req.POST.get('add_external_resources', '').split(',') for ext_id in ext_ids: if ext_id: try: external_resources.append( ExternalResource.objects.get(id=int(ext_id))) except Exception, err: log_to_file('WARNING: pastie_save: ' 'No external resource: %s %s' % ( req.POST.get('slug', '-'), ext_id)) if nosave: # get page # no need to connect with pastie display_page = pastie_display(req, None, shell, dependencies=dependencies, resources=external_resources, skin=skin) # save the draft version if req.POST.get('username', False): Draft.objects.make(req.POST.get('username'), display_page) if draftonly: hashtag = ''.join( [choice('abcdefghjkmnpqrstuvwxyz') for i in range(3)]) mdraft_url = "%s%s" % (settings.MOOSHELL_FORCE_SERVER, reverse('mdraft', args=[hashtag])) return HttpResponse(""" <p>EXPERIMENTAL</p> <p>Please load result <a target="_draft" href="%s">%s</a> on mobile and <a href="http://debug.phonegap.com/client/#jsf_%s"> debugger</a> on the desktop (Chrome, Safari, weinre app)</p> <p><a target="_blank" href="http://pmuellr.github.com/weinre/">weinre</a> service provided by <a target="_blank" href="http://phonegap.com/">PhoneGap</p>""" % ( mdraft_url, mdraft_url, hashtag)) return display_page # add user to shell if anyone logged in if req.user.is_authenticated(): shell.author = req.user try: shell.save() except Exception, err: log_to_file("ERROR: pastie_edit: " "saving shell failed %s" % str(err)) return HttpResponseNotAllowed('Error saving shell') # add saved dependencies for dep in dependencies: shell.js_dependency.add(dep) # add saved external resources for idx,ext in enumerate(external_resources): ShellExternalResource.objects.create( shell=shell, resource=ext, ord=idx) " return json with pastie url " return HttpResponse(simplejson.dumps({ 'pastie_url_relative': shell.get_absolute_url() }),mimetype='application/json' )