Пример #1
0
 def make_partenr(payload):
     '''
     create and make partenr
     make realtion between two person every person has partenr for example:
     Ali (person) has jafer (partenr), jafer is father (relation =1) of Ali
     privet /partenr post methods
     get data (person:int, partenr:int, relation:int) from json requst then
      insert data into partenr object
     scope requires authtction post:partenr
     insert into partenr (person, partenr, relation) VALUES /*from json
      requst*/(person, partenr, relation)
     return name of person (Ali), name of partenr, message descripe relation
      of relation status and 200 success
     if any problem in id person, id partenr raise 422, if relation not int
      or above 5 or under 1 or None raise 422, if name person, name partenr
       not existing raise 400
     relation={
         1: "Father",
         2: "Mother",
         3: "wife",
         4: "husband",
         5: "child"
     }
     '''
     data = request.get_json()
     person = data.get('person', None)
     partenr = data.get('partenr', None)
     relation = data.get('relation', None)
     if type(person) is not int or person is None or person <= 0:
         abort(422)
     if type(partenr) is not int or partenr is None or partenr <= 0:
         abort(422)
     if partenr == person:
         abort(404)
     if type(relation) is not int or relation is None or relation > 5 \
             or relation <= 0:
         abort(400)
     name_person = Person.query.filter_by(person_id=person).one_or_none()
     if name_person is None:
         abort(400)
     namePartenr = Person.query.filter_by(person_id=partenr).one_or_none()
     if namePartenr is None:
         abort(400)
     relations = Relations(person=person,
                           partenr=partenr,
                           relation=relation)
     relations.insert()
     message = ""
     if relation >= 5:
         message = name_person.name.format() + " became the " +\
             RELATION_CONST[relation-1] + " of " + namePartenr.name.format()
     else:
         message = namePartenr.name.format()+" became the " +\
             RELATION_CONST[relation-1] + " of " + name_person.name.format()
     return jsonify({
         "success": True,
         "person": name_person.name.format(),
         "partenr": namePartenr.name.format(),
         "relation": message
     })
Пример #2
0
def populate_crisis(root):
    """
	Finds instances of Crisis in root's tree and saves them to the database

	@type root:  Element
	@param root: root of the ElementTree passed in to populate_models()
	@rtype:      N/A
	@return:     function does not return
	"""
    for crisis in root.findall("Crisis"):
        temp_crisis = Crisis()
        temp_crisis.crisis_ID = crisis.get("ID")
        temp_crisis.name = crisis.get("Name")

        if crisis.find("Kind") is not None:
            temp_crisis.kind = crisis.find("Kind").text
        if crisis.find("Date") is not None:
            temp_crisis.date = crisis.find("Date").text
        if crisis.find("Time") is not None:
            temp_crisis.time = crisis.find("Time").text
        #populating people
        for person in crisis.iter("Person") or []:
            temp_relations = Relations()
            check = Relations.objects.filter(crisis_ID=crisis.get("ID"),
                                             person_ID=person.get("ID"))
            if len(check) == 0:
                temp_relations.populate(c_id=crisis.get("ID"),
                                        p_id=person.get("ID"))
                temp_relations.save()
        #populating organizations
        for org in crisis.iter("Org") or []:
            temp_relations = Relations()
            check = Relations.objects.filter(crisis_ID=crisis.get("ID"),
                                             org_ID=org.get("ID"))
            if len(check) == 0:
                temp_relations.populate(c_id=crisis.get("ID"),
                                        o_id=org.get("ID"))
                temp_relations.save()

        populate_li(crisis, crisis.get("ID"), "Locations")
        populate_li(crisis, crisis.get("ID"), "HumanImpact")
        populate_li(crisis, crisis.get("ID"), "EconomicImpact")
        populate_li(crisis, crisis.get("ID"), "ResourcesNeeded")
        populate_li(crisis, crisis.get("ID"), "WaysToHelp")

        populate_common(crisis, crisis.get("ID"), temp_crisis)
        temp_crisis.save()
Пример #3
0
def populate_org(root):
    """
	Finds instances of Org in root's tree and saves them to the database

	@type root:  Element
	@param root: root of the ElementTree passed in to populate_models()
	@rtype:      N/A
	@return:     function does not return
	"""
    for org in root.findall("Organization"):
        temp_org = Org()
        temp_org.org_ID = org.get("ID")
        temp_org.name = org.get("Name")
        if org.find("Kind") is not None:
            temp_org.kind = org.find("Kind").text
        if org.find("Location") is not None:
            temp_org.location = org.find("Location").text

        for crisis in org.iter("Crisis"):
            temp_relations = Relations()
            check = Relations.objects.filter(crisis_ID=crisis.get("ID"),
                                             org_ID=org.get("ID"))
            if len(check) == 0:
                temp_relations.populate(c_id=crisis.get("ID"),
                                        o_id=org.get("ID"))
                temp_relations.save()

        for person in org.iter("Person"):
            temp_relations = Relations()
            check = Relations.objects.filter(org_ID=org.get("ID"),
                                             person_ID=person.get("ID"))
            if len(check) == 0:
                temp_relations.populate(p_id=person.get("ID"),
                                        o_id=org.get("ID"))
                temp_relations.save()

        populate_li(org, org.get("ID"), "History")
        populate_li(org, org.get("ID"), "ContactInfo")

        populate_common(org, org.get("ID"), temp_org)
        temp_org.save()
Пример #4
0
def populate_person(root):
    """
	Finds instances of Person in root's tree and saves them to the database

	@type root:  Element
	@param root: root of the ElementTree passed in to populate_models()
	@rtype:      N/A
	@return:     function does not return
	"""
    for person in root.findall("Person"):
        temp_person = Person()
        temp_person.person_ID = person.get("ID")
        temp_person.name = person.get("Name")

        if person.find("Kind") is not None:
            temp_person.kind = person.find("Kind").text
        if person.find("Location") is not None:
            temp_person.location = person.find("Location").text

        for crisis in person.iter("Crisis"):
            temp_relations = Relations()
            check = Relations.objects.filter(crisis_ID=crisis.get("ID"),
                                             person_ID=person.get("ID"))
            if len(check) == 0:
                temp_relations.populate(c_id=crisis.get("ID"),
                                        p_id=person.get("ID"))
                temp_relations.save()

        for org in person.iter("Org"):
            temp_relations = Relations()
            check = Relations.objects.filter(org_ID=org.get("ID"),
                                             person_ID=person.get("ID"))
            if len(check) == 0:
                temp_relations.populate(p_id=person.get("ID"),
                                        o_id=org.get("ID"))
                temp_relations.save()

        populate_common(person, person.get("ID"), temp_person)
        temp_person.save()