def count(cls, uuid=None, order_by=None, limit=None, skip=None, desc=False, only=None, filter=None, **extrafields): request_statement = cls.get(uuid=uuid, order_by=order_by, limit=limit, skip=skip, desc=desc, only=only, filter=filter, return_query=True, **extrafields) request_count_statement = request_statement.split("RETURN")[0] + "RETURN COUNT(s)" response = gdbh.r_transaction(request_count_statement) return response[0]["COUNT(s)"]
def count(cls, uuid=None, email=None, email_confirmation_key=None, order_by=None, limit=None, skip=None, desc=False, only=None, filter=None, distinct=False, handmade=None, **extrafields): request_statement = cls.get( uuid=uuid, email=email, email_confirmation_key=email_confirmation_key, order_by=order_by, limit=limit, skip=skip, desc=desc, only=only, filter=filter, handmade=handmade, return_query=True, **extrafields) request_count_statement = None if not distinct: request_count_statement = request_statement.split( "RETURN")[0] + "RETURN COUNT(u)" else: request_count_statement = request_statement.split( "RETURN")[0] + "RETURN COUNT(DISTINCT u)" response = gdbh.r_transaction(request_count_statement) if not distinct: return response[0]["COUNT(u)"] else: return response[0]["COUNT(DISTINCT u)"]
def count(cls, uuid=None, codename=None, order_by=None, limit=None, skip=None, desc=False, only=None, filter=None, distinct=False, **extrafields): request_statement = cls.get(uuid=None, codename=None, order_by=None, limit=None, skip=None, desc=False, only=None, filter=None, distinct=False, return_query=True, **extrafields) request_count_statement = None if not distinct: request_count_statement = request_statement.split( "RETURN")[0] + "RETURN COUNT(p)" else: request_count_statement = request_statement.split( "RETURN")[0] + "RETURN COUNT(DISTINCT p)" response = gdbh.r_transaction(request_count_statement) if not distinct: return response[0]["COUNT(p)"] else: return response[0]["COUNT(DISTINCT p)"]
def get(cls, uuid=None, email=None, email_confirmation_key=None, order_by=None, limit=None, skip=None, desc=False, only=None, filter=None, distinct=False, handmade=None, return_query=False): """ This method allow the retrieving of User (or of one of its children classes) instances. :param email (required if there is neither uuid nor email_confirmation_key) : The email of a user to get an unique user instance. :param uuid (required if there is neither email nor email_confirmation_key) : The Universal Unique Identifier of a node to get an unique instance. :param email_confirmation_key (required if there is neither email nor uuid) : The confirmation key for the email validation part. :param order_by (optional, default=None) : Must be the name of the property with which the returned datas will be sorted. Examples : "datetime", "first_name", etc... :param limit (optional, default=None) : Must be an integer. This parameter defines the number of returned elements. :param skip (optional, default=None) : Must be an integer. This parameter defines the number of skipped elements. For example if self.skip = 3, the 3 first returned elements will be skipped. :param desc (optional, default=False) : Must be a boolean. If it is False the elements will be returned in an increasing order, but it is True, they will be returned in a descending order. :param only (optional, default=None) : Must be a list of field_names. If this parameter is filled, the return will not be Node instances, but a dict with "only" the mentioned fields. :param filter (optional, default=None) : Must be Q statement. You must use the Q class stored in bulb.db Example: Q(name__contains="al") | Q(age__year__lte=8) :param distinct (optional, default=False) : Must be a boolean. If it is True, the returned list will be only composed with unique elements. :param handmade: If this param is filled, no other must be filled. With 'handmade', you can insert your own get query and the function will transform the objects returned by the database into Python instance of this class. WARNING : Please consider using the gdbh.r_transaction() method (imported from bulb.db) if your request no returns full Neo4j objects but a list of properties. Examples : ❌ : MATCH (u) RETURN u.first_name, u.last_name ✅ : MATCH (u) RETURN u In addition, note that the RETURN must absolutely be named 'u' like "user". :param return_query (optional, default=False) : Must be a boolean. If true, the method will return the cypher query. :return: If uuid is None, a list will be returned. Else it will be a unique instance. """ if handmade is None: where_statement = "" property_statement = "" order_by_statement = "" limit_statement = "" skip_statement = "" desc_statement = "" # Build the property_statement. if uuid is not None and email is not None: property_statement = "{uuid:'%s', email:'%s'}" % (uuid, email) elif uuid is not None: property_statement = "{uuid:'%s'}" % uuid elif email is not None: property_statement = "{email:'%s'}" % email elif email_confirmation_key is not None: property_statement = "{email_confirmation_key:'%s'}" % email_confirmation_key # Build the match_statement. cyher_labels = node_models.DatabaseNode.format_labels_to_cypher( cls._get_labels()) match_statement = f"MATCH (u:{cyher_labels} {property_statement})" # Build the where statement. if filter is not None: # where_statement = "WHERE " + filter #removed to fix if not filter[0] != "n": where_statement = "WHERE " + filter else: where_statement = filter where_statement = where_statement.replace("n.", "u.") # Build the with_statement. with_statement = "WITH u" # Build order_by statements. if order_by is not None: order_by_statement = f"ORDER BY u.{order_by}" # Build return_statement statements. if not only: if not distinct: return_statement = "RETURN (u)" else: return_statement = "RETURN DISTINCT (u)" else: only_statement_list = [] for element in only: only_statement_list.append(f"u.{element}") only_statement = ", ".join(only_statement_list) if not distinct: return_statement = f"RETURN {only_statement}" else: return_statement = f"RETURN DISTINCT {only_statement}" # Build limit_statement. if limit is not None: if not isinstance(limit, str) and not isinstance(limit, int): bulb_logger.error( f'BULBNodeError("The \'limit\' parameter of the get() method of {cls.__name__} must be a string or an integer.")' ) raise BULBNodeError( f"The 'limit' parameter of the get() method of {cls.__name__} must be a string or an integer." ) else: limit_statement = f"LIMIT {limit}" # Build skip_statement and add its required variable. if skip is not None: if not isinstance(skip, str) and not isinstance(skip, int): bulb_logger.error( f'BULBNodeError("The \'skip\' parameter of the get() method of {cls.__name__} must be a string or an integer.")' ) raise BULBNodeError( f"The 'skip' parameter of the get() method of {cls.__name__} must be a string or an integer." ) else: skip_statement = f"SKIP {skip}" # Build desc_statement. if not isinstance(desc, bool): bulb_logger.error( f'BULBNodeError("The \'desc\' parameter of the get() method of {cls.__name__} must be a boolean.")' ) raise BULBNodeError( f"The 'desc' parameter of the get() method of {cls.__name__} must be a boolean." ) else: if desc is True: desc_statement = "DESC" request_statement = """ %s %s %s %s %s %s %s %s """ % (match_statement, where_statement, with_statement, order_by_statement, desc_statement, skip_statement, limit_statement, return_statement) if return_query is False: response = gdbh.r_transaction(request_statement) if response: if only is None: fake_instances_list = [] for user_object in response: fake_instances_list.append( cls.build_fake_instance( user_object["u"], forced_fake_instance_class=cls)) if uuid is not None or email is not None or email_confirmation_key is not None: return fake_instances_list[0] else: return fake_instances_list else: return response else: if uuid is not None or email is not None or email_confirmation_key is not None: if settings.BULB_ANONYMOUSUSER_NODE_MODEL_FILE: # TODO : This line is maybe useless, check it. AnonymousUser_node_model = get_anonymoususer_node_model( ) return AnonymousUser_node_model() else: return None else: return request_statement else: response = gdbh.r_transaction(handmade) fake_instances_list = [] for node_object in response: fake_instances_list.append( cls.build_fake_instance(node_object["u"], forced_fake_instance_class=cls)) return fake_instances_list
def get(cls, uuid=None, session_key=None, order_by=None, limit=None, skip=None, desc=False, only=None, filter=None, return_query=False): """ This method allow the retrieving of Session (or of one of its children classes) instances. :param uuid: The Universal Unique Identifier of a session to get an unique session instance. :param session_key: The session_key of a session to get an unique session instance. :param order_by: Must be the name of the property with which the returned datas will be sorted. Examples : "datetime", "first_name", etc... :param limit: Must be an integer. This parameter defines the number of returned elements. :param skip: Must be an integer. This parameter defines the number of skipped elements. For example if self.skip = 3, the 3 first returned elements will be skipped. :param desc: Must be a boolean. If it is False the elements will be returned in an increasing order, but it is True, they will be returned in a descending order. :param only: Must be a list of field_names. If this parameter is filled, the return will not be Permission instances, but a dict with "only" the mentioned fields. :param filter: Must be Q statement. You must use the Q class stored in bulb.db Example: Q(name__contains="al") | Q(age__year__lte=8) :param return_query: Must be a boolean. If true, the method will return the cypher query. :return: If uuid is None, a list will be returned. Else it will be a unique instance. """ where_statement = "" property_statement = "" order_by_statement = "" limit_statement = "" skip_statement = "" desc_statement = "" # Build the property_statement. if uuid is not None and session_key is not None: property_statement = "{uuid:'%s', session_key:'%s'}" % ( uuid, session_key) elif uuid is not None: property_statement = "{uuid:'%s'}" % uuid elif session_key is not None: property_statement = "{session_key:'%s'}" % session_key # Build the match_statement. cyher_labels = node_models.DatabaseNode.format_labels_to_cypher( cls._get_labels()) match_statement = f"MATCH (s:{cyher_labels} {property_statement})" # Build the where statement. if filter is not None: where_statement = "WHERE " + filter where_statement = where_statement.replace("n.", "s.") # Build the with_statement. with_statement = "WITH s" # Build order_by statements. if order_by is not None: order_by_statement = f"ORDER BY s.{order_by}" # Build return_statement statements. if not only: return_statement = "RETURN (s)" else: only_statement_list = [] for element in only: only_statement_list.append(f"s.{element}") only_statement = ", ".join(only_statement_list) return_statement = f"RETURN {only_statement}" # Build limit_statement. if limit is not None: if not isinstance(limit, str) and not isinstance(limit, int): raise BULBSessionError( f"The 'limit' parameter of the get() method of {cls.__name__} must be a string or an integer." ) else: limit_statement = f"LIMIT {limit}" # Build skip_statement and add its required variable. if skip is not None: if not isinstance(skip, str) and not isinstance(skip, int): raise BULBSessionError( f"The 'skip' parameter of the get() method of {cls.__name__} must be a string or an integer." ) else: skip_statement = f"SKIP {skip}" # Build desc_statement. if not isinstance(desc, bool): raise BULBSessionError( f"The 'desc' parameter of the get() method of {cls.__name__} must be a boolean." ) else: if desc is True: desc_statement = "DESC" request_statement = """ %s %s %s %s %s %s %s %s """ % (match_statement, where_statement, with_statement, order_by_statement, desc_statement, skip_statement, limit_statement, return_statement) if return_query is False: response = gdbh.r_transaction(request_statement) if response: if only is None: fake_instances_list = [] for session_object in response: fake_instances_list.append( cls.build_fake_instance( session_object["s"], forced_fake_instance_class=cls)) if uuid is not None or session_key is not None: return fake_instances_list[0] else: return fake_instances_list else: return response else: return None else: return request_statement
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'))