def get_all_files(request, config): # Open JsonDB for read only db = JsonDB(dbfile=config['FILE_LIST'], logger=config['LOGGER_NAME']) db.load() files_list_infos = {} for k, v in db.db.iteritems(): _infos = get_file_info(id_file=k, config=config, env=request.environ) if not _infos: continue files_list_infos[k] = _infos return files_list_infos
def index(): print(request.form) # if request.method == 'GET': db = JsonDB() db.load() print(request.form) if request.method == 'POST': db['todos'].append({ 'text': request.form['text'], 'priority': request.form['priority'] }) db.save() return render_template("index.html", todo=db['todos'])
def create(): # print(request.method) # print(request.form) db = JsonDB() db.load() if request.method == 'POST': db['contacts'].append({ 'first_name': request.form['first_name'], 'last_name': request.form['last_name'], 'phone': request.form['phone'], 'email': request.form['email'], 'blood_type': request.form['blood_type'], 'favorite_color': request.form['favorite_color'] }) db.save() return redirect('/') return render_template('create.html', blood_types=db['blood_types'])
def get_file(request, id_file, config): # First, open JsonDB for read-only db = JsonDB(dbfile=config['FILE_LIST']) db.load() if id_file not in db.db or db.db[id_file]['burn_after_read'] == 'Burned': return abort(404) if db.db[id_file]['burn_after_read'] == 'True': # Now, try to lock the db if not db._lock(): return "Can't lock db for burning file" db.db[id_file]['burn_after_read'] = 'Burned' db.save() db._release() filename = os.path.basename(db.db[id_file]['storage_full_filename']) LOG.info("[GET] Client %s has requested: %s (%s)" % (request.remote_addr, db.db[id_file]['real_name'], id_file)) if not os.path.isabs(config['UPLOAD_FOLDER']): path = "%s/%s" % (os.path.dirname(config['instance_path']), config['UPLOAD_FOLDER']) else: path = config['UPLOAD_FOLDER'] # If the user agent is in the display list, # format headers to direct display feature if request.user_agent.browser in config['DISPLAY_FOR']: return send_from_directory(path, filename, mimetype=db.db[id_file]['mime_type'], attachment_filename=db.db[id_file] ['real_name']) # Else keep the regular send file return send_from_directory(path, filename, mimetype=db.db[id_file]['mime_type'], attachment_filename=db.db[id_file]['real_name'], as_attachment=True)
def get_file(request, id_file, config): # First, open JsonDB for read-only db = JsonDB(dbfile=config['FILE_LIST']) db.load() if id_file not in db.db or db.db[id_file]['burn_after_read'] == 'Burned': return abort(404) if db.db[id_file]['burn_after_read'] == 'True': # Now, try to lock the db if not db._lock(): return "Can't lock db for burning file" db.db[id_file]['burn_after_read'] = 'Burned' db.save() db._release() filename = os.path.basename(db.db[id_file]['storage_full_filename']) LOG.info("[GET] Client %s has requested: %s (%s)" % (request.remote_addr, db.db[id_file]['real_name'], id_file)) if not os.path.isabs(config['UPLOAD_FOLDER']): path = "%s/%s" % (os.path.dirname(config['instance_path']), config['UPLOAD_FOLDER']) else: path = config['UPLOAD_FOLDER'] # If the user agent is in the display list, format headers to direct display feature if request.user_agent.browser in config['DISPLAY_FOR']: return send_from_directory(path, filename, mimetype=db.db[id_file]['mime_type'], attachment_filename=db.db[id_file]['real_name']) # Else keep the regular send file return send_from_directory(path, filename, mimetype=db.db[id_file]['mime_type'], attachment_filename=db.db[id_file]['real_name'], as_attachment=True)
def get_infos_file_from_md5(md5, dbfile): # Open JsonDB for read only db = JsonDB(dbfile=dbfile) db.load() return db.read(md5)
from flask import Flask, render_template, request, redirect from jsondb import JsonDB # create a new flask app app = Flask('todo') # class which reads and writes from a .json file db = JsonDB('./db.json') db.load() # index page which renders a template with todo items @app.route('/') def index(): return render_template('index.html', todos=db.data['todos']) # a view which receives a form submission # and saves a todo item to the database @app.route('/addtodo/', methods=['GET', 'POST']) def addtodo(): print(request.form) todo = {'text': request.form['text'], 'priority': request.form['priority']} db.data['todos'].append(todo) db.save() return redirect('/') app.run()
def index(): db = JsonDB() db.load() # print(db['contacts']) return render_template('index.html', contacts=db['contacts'])
def delete(index): db = JsonDB() db.load() db['contacts'].pop(index) db.save() return redirect('/')