added templatetag 'notes.inline_table'
This commit is contained in:
@@ -20,7 +20,15 @@ class Observation(Note):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "%s" % self.sujet
|
return "%s" % self.sujet
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def get_date(self):
|
||||||
if not self.created_date:
|
""" Enforce value of created_date """
|
||||||
self.created_date = self.rencontre.date
|
return self.rencontre.date
|
||||||
return super().save(*args, **kwargs)
|
|
||||||
|
def get_labels(self):
|
||||||
|
return [self.rencontre.lieu, self.rencontre.heure_debut]
|
||||||
|
|
||||||
|
def get_bg_colors(self):
|
||||||
|
return ("success", "info")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,15 @@ from django.utils.html import format_html
|
|||||||
|
|
||||||
class Note(models.Model):
|
class Note(models.Model):
|
||||||
""" Note relative à un sujet.
|
""" Note relative à un sujet.
|
||||||
|
|
||||||
|
Peut être utilisée comme classe parente.
|
||||||
|
Il faut alors définir les méthodes :
|
||||||
|
- get_header, get_small
|
||||||
|
- get_date
|
||||||
|
- get_labels
|
||||||
|
- get_bg_color
|
||||||
|
Les valeurs retournée sont utilisée par les templatetages 'notes'
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
sujet = models.ForeignKey(
|
sujet = models.ForeignKey(
|
||||||
@@ -18,33 +27,15 @@ class Note(models.Model):
|
|||||||
)
|
)
|
||||||
created_date = models.DateField('Crée le', blank=True, null=True)
|
created_date = models.DateField('Crée le', blank=True, null=True)
|
||||||
|
|
||||||
def as_table(self):
|
|
||||||
html = format_html(
|
|
||||||
"<tr><th>{} <span class='label label-info'>{}</span>\
|
|
||||||
<span class='label label-info'>{}</span></th>\
|
|
||||||
\n<tr><td>{}</td></tr>",
|
|
||||||
self.sujet,
|
|
||||||
self.created_date,
|
|
||||||
self.created_by,
|
|
||||||
self.text
|
|
||||||
)
|
|
||||||
return html
|
|
||||||
|
|
||||||
def as_inline_table(self):
|
def save(self, *args, **kwargs):
|
||||||
html = format_html(
|
if not self.created_date: # Retrieve from child class instance
|
||||||
"<tr><th class='bg-success'><strong>{}</strong> <small>{}</small> \n\
|
self.created_date = self.cast().get_date()
|
||||||
<span class='label label-info pull-right'>{}</span></th>\n\
|
return super().save(*args, **kwargs)
|
||||||
<tr><td>{}</td></tr>",
|
|
||||||
self.subclass.__qualname__,
|
|
||||||
self.created_date,
|
|
||||||
self.created_by,
|
|
||||||
self.text
|
|
||||||
)
|
|
||||||
return html
|
|
||||||
|
|
||||||
def _get_child_and_subclass(self):
|
def _get_child_and_subclass(self):
|
||||||
self._child_instance = None
|
self._child_instance = self
|
||||||
self._subclass = None
|
self._subclass = self.__class__
|
||||||
for f in self._meta.get_fields():
|
for f in self._meta.get_fields():
|
||||||
if f.is_relation and f.one_to_one:
|
if f.is_relation and f.one_to_one:
|
||||||
self._child_instance = getattr(self, f.name)
|
self._child_instance = getattr(self, f.name)
|
||||||
@@ -64,3 +55,26 @@ class Note(models.Model):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "%s of %s" % (self.subclass.__qualname__, self.created_by)
|
return "%s of %s" % (self.subclass.__qualname__, self.created_by)
|
||||||
|
|
||||||
|
## Attributes used by 'notes' template tags
|
||||||
|
# bg_color : background color of header
|
||||||
|
# labels : list of strings to put in labels
|
||||||
|
def cached_attr(name):
|
||||||
|
""" Cached property set on return value of 'get_ATTR' method on
|
||||||
|
child instance.
|
||||||
|
"""
|
||||||
|
private_name = '_%s' % name
|
||||||
|
def getter(self):
|
||||||
|
if not hasattr(self, private_name):
|
||||||
|
setattr(self,
|
||||||
|
private_name,
|
||||||
|
# Call *child instance* method
|
||||||
|
getattr(self.cast(), 'get_%s' % name)()
|
||||||
|
)
|
||||||
|
return getattr(self, private_name)
|
||||||
|
return getter
|
||||||
|
|
||||||
|
bg_colors = property( cached_attr('bg_colors'),
|
||||||
|
doc="background color of header")
|
||||||
|
labels = property( cached_attr('labels'),
|
||||||
|
doc="list of string to display as labels")
|
||||||
|
|||||||
17
notes/templatetags/notes.py
Normal file
17
notes/templatetags/notes.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
from notes.models import Note
|
||||||
|
|
||||||
|
from django import template
|
||||||
|
|
||||||
|
register = template.Library()
|
||||||
|
|
||||||
|
@register.inclusion_tag("notes/table_inline.html")
|
||||||
|
def inline_table(note):
|
||||||
|
bg_color, bg_label_color = note.bg_colors
|
||||||
|
return {
|
||||||
|
'header': note.created_date,
|
||||||
|
'small': note.subclass.__qualname__,
|
||||||
|
'bg_color': bg_color or "default",
|
||||||
|
'bg_label_color': bg_label_color or "info",
|
||||||
|
'labels': note.labels,
|
||||||
|
'text': note.text,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user