def attach_link_from_form(dbo, username, linktype, linkid, post): """ Attaches a link to a web resource from a form """ existingvid = db.query_int(dbo, "SELECT COUNT(*) FROM media WHERE WebsiteVideo = 1 " \ "AND LinkID = %d AND LinkTypeID = %d" % ( int(linkid), int(linktype) )) defvid = 0 if existingvid == 0 and post.integer("linktype") == MEDIATYPE_VIDEO_LINK: defvid = 1 mediaid = db.get_id(dbo, "media") url = post["linktarget"] if url.find("://") == -1: url = "http://" + url al.debug("attached link %s" % url, "media.attach_file_from_form") sql = db.make_insert_sql("media", ( ( "ID", db.di(mediaid) ), ( "MediaName", db.ds(url) ), ( "MediaType", post.db_integer("linktype") ), ( "MediaNotes", post.db_string("comments") ), ( "WebsitePhoto", db.di(0) ), ( "WebsiteVideo", db.di(defvid) ), ( "DocPhoto", db.di(0) ), ( "ExcludeFromPublish", db.di(0) ), # ASM2_COMPATIBILITY ( "NewSinceLastPublish", db.di(1) ), ( "UpdatedSinceLastPublish", db.di(0) ), # ASM2_COMPATIBILITY ( "LinkID", db.di(linkid) ), ( "LinkTypeID", db.di(linktype) ), ( "Date", db.nowsql() ) )) db.execute(dbo, sql) audit.create(dbo, username, "media", mediaid, str(mediaid) + ": for " + str(linkid) + "/" + str(linktype) + ": link to " + post["linktarget"])
def update_file_content(dbo, username, mid, content): """ Updates the dbfs content for the file pointed to by id """ dbfs.replace_string(dbo, content, get_name_for_id(dbo, mid)) db.execute(dbo, "UPDATE media SET Date = %s WHERE ID = %d" % ( db.nowsql(), int(mid) )) audit.edit(dbo, username, "media", str(mid) + " changed file contents")
def attach_link_from_form(dbo, username, linktype, linkid, data): """ Attaches a link to a web resource from a form """ existingvid = db.query_int(dbo, "SELECT COUNT(*) FROM media WHERE WebsiteVideo = 1 " \ "AND LinkID = %d AND LinkTypeID = %d" % ( int(linkid), int(linktype) )) defvid = 0 if existingvid == 0 and utils.df_ki(data, "linktype") == MEDIATYPE_VIDEO_LINK: defvid = 1 mediaid = db.get_id(dbo, "media") url = utils.df_ks(data, "linktarget") if url.find("://") == -1: url = "http://" + url sql = db.make_insert_sql("media", ( ( "ID", db.di(mediaid) ), ( "MediaName", db.ds(url) ), ( "MediaType", utils.df_s(data, "linktype") ), ( "MediaNotes", utils.df_t(data, "comments") ), ( "WebsitePhoto", db.di(0) ), ( "WebsiteVideo", db.di(defvid) ), ( "DocPhoto", db.di(0) ), ( "ExcludeFromPublish", db.di(0) ), ( "NewSinceLastPublish", db.di(1) ), ( "UpdatedSinceLastPublish", db.di(0) ), ( "LinkID", db.di(linkid) ), ( "LinkTypeID", db.di(linktype) ), ( "Date", db.nowsql() ) )) db.execute(dbo, sql) audit.create(dbo, username, "media", str(mediaid) + ": for " + str(linkid) + "/" + str(linktype) + ": link to " + utils.df_ks(data, "linktarget"))
def create_document_media(dbo, username, linktype, linkid, template, content): """ Creates a new media record for a document for the link given. linktype: ANIMAL, PERSON, etc linkid: ID for the link template: The name of the template used to create the document content: The document contents """ mediaid = db.get_id(dbo, "media") sql = db.make_insert_sql( "media", (("ID", db.di(mediaid)), ("MediaName", db.ds("%d.html" % mediaid)), ("MediaType", db.di(0)), ("MediaNotes", db.ds(template)), ("WebsitePhoto", db.di(0)), ("WebsiteVideo", db.di(0)), ("DocPhoto", db.di(0)), ("ExcludeFromPublish", db.di(0)), ("NewSinceLastPublish", db.di(1)), ("UpdatedSinceLastPublish", db.di(0)), ("LinkID", db.di(linkid)), ("LinkTypeID", db.di(linktype)), ("Date", db.nowsql()))) db.execute(dbo, sql) path = "" if linktype == ANIMAL: path = "/animal" elif linktype == PERSON: path = "/owner" elif linktype == LOSTANIMAL: path = "/lostanimal" elif linktype == FOUNDANIMAL: path = "/foundanimal" path += "/" + str(linkid) name = str(mediaid) + ".html" dbfs.put_string(dbo, name, path, content) audit.create(dbo, username, "media", str(mediaid) + ": for " + str(linkid) + "/" + str(linktype))
def attach_link_from_form(dbo, username, linktype, linkid, data): """ Attaches a link to a web resource from a form """ existingvid = db.query_int(dbo, "SELECT COUNT(*) FROM media WHERE WebsiteVideo = 1 " \ "AND LinkID = %d AND LinkTypeID = %d" % ( int(linkid), int(linktype) )) defvid = 0 if existingvid == 0 and utils.df_ki(data, "linktype") == MEDIATYPE_VIDEO_LINK: defvid = 1 mediaid = db.get_id(dbo, "media") url = utils.df_ks(data, "linktarget") if url.find("://") == -1: url = "http://" + url sql = db.make_insert_sql( "media", (("ID", db.di(mediaid)), ("MediaName", db.ds(url)), ("MediaType", utils.df_s(data, "linktype")), ("MediaNotes", utils.df_t(data, "comments")), ("WebsitePhoto", db.di(0)), ("WebsiteVideo", db.di(defvid)), ("DocPhoto", db.di(0)), ("ExcludeFromPublish", db.di(0)), ("NewSinceLastPublish", db.di(1)), ("UpdatedSinceLastPublish", db.di(0)), ("LinkID", db.di(linkid)), ("LinkTypeID", db.di(linktype)), ("Date", db.nowsql()))) db.execute(dbo, sql) audit.create( dbo, username, "media", str(mediaid) + ": for " + str(linkid) + "/" + str(linktype) + ": link to " + utils.df_ks(data, "linktarget"))
def update_file_content(dbo, username, mid, content): """ Updates the dbfs content for the file pointed to by id """ dbfs.replace_string(dbo, content, get_name_for_id(dbo, mid)) db.execute( dbo, "UPDATE media SET Date = %s WHERE ID = %d" % (db.nowsql(), int(mid))) audit.edit(dbo, username, "media", str(mid) + " changed file contents")
def create_blank_document_media(dbo, username, linktype, linkid): """ Creates a new media record for a blank document for the link given. linktype: ANIMAL, PERSON, etc linkid: ID for the link returns the new media id """ mediaid = db.get_id(dbo, "media") sql = db.make_insert_sql( "media", ( ("ID", db.di(mediaid)), ("MediaName", db.ds("%d.html" % mediaid)), ("MediaType", db.di(0)), ("MediaNotes", db.ds("New document")), ("WebsitePhoto", db.di(0)), ("WebsiteVideo", db.di(0)), ("DocPhoto", db.di(0)), ("ExcludeFromPublish", db.di(0)), # ASM2_COMPATIBILITY ("NewSinceLastPublish", db.di(1)), ("UpdatedSinceLastPublish", db.di(0)), # ASM2_COMPATIBILITY ("LinkID", db.di(linkid)), ("LinkTypeID", db.di(linktype)), ("Date", db.nowsql()))) db.execute(dbo, sql) path = "" if linktype == ANIMAL: path = "/animal" elif linktype == PERSON: path = "/owner" elif linktype == LOSTANIMAL: path = "/lostanimal" elif linktype == FOUNDANIMAL: path = "/foundanimal" path += "/" + str(linkid) name = str(mediaid) + ".html" dbfs.put_string(dbo, name, path, "") audit.create(dbo, username, "media", str(mediaid) + ": for " + str(linkid) + "/" + str(linktype)) return mediaid
def rotate_media(dbo, username, mid, clockwise = True): """ Rotates an image media record 90 degrees if clockwise is true, or 270 degrees if false """ mr = db.query(dbo, "SELECT * FROM media WHERE ID=%d" % int(mid)) if len(mr) == 0: raise utils.ASMError("Record does not exist") mr = mr[0] mn = mr["MEDIANAME"] # If it's not a jpg image, we can stop right now ext = mn[mn.rfind("."):].lower() if ext != ".jpg" and ext != ".jpeg": raise utils.ASMError("Image is not a JPEG file, cannot rotate") # Load the image data path = get_dbfs_path(mr["LINKID"], mr["LINKTYPEID"]) imagedata = dbfs.get_string(dbo, mn, path) imagedata = rotate_image(imagedata, clockwise) # Store it back in the dbfs and add an entry to the audit trail dbfs.put_string(dbo, mn, path, imagedata) # Update the date stamp on the media record db.execute(dbo, "UPDATE media SET Date = %s WHERE ID = %d" % (db.nowsql(), mid)) audit.edit(dbo, username, "media", "media id %d rotated, clockwise=%s" % (mid, str(clockwise)))
def create_document_media(dbo, username, linktype, linkid, template, content): """ Creates a new media record for a document for the link given. linktype: ANIMAL, PERSON, etc linkid: ID for the link template: The name of the template used to create the document content: The document contents """ mediaid = db.get_id(dbo, "media") sql = db.make_insert_sql("media", ( ( "ID", db.di(mediaid) ), ( "MediaName", db.ds("%d.html" % mediaid) ), ( "MediaType", db.di(0)), ( "MediaNotes", db.ds(template) ), ( "WebsitePhoto", db.di(0) ), ( "WebsiteVideo", db.di(0) ), ( "DocPhoto", db.di(0) ), ( "ExcludeFromPublish", db.di(0) ), # ASM2_COMPATIBILITY ( "NewSinceLastPublish", db.di(1) ), ( "UpdatedSinceLastPublish", db.di(0) ), # ASM2_COMPATIBILITY ( "LinkID", db.di(linkid) ), ( "LinkTypeID", db.di(linktype) ), ( "Date", db.nowsql() ) )) db.execute(dbo, sql) path = "" if linktype == ANIMAL: path = "/animal" elif linktype == PERSON: path = "/owner" elif linktype == LOSTANIMAL: path = "/lostanimal" elif linktype == FOUNDANIMAL: path = "/foundanimal" path += "/" + str(linkid) name = str(mediaid) + ".html" dbfs.put_string(dbo, name, path, content) audit.create(dbo, username, "media", mediaid, str(mediaid) + ": for " + str(linkid) + "/" + str(linktype))
def create_blank_document_media(dbo, username, linktype, linkid): """ Creates a new media record for a blank document for the link given. linktype: ANIMAL, PERSON, etc linkid: ID for the link returns the new media id """ mediaid = db.get_id(dbo, "media") sql = db.make_insert_sql("media", ( ( "ID", db.di(mediaid) ), ( "MediaName", db.ds("%d.html" % mediaid) ), ( "MediaType", db.di(0)), ( "MediaNotes", db.ds("New document") ), ( "WebsitePhoto", db.di(0) ), ( "WebsiteVideo", db.di(0) ), ( "DocPhoto", db.di(0) ), ( "ExcludeFromPublish", db.di(0) ), ( "NewSinceLastPublish", db.di(1) ), ( "UpdatedSinceLastPublish", db.di(0) ), ( "LinkID", db.di(linkid) ), ( "LinkTypeID", db.di(linktype) ), ( "Date", db.nowsql() ) )) db.execute(dbo, sql) path = "" if linktype == ANIMAL: path = "/animal" elif linktype == PERSON: path = "/owner" elif linktype == LOSTANIMAL: path = "/lostanimal" elif linktype == FOUNDANIMAL: path = "/foundanimal" path += "/" + str(linkid) name = str(mediaid) + ".html" dbfs.put_string(dbo, name, path, "") audit.create(dbo, username, "media", str(mediaid) + ": for " + str(linkid) + "/" + str(linktype)) return mediaid
def rotate_media(dbo, username, mid, clockwise=True): """ Rotates an image media record 90 degrees if clockwise is true, or 270 degrees if false """ mr = db.query(dbo, "SELECT * FROM media WHERE ID=%d" % int(mid)) if len(mr) == 0: raise utils.ASMError("Record does not exist") mr = mr[0] mn = mr["MEDIANAME"] # If it's not a jpg image, we can stop right now ext = mn[mn.rfind("."):].lower() if ext != ".jpg" and ext != ".jpeg": raise utils.ASMError("Image is not a JPEG file, cannot rotate") # Load the image data path = get_dbfs_path(mr["LINKID"], mr["LINKTYPEID"]) imagedata = dbfs.get_string(dbo, mn, path) imagedata = rotate_image(imagedata, clockwise) # Store it back in the dbfs and add an entry to the audit trail dbfs.put_string(dbo, mn, path, imagedata) # Update the date stamp on the media record db.execute(dbo, "UPDATE media SET Date = %s WHERE ID = %d" % (db.nowsql(), mid)) audit.edit(dbo, username, "media", "media id %d rotated, clockwise=%s" % (mid, str(clockwise)))
def attach_link_from_form(dbo, username, linktype, linkid, post): """ Attaches a link to a web resource from a form """ existingvid = db.query_int(dbo, "SELECT COUNT(*) FROM media WHERE WebsiteVideo = 1 " \ "AND LinkID = %d AND LinkTypeID = %d" % ( int(linkid), int(linktype) )) defvid = 0 if existingvid == 0 and post.integer("linktype") == MEDIATYPE_VIDEO_LINK: defvid = 1 mediaid = db.get_id(dbo, "media") url = post["linktarget"] if url.find("://") == -1: url = "http://" + url al.debug("attached link %s" % url, "media.attach_file_from_form") sql = db.make_insert_sql( "media", ( ("ID", db.di(mediaid)), ("MediaName", db.ds(url)), ("MediaType", post.db_integer("linktype")), ("MediaNotes", post.db_string("comments")), ("WebsitePhoto", db.di(0)), ("WebsiteVideo", db.di(defvid)), ("DocPhoto", db.di(0)), ("ExcludeFromPublish", db.di(0)), # ASM2_COMPATIBILITY ("NewSinceLastPublish", db.di(1)), ("UpdatedSinceLastPublish", db.di(0)), # ASM2_COMPATIBILITY ("LinkID", db.di(linkid)), ("LinkTypeID", db.di(linktype)), ("Date", db.nowsql()))) db.execute(dbo, sql) audit.create( dbo, username, "media", str(mediaid) + ": for " + str(linkid) + "/" + str(linktype) + ": link to " + post["linktarget"])
def attach_file_from_form(dbo, username, linktype, linkid, data): """ Attaches a media file from the posted form data is the web.py data object and should contain comments, the filechooser object, with filename and value props - OR a parameter called base64image containing a base64 encoded jpg image. """ ext = "" base64data = "" base64image = utils.df_ks(data, "base64image") if base64image != "": ext = ".jpg" # If an HTML5 data url was used, strip the prefix so we just have base64 data if base64image.find("data:") != -1: base64data = base64image[base64image.find(",")+1:] # Browser escaping turns base64 pluses back into spaces, so switch back base64data = base64data.replace(" ", "+") else: base64data = base64image else: ext = data.filechooser.filename ext = ext[ext.rfind("."):].lower() mediaid = db.get_id(dbo, "media") medianame = "%d%s" % ( mediaid, ext ) ispicture = ext == ".jpg" or ext == ".jpeg" ispdf = ext == ".pdf" # Does this link have anything with web/doc set? If not, set the # web/doc flags web = 0 doc = 0 existing_web = db.query_int(dbo, "SELECT COUNT(*) FROM media WHERE WebsitePhoto = 1 " \ "AND LinkID = %d AND LinkTypeID = %d" % ( int(linkid), int(linktype) )) existing_doc = db.query_int(dbo, "SELECT COUNT(*) FROM media WHERE DocPhoto = 1 " \ "AND LinkID = %d AND LinkTypeID = %d" % ( int(linkid), int(linktype) )) if existing_web == 0 and ispicture: web = 1 if existing_doc == 0 and ispicture: doc = 1 if base64image != "": filedata = base64.b64decode(base64data) else: filedata = data.filechooser.value # Is it a picture? if ispicture: # Autorotate it to match the EXIF orientation filedata = auto_rotate_image(filedata) # Scale it down to the system set size scalespec = configuration.incoming_media_scaling(dbo) if scalespec != "None": filedata = scale_image(filedata, scalespec) # Is it a PDF? If so, compress it if we can and the option is on if ispdf and gs_installed() and SCALE_PDF: filedata = scale_pdf(filedata) medianame = "%d_scaled.pdf" % mediaid # Attach the file in the dbfs path = get_dbfs_path(linkid, linktype) dbfs.put_string(dbo, medianame, path, filedata) # Are the notes for an image blank and we're defaulting them from animal comments? comments = utils.df_ks(data, "comments") if comments == "" and ispicture and linktype == ANIMAL and configuration.auto_media_notes(dbo): comments = animal.get_comments(dbo, int(linkid)) # Are the notes blank and we're defaulting them from the filename? elif comments == "" and configuration.default_media_notes_from_file(dbo) and base64image == "": comments = utils.filename_only(data.filechooser.filename) # Create the media record sql = db.make_insert_sql("media", ( ( "ID", db.di(mediaid) ), ( "MediaName", db.ds(medianame) ), ( "MediaType", db.di(0) ), ( "MediaNotes", db.ds(comments) ), ( "WebsitePhoto", db.di(web) ), ( "WebsiteVideo", db.di(0) ), ( "DocPhoto", db.di(doc) ), ( "ExcludeFromPublish", db.di(0) ), ( "NewSinceLastPublish", db.di(1) ), ( "UpdatedSinceLastPublish", db.di(0) ), ( "LinkID", db.di(linkid) ), ( "LinkTypeID", db.di(linktype) ), ( "Date", db.nowsql() ) )) db.execute(dbo, sql) audit.create(dbo, username, "media", str(mediaid) + ": for " + str(linkid) + "/" + str(linktype))
def attach_file_from_form(dbo, username, linktype, linkid, data): """ Attaches a media file from the posted form data is the web.py data object and should contain comments, the filechooser object, with filename and value props - OR a parameter called base64image containing a base64 encoded jpg image. """ ext = "" base64data = "" base64image = utils.df_ks(data, "base64image") if base64image != "": ext = ".jpg" # If an HTML5 data url was used, strip the prefix so we just have base64 data if base64image.find("data:") != -1: base64data = base64image[base64image.find(",") + 1:] # Browser escaping turns base64 pluses back into spaces, so switch back base64data = base64data.replace(" ", "+") else: base64data = base64image else: ext = data.filechooser.filename ext = ext[ext.rfind("."):].lower() mediaid = db.get_id(dbo, "media") medianame = "%d%s" % (mediaid, ext) ispicture = ext == ".jpg" or ext == ".jpeg" ispdf = ext == ".pdf" # Does this link have anything with web/doc set? If not, set the # web/doc flags web = 0 doc = 0 existing_web = db.query_int(dbo, "SELECT COUNT(*) FROM media WHERE WebsitePhoto = 1 " \ "AND LinkID = %d AND LinkTypeID = %d" % ( int(linkid), int(linktype) )) existing_doc = db.query_int(dbo, "SELECT COUNT(*) FROM media WHERE DocPhoto = 1 " \ "AND LinkID = %d AND LinkTypeID = %d" % ( int(linkid), int(linktype) )) if existing_web == 0 and ispicture: web = 1 if existing_doc == 0 and ispicture: doc = 1 if base64image != "": filedata = base64.b64decode(base64data) else: filedata = data.filechooser.value # Is it a picture? if ispicture: # Autorotate it to match the EXIF orientation filedata = auto_rotate_image(filedata) # Scale it down to the system set size scalespec = configuration.incoming_media_scaling(dbo) if scalespec != "None": filedata = scale_image(filedata, scalespec) # Is it a PDF? If so, compress it if we can and the option is on if ispdf and gs_installed() and SCALE_PDF: filedata = scale_pdf(filedata) medianame = "%d_scaled.pdf" % mediaid # Attach the file in the dbfs path = get_dbfs_path(linkid, linktype) dbfs.put_string(dbo, medianame, path, filedata) # Are the notes for an image blank and we're defaulting them from animal comments? comments = utils.df_ks(data, "comments") if comments == "" and ispicture and linktype == ANIMAL and configuration.auto_media_notes( dbo): comments = animal.get_comments(dbo, int(linkid)) # Are the notes blank and we're defaulting them from the filename? elif comments == "" and configuration.default_media_notes_from_file( dbo) and base64image == "": comments = utils.filename_only(data.filechooser.filename) # Create the media record sql = db.make_insert_sql( "media", (("ID", db.di(mediaid)), ("MediaName", db.ds(medianame)), ("MediaType", db.di(0)), ("MediaNotes", db.ds(comments)), ("WebsitePhoto", db.di(web)), ("WebsiteVideo", db.di(0)), ("DocPhoto", db.di(doc)), ("ExcludeFromPublish", db.di(0)), ("NewSinceLastPublish", db.di(1)), ("UpdatedSinceLastPublish", db.di(0)), ("LinkID", db.di(linkid)), ("LinkTypeID", db.di(linktype)), ("Date", db.nowsql()))) db.execute(dbo, sql) audit.create(dbo, username, "media", str(mediaid) + ": for " + str(linkid) + "/" + str(linktype))