def handle_abort_element(self, req_post):
     session = self.session
     claim_id = req_post['claim']
     assert claim_id == self.ref_id, "claim_id {0}, ref_id {1}".format(claim_id, self.ref_id)
     claim = session.query(Claim).filter_by(ref_id=claim_id).one()
     self.set_claim_form(claim)
     parts, part_links, _ = get_participants(session, claim_id)
     participant_id = req_post['participant']
     participant = session.query(Participant).filter_by(ref_id=participant_id).first()
     pform = ParticipantEdit(req_post)
     self.participant_form = self.finish_participant_form(pform, parts)
     element_id = req_post['element']
     if element_id == NOELEMENT:
         head_element = get_head_element(session, participant)
         if head_element:
             self.set_element_form(head_element,
                                   element_choices(session, participant))
             self.set_hidden_fields(claim_id, participant_id, str(head_element.element_id))
         else:
             self.set_hidden_fields(claim_id, participant_id, NOELEMENT)
     else:
         element = session.query(ParticipantElement).filter_by(element_id=element_id).one()
         self.set_element_form(element,
                               element_choices(session, participant))
         self.set_hidden_fields(claim_id, participant_id, element_id)
 def handle_update_participant(self, req_post):
     logging.info("Hit 'update_participant'")
     claim_id = req_post['claim']
     assert claim_id == self.ref_id, "claim_id {0}, ref_id {1}".format(claim_id, self.ref_id)
     session = self.session
     claim = session.query(Claim).filter_by(ref_id=claim_id).one()
     self.set_claim_form(claim)
     participant_id = req_post['participant_choice']
     participant = get_participant(session, participant_id)
     parts, part_links, _ = get_participants(session, claim_id)
     form = ParticipantEdit(req_post)
     form.ref_id = participant_id
     form = self.finish_participant_form(form, parts)
     form.validate()
     if form.errors:
         active_element = get_head_element(session, participant)
         self.choose_element_form(req_post,
                                  active_element,
                                  element_choices(session, participant))
     else:
         logging.info("participant_choice: {0}".format(participant_id))
         participant.type = form.type.data
         participant.label = form.label.data
         participant.publication_taxon = form.publication_taxon.data
         participant.publication_anatomy = form.publication_anatomy.data
         participant.publication_substrate = form.publication_substrate.data
         participant.publication_text = form.publication_text.data
         participant.participation_property = expand_curie(form.participation_property.data)
     self.participant_form = form
     element_id = req_post['element']
     element = get_pelement(session, element_id)
     self.set_element_form(element, element_choices(session, participant))
     self.update_graph(claim, parts, part_links)
     self.set_hidden_fields(claim_id, participant_id, element_id)
 def handle_create_element(self, part_links, parts, req_post):
     logging.info("Hit Element create {0}".format(repr(req_post)))
     claim_id = req_post['claim']
     assert self.ref_id == claim_id, "{0} != {1}".format(repr(self.ref_id), repr(claim_id))
     session = self.session
     claim = session.query(Claim).filter_by(ref_id=claim_id).first()
     # TODO this or req_post['element'] ?
     e_choice = self.int_post_field('element_choice')
     if e_choice:
         parent_element = get_pelement(session, e_choice)
         participant = get_participant(session, parent_element.participant_id)
     else:  # find the participant w/o elements
         participant = self.find_participant_without_elements(claim)
     self.set_claim_form(claim)
     self.set_participant_form(participant, parts)
     form = ElementCreate(req_post)
     form.expression_type.data = "None"
     form.term.choices = term_choice(session)
     form.individual.choices = individual_choice(session, None)  # narrative?
     form.expression_type.choices = expression_type_choice(session)
     form.parent_link_property.choices = property_choice(session)
     form.element_choice.choices = element_choices(session, participant)
     self.element_form = form
     self.update_graph(claim, parts, part_links)
     self.set_hidden_fields(claim_id, participant.ref_id, req_post['element'])
 def handle_update_element(self, part_links, parts, req_post):
     claim_id = req_post['claim']
     element_id = req_post['element']
     old_participant_id = req_post['participant']
     assert self.ref_id == claim_id
     session = self.session
     claim = session.query(Claim).filter_by(ref_id=claim_id).first()
     form = ElementEdit(req_post)
     element = get_pelement(session, element_id)
     participant = element.participant
     assert old_participant_id == participant.ref_id, "{0} != {1}".format(old_participant_id, participant.ref_id)
     form.element_choice.choices = element_choices(session, participant)
     self.element_form = self.set_element_form_choices(form)
     form.validate()
     if form.element_choice in form.errors:
         logging.info("Choice was {0}".format(form.element_choice.data))
         parts, part_links, active = get_participants(session, claim_id)
         self.set_claim_form(claim)
         self.set_participant_form(active, parts)
     else:
         logging.info("element_choice: {0}".format(element_id))
         logging.info("Errors were: {0}".format(form.errors))
         self.set_claim_form(claim)
         self.set_participant_form(participant, parts)
         # self.choose_element_form(req_post, element, choices)
     if part_links:
         self.update_graph(claim, parts, part_links)
     self.set_hidden_fields(claim_id, participant.ref_id, element_id)
 def handle_update_claim(self, req_post):
     logging.info("Hit 'update_claim'")
     claim_id = req_post['claim']
     assert claim_id == self.ref_id, "claim_id {0}, ref_id {1}".format(claim_id, self.ref_id)
     session = self.session
     claim = session.query(Claim).filter_by(ref_id=claim_id).first()
     scf = self.set_claim_form_choices(ClaimEdit(req_post))
     self.claim_form = scf
     if scf.validate():
         logging.info("Validated claim form")
         claim.publication = expand_curie(scf.publication.data)
         if scf.narrative.data == "None":
             claim.narrative = None
         else:
             claim.narrative = expand_curie(scf.narrative.data)
         claim.behavior_term = expand_curie(scf.behavior_term.data)
         claim.publication_behavior = scf.publication_behavior.data
         claim.evidence = expand_curie(scf.evidence.data)
     parts, part_links, _ = get_participants(session, claim_id)
     participant_id = req_post['participant']
     participant = get_participant(session, participant_id)
     element_id = req_post['element']
     element = get_pelement(session, element_id)
     self.set_participant_form(participant, parts)
     self.set_element_form(element, element_choices(session, participant))
     if part_links:
         self.update_graph(claim, parts, part_links)
     self.set_hidden_fields(claim_id, participant_id, element_id)
 def handle_commit_element(self, part_links, parts, req_post):
     logging.info("Hit commit element")
     claim_id = req_post['claim']
     assert self.ref_id == claim_id, "{0} != {1}".format(repr(self.ref_id), repr(claim_id))
     session = self.session
     claim = session.query(Claim).filter_by(ref_id=claim_id).first()
     e_choice = self.int_post_field('element_choice')
     if e_choice:
         active_element = get_pelement(session, e_choice)
         participant = active_element.participant
     else:
         participant = self.find_participant_without_elements(claim)
         active_element = None  # best choice here?
     form = ElementCreate(req_post)
     form.element_choice.choices = element_choices(session, participant)
     form = self.set_element_form_choices(form)
     participant_id = participant.ref_id
     if self.enforce_element_type_constraints(form):
         self.report_bad_fields(form)
         logging.info("errors(1): {0}".format(form.errors))
         new_element = ParticipantElement(participant_id=participant_id, old_id=0)
         expression_type = form.expression_type.data
         new_element.expression_type = expression_type
         session.add(new_element)
         transaction.commit()
         element_id = new_element.element_id
         logging.info("New element is {0}".format(repr(element_id)))
         if expression_type == "individual":
             p2i = Pelement2Individual()
             p2i.element = element_id
             p2i.individual = expand_curie(form.individual.data)
             p2i.dummy_id = 0
             session.add(p2i)
         elif expression_type == 'some_term':
             p2t = Pelement2Term()
             p2t.element = element_id
             p2t.term = expand_curie(form.term.data)
             p2t.dummy_id = 0
             session.add(p2t)
             if active_element:
                 plink = ParticipantLink()
                 plink.child = element_id
                 plink.parent = active_element.element_id
                 session.add(plink)
             else:
                 self.update_participant_links(participant_id, element_id)
     else:
         logging.info("errors(2): {0}".format(form.errors))
         form.element_choice.choices = [("None", "None")]
         form.expression_type.choices = expression_type_choice(session)
         form.parent_link_property.choices = property_choice(session)
         element_id = NOELEMENT
     self.set_claim_form(claim)
     participant = get_participant(session, participant_id)
     self.set_participant_form(participant, parts)
     self.element_form = form
     transaction.commit()
     self.update_graph(claim, parts, part_links)
     self.set_hidden_fields(claim_id, participant_id, element_id)
 def handle_choose_element(self, part_links, parts, req_post):
     session = self.session
     assert self.ref_id == req_post['claim']
     claim = session.query(Claim).filter_by(ref_id=req_post['claim']).one()
     participant_id = req_post['participant']
     element_id = req_post['element']
     element = get_pelement(session, element_id)
     assert element.participant.ref_id == participant_id
     logging.info("element_choice: {0}".format(element.element_id))
     choices = element_choices(session, element.participant)
     self.set_claim_form(claim)
     self.set_participant_form(element.participant, parts)
     self.choose_element_form(req_post, element, choices)
     self.update_graph(claim, parts, part_links)
     self.set_hidden_fields(claim.ref_id, participant_id, element_id)
 def handle_element_down_button(self, part_links, parts, req_post):
     session = self.session
     claim_id = req_post['claim']
     assert claim_id == self.ref_id, "claim_id {0}, ref_id {1}".format(claim_id, self.ref_id)
     claim = session.query(Claim).filter_by(ref_id=claim_id).one()
     origin_element = req_post.get('element_choice')
     element = get_element_child(origin_element, session)
     logging.info("element_choice: {0}".format(element.element_id))
     participant = element.participant
     choices = element_choices(session, participant)
     self.set_claim_form(claim)
     self.set_participant_form(participant, parts)
     self.choose_element_form(req_post, element, choices)
     logging.info("parent_link: {0}".format(self.element_form.parent_link_property.data))
     self.update_graph(claim, parts, part_links)
     self.element_form.element_choice.data = str(element.element_id)
     self.set_hidden_fields(claim_id, participant.ref_id, str(element.element_id))
 def handle_choose_participant(self, req_post):
     claim_id = req_post['claim']
     assert claim_id == self.ref_id, "claim_id {0}, ref_id {1}".format(claim_id, self.ref_id)
     session = self.session
     claim = session.query(Claim).filter_by(ref_id=claim_id).first()
     participant_id = req_post['participant_choice'] # not participant
     participant = get_participant(session, participant_id)
     head_element = get_head_element(session, participant)
     parts, part_links, active = get_participants(self.session, claim_id)
     self.set_claim_form(claim)
     self.choose_participant_form(participant, req_post, parts)
     if head_element:
         self.set_element_form(head_element,
                               element_choices(self.session, participant))
         element_id = str(head_element.element_id)
     else:
         self.element_form = ElementCreateOnly(None)
         self.element_form.expression_type.data = "None"
         self.element_form.expression_type.choices = expression_type_choice(session)
         element_id = NOELEMENT
     self.update_graph(claim, parts, part_links)
     self.set_hidden_fields(claim_id, participant_id, element_id)
 def handle_commit_claim(self, req_post):
     logging.info("Hit 'commit_claim'")
     scf = ClaimCreate(req_post)
     self.set_claim_form_choices(scf)
     if scf.validate():
         scf = ClaimEdit(req_post)
         self.set_claim_form_choices(scf)
         claim = Claim()
         new_ref_id = request_uid(self.session)
         claim.ref_id = new_ref_id
         claim.publication = expand_curie(scf.publication.data)
         if scf.narrative.data == "None":
             claim.narrative = None
         else:
             claim.narrative = expand_curie(scf.narrative.data)
         claim.behavior_term = expand_curie(scf.behavior_term.data)
         claim.publication_behavior = scf.publication_behavior.data
         claim.evidence = expand_curie(scf.evidence.data)
         transaction.commit()  # make sure the ref_id is committed first
         self.session.add(claim)
         # commit so HTTPFound 'exception' doesn't drop transaction (TODO - is there a better way)
         transaction.commit()
         logging.info("About to redirect to {0}".format(generate_route_id(self.request, 'claim_edit', new_ref_id)))
         # TODO - can this setup forms and return normally?
         # self.set_hidden_fields("test claim", "test participant", "test element")
         return HTTPFound(location=generate_route_id(self.request, 'claim_edit', new_ref_id))
     else:
         session = self.session
         old_claim_id = req_post['claim']
         old_participant_id = req_post['participant']
         old_element_id = req_post['element']
         parts, part_links, _ = get_participants(session, old_claim_id)
         self.claim_form = scf
         participant = parts[old_participant_id]
         self.set_participant_form(participant, parts)
         element = session.query(ParticipantElement).filter_by(element_id=old_element_id).one()
         self.set_element_form(element, element_choices(session, participant))
         self.set_hidden_fields(old_claim_id, old_participant_id, old_element_id)
         return self.render()
 def handle_element_sibling_button(self, part_links, parts, req_post):
     logging.info("Hit 'sibling'")
     session = self.session
     claim_id = req_post['claim']
     assert claim_id == self.ref_id, "claim_id {0}, ref_id {1}".format(claim_id, self.ref_id)
     claim = session.query(Claim).filter_by(ref_id=claim_id).one()
     origin_element = req_post.get('element_choice')
     element = get_element_sibling(origin_element, self.session)
     logging.info("element_choice: {0}".format(element.element_id))
     session = self.session
     e_choice = self.int_post_field('element_choice')
     if e_choice:
         active_element = get_pelement(session, e_choice)
     else:
         raise RuntimeError("Element choice %0 did not return a participant element".format(e_choice))
     participant = active_element.participant
     choices = element_choices(session, element.participant)
     self.set_claim_form(claim)
     self.set_participant_form(participant, parts)
     self.choose_element_form(req_post, element, choices)
     self.element_form.element_choice.data = str(element.element_id)
     self.update_graph(claim, parts, part_links)
     self.set_hidden_fields(claim_id, participant.ref_id, str(element.element_id))
 def handle_abort_participant(self, req_post):
     session = self.session
     claim_id = req_post['claim']
     assert claim_id == self.ref_id, "claim_id {0}, ref_id {1}".format(claim_id, self.ref_id)
     claim = self.session.query(Claim).filter_by(ref_id=claim_id).one()
     self.set_claim_form(claim)
     parts, part_links, _ = get_participants(session, claim_id)
     participant_id = req_post['participant']
     participant = self.session.query(Participant).filter_by(ref_id=participant_id).first()
     form = ParticipantEdit(req_post)
     form.ref_id = participant_id
     self.participant_form = self.finish_participant_form(form, parts)
     if participant:
         head_element = get_head_element(session, participant)
         self.set_element_form(head_element,
                               element_choices(session, participant))
         if head_element:
             self.set_hidden_fields(claim_id, participant_id, head_element.id)
         else:
             self.set_hidden_fields(claim_id, participant_id, NOELEMENT)
     else:
         self.element_form = InactiveElementEdit()
         self.set_hidden_fields(claim_id, NOPARTICIPANT, NOELEMENT)
 def handle_form_g_e_t_request(self):
     ref_id = self.ref_id
     logging.info("GET request")
     if ref_id:
         claim = self.session.query(Claim).filter_by(ref_id=ref_id).one()
         self.set_claim_form(claim)
         parts, part_links, active = get_participants(self.session, ref_id)
         if parts:
             active_element = get_head_element(self.session, active)
             self.set_participant_form(active, parts)
             if active_element:
                 choices = element_choices(self.session, active)
                 self.set_element_form(active_element, choices)
                 self.set_hidden_fields(claim.ref_id, active.ref_id, str(active_element.element_id))
             else:
                 self.element_form = InactiveElementEdit(None)
                 self.set_hidden_fields(claim.ref_id, active.ref_id, NOELEMENT)
             if part_links:
                 self.update_graph(claim, parts, part_links)
         else:
             form = ParticipantCreate(None)
             form.ref_id = 'UNASSIGNED'
             if active:
                 form.type.data = active.type
             else:
                 form.type.data = 'UNASSIGNED'  # should guess from claim
             self.participant_form = self.finish_participant_form(form, parts)  # ?
             self.element_form = InactiveElementEdit(None)
             self.set_hidden_fields(claim.ref_id, NOPARTICIPANT, NOELEMENT)
     else:  # GET for claim edit w/o ref_id means create a new claim
         self.claim_form = ClaimCreate(None)
         self.set_claim_form_choices(self.claim_form)
         inactive_participant_form = InactiveParticipantEdit(None)
         inactive_element_form = InactiveElementEdit(None)
         self.participant_form = inactive_participant_form
         self.element_form = inactive_element_form
         self.set_hidden_fields(NOCLAIM, NOPARTICIPANT, NOELEMENT)