def edit_paste(slug): paste = get_by_slug(slug) form = PasteForm(title=paste.title, slug=paste.title, body=paste.body, explanation=paste.explanation, language=paste.language, labels=paste.labels, source=paste.source) if form.validate_on_submit(): if form.title.data != paste.title: paste.slug = slugify(form.title.data) paste.title = form.title.data paste.body = form.body.data paste.explanation = form.explanation.data paste.language = form.language.data paste.labels = form.labels.data paste.source = form.source.data paste.save() flash('Paste edited') return redirect(url_for('pastebin.pastes')) return render_template('pastebin/edit.html', paste=paste, form=form)
def index(): form = PasteForm() if form.validate_on_submit(): source = form.source.data highlighting = form.highlighting.data resource = form.resource.data is_resource = False if resource: source = resource highlighting = 'text' is_resource = True paste = Paste(source, highlighting, form.expiration.data, form.title.data, form.password.data, is_resource) db.session.add(paste) db.session.commit() return redirect(url_for('view', slug=paste.slug)) else: form.flash_errors() return render_template('index.html', form=form, js={ 'bucket_region': app.config['BOTO3_REGION'], 'bucket_name': app.config['AWS_S3_BUCKET'], 'identity_pool_id': app.config['AWS_COGNITO_IDENTITY_POOL'], })
def new_paste(): form = PasteForm(title=None, body=None, language=None, labels=None, explain=None, source=None) if form.validate_on_submit(): paste = db.Paste() paste.title = form.title.data # If a Paste with this slug already exists: paste.slug = slugify(form.title.data) pre_pastes = db.Paste.find({'slug': form.slug.data, 'deleted': False}) if pre_pastes.count(): paste.slug += '-%s' % (pre_pastes.count() +1,) paste.body = form.body.data paste.explanation = form.explanation.data paste.language = form.language.data paste.labels = form.labels.data paste.source = form.source.data paste.save() flash("Paste added") return redirect(url_for('pastebin.pastes')) return render_template('pastebin/new.html', form=form)
def edit_paste(slug): paste = get_by_slug(slug) form = PasteForm(title = paste.title, slug = paste.title, body = paste.body, explanation = paste.explanation, language = paste.language, labels = paste.labels, source = paste.source) if form.validate_on_submit(): if form.title.data != paste.title: paste.slug = slugify(form.title.data) paste.title = form.title.data paste.body = form.body.data paste.explanation = form.explanation.data paste.language = form.language.data paste.labels = form.labels.data paste.source = form.source.data paste.save() flash('Paste edited') return redirect(url_for('pastebin.pastes')) return render_template('pastebin/edit.html', paste = paste, form = form)
def discuss_paste(request,hash_key): """ Creates a new Paste, linking it with a previous one. In this way, Pastes may be enhanced and diffs can be performed """ paste_discussed = get_object_or_404(Paste, hash_key=hash_key) update_last_view(paste_discussed) clean_pastes() form = PasteForm(instance=paste_discussed) language = paste_discussed.language private = paste_discussed.private if request.method == 'POST': form = PasteForm(request.POST) if form.is_valid(): tempPaste = form.save(commit=False) tempPaste.hash_key = tempPaste.hash() # recompute the hash key if there is a collision while (is_collision(tempPaste.hash_key)): tempPaste.hash_key = tempPaste.hash() tempPaste.in_response_to = paste_discussed tempPaste.private = private tempPaste.save() return HttpResponseRedirect(tempPaste.get_absolute_url()) # visualize the Paste''' return render_to_response('discuss_paste.html', { 'error_message': None, 'paste_discussed': paste_discussed, 'form': form, },context_instance=RequestContext(request))
def new_paste(): form = PasteForm(title=None, body=None, language=None, labels=None, explain=None, source=None) if form.validate_on_submit(): paste = db.Paste() paste.title = form.title.data # If a Paste with this slug already exists: paste.slug = slugify(form.title.data) pre_pastes = db.Paste.find({'slug': form.slug.data, 'deleted': False}) if pre_pastes.count(): paste.slug += '-%s' % (pre_pastes.count() + 1, ) paste.body = form.body.data paste.explanation = form.explanation.data paste.language = form.language.data paste.labels = form.labels.data paste.source = form.source.data paste.save() flash("Paste added") return redirect(url_for('pastebin.pastes')) return render_template('pastebin/new.html', form=form)
def show_paste(paste_id): paste= Paste.query.get(paste_id) if paste == None: paste= Paste("") nav = Paste.query.all() username = request.cookies.get('username') paste_form = PasteForm() paste_form.populate(paste) return render_template('paste.html', form= paste_form, nav= nav, username= username)
def create(request): if request.method == 'POST': form = PasteForm(request.POST) if form.is_valid(): paste = form.save() return HttpResponseRedirect(paste.get_absolute_url()) else: form = PasteForm() return render_to_response('geopastebin/create.html', { 'form' : form, }, context_instance=RequestContext(request))
def match_modules(): data= getPostData() if data != None: if "data" in data: m= module_handler.match(data["data"]) m.sort(key= lambda x:x["score"], reverse= True) paste_form= PasteForm() paste_form.populateModules(m) return render_template('module.html', form= paste_form) return "Error while trying to match modules!";
def delete_paste(slug): paste = db.Paste.find({"slug": slug}) form = PasteForm(delete = paste.deleted) if form.validate_on_submit(): paste.deleted = form.delete.data if paste.deleted: paste.delete_date = datetime.utcnow() paste.save() flash("Paste deleted") return redirect(url_for('pastebin.pastes')) return render_template('pastebin/delete.html', paste=paste, form=form)
def new(): form = PasteForm(request.form) if request.method == 'POST' and form.validate(): title = form.title.data text = form.text.data lang = form.language.data lexer = get_lexer_by_name(lang, stripall=False) formatter = HtmlFormatter(linenos=True, cssclass="colorful") data = highlight(text, lexer, formatter) paste = Paste(title=title, data=data, language=lang) db.session.add(paste) db.session.commit() return redirect(url_for('show', paste_id=paste.id)) return render_template("paste_form.html", form=form)
def index(request): """Displays form.""" data = {"menu": "index", "max_characters": settings.MAX_CHARACTERS} if request.method == "POST": paste = Paste( slug=random_id(), paste_ip=request.META["REMOTE_ADDR"], paste_agent=request.META["HTTP_USER_AGENT"] ) form = PasteForm(request.POST, instance=paste) if not form.is_valid(): data["form"] = form return render(request, "paste/index.html", data) form.save() # Some logic added to overrided method, see forms.py return redirect(reverse("paste", kwargs={"slug": paste.slug, "renderer": settings.DEFAULT_RENDERER})) data["form"] = PasteForm() return render(request, "paste/index.html", data)
def delete_paste(slug): paste = db.Paste.find({"slug": slug}) form = PasteForm(delete=paste.deleted) if form.validate_on_submit(): paste.deleted = form.delete.data if paste.deleted: paste.delete_date = datetime.utcnow() paste.save() flash("Paste deleted") return redirect(url_for('pastebin.pastes')) return render_template('pastebin/delete.html', paste=paste, form=form)
def home(request): if request.method == 'POST': logging.info(request.POST) print request.POST form = PasteForm(request.POST) if form.is_valid(): guest = User.objects.get_or_create(username='******')[0] Snippet.objects.create(owner=guest,title=request.POST['title'], language=request.POST['language'],code=request.POST['snippet']) return HttpResponseRedirect(reverse('home')) else: form = PasteForm() template = "index.html" return render_to_response(template,{'form': form}, context_instance=RequestContext(request))
def home(request): if request.method == 'POST': logging.info(request.POST) print request.POST form = PasteForm(request.POST) if form.is_valid(): guest = User.objects.get_or_create(username='******')[0] Snippet.objects.create(owner=guest, title=request.POST['title'], language=request.POST['language'], code=request.POST['snippet']) return HttpResponseRedirect(reverse('home')) else: form = PasteForm() template = "index.html" return render_to_response(template, {'form': form}, context_instance=RequestContext(request))
def add_paste(request): """ Adds a new paste in the database """ clean_pastes() if request.method == 'POST': # If the form has been submitted... form = PasteForm(request.POST) # creates a PasteForm object with the posted data if form.is_valid(): tempPaste = form.save(commit=False) tempPaste.hash_key = tempPaste.hash() # recompute the hash key if there is a collision while (is_collision(tempPaste.hash_key)): tempPaste.hash_key = tempPaste.hash() tempPaste.save() remember_the_paste(request,tempPaste) return HttpResponseRedirect(tempPaste.get_absolute_url()) # visualize the Paste else: form = PasteForm() # A new form return render_to_response('paste_form.html', { 'error_message': None, 'form': form, },context_instance=RequestContext(request))
def index(request): if request.method == 'POST': form = PasteForm(request.POST) if form.is_valid: form.save() form = PasteForm() else: form = PasteForm() pastes = Paste.objects.all() paginator = Paginator(pastes, 15) page = request.GET.get('page') try: data = paginator.page(page) except PageNotAnInteger: data = paginator.page(1) except EmptyPage: data = paginator.page(paginator.num_pages) return render(request, 'base.html', {'form': form, 'data': data})
def create_paste(): form= PasteForm(request.form) if request.method == 'POST' : paste= None if form.id.data != None: paste= Paste.query.get(form.id.data) if paste != None: paste.author= form.author.data paste.content= form.content.data paste.name= form.name.data #remove modules for module in paste.modules: db.session.delete(module) if paste == None: paste= Paste(author= form.author.data, content= form.content.data, name= form.name.data) db.session.add(paste) db.session.commit() for module in form.modules: if module.expanded.data == 0: #skip content of non-expanded modules module.content.data= "" m= Module(name= module.mod_name.data, score= module.score.data, content= module.content.data, module_id= module.module_id.data, paste_id= paste.id) db.session.add(m) db.session.commit() return redirect(url_for('show_paste', paste_id=paste.id)) return "0"
def index(request): """Displays form.""" data = {'menu': 'index', 'max_characters': settings.MAX_CHARACTERS} if request.method == 'POST': paste = Paste(slug=random_id(), paste_ip=request.META['REMOTE_ADDR'], paste_agent=request.META['HTTP_USER_AGENT']) form = PasteForm(request.POST, instance=paste) if not form.is_valid(): data['form'] = form return render(request, 'paste/index.html', data) form.save() # Some logic added to overrided method, see forms.py return redirect( reverse('paste', kwargs={ 'slug': paste.slug, 'renderer': settings.DEFAULT_RENDERER })) data['form'] = PasteForm() return render(request, 'paste/index.html', data)