diff options
Diffstat (limited to '')
-rw-r--r-- | archivist/cli.py | 76 | ||||
-rw-r--r-- | archivist/model.py | 9 |
2 files changed, 82 insertions, 3 deletions
diff --git a/archivist/cli.py b/archivist/cli.py index f73a7a4..c72f947 100644 --- a/archivist/cli.py +++ b/archivist/cli.py @@ -33,12 +33,24 @@ def drop(): drop_tables() @cli.group() -def list(): +def tag(): + """Handling of tags""" pass -@list.command("prefixes") +@tag.command('list') +def list_tags(): + from .model import Tag + + print("Tags") + print("====") + print() + + for t in Tag.select(): + print(' *', t) + +@tag.command('prefixes') def list_prefixes(): - from .model import Prefix, db + from .model import Prefix print("Prefixes") print("========") @@ -46,3 +58,61 @@ def list_prefixes(): for p in Prefix.select(): print(" * %s (builtin: %s; pseudo: %s)" % (p.name, p.builtin, p.pseudo)) + +class PrefixTag: + def __init__(self, tag, prefix = None): + self.tag = tag + self.prefix = prefix + + def __str__(self): + if self.prefix: + return '%s:%s' % (self.prefix, self.tag) + else: + return self.tag + +class PrefixTagType(click.ParamType): + name = 'prefixed tag' + + def convert(self, value, param, ctx): + try: + prefix, tag = value.split(':') + if not tag: + return PrefixTag(prefix) + + return PrefixTag(tag, prefix) + except ValueError: + self.fail("%s is an invalid tag. Correct form '[prefix:]tag'." % value, param, ctx) + +TAG = PrefixTagType() + +@tag.command('add') +@click.argument('name', type = TAG) +@click.argument('description', required = False) +def add_tag(name, description): + from .model import Tag, Prefix, db + + with db.atomic(): + if name.prefix: + prefix, created = Prefix.get_or_create(name = name.prefix) + + if not created and prefix.pseudo: + raise click.UsageError("Prefix '%s' is not allowed to carry additional tags." % name.prefix) + + tag, created = Tag.create_or_get(name = name.tag, prefix = name.prefix, description = description) + if not created: + print("Tag already existed:", tag) + +@tag.command('edit') +@click.option('--description') +@click.argument('name', type = TAG) +def edit_tag(description, name): + from .model import Tag, Prefix, db + + try: + tag = Tag.get(name = name.tag, prefix = name.prefix) + except Tag.DoesNotExist: + raise click.UsageError("Tag '%s' does not exist." % name) + + if description: + tag.description = description + tag.save() diff --git a/archivist/model.py b/archivist/model.py index b8a1604..656926c 100644 --- a/archivist/model.py +++ b/archivist/model.py @@ -48,6 +48,9 @@ class Prefix(BaseModel): pseudo = BooleanField(default = False) description = CharField(null=True) + def __str__ (self): + return self.name + @table class Tag(BaseModel): name = CharField() @@ -63,6 +66,12 @@ class Tag(BaseModel): (('name', 'prefix'), True) ] + def __str__(self): + prefix = self.prefix.name + ':' if self.prefix else '' + description = ' -- ' + self.description if self.description else '' + + return prefix + self.name + description + @table class DocumentTag(BaseModel): document = ForeignKeyField(Document, related_name = 'tags') |