Skip to content
Snippets Groups Projects
Commit e4806cf7 authored by REIG Julien's avatar REIG Julien
Browse files

Merge branch 'resource-state' into 'master'

add deleted to resources

See merge request !14
parents b512456d b07f5434
No related branches found
No related tags found
1 merge request!14add deleted to resources
from consts.numerical_values import HEXADECIMAL_WORD_LENGTH
from utils.time_utils import current_milli_time
from utils.hex_generator import generate_hexadecimal
from utils.time_utils import current_milli_time
from .command import Command
......@@ -13,22 +13,26 @@ class Resource:
self.last_update = current_milli_time()
self.commands = [] if commands is None else commands
self.commands_count = len(self.commands)
self.deleted = False
def __str__(self):
return f"Resource(id={self.id}, name={self.name}, last_update={self.last_update}, commands={self.commands})"
return f"Resource(id={self.id}, name={self.name}, last_update={self.last_update}, commands={self.commands}, commands_count={self.commands_count}, deleted={self.deleted})"
def __repr__(self):
return self.__str__()
def add_command(self, command):
def add_command(self, command: Command):
if (command.command == "DELETE"):
self.deleted = True
self.commands.append(command)
self.commands_count = len(self.commands)
@staticmethod
def from_tuple(data, commands: list[Command] = None, commands_count: int = None):
def from_tuple(data, commands: list[Command] = None, commands_count: int = None, deleted: bool = False):
resource = Resource(data[1])
resource.id = data[0]
resource.last_update = data[2]
resource.deleted = deleted
if (commands is not None):
resource.commands = commands
resource.commands_count = len(resource.commands)
......@@ -37,10 +41,11 @@ class Resource:
return resource
@staticmethod
def from_full_dict(data):
def from_full_dict(data: dict):
resource = Resource(data["name"])
resource.id = data["id"]
resource.last_update = data["lastUpdate"]
resource.deleted = data.get("deleted", False)
resource.commands = [Command.from_full_dict(
command) for command in data["commands"]]
resource.commands_count = len(resource.commands)
......@@ -54,6 +59,7 @@ class Resource:
"id": self.id,
"name": self.name,
"lastUpdate": self.last_update,
"deleted": self.deleted,
"commands": [command.to_dict() for command in self.commands]
}
......@@ -62,5 +68,6 @@ class Resource:
"id": self.id,
"name": self.name,
"lastUpdate": self.last_update,
"deleted": self.deleted,
"commandsCount": self.commands_count
}
......@@ -65,9 +65,9 @@ def get_resources(with_commands: bool = False, page: int = 1, page_size=20) -> O
resources: list[tuple] = cursor.fetchall()
cursor.close()
if with_commands:
return [Resource.from_tuple(resource, commands=get_commands(resource[0])) for resource in resources]
return [Resource.from_tuple(resource, commands=get_commands(resource[0]), deleted=resource_id_is_deleted(resource[0])) for resource in resources]
return [Resource.from_tuple(resource, commands_count=get_commands_count(resource[0])) for resource in resources]
return [Resource.from_tuple(resource, commands_count=get_commands_count(resource[0]),deleted=resource_id_is_deleted(resource[0])) for resource in resources]
except Exception as e:
print(e, file=sys.stderr)
return None
......@@ -96,20 +96,21 @@ def get_resource(id: str, with_commands: bool = False) -> Optional[Resource]:
cursor.close()
if resource is None:
return None
deleted = resource_id_is_deleted(id)
if with_commands:
return Resource.from_tuple(resource, commands=get_commands(id))
return Resource.from_tuple(resource, commands_count=get_commands_count(id))
return Resource.from_tuple(resource, commands=get_commands(id), deleted=deleted)
return Resource.from_tuple(resource, commands_count=get_commands_count(id), deleted=deleted)
except Exception as e:
print(e, file=sys.stderr)
return None
def resource_is_deleted(resource: Resource) -> bool:
def resource_id_is_deleted(resource_id: int) -> bool:
with sqlite3.connect(db_file) as db_conn:
try:
cursor = db_conn.cursor()
cursor.execute(
"SELECT command FROM commands WHERE resource_id = ? ORDER BY create_date DESC LIMIT 1", (resource.id,))
"SELECT command FROM commands WHERE resource_id = ? ORDER BY create_date DESC LIMIT 1", (resource_id,))
command = cursor.fetchone()
cursor.close()
return command[0] == "DELETE"
......@@ -117,6 +118,9 @@ def resource_is_deleted(resource: Resource) -> bool:
print(e, file=sys.stderr)
return True
def resource_is_deleted(resource: Resource) -> bool:
return resource_id_is_deleted(resource.id)
def insert_command(resource_id: str, command: Command) -> bool:
with sqlite3.connect(db_file) as db_conn:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment