Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions taccsite_cms/contrib/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
TEXT_FOR_NESTED_PLUGIN_CONTENT_SWAP = '\
<dl>\
<dt>To add {element},</dt>\
<dd>nest "{plugin_name}" plugin inside this plugin.</dd>\
<dt>To edit {element},</dt>\
<dd>edit existing nested "{plugin_name}" plugin.</dd>\
</dl>'
31 changes: 31 additions & 0 deletions taccsite_cms/contrib/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Get Django `models.CharField` `choices`
def get_choices(choice_dict):
"""Get a sequence for a Django model field choices from a dictionary.
:param Dict[str, Dict[str, str]] dictionary: choice as key for dictionary of classnames and descriptions
:return: a sequence for django.db.models.CharField.choices
:rtype: List[Tuple[str, str], ...]
"""
choices = []

for key, data in choice_dict.items():
choice = (key, data['description'])
choices.append(choice)

return choices



# GH-93, GH-142, GH-133: Upcoming functions here (ease merge conflict, maybe)



# Concatenate a list of CSS classes
# SEE: https://github.com/django-cms/djangocms-bootstrap4/blob/master/djangocms_bootstrap4/helpers.py
def concat_classnames(classes):
"""Concatenate a list of classname strings (without failing on None)"""
# SEE: https://stackoverflow.com/a/20271297/11817077
return ' '.join(_class for _class in classes if _class)



# GH-93, GH-142, GH-133: Upcoming functions here (ease merge conflict, maybe)
Empty file.
136 changes: 136 additions & 0 deletions taccsite_cms/contrib/taccsite_data_list/cms_plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from django.utils.translation import gettext_lazy as _

from taccsite_cms.contrib.constants import TEXT_FOR_NESTED_PLUGIN_CONTENT_SWAP
from taccsite_cms.contrib.helpers import concat_classnames

from .models import TaccsiteDataList, TaccsiteDataListItem
from .constants import ORIENTATION_DICT, TYPE_STYLE_DICT, DENSITY_DICT



# Helpers

def get_classname(dict, value):
"""Get layout class based on value."""
return dict.get(value, {}).get('classname')



# Plugins

@plugin_pool.register_plugin
class TaccsiteDataListPlugin(CMSPluginBase):
"""
Components > "Data List" Plugin
https://confluence.tacc.utexas.edu/x/EiIFDg
"""
module = 'TACC Site'
model = TaccsiteDataList
name = _('Data List')
render_template = 'data_list.html'

cache = True
text_enabled = False
allow_children = True
child_classes = [
'TaccsiteDataListItemPlugin'
]

fieldsets = [
(_('Required configuration'), {
'fields': (
'type_style',
'orientation',
'density',
)
}),
(_('Optional configuration'), {
'fields': (
'truncate_values',
)
}),
(_('Advanced settings'), {
'classes': ('collapse',),
'fields': (
'attributes',
)
}),
]

# Render

def render(self, context, instance, placeholder):
context = super().render(context, instance, placeholder)
request = context['request']

classes = concat_classnames([
'c-data-list',
get_classname(ORIENTATION_DICT, instance.orientation),
get_classname(TYPE_STYLE_DICT, instance.type_style),
get_classname(DENSITY_DICT, instance.density),
'c-data-list--should-truncate-values'
if instance.truncate_values else '',
instance.attributes.get('class'),
])
instance.attributes['class'] = classes

return context

@plugin_pool.register_plugin
class TaccsiteDataListItemPlugin(CMSPluginBase):
"""
Components > "Data List Item" Plugin
https://confluence.tacc.utexas.edu/x/EiIFDg
"""
module = 'TACC Site'
model = TaccsiteDataListItem
name = _('Data List Item')
render_template = 'data_list_item.html'

cache = True
text_enabled = False
allow_children = True
child_classes = [
'LinkPlugin'
]
max_children = 1 # Only a label until we know what value will need

fieldsets = [
(None, {
'fields': (
('key', 'value'),
),
}),
(_('Link'), {
'classes': ('collapse',),
'description': TEXT_FOR_NESTED_PLUGIN_CONTENT_SWAP.format(
element='a link',
plugin_name='Link'
) + '\
<br />\
The "Link" plugin\'s "Display name" field takes precedence over this plugin\'s "Label" field. <small>If "Link" plugin is not rendered, then check "Advanced settings" of this plugin.</small>',
'fields': (),
}),
(_('Advanced settings'), {
'classes': ('collapse',),
'fields': (
'use_plugin_as_key',
),
})
]

# Render

def render(self, context, instance, placeholder):
context = super().render(context, instance, placeholder)
request = context['request']

parent_plugin_instance = instance.parent.get_plugin_instance()[0]

context.update({
'parent_plugin_instance': parent_plugin_instance
})

return context
38 changes: 38 additions & 0 deletions taccsite_cms/contrib/taccsite_data_list/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# TODO: Consider using an Enum (and an Abstract Enum with `get_choices` and `get_classname` methods)

ORIENTATION_DICT = {
'horizontal': {
'classname': 'c-data-list--is-horz',
'description': 'Horizontal (all data on one row)',
'short_description': 'Horizontal',
},
'vertical': {
'classname': 'c-data-list--is-vert',
'description': 'Vertical (every label and value has its own row)',
'short_description': 'Vertical',
},
}

TYPE_STYLE_DICT = {
'table': {
'description': 'Table (e.g. Columns)',
'short_description': 'Table',
},
'dlist': {
'description': 'List (e.g. Glossary, Metadata)',
'short_description': 'List',
},
}

DENSITY_DICT = {
'default': {
'classname': 'c-data-list--is-wide',
'description': 'Default (ample extra space)',
'short_description': 'Default',
},
'compact': {
'classname': 'c-data-list--is-narrow',
'description': 'Compact (minimal extra space)',
'short_description': 'Compact',
},
}
46 changes: 46 additions & 0 deletions taccsite_cms/contrib/taccsite_data_list/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by Django 2.2.16 on 2021-08-06 20:17

from django.db import migrations, models
import django.db.models.deletion
import djangocms_attributes_field.fields


class Migration(migrations.Migration):

initial = True

dependencies = [
('cms', '0022_auto_20180620_1551'),
]

