theenglishway (time) 6 лет назад
Родитель
Сommit
4405e191d5
4 измененных файлов с 61 добавлено и 1 удалено
  1. 19 0
      planner/cli.py
  2. 3 1
      planner/models/__init__.py
  3. 35 0
      planner/models/comments.py
  4. 4 0
      planner/models/items.py

+ 19 - 0
planner/cli.py

@@ -28,6 +28,12 @@ def item(ctx):
     """Handle items"""
     ctx.obj['model'] = Item
 
+@main.group()
+@click.pass_context
+def comment(ctx):
+    """Handle comments"""
+    ctx.obj['model'] = Comment
+
 def save_or_display_errors(model, data):
     ok, m_doc = model.validate(data)
     if ok :
@@ -108,6 +114,19 @@ def new_item(ctx, name, description, milestone_id=None, sprint_id=None, category
 
     save_or_display_errors(model, data)
 
+@comment.command('new')
+@click.option('--item_id', prompt='ID of item on which to comment', type=int)
+@click.option('--text', prompt='Text', type=str)
+@click.pass_context
+def new_comment(ctx, item_id, text):
+    """Add a new comment"""
+    model = ctx.obj['model']
+    data = {
+        'text': text,
+        'item_id': item_id,
+    }
+    save_or_display_errors(model, data)
+
 @click.command()
 @click.pass_context
 def list(ctx):

+ 3 - 1
planner/models/__init__.py

@@ -1,9 +1,11 @@
 from .milestones import Milestone
 from .sprints import Sprint
 from .items import Item
+from .comments import Comment
 
 __all__ = [
     'Milestone',
     'Sprint',
-    'Item'
+    'Item',
+    'Comment'
 ]

+ 35 - 0
planner/models/comments.py

@@ -0,0 +1,35 @@
+from dataclasses import dataclass, field
+from datetime import datetime
+
+from .mixins import SchemaMixin, DbMixin
+from .items import Item
+
+
+@dataclass(repr=False)
+class Comment(SchemaMixin, DbMixin):
+    document: dict
+    text: str
+    item_id: int = None
+    item: Item = field(init=False)
+    uuid: str = None
+    date_added: datetime = None
+
+    schema_yaml = """
+        text:
+            required: true
+            type: string
+        item_id:
+            is_fk: item
+            required: true
+        date_added:
+            type: datetime
+            default_setter: utcnow
+    """
+    def __post_init__(self):
+        self.item = Item.fetch(self.item_id)
+
+    def __repr__(self):
+        return "{}(name='{}', item='{}')".format(self.__class__.__qualname__, self.item, repr(self.item))
+
+    def __terminal__(self):
+        return f"{self.__class__.__qualname__} : {self.text} ({self.item})"

+ 4 - 0
planner/models/items.py

@@ -61,6 +61,10 @@ class Item(SchemaMixin, DbMixin):
         self.milestone = Milestone.fetch(self.milestone_id) if self.milestone_id else None
         self.sprint = Sprint.fetch(self.sprint_id) if self.sprint_id else None
 
+    @classmethod
+    def class_name(cls):
+        return 'item'
+
     @classmethod
     def get(cls, document):
         category_to_kls = {