def guess_agegroup(dbo, s): """ Guesses an agegroup, returns the default if no match is found """ s = str(s).lower() guess = db.query_string( dbo, "SELECT ItemValue FROM configuration WHERE ItemName LIKE 'AgeGroup%Name' AND LOWER(ItemValue) LIKE '%" + db.escape(s) + "%'") if guess != "": return guess return db.query_string( dbo, "SELECT ItemValue FROM configuration WHERE ItemName LIKE 'AgeGroup2Name'" )
def get_file(dbo, name, path, saveto): """ Gets DBFS file contents and saves them to the filename given. Returns True for success """ if path != "": s = db.query_string(dbo, "SELECT Content FROM dbfs WHERE Name = '%s' AND Path = '%s'" % (name, path)) else: s = db.query_string(dbo, "SELECT Content FROM dbfs WHERE Name = '%s'" % name) if s != "": f = open(saveto, "wb") f.write(base64.b64decode(s)) f.close() return True return False
def check_and_scale_pdfs(dbo, force = False): """ Goes through all PDFs in the database to see if they have been scaled (have a suffix of _scaled.pdf) and scales down any unscaled ones. If force is set, then all PDFs are checked and scaled again even if they've been scaled before. """ if not configuration.scale_pdfs(dbo): al.warn("ScalePDFs config option disabled in this database, not scaling pdfs", "media.check_and_scale_pdfs", dbo) return if force: mp = db.query(dbo, \ "SELECT ID, MediaName FROM media WHERE LOWER(MediaName) LIKE '%.pdf' ORDER BY ID DESC") else: mp = db.query(dbo, \ "SELECT ID, MediaName FROM media WHERE LOWER(MediaName) LIKE '%.pdf' AND " \ "LOWER(MediaName) NOT LIKE '%_scaled.pdf' ORDER BY ID DESC") for i, m in enumerate(mp): filepath = db.query_string(dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % m["MEDIANAME"]) original_name = str(m["MEDIANAME"]) new_name = str(m["ID"]) + "_scaled.pdf" odata = dbfs.get_string(dbo, original_name) data = scale_pdf(odata) al.debug("scaling %s (%d of %d): old size %d, new size %d" % (new_name, i, len(mp), len(odata), len(data)), "check_and_scale_pdfs", dbo) # Update the media entry with the new name db.execute(dbo, "UPDATE media SET MediaName = '%s' WHERE ID = %d" % ( new_name, m["ID"])) # Update the dbfs entry from old name to new name (will be overwritten in a minute but safer than delete) dbfs.rename_file(dbo, filepath, original_name, new_name) # Store the PDF file data with the new name - if there was a need to change it if len(data) < len(odata): dbfs.put_string(dbo, new_name, filepath, data) al.debug("found and scaled %d pdfs" % len(mp), "media.check_and_scale_pdfs", dbo)
def scale_animal_images(dbo): """ Goes through all animal images in the database and scales them to the current incoming media scaling factor. """ mp = db.query(dbo, "SELECT MediaName FROM media WHERE LOWER(MediaName) LIKE '%.jpg' AND LinkTypeID = 0") for i, m in enumerate(mp): filepath = db.query_string(dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % m["MEDIANAME"]) name = str(m["MEDIANAME"]) inputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) outputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) odata = dbfs.get_string(dbo, name) inputfile.write(odata) inputfile.flush() inputfile.close() outputfile.close() al.debug("scaling %s (%d of %d)" % (name, i, len(mp)), "media.scale_animal_images", dbo) scale_image_file(inputfile.name, outputfile.name, configuration.incoming_media_scaling(dbo)) f = open(outputfile.name, "r") data = f.read() f.close() os.unlink(inputfile.name) os.unlink(outputfile.name) # Update the image file data dbfs.put_string(dbo, name, filepath, data) al.debug("scaled %d images" % len(mp), "media.scale_animal_images", dbo)
def check_and_scale_pdfs(dbo): """ Goes through all PDFs in the database to see if they have been scaled (have a suffix of _scaled.pdf) and scales down any unscaled ones. """ if not SCALE_PDF_DURING_BATCH: al.warn("SCALE_PDF_DURING_BATCH is disabled, not scaling pdfs", "media.check_and_scale_pdfs", dbo) if not configuration.scale_pdfs(dbo): al.warn( "ScalePDFs config option disabled in this database, not scaling pdfs", "media.check_and_scale_pdfs", dbo) mp = db.query(dbo, \ "SELECT MediaName FROM media WHERE LOWER(MediaName) LIKE '%.pdf' AND " \ "LOWER(MediaName) NOT LIKE '%_scaled.pdf'") for m in mp: filepath = db.query_string( dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % m["MEDIANAME"]) original_name = str(m["MEDIANAME"]) new_name = str(m["MEDIANAME"]) new_name = new_name[0:len(new_name) - 4] + "_scaled.pdf" odata = dbfs.get_string(dbo, original_name) data = scale_pdf(odata) # Update the media entry with the new name db.execute(dbo, "UPDATE media SET MediaName = '%s' WHERE MediaName = '%s'" % \ ( new_name, original_name)) # Update the dbfs entry with the new name dbfs.rename_file(dbo, filepath, original_name, new_name) # Update the PDF file data dbfs.put_string(dbo, new_name, filepath, data) al.debug("found and scaled %d pdfs" % len(mp), "media.check_and_scale_pdfs", dbo)
def scale_animal_images(dbo): """ Goes through all animal images in the database and scales them to the current incoming media scaling factor. """ mp = db.query( dbo, "SELECT MediaName FROM media WHERE LOWER(MediaName) LIKE '%.jpg' AND LinkTypeID = 0" ) for i, m in enumerate(mp): filepath = db.query_string( dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % m["MEDIANAME"]) name = str(m["MEDIANAME"]) inputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) outputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) odata = dbfs.get_string(dbo, name) inputfile.write(odata) inputfile.flush() inputfile.close() outputfile.close() al.debug("scaling %s (%d of %d)" % (name, i, len(mp)), "media.scale_animal_images", dbo) scale_image_file(inputfile.name, outputfile.name, configuration.incoming_media_scaling(dbo)) f = open(outputfile.name, "r") data = f.read() f.close() os.unlink(inputfile.name) os.unlink(outputfile.name) # Update the image file data dbfs.put_string(dbo, name, filepath, data) al.debug("scaled %d images" % len(mp), "media.scale_animal_images", dbo)
def get_location_filter(dbo, user): """ Returns the location filter (comma separated list of IDs) for a user, or "" if it doesn't have one. """ return db.query_string( dbo, "SELECT LocationFilter FROM users WHERE UserName LIKE '%s'" % user)
def get_found_person_name(dbo, aid): """ Returns the contact name for a found animal """ return db.query_string( dbo, "SELECT o.OwnerName FROM animalfound a INNER JOIN owner o ON a.OwnerID = o.ID WHERE a.ID = %d" % int(aid))
def get_theme_override(dbo, username): """ Returns a user's theme override, or empty string if it doesn't have one """ try: return db.query_string(dbo, "SELECT ThemeOverride FROM users WHERE UserName Like '%s'" % username) except: return ""
def get_person_name(dbo, wid): """ Returns the contact name for the waitinglist with id """ return db.query_string( dbo, "SELECT o.OwnerName FROM animalwaitinglist a INNER JOIN owner o ON a.OwnerID = o.ID WHERE a.ID = %d" % int(wid))
def get_onlineformincoming_formfooter(dbo, collationid): """ Given a collation id for an incoming form, try and find the original onlineform footer. """ return db.query_string(dbo, "SELECT o.Footer FROM onlineform o " \ "INNER JOIN onlineformincoming oi ON oi.FormName = o.Name " \ "WHERE oi.CollationID = %d" % int(collationid))
def get_file(dbo, name, path, saveto): """ Gets DBFS file contents and saves them to the filename given. Returns True for success """ if path != "": s = db.query_string( dbo, "SELECT Content FROM dbfs WHERE Name = '%s' AND Path = '%s'" % (name, path)) else: s = db.query_string( dbo, "SELECT Content FROM dbfs WHERE Name = '%s'" % name) if s != "": f = open(saveto, "wb") f.write(base64.b64decode(s)) f.close() return True return False
def get_theme_override(dbo, username): """ Returns a user's theme override, or empty string if it doesn't have one """ try: return db.query_string( dbo, "SELECT ThemeOverride FROM users WHERE UserName Like '%s'" % username) except: return ""
def insert_onlineformincoming_from_form(dbo, post, remoteip): """ Create onlineformincoming records from posted data. We create a row for every key/value pair in the posted data with a unique collation ID. """ IGNORE_FIELDS = [ "formname", "flags", "redirect", "account", "filechooser", "method" ] collationid = db.query_int(dbo, "SELECT MAX(CollationID) FROM onlineformincoming") + 1 formname = post["formname"] posteddate = i18n.now(dbo.timezone) flags = post["flags"] for k, v in post.data.iteritems(): if k not in IGNORE_FIELDS: label = "" displayindex = 0 fieldname = k # Form fields should have a _ONLINEFORMFIELD.ID suffix we can use to get the # original label and display position if k.find("_") != -1: fid = utils.cint(k[k.rfind("_")+1:]) fieldname = k[0:k.rfind("_")] if fid != 0: fld = db.query(dbo, "SELECT Label, DisplayIndex FROM onlineformfield WHERE ID = %d" % fid) if len(fld) > 0: label = fld[0]["LABEL"] displayindex = fld[0]["DISPLAYINDEX"] sql = db.make_insert_sql("onlineformincoming", ( ( "CollationID", db.di(collationid)), ( "FormName", db.ds(formname)), ( "PostedDate", db.ddt(posteddate)), ( "Flags", db.ds(flags)), ( "FieldName", db.ds(fieldname)), ( "Label", db.ds(label)), ( "DisplayIndex", db.di(displayindex)), ( "Host", db.ds(remoteip)), ( "Value", post.db_string(k)) )) db.execute(dbo, sql) # Sort out the preview of the first few fields fieldssofar = 0 preview = [] for fld in get_onlineformincoming_detail(dbo, collationid): if fieldssofar < 3: fieldssofar += 1 preview.append( fld["LABEL"] + ": " + fld["VALUE"] ) db.execute(dbo, "UPDATE onlineformincoming SET Preview = %s WHERE CollationID = %s" % ( db.ds(", ".join(preview)), db.di(collationid) )) # Did the original form specify some email addresses to send # incoming submissions to? email = db.query_string(dbo, "SELECT o.EmailAddress FROM onlineform o " \ "INNER JOIN onlineformincoming oi ON oi.FormName = o.Name " \ "WHERE oi.CollationID = %d" % int(collationid)) if email is not None and email.strip() != "": utils.send_email(dbo, configuration.email(dbo), email, "", "%s - %s" % (formname, ", ".join(preview)), get_onlineformincoming_plain(dbo, collationid)) return collationid
def update_pass_homecheck(dbo, user, personid, comments): """ Marks a person as homechecked and appends any comments supplied to their record. """ by = users.get_personid(dbo, user) if by != 0: db.execute(dbo, "UPDATE owner SET HomeCheckedBy = %d WHERE ID = %d" % (by, personid)) db.execute(dbo, "UPDATE owner SET IDCheck = 1, DateLastHomeChecked = %s WHERE ID = %d" % ( db.dd(now(dbo.timezone)), personid )) if comments != "": com = db.query_string(dbo, "SELECT Comments FROM owner WHERE ID = %d" % personid) com += "\n" + comments db.execute(dbo, "UPDATE owner SET Comments = %s WHERE ID = %d" % ( db.ds(com), personid ))
def update_pass_homecheck(dbo, user, personid, comments): """ Marks a person as homechecked and appends any comments supplied to their record. """ by = users.get_personid(dbo, user) if by != 0: db.execute( dbo, "UPDATE owner SET HomeCheckedBy = %d WHERE ID = %d" % (by, personid)) db.execute( dbo, "UPDATE owner SET IDCheck = 1, DateLastHomeChecked = %s WHERE ID = %d" % (db.dd(now(dbo.timezone)), personid)) if comments != "": com = db.query_string( dbo, "SELECT Comments FROM owner WHERE ID = %d" % personid) com += "\n" + comments db.execute( dbo, "UPDATE owner SET Comments = %s WHERE ID = %d" % (db.ds(com), personid))
def check_and_scale_pdfs(dbo): """ Goes through all PDFs in the database to see if they have been scaled (have a suffix of _scaled.pdf) and scales down any unscaled ones. """ if not gs_installed(): al.warn("ghostscript is not installed, can't scale pdfs", "media.check_and_scale_pdfs", dbo) return mp = db.query(dbo, \ "SELECT MediaName FROM media WHERE LOWER(MediaName) LIKE '%.pdf' AND " \ "LOWER(MediaName) NOT LIKE '%_scaled.pdf'") for m in mp: filepath = db.query_string( dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % m["MEDIANAME"]) original_name = str(m["MEDIANAME"]) new_name = str(m["MEDIANAME"]) new_name = new_name[0:len(new_name) - 4] + "_scaled.pdf" inputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) outputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) odata = dbfs.get_string(dbo, original_name) inputfile.write(odata) inputfile.flush() inputfile.close() outputfile.close() scale_pdf_file(inputfile.name, outputfile.name) f = open(outputfile.name, "r") data = f.read() f.close() os.unlink(inputfile.name) os.unlink(outputfile.name) # Update the media entry with the new name db.execute(dbo, "UPDATE media SET MediaName = '%s' WHERE MediaName = '%s'" % \ ( new_name, original_name)) # Update the dbfs entry with the new name dbfs.rename_file(dbo, filepath, original_name, new_name) # Update the PDF file data dbfs.put_string(dbo, new_name, filepath, data) al.debug("found and scaled %d pdfs" % len(mp), "media.check_and_scale_pdfs", dbo)
def check_and_scale_pdfs(dbo): """ Goes through all PDFs in the database to see if they have been scaled (have a suffix of _scaled.pdf) and scales down any unscaled ones. """ if not gs_installed(): al.warn("ghostscript is not installed, can't scale pdfs", "media.check_and_scale_pdfs", dbo) return mp = db.query(dbo, \ "SELECT MediaName FROM media WHERE LOWER(MediaName) LIKE '%.pdf' AND " \ "LOWER(MediaName) NOT LIKE '%_scaled.pdf'") for m in mp: filepath = db.query_string(dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % m["MEDIANAME"]) original_name = str(m["MEDIANAME"]) new_name = str(m["MEDIANAME"]) new_name = new_name[0:len(new_name)-4] + "_scaled.pdf" inputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) outputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) odata = dbfs.get_string(dbo, original_name) inputfile.write(odata) inputfile.flush() inputfile.close() outputfile.close() scale_pdf_file(inputfile.name, outputfile.name) f = open(outputfile.name, "r") data = f.read() f.close() os.unlink(inputfile.name) os.unlink(outputfile.name) # Update the media entry with the new name db.execute(dbo, "UPDATE media SET MediaName = '%s' WHERE MediaName = '%s'" % \ ( new_name, original_name)) # Update the dbfs entry with the new name dbfs.rename_file(dbo, filepath, original_name, new_name) # Update the PDF file data dbfs.put_string(dbo, new_name, filepath, data) al.debug("found and scaled %d pdfs" % len(mp), "media.check_and_scale_pdfs", dbo)
def scale_all_odt(dbo): """ Goes through all odt files attached to records in the database and scales them down (throws away images and objects so only the text remains to save space) """ mo = db.query(dbo, "SELECT MediaName FROM media WHERE LOWER(MediaName) LIKE '%.odt'") for i, m in enumerate(mo): name = str(m["MEDIANAME"]) al.debug("scaling %s (%d of %d)" % (name, i, len(mo)), "media.scale_all_odt", dbo) print "%s (%d of %d)" % (name, i, len(mo)) odata = dbfs.get_string(dbo, name) if odata == "": al.error("file %s does not exist" % name, "media.scale_all_odt", dbo) print "file %s does not exist" % name continue path = db.query_string(dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % name) ndata = scale_odt(odata) if len(ndata) < 512: al.error("scaled odt %s came back at %d bytes, abandoning" % (name, len(ndata)), "scale_all_odt", dbo) print "file too small < 512, doing nothing" else: print "old size: %d, new size: %d" % (len(odata), len(ndata)) dbfs.put_string(dbo, name, path, ndata)
def get_basecolour_name(dbo, cid): if id is None: return "" return db.query_string( dbo, "SELECT BaseColour FROM basecolour WHERE ID = %d" % cid)
def guess_agegroup(dbo, s): """ Guesses an agegroup, returns the default if no match is found """ s = str(s).lower() guess = db.query_string(dbo, "SELECT ItemValue FROM configuration WHERE ItemName LIKE 'AgeGroup%Name' AND LOWER(ItemValue) LIKE '%" + db.escape(s) + "%'") if guess != "": return guess return db.query_string(dbo, "SELECT ItemValue FROM configuration WHERE ItemName LIKE 'AgeGroup2Name'")
def get_onlineform_name(dbo, formid): """ Returns the name of a form """ return db.query_string( dbo, "SELECT Name FROM onlineform WHERE ID = %d" % int(formid))
def get_person_name(dbo, personid): """ Returns the full person name for an id """ return db.query_string( dbo, "SELECT OwnerName FROM owner WHERE ID = %d" % int(personid))
def get_name_for_id(dbo, mid): return db.query_string(dbo, "SELECT MediaName FROM media WHERE ID = %d" % mid)
def get_sex_name(dbo, sid): if id is None: return "" return db.query_string(dbo, "SELECT Sex FROM lksex WHERE ID = %d" % sid)
def get_account_code(dbo, accountid): """ Returns the code for an accountid """ return db.query_string(dbo, "SELECT Code FROM accounts WHERE ID = %d" % int(accountid))
def get_account_code(dbo, accountid): """ Returns the code for an accountid """ return db.query_string( dbo, "SELECT Code FROM accounts WHERE ID = %d" % int(accountid))
def get_found_person_name(dbo, aid): """ Returns the contact name for a found animal """ return db.query_string(dbo, "SELECT o.OwnerName FROM animalfound a INNER JOIN owner o ON a.OwnerID = o.ID WHERE a.ID = %d" % int(aid))
def get_name_for_id(dbo, dbfsid): """ Returns the filename of the item with id dbfsid """ return db.query_string(dbo, "SELECT Name FROM dbfs WHERE ID = %d" % dbfsid)
def get_web_preferred_name(dbo, linktype, linkid): return db.query_string(dbo, "SELECT MediaName FROM media " \ "WHERE LinkTypeID = %d AND WebsitePhoto = 1 AND LinkID = %d" % (linktype, linkid))
def get_coattype_name(dbo, cid): if id is None: return "" return db.query_string( dbo, "SELECT CoatTypeName FROM lkcoattype WHERE ID = %d" % cid)
def get_internallocation_name(dbo, lid): if id is None: return "" return db.query_string( dbo, "SELECT LocationName FROM internallocation WHERE ID = %d" % lid)
def get_logtype_name(dbo, tid): if tid is None: return "" return db.query_string(dbo, "SELECT LogTypeName FROM logtype WHERE ID = %d" % tid)
def get_species_name(dbo, sid): if id is None: return "" return db.query_string( dbo, "SELECT SpeciesName FROM species WHERE ID = %d" % sid)
def get_movementtype_name(dbo, mid): if mid is None: return "" return db.query_string(dbo, "SELECT MovementType FROM lksmovementtype WHERE ID = %d" % int(mid))
def get_onlineformincoming_name(dbo, collationid): """ Returns the form name for a collation id """ return db.query_string(dbo, "SELECT FormName FROM onlineformincoming WHERE CollationID = %d LIMIT 1" % int(collationid))
def get_onlineform_name(dbo, formid): """ Returns the name of a form """ return db.query_string(dbo, "SELECT Name FROM onlineform WHERE ID = %d" % int(formid))
def get_size_name(dbo, sid): if sid is None: return "" return db.query_string(dbo, "SELECT Size FROM lksize WHERE ID = %d" % sid)
def get_diarytask_name(dbo, taskid): """ Returns the name for a diarytask """ return db.query_string(dbo, "SELECT Name FROM diarytaskhead WHERE ID=%d" % int(taskid))
def get_species_name(dbo, sid): if id is None: return "" return db.query_string(dbo, "SELECT SpeciesName FROM species WHERE ID = %d" % sid)
def get_onlineformincoming_name(dbo, collationid): """ Returns the form name for a collation id """ return db.query_string( dbo, "SELECT FormName FROM onlineformincoming WHERE CollationID = %d LIMIT 1" % int(collationid))
def get_person_name(dbo, wid): """ Returns the contact name for the waitinglist with id """ return db.query_string(dbo, "SELECT o.OwnerName FROM animalwaitinglist a INNER JOIN owner o ON a.OwnerID = o.ID WHERE a.ID = %d" % int(wid))
def get_animaltype_name(dbo, aid): if id is None: return "" return db.query_string( dbo, "SELECT AnimalType FROM animaltype WHERE ID = %d" % aid)
def get_urgency_name(dbo, uid): if id is None: return "" return db.query_string(dbo, "SELECT Urgency FROM lkurgency WHERE ID = %d" % uid)
def get_breed_name(dbo, bid): if id is None: return "" return db.query_string(dbo, "SELECT BreedName FROM breed WHERE ID = %d" % bid)
def get_animaltype_name(dbo, aid): if id is None: return "" return db.query_string(dbo, "SELECT AnimalType FROM animaltype WHERE ID = %d" % aid)
def get_entryreason_name(dbo, rid): if id is None: return "" return db.query_string( dbo, "SELECT ReasonName FROM entryreason WHERE ID = %d" % rid)
def get_basecolour_name(dbo, cid): if id is None: return "" return db.query_string(dbo, "SELECT BaseColour FROM basecolour WHERE ID = %d" % cid)
def get_movementtype_name(dbo, mid): if mid is None: return "" return db.query_string( dbo, "SELECT MovementType FROM lksmovementtype WHERE ID = %d" % int(mid))
def get_size_name(dbo, sid): if id is None: return "" return db.query_string(dbo, "SELECT Size FROM lksize WHERE ID = %d" % sid)
def get_coattype_name(dbo, cid): if id is None: return "" return db.query_string(dbo, "SELECT CoatTypeName FROM lkcoattype WHERE ID = %d" % cid)
def get_donationtype_name(dbo, did): if did is None: return "" return db.query_string(dbo, "SELECT DonationName FROM donationtype WHERE ID = %d" % did)
def get_entryreason_name(dbo, rid): if rid is None: return "" return db.query_string(dbo, "SELECT ReasonName FROM entryreason WHERE ID = %d" % rid)
def get_stock_location_name(dbo, slid): if slid is None: return "" return db.query_string(dbo, "SELECT LocationName FROM stocklocation WHERE ID = %d" % slid)
def get_internallocation_name(dbo, lid): if lid is None: return "" return db.query_string(dbo, "SELECT LocationName FROM internallocation WHERE ID = %d" % lid)