def upload_file(): post = request.form newfile = request.files.get("newfile") try: newname = newfile.filename except AttributeError: # Will happen if newfile is None abort(400, "No file specified") target_file = os.path.join(UPLOAD_DIR, newname.replace(os.sep, "_")) with open(target_file, "wb") as file_obj: shutil.copyfileobj(newfile.stream, file_obj) newfile.stream.close() file_size = os.stat(target_file)[stat.ST_SIZE] fsize = utils.human_fmt(file_size).replace(" ","") # Don't use the CDN; use the generic download URL that will redirect. fldr = {"c": "cb", "d": "dabo"}.get(post["section"], "") cfile = os.path.join(DLBASE, fldr, newname) sql = """INSERT INTO files (ctype, ctitle, mdesc, cfile, ccosttype, ncost, csize, cauthor, cauthoremail, dlastupd, lpublish) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""" args = (post.get("section"), post.get("title"), post.get("description"), cfile, post.get("file_license"), post.get("cost"), fsize, post.get("author"), post.get("author_email"), datetime.date.today(), False) crs = utils.get_cursor() crs.execute(sql, args) body = """Originating IP = %s Section = %s Title = %s File = %s License = %s Cost = %s Size = %s Author = %s Email = %s Description: %s """ % (request.remote_addr, post.get("section"), post.get("title"), newname, post.get("file_license"), post.get("cost"), fsize, post.get("author"), post.get("author_email"), post.get("description")) msg = """From: File Uploads <*****@*****.**> X-Mailer: flask script To: Ed Leafe <*****@*****.**> Subject: New Uploaded File Date: %s %s """ % (time.strftime("%c"), body) smtp = smtplib.SMTP("mail.leafe.com") smtp.sendmail("*****@*****.**", "*****@*****.**", msg) g.message = "Your file has been uploaded." return render("upload.html")
def show(pkid): sql = "select * from image where pkid = %s" with utils.DbCursor() as crs: res = crs.execute(sql, (pkid, )) if not res: abort(404) g.image = crs.fetchone() g.image["size"] = utils.human_fmt(g.image["size"]) return render_template("image_detail.html")
def _after_get(cls, rec): """We need to add the frameset name, if any, to the record.""" sql = """select frameset.name from frameset where frameset.pkid = %s;""" with utils.DbCursor() as crs: crs.execute(sql, (rec["frameset_id"], )) name_rec = crs.fetchone() rec["frameset_name"] = name_rec.get("name") if name_rec else "" fs = rec["freespace"] rec["freespace"] = utils.human_fmt(fs)
def GET_list(orient=None, filt=None): g.orient = orient or "A" orient_order = "HVSAH" orient_pos = orient_order.index(g.orient) g.next_orient = orient_order[orient_pos + 1] kwargs = {"orientation": orient} if orient else {} if filt: kwargs["keywords"] = filt g.images = [img.to_dict() for img in entities.Image.list(**kwargs)] # list_db_images(orient=g.orient, filt=filt) for img in g.images: img["size"] = utils.human_fmt(img["size"]) g.thumb_path = os.path.join(IMAGE_FOLDER, "thumbs") return render_template("image_list.html")
def _after_list(cls, recs): sql = """select frame.pkid, frameset.name, frameset.album_id from frame join frameset on frame.frameset_id = frameset.pkid;""" with utils.DbCursor() as crs: crs.execute(sql) frameset_name_recs = crs.fetchall() name_mapping = {rec["pkid"]: rec["name"] for rec in frameset_name_recs} album_mapping = { rec["pkid"]: rec["album_id"] for rec in frameset_name_recs } for rec in recs: rec["frameset_name"] = name_mapping.get(rec["pkid"], "-none-") rec["album_id"] = album_mapping.get(rec["pkid"], rec["album_id"]) rec["freespace"] = utils.human_fmt(rec["freespace"])
def manage_images(album_id, filter_term=None): album_obj = entities.Album.get(album_id) if album_obj.smart: return view_smart_images(album_obj) g.album = album_obj.to_dict() orientation = g.album["orientation"] all_images = entities.Image.list() imgs = [ img.to_dict() for img in all_images if img.orientation in ("S", orientation) ] for img in imgs: img["size"] = utils.human_fmt(img["size"]) img["selected"] = img["pkid"] in album_obj.image_ids if filter_term: imgs = [ img for img in imgs if img["selected"] or filter_term in img["keywords"] ] crea = [(im["name"], im["created"], type(im["created"])) for im in imgs] imgs.sort(key=lambda x: (x["selected"], x["created"]), reverse=True) g.images = imgs g.image_count = len(imgs) return render_template("album_images.html")