Browse Source

Add some color

theenglishway (time) 6 năm trước cách đây
mục cha
commit
150bccd7e2

+ 3 - 5
planner/models/comments.py

@@ -1,16 +1,14 @@
 from dataclasses import dataclass, field
 from datetime import datetime
 
-from .mixins import SchemaMixin, DbMixin
+from .mixins import TemplateMixin, SchemaMixin, DbMixin
 from .items import Item
 
 
 @dataclass(repr=False)
-class Comment(SchemaMixin, DbMixin):
+class Comment(TemplateMixin, SchemaMixin, DbMixin):
     cache_keys = ('item_id', 'date_added')
-    template_term = """
-    {{ obj.uuid }} {{ obj.text }}
-    """
+    template_term = """{{ obj.text }} {{ Fore.MAGENTA }}{{ obj.short_uuid }}{{ Style.RESET_ALL }}"""
 
     document: dict
     text: str

+ 4 - 6
planner/models/items.py

@@ -3,21 +3,19 @@ from datetime import datetime
 from typing import List
 from tinydb import Query
 
-from .mixins import SchemaMixin, DbMixin
+from .mixins import TemplateMixin, SchemaMixin, DbMixin
 from .milestones import Milestone
 from .sprints import Sprint
 from planner import db
 
 
 @dataclass(repr=False)
-class Item(SchemaMixin, DbMixin):
+class Item(TemplateMixin, SchemaMixin, DbMixin):
     cache_keys = ('name', 'milestone_id', 'sprint_id')
-    template_term = """
-    {{ obj.uuid }} {{ obj.name }}
+    template_term = """{{ obj.name }} {{ Fore.MAGENTA }}{{ obj.short_uuid }}{{ Style.RESET_ALL }}
     {% for comment in obj.comments %}
         {{- comment.as_term }}
-    {% endfor %}
-    """
+    {%- endfor %}"""
 
     document: dict
     name: str

+ 3 - 4
planner/models/milestones.py

@@ -6,14 +6,13 @@ from dataclasses import dataclass, field
 from datetime import datetime
 
 from planner import db
-from .mixins import SchemaMixin, DbMixin
+from .mixins import TemplateMixin, SchemaMixin, DbMixin
 
 
 @dataclass(repr=False)
-class Milestone(SchemaMixin, DbMixin):
+class Milestone(TemplateMixin, SchemaMixin, DbMixin):
     cache_keys = ('name',)
-    template_term = """
-    {{- obj.uuid }} {{ obj.name }}
+    template_term = """Milestone '{{- obj.name }}' {{ Fore.MAGENTA }}{{ obj.short_uuid }}{{ Style.RESET_ALL }}
     {% for item in obj.items %}
         {{- item.as_term }}
     {% endfor %}"""

+ 15 - 4
planner/models/mixins.py

@@ -1,16 +1,27 @@
 import yaml
-from jinja2 import Template
+from jinja2 import Environment
 import uuid
+from colorama import Fore, Back, Style
 from planner import db, validator
 
 
-class SchemaMixin:
-    template_term = """{{ obj.uuid }} {{ obj }}"""
+class TemplateMixin:
+    template_term = """{{ obj.short_uuid }} {{ obj }}"""
+    env = Environment()
 
     @property
     def as_term(self):
-        return Template(self.template_term).render(obj=self)
+        return self.env.from_string(
+            self.template_term,
+            globals={'Fore': Fore, 'Back': Back, 'Style': Style}
+        ).render(obj=self)
+
+    @property
+    def short_uuid(self):
+        return self.uuid[:8]
 
+
+class SchemaMixin:
     @classmethod
     def class_name(cls):
         return cls.__qualname__.lower()

+ 6 - 2
planner/models/sprints.py

@@ -3,14 +3,18 @@ from datetime import datetime
 from typing import List
 from tinydb import Query
 
-from .mixins import SchemaMixin, DbMixin
+from .mixins import TemplateMixin, SchemaMixin, DbMixin
 from .milestones import Milestone
 from planner import db
 
 
 @dataclass(repr=False)
-class Sprint(SchemaMixin, DbMixin):
+class Sprint(TemplateMixin, SchemaMixin, DbMixin):
     cache_keys = ('name',)
+    template_term = """Sprint '{{- obj.name }}' {{ Fore.MAGENTA }}{{ obj.short_uuid }}{{ Style.RESET_ALL }}
+    {% for item in obj.items %}
+        {{- item.as_term }}
+    {% endfor %}"""
 
     document: dict
     name: str

+ 1 - 0
requirements.txt

@@ -1,4 +1,5 @@
 Cerberus
 Click
+colorama
 PyYaml
 TinyDb