Example #1
0
def upload_document_repository(dbo, fc):
    """
    Attaches a document from a form filechooser object and puts
    it in the /document_repository directory. 
    """
    ext = ""
    ext = fc.filename
    filename = utils.filename_only(fc.filename)
    filedata = fc.value
    ext = ext[ext.rfind("."):].lower()
    put_string(dbo, filename, "/document_repository", filedata)
Example #2
0
def upload_document_repository(dbo, fc):
    """
    Attaches a document from a form filechooser object and puts
    it in the /document_repository directory. 
    """
    ext = ""
    ext = fc.filename
    filename = utils.filename_only(fc.filename)
    filedata = fc.value
    ext = ext[ext.rfind("."):].lower()
    put_string(dbo, filename, "/document_repository", filedata)
Example #3
0
def upload_report_image(dbo, fc):
    """
    Attaches an image from a form filechooser object and puts
    it in the /reports directory. 
    """
    ext = ""
    ext = fc.filename
    filename = utils.filename_only(fc.filename)
    filedata = fc.value
    ext = ext[ext.rfind("."):].lower()
    ispicture = ext == ".jpg" or ext == ".jpeg" or ext == ".png" or ext == ".gif"
    if not ispicture:
        raise utils.ASMValidationError("upload_report_image only accepts images.")
    put_string(dbo, filename, "/reports", filedata)
Example #4
0
def upload_report_image(dbo, fc):
    """
    Attaches an image from a form filechooser object and puts
    it in the /reports directory. 
    """
    ext = ""
    ext = fc.filename
    filename = utils.filename_only(fc.filename)
    filedata = fc.value
    ext = ext[ext.rfind("."):].lower()
    ispicture = ext == ".jpg" or ext == ".jpeg" or ext == ".png" or ext == ".gif"
    if not ispicture:
        raise utils.ASMValidationError(
            "upload_report_image only accepts images.")
    put_string(dbo, filename, "/reports", filedata)
Example #5
0
def upload_document_repository(dbo, path, filename, filedata):
    """
    Attaches a document from a form filechooser object and puts
    it in the /document_repository directory. 
    An extra path portion can be specified in path.
    """
    ext = ""
    ext = filename
    filename = utils.filename_only(filename)
    ext = ext[ext.rfind("."):].lower()
    if path != "" and path.startswith("/"): path = path[1:]
    if path == "":
        filepath = "/document_repository/%s" % filename
    else:
        path = sanitise_path(path)
        filepath = "/document_repository/%s/%s" % (path, filename)
    put_string_filepath(dbo, filepath, filedata)
Example #6
0
File: dbfs.py Project: magul/asm3
def upload_document_repository(dbo, path, fc):
    """
    Attaches a document from a form filechooser object and puts
    it in the /document_repository directory. 
    An extra path portion can be specified in path.
    """
    ext = ""
    ext = fc.filename
    filename = utils.filename_only(fc.filename)
    filedata = fc.value
    ext = ext[ext.rfind("."):].lower()
    if path != "" and path.startswith("/"): path = path[1:]
    if path == "":
        filepath = "/document_repository/%s" % filename
    else:
        path = sanitise_path(path)
        filepath = "/document_repository/%s/%s" % (path, filename)
    put_string_filepath(dbo, filepath, filedata)