operations = [
migrations.CreateModel(
name='TaccsiteDataList',
fields=[
('cmsplugin_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='taccsite_data_list_taccsitedatalist', serialize=False, to='cms.CMSPlugin')),
('orientation', models.CharField(choices=[('horizontal', 'Horizontal (all data on one row)'), ('vertical', 'Vertical (every label and value has its own row)')], help_text='The direction in which to lay out the data. Hint: Choose based on the amount of space available in the layout for the data.', max_length=255, verbose_name='Orientation')),
('type_style', models.CharField(choices=[('table', 'Table (e.g. Columns)'), ('dlist', 'List (e.g. Glossary, Metadata)')], help_text='The type of data to display, glossary/metadata or tabular. Notice: Each type of list has a slightly different style.', max_length=255, verbose_name='Type / Style')),
('density', models.CharField(choices=[('default', 'Default (ample extra space)'), ('compact', 'Compact (minimal extra space)')], help_text='The amount of extra space in the layout. Hint: Choose based on the amount of space available in the layout for the data.', max_length=255, verbose_name='Density (Layout Spacing)')),
('truncate_values', models.BooleanField(default=False, help_text='Truncate values if there is not enough space to show the label and the value. Notice: Labels are truncated as necessary.', verbose_name='Truncate the values (as necessary)')),
('attributes', djangocms_attributes_field.fields.AttributesField(default=dict)),
],
options={
'abstract': False,
},
bases=('cms.cmsplugin',),
),
migrations.CreateModel(
name='TaccsiteDataListItem',
fields=[
('cmsplugin_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, related_name='taccsite_data_list_taccsitedatalistitem', serialize=False, to='cms.CMSPlugin')),
('key', models.CharField(help_text='A label for the data value. To create a link, add a child plugin.', max_length=50, verbose_name='Label')),
('value', models.CharField(help_text='The data value.', max_length=100, verbose_name='Value')),
('use_plugin_as_key', models.BooleanField(default=True, help_text='If a child plugin is added, and this option is checked, then the child plugin will be used (not the Label field text).', verbose_name='Support child plugin for Label')),
('attributes', djangocms_attributes_field.fields.AttributesField(default=dict)),
],
options={
'abstract': False,
},
bases=('cms.cmsplugin',),
),
]
Empty file.
107 changes: 107 additions & 0 deletions taccsite_cms/contrib/taccsite_data_list/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from cms.models.pluginmodel import CMSPlugin

from django.db import models
from django.utils.translation import gettext_lazy as _

from djangocms_attributes_field import fields

from taccsite_cms.contrib.helpers import get_choices

from .constants import ORIENTATION_DICT, TYPE_STYLE_DICT, DENSITY_DICT



# Helpers

def get_short_description(dict, value):
"""Get layout class based on value."""
return dict.get(value, {}).get('short_description')



# Constants

ORIENTATION_CHOICES = get_choices(ORIENTATION_DICT)
TYPE_STYLE_CHOICES = get_choices(TYPE_STYLE_DICT)
DENSITY_CHOICES = get_choices(DENSITY_DICT)



# Models

class TaccsiteDataList(CMSPlugin):
"""
Components > "Data List" Model
"""
orientation = models.CharField(
verbose_name=_('Orientation'),
help_text=_('The direction in which to lay out the data. Hint: Choose based on the amount of space available in the layout for the data.'),
choices=ORIENTATION_CHOICES,
blank=False,
max_length=255,
)
type_style = models.CharField(
verbose_name=_('Type / Style'),
help_text=_('The type of data to display, glossary/metadata or tabular. Notice: Each type of list has a slightly different style.'),
choices=TYPE_STYLE_CHOICES,
blank=False,
max_length=255,
)
density = models.CharField(
verbose_name=_('Density (Layout Spacing)'),
help_text=_('The amount of extra space in the layout. Hint: Choose based on the amount of space available in the layout for the data.'),
choices=DENSITY_CHOICES,
blank=False,
max_length=255,
)
truncate_values = models.BooleanField(
verbose_name=_('Truncate the values (as necessary)'),
help_text=_('Truncate values if there is not enough space to show the label and the value. Notice: Labels are truncated as necessary.'),
default=False,
)

attributes = fields.AttributesField()

def get_short_description(self):
orientation = get_short_description(ORIENTATION_DICT, self.orientation)
type_style = get_short_description(TYPE_STYLE_DICT, self.type_style)
density = get_short_description(DENSITY_DICT, self.density)

return density + ', ' + orientation + ' ' + type_style

class TaccsiteDataListItem(CMSPlugin):
"""
Components > "Data List Item" Model
"""
key = models.CharField(
verbose_name=_('Label'),
help_text=_('A label for the data value.'),
blank=True,
max_length=50,
)
value = models.CharField(
verbose_name=_('Value'),
help_text=_('The data value.'),
blank=False,
max_length=100,
)
use_plugin_as_key = models.BooleanField(
verbose_name=_('Support child plugin for Label'),
help_text=_('If a child plugin is added, and this option is checked, then the child plugin will be used (not the Label field text).'),
default=True,
)

attributes = fields.AttributesField()

def get_short_description(self):
key = self.key
val = self.value
max_len = 4

should_truncate_key = len(key) > max_len
key_desc = key[0:max_len] + '…' if should_truncate_key else key

should_truncate_val = len(key) > max_len
val_desc = val[0:max_len] + '…' if should_truncate_val else val

return key_desc + ': ' + val_desc
21 changes: 21 additions & 0 deletions taccsite_cms/contrib/taccsite_data_list/templates/data_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% load cms_tags %}

{% if instance.type_style == 'dlist' %}

<dl {# class="c-data-list" #}{{ instance.attributes_str }}>
{% for plugin_instance in instance.child_plugin_instances %}
{% render_plugin plugin_instance %}
{% endfor %}
</dl>

{% elif instance.type_style == 'table' %}

<table {# class="c-data-list" #}{{ instance.attributes_str }}>
<tbody>
{% for plugin_instance in instance.child_plugin_instances %}
{% render_plugin plugin_instance %}
{% endfor %}
</tbody>
</table>

{% endif %}
Loading