def grabLinks(crisis): """Takes an ElementTree for a specific entity, and adds all associated links to the datastore""" assert(crisis is not None) links = [] imgvid_tags = ["primaryImage","image"] for ref in crisis.findall('.//ref'): for l in ref: new_link = Link() if (l.tag): new_link.link_type = l.tag if (l.find('./site') != None): new_link.link_site = l.find('./site').text if (l.find('./title') != None): new_link.title = l.find('./title').text try: if (l.find('./url') != None): if (l.tag in imgvid_tags and check == 1): if (check_url(l.find('./url').text)): new_link.link_url = l.find('./url').text else: new_link.link_url = None else: new_link.link_url = l.find('./url').text except DownloadError: new_link.link_url = None except AttributeError: new_link.link_url = None if (l.find('./description') != None): new_link.description = l.find('./description').text new_link.link_parent = crisis.attrib['id'] try: if new_link.link_url != None: q = db.GqlQuery("SELECT * FROM Link WHERE link_parent='" + crisis.attrib['id'] + "' AND link_url='" + new_link.link_url + "' AND link_type='" + new_link.link_type + "'") if (not q.count()): #new_link.put() links.append(new_link) except db.BadQueryError, e: logging.info("Error Caught: "+ str(e)) #new_link.put() links.append(new_link)
def test_exportlinks1(self): tree = Element("worldCrises", {"xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation" : "wc.xsd"}) person1 = Person(elemid = "bobs", name = "Bob", info_type = "Salamander", info_birthdate_time = "12:00PM", info_birthdate_day = 12, info_birthdate_month = 12, info_birthdate_year = 1900, info_birthdate_misc = "born under the full moon...", info_nationality = "Swedish", info_biography = "Bob swam a lot, as salamanders do...", orgrefs = ["salamanders united", "salamander liberation front"], crisisrefs = ["swamp famine", "west swamp drought"]) ptree = SubElement(tree, "person", {"id" : "bobs"}) XMLHelpers.buildPerson(ptree, person1) link1 = Link(link_parent = "bobs", link_type = "salad", title = "don't click me!!!", link_url = "http://www.nevergohere.com", description = "you really shouldn't go there...", link_site = "a bad place") link1.put() r = ElementTree.SubElement(ptree, "ref") XMLHelpers.exportLinks(person1, r) for ref in ptree.findall('.//ref'): for l in ref: new_link = Link() if (l.tag): new_link.link_type = l.tag if (l.find('./site') != None): new_link.link_site = l.find('./site').text if (l.find('./title') != None): new_link.title = l.find('./title').text if (l.find('./url') != None): new_link.link_url = db.Link(l.find('./url').text) if (l.find('./description') != None): new_link.description = l.find('./description').text new_link.link_parent = ptree.attrib['id'] #self.assert_(False) self.assertEqual(new_link.link_type , link1.link_type) self.assert_(new_link.link_site == link1.link_site) self.assert_(new_link.title == link1.title) self.assert_(new_link.link_url == link1.link_url) self.assert_(new_link.description == link1.description) self.assert_(new_link.link_parent == link1.link_parent)
def test_exportlinks3(self): tree = Element("worldCrises", {"xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation" : "wc.xsd"}) crisis1 = Crisis(elemid = "hunger", name = "hunger", misc = "na", info_history = "this year", info_help = "help", info_resources = "awareness", info_type = "hunger attack", date_time = "11 am", date_day = 18, date_month = 03, date_year = 2012, date_misc = "still alive", location_city = "houston", location_region = "texas", location_country = "USA", impact_human_deaths = 200, impact_human_displaced = 20, impact_human_injured = 1, impact_human_missing = 32, impact_human_misc = "none", impact_economic_amount = 400, impact_economic_currency = "yen", impact_economic_misc = "misc", orgrefs = ["dea", "cia"], personrefs = ["you", "me"]) ctree = SubElement(tree, "crisis", {"id" : "hunger"}) XMLHelpers.buildCrisis(ctree, crisis1) link1 = Link(link_parent = "hunger", link_type = "salad", title = "don't click me!!!", link_url = "http://www.nevergohere.com", description = "you really shouldn't go there...", link_site = "a bad place") XMLHelpers.link_list = [] XMLHelpers.link_list.append(link1) XMLHelpers.exportLinks(crisis1, ctree) for ref in ctree.findall('.//ref'): for l in ref: new_link = Link() if (l.tag): new_link.link_type = l.tag if (l.find('./site') != None): new_link.link_site = l.find('./site').text if (l.find('./title') != None): new_link.title = l.find('./title').text if (l.find('./url') != None): new_link.link_url = db.Link(l.find('./url').text) if (l.find('./description') != None): new_link.description = l.find('./description').text new_link.link_parent = ctree.attrib['id'] self.assert_(new_link.link_type == link1.link_type) self.assert_(new_link.link_site == link1.link_site) self.assert_(new_link.title == link1.title) self.assert_(new_link.link_url == link1.link_url) self.assert_(new_link.description == link1.description) self.assertEqual(new_link.link_parent, link1.link_parent)
def test_exportlinks2(self): tree = Element("worldCrises", {"xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation" : "wc.xsd"}) organization1 = Organization(elemid = "Franch", name = "French pride", info_type = "non-existant", info_history = "white flags", info_contacts_phone = "1234567890", info_contacts_email = "*****@*****.**", info_contacts_address = "French", info_contacts_city = "Paris", info_contacts_state = "Canada", info_contacts_country = "USA", info_contacts_zip = "7890", info_loc_city = "Alaska", info_loc_region = "Ukraine", info_loc_country = "Antarctica", personrefs = ["baquettes", "crumpets"], crisisrefs = ["war", "nazis"], misc = "") otree = SubElement(tree, "organization", {"id" : "Franch"}) XMLHelpers.buildOrganization(otree, organization1) link1 = Link(link_parent = "Franch", link_type = "salad", title = "don't click me!!!", link_url = "http://www.nevergohere.com", description = "you really shouldn't go there...", link_site = "a bad place") XMLHelpers.link_list = [] XMLHelpers.link_list.append(link1) XMLHelpers.exportLinks(organization1, otree) for ref in otree.findall('.//ref'): for l in ref: new_link = Link() if (l.tag): new_link.link_type = l.tag if (l.find('./site') != None): new_link.link_site = l.find('./site').text if (l.find('./title') != None): new_link.title = l.find('./title').text if (l.find('./url') != None): new_link.link_url = db.Link(l.find('./url').text) if (l.find('./description') != None): new_link.description = l.find('./description').text new_link.link_parent = otree.attrib['id'] self.assert_(new_link.link_type == link1.link_type) self.assert_(new_link.link_site == link1.link_site) self.assert_(new_link.title == link1.title) self.assert_(new_link.link_url == link1.link_url) self.assert_(new_link.description == link1.description) self.assertEqual(new_link.link_parent, link1.link_parent)