From 5fd1ac1277c04deefd9a44e70419c1f8cadb7af1 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sat, 25 Mar 2023 17:03:47 +0200 Subject: [PATCH] (Updated) groups source creation. --- CHANGELOG.org | 2 ++ tui_rsync/models/models.py | 25 +++++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.org b/CHANGELOG.org index a24cf71..2c84f49 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -43,4 +43,6 @@ Added *update label* option for source. ** 0.8.8 <2023-03-25 Sat> Added better label prompt for choosing labels. +** 0.8.9 <2023-03-25 Sat> + Updated groups source creation. diff --git a/tui_rsync/models/models.py b/tui_rsync/models/models.py index da7c16c..3114258 100644 --- a/tui_rsync/models/models.py +++ b/tui_rsync/models/models.py @@ -135,10 +135,8 @@ class Group(BaseModel): label=label, sources=[], ) - for source_label in source_labels: - # continue: - src = Source.get(label=source_label) - group_src, _ = GroupSource.get_or_create(group=group, source=src) + + GroupSource.create_group_sources(group, source_labels) group.save() return group @@ -158,6 +156,21 @@ class GroupSource(BaseModel): group = ForeignKeyField(Group, backref='sources') source = ForeignKeyField(Source) + @staticmethod + def create_group_source(group:Group, source_label:str) -> bool: + """ + Return group-source created status + """ + src = Source.get(label=source_label) + group_src, created = GroupSource.get_or_create(group=group, source=src) + group_src.save() + return created + + @staticmethod + def create_group_sources(group:Group, source_labels:list[str]): + for source_label in source_labels: + GroupSource.create_group_source(group, source_label) + def create_tables(): with db: tables = [ @@ -179,6 +192,10 @@ def all_labels(): return Source.select(Source.label) def all_labels_except(labels): + if labels == None: + return all_labels() + if len(labels) == 0: + return all_labels() with db: return Source.select(Source.label).where(Source.label.not_in(labels))