def pate_create(): content = flask.request.form.get('content', None) if not content: flask.abort(400) try: timeout = int(flask.request.form.get('timeout', 60)) except (ValueError, TypeError): return 'invalid "timeout" value', 400 if timeout < 1: return 'timeout has to be greater than 0', 400 # convert to minutes if timeout > 60 * 60 * 24 * 2: return 'timeout has to be smaller than 2 days', 400 paste = Paste(content=content) paste.save(timeout) app.logger.info('page created by %s, size %s', flask.request.remote_addr, len(paste.content)) # context negotiation does not work well here user_agent = flask.request.headers.get('user-agent') if user_agent and user_agent.startswith('curl'): url = 'http://{}/{}\n'.format(flask.request.host, paste.key) return flask.Response(url, content_type='text/plain; charset=utf-8') return flask.redirect(flask.url_for('.paste_show', key=paste.key))
def uploadFile(): f = request.files['file'] if allowed_file(f.filename): foundPaste = Paste.query.filter_by(url=f.filename).first() if foundPaste is not None: return redirect('/error') try: now = datetime.datetime.now() username = request.form['username'] print('username', username) newPaste = Paste(url=f.filename, date=now.strftime("%d-%m-%Y"), language='none', uploadType='file', username=username) db.session.add(newPaste) db.session.commit() except Exception as e: print(e) return redirect('/error') filename = secure_filename(f.filename) f.save(os.path.join(APP_ROOT + '/files/') + filename) return redirect('/') else: return redirect('/error')
def create_paste(): """ Create a paste and associate with a generated key that will be used to uniquely identify it. """ try: data = request.get_json() content = data.get("content", None) if not content: raise Exception except Exception: abort(400) # check that key doesn't already exist in the db key = get_random_url_suffix(4) while True: if Paste.query.get(key): key = get_random_url_suffix(4) continue else: break try: paste = Paste(key=key, content=content) paste.insert() except Exception: db.session.rollback() abort(500) return jsonify({"success": True, "data": paste.format()})
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 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 download(site, links): for link in links: if not link in previous: r = requests.get(site['raw'] % link, headers=config.header) paste = Paste(_id=link, content=r.text, filetype=test_filetype(r.text), site=site['archive']) try: paste.store(db) print "Stored new object %s" % link except couchdb.http.ResourceConflict as error: print "Object %s allready stored: %s" % (link, error) time.sleep(random.uniform(0.2, 2))
def new(request): while True: paste_hash = "".join([ random.choice(string.digits + string.ascii_lowercase) for i in range(6) ]) try: Paste.objects.get(paste_hash=paste_hash) except Paste.DoesNotExist: paste = Paste(create_time=timezone.now(), modified_time=timezone.now(), paste_hash=paste_hash) paste.save() return HttpResponseRedirect( reverse('paste:edit', args=(paste.paste_hash, ))) else: pass
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 post(): file = request.files['file'] key = Paste.generate_random_resource_key() Paste.upload_file(key, file) paste = Paste( key, 'text', request.form.get('expiration', 10080), request.form.get('title', file.filename), request.form.get('password'), True, ) db.session.add(paste) db.session.commit() return jsonify(code=200, slug=paste.slug, url=url_for('view', slug=paste.slug, _external=True))
def get_info(html): title, username, content, date, lables = '', '', '', '', [] title_wrapper = html.find('div', class_='pre-info') if title_wrapper is not None: title = title_wrapper.h4.text.strip() username_time_wrapper = html.find('div', class_='pre-footer') date_str = '' if username_time_wrapper.a is not None: username = username_time_wrapper.a.text date_str = (str(username_time_wrapper.div.div.text).split()[4:-1]) else: username = '******' date_str = (str(username_time_wrapper.div.div.text).split()[4:-1]) if date_str != '': date = " ".join(date_str).replace(",", "") date = str(arrow.get(date, 'DD MMM YYYY HH:mm:ss').to('UTC')) content_wrapper = username_time_wrapper = html.find('div', class_='text') if content_wrapper is not None: for li in content_wrapper.findAll('li'): content += li.div.text.strip() lables = analyze(content) return Paste(username, title, content, date, lables)
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 geturl(): url = request.get_json().get('url') language = request.get_json().get('language') username = request.get_json().get('username') foundPaste = Paste.query.filter_by(url=url).first() if foundPaste is not None: return jsonify({'success': False, 'status': 'URL already exists'}) file = open('files/' + url + '.txt', 'w') file.write(request.get_json().get('pasteData')) file.close() try: now = datetime.datetime.now() newPaste = Paste(url=url, date=now.strftime("%d-%m-%Y"), language=language, uploadType='code', username=username) db.session.add(newPaste) db.session.commit() return jsonify({'success': True}) except Exception as e: print(e) return jsonify({'success': False})