Example #7
0
def attach_file_from_form(dbo, username, linktype, linkid, post):
    """
    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 = post["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
        al.debug(
            "received HTML5 base64 image data (%d bytes)" % (len(base64image)),
            "media.attach_file_from_form", dbo)
    else:
        ext = post.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 = post.filedata()
        al.debug(
            "received POST file data '%s' (%d bytes)" %
            (post.filename(), len(filedata)), "media.attach_file_from_form",
            dbo)

    # Is it a picture?
    if ispicture:
        # Autorotate it to match the EXIF orientation
        filedata = auto_rotate_image(dbo, filedata)
        # Scale it down to the system set size
        scalespec = configuration.incoming_media_scaling(dbo)
        if scalespec != "None":
            filedata = scale_image(filedata, scalespec)
            al.debug(
                "scaled image to %s (%d bytes)" % (scalespec, len(filedata)),
                "media.attach_file_from_form", dbo)

    # Is it a PDF? If so, compress it if we can and the option is on
    if ispdf and SCALE_PDF_DURING_ATTACH and configuration.scale_pdfs(dbo):
        filedata = scale_pdf(filedata)
        medianame = "%d_scaled.pdf" % mediaid
        al.debug("compressed PDF (%d bytes)" % (len(filedata)),
                 "media.attach_file_from_form", dbo)

    # 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 = post["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(post.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)),
            # ASM2_COMPATIBILITY
            ("NewSinceLastPublish", db.di(1)),
            ("UpdatedSinceLastPublish", db.di(0)),
            # ASM2_COMPATIBILITY
            ("LinkID", db.di(linkid)),
            ("LinkTypeID", db.di(linktype)),
            ("Date", db.dd(i18n.now(dbo.timezone)))))
    db.execute(dbo, sql)
    audit.create(dbo, username, "media",
                 str(mediaid) + ": for " + str(linkid) + "/" + str(linktype))
Example #8
0
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))
Example #9
0
File: media.py Project: magul/asm3
def attach_file_from_form(dbo, username, linktype, linkid, post):
    """
    Attaches a media file from the posted form
    data is the web.py data object and should contain
    comments and either the filechooser object, with filename and value 
    OR filedata, filetype and filename parameters (filetype is the MIME type, filedata is base64 encoded contents)
    """
    ext = ""
    filedata = post["filedata"]
    filename = post["filename"]
    comments = post["comments"]
    checkpref = True
    if filedata != "":
        checkpref = False
        filetype = post["filetype"]
        if filetype.startswith("image"): ext = ".jpg"
        elif filetype.find("pdf") != -1: ext = ".pdf"
        elif filetype.find("html") != -1: ext = ".html"
        # Strip the data:mime prefix so we just have base64 data
        if filedata.startswith("data:"):
            filedata = filedata[filedata.find(",")+1:]
            # Browser escaping turns base64 pluses back into spaces, so switch back
            filedata = filedata.replace(" ", "+")
        filedata = base64.b64decode(filedata)
        al.debug("received HTML5 file data '%s' (%d bytes)" % (filename, len(filedata)), "media.attach_file_from_form", dbo)
    else:
        # It's a traditional form post with a filechooser, we should make
        # it the default web/doc picture after posting if none is available.
        checkpref = True
        ext = post.filename()
        ext = ext[ext.rfind("."):].lower()
        filedata = post.filedata()
        filename = post.filename()
        al.debug("received POST file data '%s' (%d bytes)" % (filename, len(filedata)), "media.attach_file_from_form", dbo)

    mediaid = db.get_id(dbo, "media")
    medianame = "%d%s" % ( mediaid, ext )
    ispicture = ext == ".jpg" or ext == ".jpeg"
    ispdf = ext == ".pdf"

    # Is it a picture?
    if ispicture:
        # Autorotate it to match the EXIF orientation
        filedata = auto_rotate_image(dbo, filedata)
        # Scale it down to the system set size
        scalespec = configuration.incoming_media_scaling(dbo)
        if scalespec != "None":
            filedata = scale_image(filedata, scalespec)
            al.debug("scaled image to %s (%d bytes)" % (scalespec, len(filedata)), "media.attach_file_from_form", dbo)

    # Is it a PDF? If so, compress it if we can and the option is on
    if ispdf and SCALE_PDF_DURING_ATTACH and configuration.scale_pdfs(dbo):
        filedata = scale_pdf(filedata)
        medianame = "%d_scaled.pdf" % mediaid
        al.debug("compressed PDF (%d bytes)" % (len(filedata)), "media.attach_file_from_form", dbo)

    # 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?
    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):
        comments = utils.filename_only(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(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.dd(i18n.now(dbo.timezone)))
        ))
    db.execute(dbo, sql)
    audit.create(dbo, username, "media", mediaid, str(mediaid) + ": for " + str(linkid) + "/" + str(linktype))

    if ispicture and checkpref:
        check_default_web_doc_pic(dbo, mediaid, linkid, linktype)
Example #10
0
def attach_file_from_form(dbo, username, linktype, linkid, post):
    """
    Attaches a media file from the posted form
    data is the web.py data object and should contain
    comments and either the filechooser object, with filename and value 
    OR filedata, filetype and filename parameters (filetype is the MIME type, filedata is base64 encoded contents)
    """
    ext = ""
    filedata = post["filedata"]
    filename = post["filename"]
    comments = post["comments"]
    if filedata != "":
        filetype = post["filetype"]
        if filetype.startswith("image") or filename.lower().endswith(".jpg"):
            ext = ".jpg"
        elif filetype.startswith("image") or filename.lower().endswith(".png"):
            ext = ".png"
        elif filetype.find("pdf") != -1 or filename.lower().endswith(".pdf"):
            ext = ".pdf"
        elif filetype.find("html") != -1 or filename.lower().endswith(".html"):
            ext = ".html"
        # Strip the data:mime prefix so we just have base64 data
        if filedata.startswith("data:"):
            filedata = filedata[filedata.find(",") + 1:]
            # Browser escaping turns base64 pluses back into spaces, so switch back
            filedata = filedata.replace(" ", "+")
        filedata = base64.b64decode(filedata)
        al.debug(
            "received data URI '%s' (%d bytes)" % (filename, len(filedata)),
            "media.attach_file_from_form", dbo)
        if ext == "":
            msg = "could not determine extension from file.type '%s', abandoning" % filetype
            al.error(msg, "media.attach_file_from_form", dbo)
            raise utils.ASMValidationError(msg)
    else:
        # It's a traditional form post with a filechooser, we should make
        # it the default web/doc picture after posting if none is available.
        ext = post.filename()
        ext = ext[ext.rfind("."):].lower()
        filedata = post.filedata()
        filename = post.filename()
        al.debug(
            "received POST file data '%s' (%d bytes)" %
            (filename, len(filedata)), "media.attach_file_from_form", dbo)

    # If we receive some images in formats other than JPG, we'll
    # pretend they're jpg as that's what they'll get transformed into
    # by scale_image
    if ext == ".png":
        ext = ".jpg"

    mediaid = dbo.get_id("media")
    medianame = "%d%s" % (mediaid, ext)
    ispicture = ext == ".jpg" or ext == ".jpeg"
    ispdf = ext == ".pdf"
    excludefrompublish = 0
    if configuration.auto_new_images_not_for_publish(dbo) and ispicture:
        excludefrompublish = 1

    # Are we allowed to upload this type of media?
    if ispicture and not configuration.media_allow_jpg(dbo):
        msg = "upload of media type jpg is disabled"
        al.error(msg, "media.attach_file_from_form", dbo)
        raise utils.ASMValidationError(msg)
    if ispdf and not configuration.media_allow_pdf(dbo):
        msg = "upload of media type pdf is disabled"
        al.error(msg, "media.attach_file_from_form", dbo)
        raise utils.ASMValidationError(msg)

    # Is it a picture?
    if ispicture:
        # Autorotate it to match the EXIF orientation
        filedata = auto_rotate_image(dbo, filedata)
        # Scale it down to the system set size
        scalespec = configuration.incoming_media_scaling(dbo)
        if scalespec != "None":
            filedata = scale_image(filedata, scalespec)
            al.debug(
                "scaled image to %s (%d bytes)" % (scalespec, len(filedata)),
                "media.attach_file_from_form", dbo)

    # Is it a PDF? If so, compress it if we can and the option is on
    if ispdf and SCALE_PDF_DURING_ATTACH and configuration.scale_pdfs(dbo):
        orig_len = len(filedata)
        filedata = scale_pdf(filedata)
        if len(filedata) < orig_len:
            al.debug("compressed PDF (%d bytes)" % (len(filedata)),
                     "media.attach_file_from_form", dbo)

    # Attach the file in the dbfs
    path = get_dbfs_path(linkid, linktype)
    dbfsid = dbfs.put_string(dbo, medianame, path, filedata)

    # Are the notes for an image blank and we're defaulting them from animal 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):
        comments = utils.filename_only(filename)

    # Create the media record
    dbo.insert(
        "media",
        {
            "ID": mediaid,
            "DBFSID": dbfsid,
            "MediaSize": len(filedata),
            "MediaName": medianame,
            "MediaMimeType": mime_type(medianame),
            "MediaType": 0,
            "MediaNotes": comments,
            "WebsitePhoto": 0,
            "WebsiteVideo": 0,
            "DocPhoto": 0,
            "ExcludeFromPublish": excludefrompublish,
            # ASM2_COMPATIBILITY
            "NewSinceLastPublish": 1,
            "UpdatedSinceLastPublish": 0,
            # ASM2_COMPATIBILITY
            "LinkID": linkid,
            "LinkTypeID": linktype,
            "Date": dbo.now(),
            "RetainUntil": None
        },
        username,
        setCreated=False,
        generateID=False)

    # Verify this record has a web/doc default if we aren't excluding it from publishing
    if ispicture and excludefrompublish == 0:
        check_default_web_doc_pic(dbo, mediaid, linkid, linktype)

    return mediaid