def execute_diary_task(dbo, username, tasktype, taskid, linkid, selecteddate): """ Runs a diary task tasktype: ANIMAL or PERSON taskid: The ID of the diarytaskhead record to run linkid: The ID of the animal or person to run against selecteddate: If the task has any detail records with a pivot of 0, the date to supply (as python date) """ def fix(s): return s.replace("<", "<").replace(">", ">") rollingdate = i18n.now(dbo.timezone) dtd = db.query(dbo, "SELECT * FROM diarytaskdetail WHERE DiaryTaskHeadID = %d ORDER BY ID" % int(taskid)) tags = {} linktype = ANIMAL if tasktype == "ANIMAL": linktype = ANIMAL tags = wordprocessor.animal_tags(dbo, animal.get_animal(dbo, int(linkid))) elif tasktype == "PERSON": linktype = PERSON tags = wordprocessor.person_tags(dbo, person.get_person(dbo, int(linkid))) for d in dtd: if d["DAYPIVOT"] == 9999: rollingdate = selecteddate else: rollingdate = i18n.add_days(rollingdate, int(d["DAYPIVOT"])) insert_diary(dbo, username, linktype, int(linkid), rollingdate, \ d["WHOFOR"], \ wordprocessor.substitute_tags(fix(d["SUBJECT"]), tags, True), \ wordprocessor.substitute_tags(fix(d["NOTE"]), tags, True))
def execute_diary_task(dbo, username, tasktype, taskid, linkid, selecteddate): """ Runs a diary task tasktype: ANIMAL or PERSON taskid: The ID of the diarytaskhead record to run linkid: The ID of the animal or person to run against selecteddate: If the task has any detail records with a pivot of 0, the date to supply (as python date) """ def fix(s): return s.replace("<", "<").replace(">", ">") rollingdate = dbo.today() dtd = dbo.query( "SELECT * FROM diarytaskdetail WHERE DiaryTaskHeadID = ? ORDER BY OrderIndex", [taskid]) tags = {} linktype = ANIMAL if tasktype == "ANIMAL": linktype = ANIMAL tags = wordprocessor.animal_tags(dbo, animal.get_animal(dbo, int(linkid))) elif tasktype == "PERSON": linktype = PERSON tags = wordprocessor.person_tags(dbo, person.get_person(dbo, int(linkid))) for d in dtd: if d["DAYPIVOT"] == 9999: rollingdate = selecteddate else: rollingdate = i18n.add_days(rollingdate, int(d["DAYPIVOT"])) insert_diary(dbo, username, linktype, int(linkid), rollingdate, \ d["WHOFOR"], \ wordprocessor.substitute_tags(fix(d["SUBJECT"]), tags, True), \ wordprocessor.substitute_tags(fix(d["NOTE"]), tags, True))
def animals_to_page(dbo, animals, style="", speciesid=0, animaltypeid=0, locationid=0): """ Returns a page of animals. animals: A resultset containing animal records style: The HTML publishing template to use speciesid: 0 for all species, or a specific one animaltypeid: 0 for all animal types or a specific one locationid: 0 for all internal locations or a specific one """ # Get the specified template head, body, foot = template.get_html_template(dbo, style) if head == "": head, body, foot = get_animal_view_template(dbo) # Substitute the header and footer tags org_tags = wordprocessor.org_tags(dbo, "system") head = wordprocessor.substitute_tags(head, org_tags, True, "$$", "$$") foot = wordprocessor.substitute_tags(foot, org_tags, True, "$$", "$$") # Run through each animal and generate body sections bodies = [] for a in animals: if speciesid > 0 and a.SPECIESID != speciesid: continue if animaltypeid > 0 and a.ANIMALTYPEID != animaltypeid: continue if locationid > 0 and a.SHELTERLOCATION != locationid: continue # Translate website media name to the service call for images if smcom.active(): a.WEBSITEMEDIANAME = "%s?account=%s&method=animal_image&animalid=%d" % ( SERVICE_URL, dbo.database, a.ID) else: a.WEBSITEMEDIANAME = "%s?method=animal_image&animalid=%d" % ( SERVICE_URL, a.ID) # Generate tags for this row tags = wordprocessor.animal_tags_publisher(dbo, a) tags = wordprocessor.append_tags(tags, org_tags) # Add extra tags for websitemedianame2-4 if they exist if a.WEBSITEIMAGECOUNT > 1: tags["WEBMEDIAFILENAME2"] = "%s&seq=2" % a.WEBSITEMEDIANAME if a.WEBSITEIMAGECOUNT > 2: tags["WEBMEDIAFILENAME3"] = "%s&seq=3" % a.WEBSITEMEDIANAME if a.WEBSITEIMAGECOUNT > 3: tags["WEBMEDIAFILENAME4"] = "%s&seq=4" % a.WEBSITEMEDIANAME # Set the description if configuration.publisher_use_comments(dbo): a.WEBSITEMEDIANOTES = a.ANIMALCOMMENTS # Add extra publishing text, preserving the line endings notes = utils.nulltostr(a.WEBSITEMEDIANOTES) notes += configuration.third_party_publisher_sig(dbo).replace( "\n", "<br/>") tags["WEBMEDIANOTES"] = notes bodies.append( wordprocessor.substitute_tags(body, tags, True, "$$", "$$")) return "%s\n%s\n%s" % (head, "\n".join(bodies), foot)
def substituteBodyTags(self, searchin, a): """ Substitutes any tags in the body for animal data """ tags = wordprocessor.animal_tags_publisher(self.dbo, a) tags["TotalAnimals"] = str(self.totalAnimals) tags["IMAGE"] = str(a["WEBSITEMEDIANAME"]) # Note: WEBSITEMEDIANOTES becomes ANIMALCOMMENTS in get_animal_data when publisher_use_comments is on notes = utils.nulltostr(a["WEBSITEMEDIANOTES"]) # Add any extra text notes += configuration.third_party_publisher_sig(self.dbo) # Preserve line endings in the bio notes = notes.replace("\n", "**le**") tags["WEBMEDIANOTES"] = notes output = wordprocessor.substitute_tags(searchin, tags, True, "$$", "$$") output = output.replace("**le**", "<br />") return output
def get_animal_view(dbo, animalid): """ Constructs the animal view page to the template. """ a = dbo.first_row( get_animal_data(dbo, animalid=animalid, include_additional_fields=True, strip_personal_data=True)) # If the animal is not adoptable, bail out if a is None: raise utils.ASMPermissionError("animal is not adoptable (None)") if not is_animal_adoptable(dbo, a): raise utils.ASMPermissionError("animal is not adoptable (False)") # If the option is on, use animal comments as the notes if configuration.publisher_use_comments(dbo): a.WEBSITEMEDIANOTES = a.ANIMALCOMMENTS head, body, foot = get_animal_view_template(dbo) if head == "": head = "<!DOCTYPE html>\n<html>\n<head>\n<title>$$SHELTERCODE$$ - $$ANIMALNAME$$</title></head>\n<body>" body = "<h2>$$SHELTERCODE$$ - $$ANIMALNAME$$</h2><p><img src='$$WEBMEDIAFILENAME$$'/></p><p>$$WEBMEDIANOTES$$</p>" foot = "</body>\n</html>" if smcom.active(): a.WEBSITEMEDIANAME = "%s?account=%s&method=animal_image&animalid=%d" % ( SERVICE_URL, dbo.database, animalid) else: a.WEBSITEMEDIANAME = "%s?method=animal_image&animalid=%d" % ( SERVICE_URL, animalid) s = head + body + foot tags = wordprocessor.animal_tags_publisher(dbo, a) tags = wordprocessor.append_tags(tags, wordprocessor.org_tags(dbo, "system")) # Add extra tags for websitemedianame2-10 if they exist for x in range(2, 11): if a.WEBSITEIMAGECOUNT > x - 1: tags["WEBMEDIAFILENAME%d" % x] = "%s&seq=%d" % (a.WEBSITEMEDIANAME, x) # Add extra publishing text, preserving the line endings notes = utils.nulltostr(a.WEBSITEMEDIANOTES) notes += configuration.third_party_publisher_sig(dbo) notes = notes.replace("\n", "**le**") tags["WEBMEDIANOTES"] = notes tags["WEBSITEMEDIANOTES"] = notes s = wordprocessor.substitute_tags(s, tags, True, "$$", "$$") s = s.replace("**le**", "<br />") return s
if page_name != "": post_to = page_id # Grab the animal details a = animal.get_animal(dbo, utils.cint(oauth_state[1:])) # Bail out if we have a problem if a is None: raise utils.ASMValidationError( "Facebook response did not contain a valid animal ID (got %s)" % oauth_state[1:]) # Generate the body of the post from our facebook template tags = wordprocessor.animal_tags(dbo, a) template = configuration.facebook_template(dbo) posttext = wordprocessor.substitute_tags(template, tags, False, "$$", "$$") # Post on the wall try: l = dbo.locale fb_url = "https://graph.facebook.com/%s/photos?access_token=%s" % ( post_to, access_token) al.debug( "FB posting photo and text '%s' to '%s' at %s" % (posttext, page_name, fb_url), "social.post_animal_facebook", dbo) imagedata = dbfs.get_string(dbo, a["WEBSITEMEDIANAME"]) req, hdr, response = utils.post_multipart( fb_url, (("message", utils.decode_html(posttext).encode("utf-8")), ), (("source", "pic.jpg", imagedata), ))
# If we're posting as us, but to a page, post to the page # but stick with the user's access_token if page_name != "": post_to = page_id # Grab the animal details a = animal.get_animal(dbo, utils.cint(oauth_state[1:])) # Bail out if we have a problem if a is None: raise utils.ASMValidationError("Facebook response did not contain a valid animal ID (got %s)" % oauth_state[1:]) # Generate the body of the post from our facebook template tags = wordprocessor.animal_tags(dbo, a) template = configuration.facebook_template(dbo) posttext = wordprocessor.substitute_tags(template, tags, False, "$$", "$$") # Post on the wall try: l = dbo.locale fb_url = "https://graph.facebook.com/%s/photos?access_token=%s" % (post_to, access_token) al.debug("FB posting photo and text '%s' to '%s' at %s" % (posttext, page_name, fb_url), "social.post_animal_facebook", dbo) imagedata = dbfs.get_string(dbo, a["WEBSITEMEDIANAME"]) req, hdr, response = utils.post_multipart(fb_url, ( ("message", utils.decode_html(posttext).encode("utf-8")),), ( ("source", "pic.jpg", imagedata), )) al.debug("FB response: %s" % response, "social.post_animal_facebook", dbo) # If the option is on and all was ok, make a note in the log if configuration.facebook_log(dbo): al.debug("FB writing entry to animal log: %s %s" % (a["SHELTERCODE"], a["ANIMALNAME"]), "social.post_animal_facebook", dbo) log.add_log(dbo, user, log.ANIMAL, utils.cint(oauth_state[1:]), configuration.facebook_log_type(dbo),