|
@@ -1,7 +1,7 @@
|
|
|
from contextlib import closing
|
|
|
from os.path import join
|
|
|
from time import time
|
|
|
-from typing import List, Optional, Tuple, Iterator
|
|
|
+from typing import List, Optional, Tuple, Iterator, Union
|
|
|
|
|
|
from pycs.database.Collection import Collection
|
|
|
from pycs.database.File import File
|
|
@@ -77,26 +77,48 @@ class Project:
|
|
|
|
|
|
return None
|
|
|
|
|
|
+ def label_by_reference(self, reference: str) -> Optional[Label]:
|
|
|
+ """
|
|
|
+ get a label using its reference string
|
|
|
+
|
|
|
+ :param reference: reference string
|
|
|
+ :return: label
|
|
|
+ """
|
|
|
+ with closing(self.database.con.cursor()) as cursor:
|
|
|
+ cursor.execute('SELECT * FROM labels WHERE reference = ? AND project = ?',
|
|
|
+ (reference, self.identifier))
|
|
|
+ row = cursor.fetchone()
|
|
|
+
|
|
|
+ if row is not None:
|
|
|
+ return Label(self.database, row)
|
|
|
+
|
|
|
+ return None
|
|
|
+
|
|
|
def create_label(self, name: str, reference: str = None,
|
|
|
- parent_id: int = None) -> Tuple[Optional[Label], bool]:
|
|
|
+ parent: Union[Label, int, str] = None) -> Tuple[Optional[Label], bool]:
|
|
|
"""
|
|
|
create a label for this project. If there is already a label with the same reference
|
|
|
in the database its name is updated.
|
|
|
|
|
|
:param name: label name
|
|
|
:param reference: label reference
|
|
|
- :param parent_id: parent's identifier
|
|
|
+ :param parent: either parent identifier, parent reference string or `Label` object
|
|
|
:return: created or edited label, insert
|
|
|
"""
|
|
|
created = int(time())
|
|
|
|
|
|
+ if isinstance(parent, str):
|
|
|
+ parent = self.label_by_reference(parent)
|
|
|
+ if isinstance(parent, Label):
|
|
|
+ parent = parent.identifier
|
|
|
+
|
|
|
with closing(self.database.con.cursor()) as cursor:
|
|
|
cursor.execute('''
|
|
|
INSERT INTO labels (project, parent, created, reference, name)
|
|
|
VALUES (?, ?, ?, ?, ?)
|
|
|
ON CONFLICT (project, reference) DO
|
|
|
UPDATE SET parent = ?, name = ?
|
|
|
- ''', (self.identifier, parent_id, created, reference, name, parent_id, name))
|
|
|
+ ''', (self.identifier, parent, created, reference, name, parent, name))
|
|
|
|
|
|
# lastrowid is 0 if on conflict clause applies.
|
|
|
# If this is the case we do an extra query to receive the row id.
|