def test_update_contact_organization(self): contact, role = helpers.link_contact_role_for_organization( self.user, self.organization_node, self.contact_node.handle_id, self.role ) self.assertEqual(len(self.organization_node.relationships), 1) anther_role = Role.objects.get_or_create(name="NOC Manager")[0] relationship_id = \ self.organization_node.get_relations()\ .get('Works_for')[0]['relationship_id'] contact, role = helpers.link_contact_role_for_organization( self.user, self.organization_node, self.contact_node.handle_id, anther_role, relationship_id ) self.assertEqual(len(self.organization_node.relationships), 1) self.assertEqual(role.name, anther_role.name)
def test_link_contact_role_for_organization(self): self.assertEqual(len(self.organization_node.relationships), 0) contact, role = helpers.link_contact_role_for_organization( self.user, self.organization_node, self.contact_node.handle_id, self.role ) self.assertEqual(role.name, self.role.name) self.assertEqual( contact.get_node(), self.organization_node.get_relations().get('Works_for')[0].get('node') )
def mutate_and_get_payload(cls, root, info, **input): if not info.context or not info.context.user.is_authenticated: raise GraphQLAuthException() user = info.context.user errors = None rolerelation = None # get input contact_handle_id = input.get('contact_handle_id', None) organization_id = input.get('organization_id', None) role_id = input.get('role_id', None) relation_id = input.get('relation_id', None) if role_id: role_handle_id = relay.Node.from_global_id(role_id)[1] else: default_role = RoleModel.objects.get(slug=DEFAULT_ROLE_KEY) role_handle_id = default_role.handle_id organization_handle_id = relay.Node.from_global_id(organization_id)[1] # get entities and check permissions contact_nh = None organization_nh = None role_model = None add_error_contact = False add_error_organization = False add_error_role = False if sriutils.authorice_write_resource(user, contact_handle_id): try: contact_nh = NodeHandle.objects.get(handle_id=contact_handle_id) except: add_error_contact = True else: add_error_contact = True if add_error_contact: error = ErrorType( field="contact_handle_id", messages=["The selected contact doesn't exist"] ) errors.append(error) if sriutils.authorice_write_resource(user, organization_handle_id): try: organization_nh = NodeHandle.objects.get(handle_id=organization_handle_id) except: add_error_organization = True else: add_error_organization = True if add_error_organization: error = ErrorType( field="organization_handle_id", messages=["The selected organization doesn't exist"] ) errors.append(error) try: role_model = RoleModel.objects.get(handle_id=role_handle_id) except: add_error_role = True if add_error_role: error = ErrorType( field="role_handle_id", messages=["The selected role doesn't exist"] ) errors.append(error) # link contact with organization if not errors: contact, rolerelation = helpers.link_contact_role_for_organization( user, organization_nh.get_node(), contact_nh.handle_id, role_model, relation_id ) return cls(errors=errors, rolerelation=rolerelation)
def do_request(cls, request, **kwargs): form_class = kwargs.get('form_class') nimetaclass = getattr(cls, 'NIMetaClass') graphql_type = getattr(nimetaclass, 'graphql_type') nimetatype = getattr(graphql_type, 'NIMetaType') node_type = getattr(nimetatype, 'ni_type').lower() node_meta_type = getattr(nimetatype, 'ni_metatype').capitalize() id = request.POST.get('id') has_error = False # check authorization handle_id = relay.Node.from_global_id(id)[1] authorized = sriutils.authorice_write_resource(request.user, handle_id) if not authorized: raise GraphQLAuthException() # Get needed data from node nh, organization = helpers.get_nh_node(handle_id) relations = organization.get_relations() out_relations = organization.get_outgoing_relations() if request.POST: # set handle_id into POST data and remove relay id post_data = request.POST.copy() post_data.pop('id') post_data.update({'handle_id': handle_id}) # replace relay ids for handle_id in contacts if present for field, roledict in DEFAULT_ROLES.items(): if field in post_data: handle_id = post_data.get(field) handle_id = relay.Node.from_global_id(handle_id)[1] post_data.pop(field) post_data.update({field: handle_id}) relay_extra_ids = ('relationship_parent_of', 'relationship_uses_a') for field in relay_extra_ids: handle_id = post_data.get(field) if handle_id: handle_id = relay.Node.from_global_id(handle_id)[1] post_data.pop(field) post_data.update({field: handle_id}) form = form_class(post_data) form.strict_validation = True if form.is_valid(): # Generic node update # use property keys to avoid inserting contacts as a string property of the node property_keys = [ 'name', 'description', 'organization_id', 'type', 'incident_management_info', 'affiliation_customer', 'affiliation_end_customer', 'affiliation_provider', 'affiliation_partner', 'affiliation_host_user', 'affiliation_site_owner', 'website', 'organization_number' ] helpers.form_update_node(request.user, organization.handle_id, form, property_keys) # specific role setting for field, roledict in DEFAULT_ROLES.items(): if field in form.cleaned_data: contact_id = form.cleaned_data[field] role = RoleModel.objects.get(slug=field) set_contact = helpers.get_contact_for_orgrole(organization.handle_id, role) if contact_id: if set_contact: if set_contact.handle_id != contact_id: helpers.unlink_contact_with_role_from_org(request.user, organization, role) helpers.link_contact_role_for_organization(request.user, organization, contact_id, role) else: helpers.link_contact_role_for_organization(request.user, organization, contact_id, role) elif set_contact: helpers.unlink_contact_and_role_from_org(request.user, organization, set_contact.handle_id, role) # Set child organizations if form.cleaned_data['relationship_parent_of']: organization_nh = NodeHandle.objects.get(handle_id=form.cleaned_data['relationship_parent_of']) helpers.set_parent_of(request.user, organization, organization_nh.handle_id) if form.cleaned_data['relationship_uses_a']: procedure_nh = NodeHandle.objects.get(handle_id=form.cleaned_data['relationship_uses_a']) helpers.set_uses_a(request.user, organization, procedure_nh.handle_id) return has_error, { graphql_type.__name__.lower(): nh } else: # get the errors and return them has_error = True errordict = cls.format_error_array(form.errors) return has_error, errordict else: # get the errors and return them has_error = True errordict = cls.format_error_array(form.errors) return has_error, errordict
def do_request(cls, request, **kwargs): form_class = kwargs.get('form_class') nimetaclass = getattr(cls, 'NIMetaClass') graphql_type = getattr(nimetaclass, 'graphql_type') nimetatype = getattr(graphql_type, 'NIMetaType') node_type = getattr(nimetatype, 'ni_type').lower() node_meta_type = getattr(nimetatype, 'ni_metatype').capitalize() context_method = getattr(nimetatype, 'context_method') has_error = False context = context_method() # check it can write on this context authorized = sriutils.authorize_create_resource(request.user, context) if not authorized: raise GraphQLAuthException() # Get needed data from node if request.POST: # replace relay ids for handle_id in contacts if present post_data = request.POST.copy() for field, roledict in DEFAULT_ROLES.items(): if field in post_data: handle_id = post_data.get(field) handle_id = relay.Node.from_global_id(handle_id)[1] post_data.pop(field) post_data.update({field: handle_id}) relay_extra_ids = ('relationship_parent_of', 'relationship_uses_a') for field in relay_extra_ids: handle_id = post_data.get(field) if handle_id: try: handle_id = relay.Node.from_global_id(handle_id)[1] post_data.pop(field) post_data.update({field: handle_id}) except BinasciiError: pass # the id is already in handle_id format form = form_class(post_data) form.strict_validation = True if form.is_valid(): try: nh = helpers.form_to_generic_node_handle(request, form, node_type, node_meta_type, context) except UniqueNodeError: has_error = True return has_error, [ErrorType(field="_", messages=["A {} with that name already exists.".format(node_type)])] # Generic node update # use property keys to avoid inserting contacts as a string property of the node property_keys = [ 'name', 'description', 'organization_id', 'type', 'incident_management_info', 'affiliation_customer', 'affiliation_end_customer', 'affiliation_provider', 'affiliation_partner', 'affiliation_host_user', 'affiliation_site_owner', 'website', 'organization_number' ] helpers.form_update_node(request.user, nh.handle_id, form, property_keys) nh_reload, organization = helpers.get_nh_node(nh.handle_id) # add default context NodeHandleContext(nodehandle=nh, context=context).save() # specific role setting for field, roledict in DEFAULT_ROLES.items(): if field in form.cleaned_data: contact_id = form.cleaned_data[field] role = RoleModel.objects.get(slug=field) set_contact = helpers.get_contact_for_orgrole(organization.handle_id, role) if contact_id: if set_contact: if set_contact.handle_id != contact_id: helpers.unlink_contact_with_role_from_org(request.user, organization, role) helpers.link_contact_role_for_organization(request.user, organization, contact_id, role) else: helpers.link_contact_role_for_organization(request.user, organization, contact_id, role) elif set_contact: helpers.unlink_contact_and_role_from_org(request.user, organization, set_contact.handle_id, role) # Set child organizations if form.cleaned_data['relationship_parent_of']: organization_nh = NodeHandle.objects.get(handle_id=form.cleaned_data['relationship_parent_of']) helpers.set_parent_of(request.user, organization, organization_nh.handle_id) if form.cleaned_data['relationship_uses_a']: procedure_nh = NodeHandle.objects.get(handle_id=form.cleaned_data['relationship_uses_a']) helpers.set_uses_a(request.user, organization, procedure_nh.handle_id) return has_error, { graphql_type.__name__.lower(): nh } else: # get the errors and return them has_error = True errordict = cls.format_error_array(form.errors) return has_error, errordict else: # get the errors and return them has_error = True errordict = cls.format_error_array(form.errors) return has_error, errordict
def setUp(self, group_dict=None): super(Neo4jGraphQLCommunityTest, self).setUp(group_dict=group_dict) # create nodes self.organization1 = self.create_node('organization1', 'organization', meta='Logical') self.organization2 = self.create_node('organization2', 'organization', meta='Logical') self.contact1 = self.create_node('contact1', 'contact', meta='Relation') self.contact2 = self.create_node('contact2', 'contact', meta='Relation') self.group1 = self.create_node('group1', 'group', meta='Logical') self.group2 = self.create_node('group2', 'group', meta='Logical') self.role1 = Role(name='role1').save() self.role2 = Role(name='role2').save() # add nodes to the appropiate context self.perm_rel_org1 = \ NodeHandleContext(nodehandle=self.organization1, \ context=self.community_ctxt) self.perm_rel_org1.save() self.perm_rel_org2 = \ NodeHandleContext(nodehandle=self.organization2, \ context=self.community_ctxt) self.perm_rel_org2.save() NodeHandleContext(nodehandle=self.contact1, context=self.community_ctxt).save() NodeHandleContext(nodehandle=self.contact2, context=self.community_ctxt).save() NodeHandleContext(nodehandle=self.group1, context=self.community_ctxt).save() NodeHandleContext(nodehandle=self.group2, context=self.community_ctxt).save() # add some data contact1_data = { 'first_name': 'Jane', 'last_name': 'Doe', 'name': 'Jane Doe', } for key, value in contact1_data.items(): self.contact1.get_node().add_property(key, value) contact2_data = { 'first_name': 'John', 'last_name': 'Smith', 'name': 'John Smith', } for key, value in contact2_data.items(): self.contact2.get_node().add_property(key, value) organization1_data = { 'type': 'university_college', 'organization_id': 'ORG1', 'affiliation_customer': True, } for key, value in organization1_data.items(): self.organization1.get_node().add_property(key, value) organization2_data = { 'type': 'university_coldep', 'organization_id': 'ORG2', 'affiliation_end_customer': True, } for key, value in organization2_data.items(): self.organization2.get_node().add_property(key, value) # create relationships self.contact1.get_node().add_group(self.group1.handle_id) self.contact2.get_node().add_group(self.group2.handle_id) helpers.link_contact_role_for_organization( self.context.user, self.organization1.get_node(), self.contact1.handle_id, self.role1 ) helpers.link_contact_role_for_organization( self.context.user, self.organization2.get_node(), self.contact2.handle_id, self.role2 ) # create dummy dropdown dropdown = Dropdown.objects.get_or_create(name='contact_type')[0] dropdown.save() ch1 = Choice.objects.get_or_create(dropdown=dropdown, name='Person', value='person')[0] ch2 = Choice.objects.get_or_create(dropdown=dropdown, name='Group', value='group')[0] ch1.save() ch2.save()