def _check_property_datas(self, node_object, property_recovered_datas): """ This method checks and enforces the restrictions (required, unique, etc...) of a field. :param node_object: An instance of the Node class. :param property_recovered_datas: The dictionary of values recovered during the instantiation of the Node class or one of these children. :return: """ property_field_name = self.key try: property_associate_datas = property_recovered_datas[ property_field_name] except KeyError: # Check if a property is 'required' : if self.required and self.default is None: raise N4DRequiredConstraintError( f"An instance of {node_object.__class__.__name__} must have a '{property_field_name}'." ) else: # Check if a property is 'unique' : if self.unique: # Try to get an instance of the current node class with the same property if isinstance(property_associate_datas, str): query = 'MATCH (n:%s) WHERE n.%s = "%s" RETURN (n)' % ( node_object.labels, property_field_name, property_associate_datas) result = gdbh.r_transaction(query) else: query = 'MATCH (n:%s) WHERE n.%s = %s RETURN (n)' % ( node_object.labels, property_field_name, property_associate_datas) result = gdbh.r_transaction(query) # If an instance is found, raise an error if result: raise N4DUniqueConstraintError( f"An instance of {node_object.__class__.__name__} must have an UNIQUE '{property_field_name}'." ) else: return True else: return True finally: # Checks if a property is not be at the same time 'required' and with a 'default' value : if self.required and self.default is not None: raise N4DAttributeError( 'A field must not have "required=True" and a "default" value.' )
def get_session(self): response = gdbh.r_transaction(""" MATCH (:User {uuid:'%s'})<-[:IS_SESSION_OF]-(s:Session) RETURN (s) """ % self.uuid) if response: fake_session_instance = FakeClass() fake_session_instance.__class__ = Session try: setattr(fake_session_instance, 'session_key', response[0]['s']._properties['session_key']) setattr(fake_session_instance, 'session_data', response[0]['s']._properties['session_data']) setattr(fake_session_instance, 'expire_date', response[0]['s']._properties['expire_date']) except AttributeError: # If ne session isn't stored, it will not have attribute 'properties'. return None else: return fake_session_instance else: return None
def get(cls, group_name): response = gdbh.r_transaction( "MATCH (g:Group {name: '%s'}) RETURN (g)" % group_name) if response: fake_permission_instance = FakeClass() fake_permission_instance.__class__ = cls setattr(fake_permission_instance, 'uuid', response[0]['g']._properties['uuid']) setattr(fake_permission_instance, 'name', response[0]['g']._properties['name']) return fake_permission_instance else: return None
def get(cls, permission_codename): response = gdbh.r_transaction( 'MATCH (p:Permission {codename:"%s"}) RETURN (p)' % permission_codename) if response: fake_permission_instance = FakeClass() fake_permission_instance.__class__ = cls setattr(fake_permission_instance, 'description', response[0]['p']._properties['description']) setattr(fake_permission_instance, 'codename', response[0]['p']._properties['codename']) return fake_permission_instance else: return None
def get(cls, uuid=None, email=None): if uuid and email is None: properties = f"uuid: '{uuid}'" elif email and uuid is None: properties = f"email: '{email}'" elif uuid and email: properties = f"uuid: '{uuid}', email: '{email}'" else: raise N4DGetUserError( "To get an user, you must provide his 'uuid' or his 'email' or both." ) response = gdbh.r_transaction("MATCH (u:User {%s}) RETURN (u)" % properties) if response: fake_user_instance = FakeClass() fake_user_instance.__class__ = cls setattr(fake_user_instance, 'is_super_user', response[0]['u']._properties['is_super_user']) setattr(fake_user_instance, 'is_staff_user', response[0]['u']._properties['is_staff_user']) setattr(fake_user_instance, 'is_active_user', response[0]['u']._properties['is_active_user']) setattr(fake_user_instance, 'uuid', response[0]['u']._properties['uuid']) setattr(fake_user_instance, 'first_name', response[0]['u']._properties['first_name']) setattr(fake_user_instance, 'last_name', response[0]['u']._properties['last_name']) setattr(fake_user_instance, 'email', response[0]['u']._properties['email']) setattr(fake_user_instance, 'password', response[0]['u']._properties['password']) setattr(fake_user_instance, 'registration_datetime', response[0]['u']._properties['registration_datetime']) return fake_user_instance else: return AnonymousUser()
def get_groups(self): request_response = gdbh.r_transaction(""" MATCH (:User {uuid:'%s'})-[:IS_IN]->(g:Group) RETURN (g) """ % self.uuid) if request_response: render = set() for group in request_response: fake_group_instance = FakeClass() fake_group_instance.__class__ = Group setattr(fake_group_instance, 'uuid', group['g']._properties['uuid']) setattr(fake_group_instance, 'name', group['g']._properties['name']) render.add(fake_group_instance) if render: return render return None
def _get_only_user_permissions(self): request_response = gdbh.r_transaction(""" MATCH (:User {uuid:'%s'})-[:CAN]->(p:Permission) RETURN p """ % self.uuid) if request_response: render = set() for permission in request_response: fake_permission_instance = FakeClass() fake_permission_instance.__class__ = Permission setattr(fake_permission_instance, 'description', permission['p']._properties['description']) setattr(fake_permission_instance, 'codename', permission['p']._properties['codename']) render.add(fake_permission_instance) if render: return render return None
def get_users(self): request_response = gdbh.r_transaction(""" MATCH (u:User)-[:IS_IN]->(:Group {uuid:'%s'}) RETURN u """ % self.uuid) if request_response: render = set() for user in request_response: fake_user_instance = FakeClass() fake_user_instance.__class__ = User setattr(fake_user_instance, 'is_super_user', user['u']._properties['is_super_user']) setattr(fake_user_instance, 'is_staff_user', user['u']._properties['is_staff_user']) setattr(fake_user_instance, 'is_active_user', user['u']._properties['is_active_user']) setattr(fake_user_instance, 'uuid', user['u']._properties['uuid']) setattr(fake_user_instance, 'first_name', user['u']._properties['first_name']) setattr(fake_user_instance, 'last_name', user['u']._properties['last_name']) setattr(fake_user_instance, 'email', user['u']._properties['email']) setattr(fake_user_instance, 'password', user['u']._properties['password']) setattr(fake_user_instance, 'registration_datetime', user['u']._properties['registration_datetime']) render.add(fake_user_instance) if render: return render return None
def get(cls, session_key): response = gdbh.r_transaction("MATCH (s:Session {session_key:'%s'}) RETURN (s)" % session_key) if response: return response else: raise N4DSessionDoesNotExist(f"No Session found with session_key = '{session_key}'.")
def clear_expired_sessions(cls): gdbh.r_transaction(""" MATCH (s:Session) WHERE s.expire_date < datetime('%s') DETACH DELETE (s) """ % str(timezone.now()).replace(' ', 'T'))