def import_entries(): '''import db entries from json (on server)''' if not session.get('logged_in'): abort(401) # read json with open(current_app.config['DB_ENTRIES_JSON_DUMP'], 'r') as f: entries = json.load(f) # instantiate pages and write to db for entry in entries: # check if already present cur = g.db.execute( '''SELECT ref FROM entries WHERE id = ?''', (entry['id'],)) row = cur.fetchone() if row != None: flash("Entry already in db: ID: {} ref: {}".format( entry['id'], row[0] )) continue page_obj = Page( entry['id'], entry['type'], entry['title'], entry['author'], entry['date_norm'], entry['time_norm'], entry['tags'], entry['body_md'], entry['pub'] ) page_obj.db_write_new_entry() flash("Imported entry ID: {}".format(entry['id'])) return redirect(url_for('interface.overview'))
def edit(): '''edit a page entry''' if not session.get('logged_in'): abort(401) # POST # button pressed on edit page (preview / save / cancel) if request.method == 'POST': action = request.form['actn'] if action == "cancel": return redirect(url_for('interface.overview')) # request data and instantiate Page object type = request.form['type'] if type == 'custom': custom_type = request.form['custom_type'] if custom_type == '': type = 'undefined' flash("Warning: custom type not specified, setting to undefined.") else: type = custom_type page_obj = Page( request.form['id'], type, request.form['title'], current_app.config['AUTHOR_NAME'], #request.form['date'], #request.form['time'], datetime.now().strftime('%Y-%m-%d'), datetime.now().strftime('%H:%M'), request.form['tags'], request.form['text-input'] ) # actions # add images from subpath if action == "add_imgs": images_subpath = request.form['imagepath'] # create and add image markdown images = get_images_from_path(images_subpath) img_md = gen_image_md(images_subpath, images) # update object page_obj.body_md = page_obj.body_md + img_md page_obj.update_images() # return return render_template( 'edit.html', preview = False, id = page_obj.id, page = page_obj, images = page_obj.images ) # upload selected images elif action == "upld_imgs": if not request.files.getlist("files"): abort(404) else: files = request.files.getlist("files") filenames = [] for file in files: if file and allowed_image_file(file.filename): subpath = gen_image_subpath() filename = secure_filename(file.filename) filepath_abs = os.path.join( current_app.config['RUN_ABSPATH'], 'media', subpath, filename ) if not os.path.isfile(filepath_abs): file.save(filepath_abs) else: flash("File w/ same name already present, not saved: {}".format(filename)) filenames.append(filename) else: flash("Not a valid image file: {}".format(file.filename)) if filenames != []: # generate markdown img_md = gen_image_md(subpath, filenames) # update object page_obj.body_md = page_obj.body_md + img_md page_obj.update_images() # return to edit return render_template( 'edit.html', preview = False, id = page_obj.id, page = page_obj, images = page_obj.images ) elif action == "preview" or action == "save": if action == "preview": return render_template( 'edit.html', preview = True, id = page_obj.id, page = page_obj, images = page_obj.images ) elif action == "save": if page_obj.id == "new": page_obj.db_write_new_entry() flash("New Page saved successfully!") else: page_obj.db_update_entry() flash("Page ID {} saved successfully!".format(page_obj.id)) return redirect(url_for('interface.overview')) else: abort(404) # GET # (loading from overview) else: id = request.args.get('id') if id == "new": # create new return render_template( 'edit.html', preview = False, id = id, page = None ) elif id == None: # abort for now abort(404) else: row = db_load_to_edit(id) return render_template( 'edit.html', preview = False, id = id, page = row, images = get_images_from_md(row['body_md']) )