-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3074 from digitalocean/969-custom-links
969 custom links
- Loading branch information
Showing
25 changed files
with
301 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Generated by Django 2.2 on 2019-04-15 19:28 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('contenttypes', '0002_remove_content_type_name'), | ||
('extras', '0021_add_color_comments_changelog_to_tag'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='CustomLink', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False)), | ||
('name', models.CharField(max_length=100, unique=True)), | ||
('text', models.CharField(max_length=200)), | ||
('url', models.CharField(max_length=200)), | ||
('weight', models.PositiveSmallIntegerField(default=100)), | ||
('group_name', models.CharField(blank=True, max_length=50)), | ||
('button_class', models.CharField(default='default', max_length=30)), | ||
('new_window', models.BooleanField()), | ||
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')), | ||
], | ||
options={ | ||
'ordering': ['group_name', 'weight', 'name'], | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from collections import OrderedDict | ||
|
||
from django import template | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.utils.safestring import mark_safe | ||
from jinja2 import Environment | ||
|
||
from extras.models import CustomLink | ||
|
||
|
||
register = template.Library() | ||
|
||
LINK_BUTTON = '<a href="{}"{} class="btn btn-sm btn-{}">{}</a>\n' | ||
GROUP_BUTTON = '<div class="btn-group">\n' \ | ||
'<button type="button" class="btn btn-sm btn-{} dropdown-toggle" data-toggle="dropdown">\n' \ | ||
'{} <span class="caret"></span>\n' \ | ||
'</button>\n' \ | ||
'<ul class="dropdown-menu pull-right">\n' | ||
GROUP_LINK = '<li><a href="{}"{}>{}</a></li>\n' | ||
|
||
|
||
@register.simple_tag() | ||
def custom_links(obj): | ||
""" | ||
Render all applicable links for the given object. | ||
""" | ||
content_type = ContentType.objects.get_for_model(obj) | ||
custom_links = CustomLink.objects.filter(content_type=content_type) | ||
if not custom_links: | ||
return '' | ||
|
||
context = { | ||
'obj': obj, | ||
} | ||
template_code = '' | ||
group_names = OrderedDict() | ||
|
||
# Organize custom links by group | ||
for cl in custom_links: | ||
if cl.group_name and cl.group_name in group_names: | ||
group_names[cl.group_name].append(cl) | ||
elif cl.group_name: | ||
group_names[cl.group_name] = [cl] | ||
|
||
# Add non-grouped links | ||
for cl in custom_links: | ||
if not cl.group_name: | ||
link_target = ' target="_blank"' if cl.new_window else '' | ||
template_code += LINK_BUTTON.format( | ||
cl.url, link_target, cl.button_class, cl.text | ||
) | ||
|
||
# Add grouped links to template | ||
for group, links in group_names.items(): | ||
template_code += GROUP_BUTTON.format( | ||
links[0].button_class, group | ||
) | ||
for cl in links: | ||
link_target = ' target="_blank"' if cl.new_window else '' | ||
template_code += GROUP_LINK.format( | ||
cl.url, link_target, cl.text | ||
) | ||
template_code += '</ul>\n</div>\n' | ||
|
||
# Render template | ||
rendered = Environment().from_string(source=template_code).render(**context) | ||
|
||
return mark_safe(rendered) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.