Skip to content

Commit

Permalink
Render content blocks from database (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
cutwater authored Nov 27, 2017
1 parent 57f290a commit 0c96633
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 41 deletions.
15 changes: 10 additions & 5 deletions galaxy/main/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# along with Galaxy. If not, see <http://www.apache.org/licenses/>.

from django.contrib import admin
from galaxy.main.models import Platform, CloudPlatform, Role, RoleVersion
from galaxy.main import models


class PlatformAdmin(admin.ModelAdmin):
Expand All @@ -35,7 +35,12 @@ class RoleVersionAdmin(admin.ModelAdmin):
pass


admin.site.register(Platform, PlatformAdmin)
admin.site.register(CloudPlatform, CloudPlatformAdmin)
admin.site.register(Role, RoleAdmin)
admin.site.register(RoleVersion, RoleVersionAdmin)
class ContentBlockAdmin(admin.ModelAdmin):
pass


admin.site.register(models.Platform, PlatformAdmin)
admin.site.register(models.CloudPlatform, CloudPlatformAdmin)
admin.site.register(models.Role, RoleAdmin)
admin.site.register(models.RoleVersion, RoleVersionAdmin)
admin.site.register(models.ContentBlock, ContentBlockAdmin)
102 changes: 102 additions & 0 deletions galaxy/main/migrations/0055_contentblock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import galaxy.main.mixins

MAIN_TITLE_BLOCK = """
<h1><span class="ansible-color">Galaxy</span> is your hub for finding,
reusing and sharing Ansible content</h1>
"""

MAIN_SHARE_BLOCK = """
<div class="share-icon"><i class="fa fa-share-alt fa-2x"></i></div>
<div class="info-label">Share</div>
<div class="content-txt">
<p>Help other Ansible users by sharing the awesome roles you create.</p>
<p>Maybe you have a role for installing and configuring a popular software
package, or a role for deploying software built by your company. Whatever
it is, use Galaxy to share it with the community.</p>
<p>Top content authors will be featured on the
<a href="/explore#">Explore page</a>, achieving worldwide fame!
Or at least fame on the internet among other developers and sysadmins.</p>
</div>
"""

MAIN_DOWNLOADS_BLOCK = """
<div class="download-icon"><i class="fa fa-cloud-download fa-2x"></i></div>
<div class="info-label">Download</div>
<div class="content-txt">
<p>Jump-start your automation project with great content from the Ansible
community. Galaxy provides pre-packaged units of work known to Ansible
as <a href="http://docs.ansible.com/playbooks_roles.html#roles"
target="_blank">roles</a>.</p>
<p>Roles can be dropped into Ansible PlayBooks and immediately put to work.
You'll find roles for provisioning infrastructure, deploying
applications, and all of the tasks you do everyday.</p>
<p>Use <a href="/list#/roles">Search</a> to find roles for your project,
then download them onto your Ansible host using the
<a href="http://docs.ansible.com/ansible/galaxy.html#the-ansible-galaxy-command-line-tool"
target="_blank">ansible-galaxy</a> command that comes bundled
with Ansible.</p>
<p>For example:</p>
<pre>
$ ansible-galaxy install username.rolename
</pre>
</div>
"""

MAIN_FEATURED_BLOG_BLOCK = """
<span class="upcase title">BLOG:</span>
<a href="https://ansible.com/blog" target="_blank" class="blog-link">
Read the latest from The Inside Playbook, and keep up with what's happening
in the Ansible universe.</a>
"""


def upgrade_contentblocks_data(apps, schema_editor):
ContentBlock = apps.get_model("main", "ContentBlock")
db_alias = schema_editor.connection.alias
ContentBlock.objects.using(db_alias).bulk_create([
ContentBlock(name='main-title', content=MAIN_TITLE_BLOCK),
ContentBlock(name='main-share', content=MAIN_SHARE_BLOCK),
ContentBlock(name='main-downloads', content=MAIN_DOWNLOADS_BLOCK),
ContentBlock(name='main-featured-blog',
content=MAIN_FEATURED_BLOG_BLOCK),
])


def downgrade_contentblocks_data(apps, schema_editor):
pass


class Migration(migrations.Migration):

dependencies = [
('main', '0054_role_type_demo'),
]

operations = [
migrations.CreateModel(
name='ContentBlock',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False,
auto_created=True, primary_key=True)),
('created', models.DateTimeField(auto_now_add=True)),
('modified', models.DateTimeField(auto_now=True)),
('name', models.SlugField(unique=True)),
('content', models.TextField(verbose_name=b'content',
blank=True)),
],
options={
'abstract': False,
},
bases=(models.Model, galaxy.main.mixins.DirtyMixin),
),
migrations.RunPython(upgrade_contentblocks_data,
downgrade_contentblocks_data)
]
10 changes: 9 additions & 1 deletion galaxy/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
'PrimordialModel', 'Platform', 'CloudPlatform', 'Category', 'Tag',
'Role', 'ImportTask', 'ImportTaskMessage', 'RoleRating', 'RoleVersion',
'UserAlias', 'NotificationSecret', 'Notification', 'Repository',
'Subscription', 'Stargazer', 'Namespace'
'Subscription', 'Stargazer', 'Namespace', 'ContentBlock'
]

###################################################################################
Expand Down Expand Up @@ -974,3 +974,11 @@ class RefreshRoleCount (PrimordialModel):
default=0,
null=True
)


class ContentBlock(BaseModel):
name = models.SlugField(unique=True)
content = models.TextField('content', blank=True)

def __unicode__(self):
return self.name
48 changes: 48 additions & 0 deletions galaxy/main/templatetags/contentblock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# (c) 2012-2017, Ansible by Red Hat
#
# This file is part of Ansible Galaxy
#
# Ansible Galaxy is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by
# the Apache Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Ansible Galaxy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License for more details.
#
# You should have received a copy of the Apache License
# along with Galaxy. If not, see <http://www.apache.org/licenses/>.

from django import template

from galaxy.main import models

register = template.Library()


# TODO(cutwater): Pass context variable as parameter
class ContentBlockNode(template.Node):
def __init__(self, block_name):
self.blockname = block_name

def render(self, context):
# FIXME(cutwater): THIS IS UNSAFE
# Injects content from database as is. Additional sanitizing required.
# Consider using `bleach` python library for that purpose.
try:
return context['contentblocks'][self.blockname].content
except KeyError:
block = models.ContentBlock.objects.get(name=self.blockname)
return block.content


@register.tag('contentblock')
def contentblock_tag(parser, token):
bits = token.split_contents()
if len(bits) != 2:
raise template.TemplateSyntaxError(
"'{0}' takes one argument (name)".format(bits[0]))
block_name = bits[1]
return ContentBlockNode(block_name)
12 changes: 6 additions & 6 deletions galaxy/main/templatetags/galaxyhelpers.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# (c) 2012-2014, Ansible, Inc. <support@ansible.com>
# (c) 2012-2017, Ansible by Red Hat
#
# This file is part of Ansible Galaxy
#
# Ansible Galaxy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# it under the terms of the Apache License as published by
# the Apache Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Ansible Galaxy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# Apache License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# You should have received a copy of the Apache License
# along with Galaxy. If not, see <http://www.apache.org/licenses/>.

import markdown as md

Expand Down
9 changes: 8 additions & 1 deletion galaxy/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from django.views.generic.detail import DetailView

# local stuff
from models import Role, Namespace
from galaxy.main.models import Role, Namespace, ContentBlock

# rst2html5-tools
from html5css3 import Writer
Expand Down Expand Up @@ -145,6 +145,13 @@ def build_standard_context(request):

def home(request):
context = build_standard_context(request)
contentblocks = ContentBlock.objects.filter(name__in=[
'main-title',
'main-share',
'main-downloads',
'main-featured-blog'
]).all()
context['contentblocks'] = {item.name: item for item in contentblocks}
return render_to_response('home.html', context)


Expand Down
32 changes: 5 additions & 27 deletions galaxy/templates/home_content.html
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
{% load contentblock %}

<div id="galaxy-info">
<div class="container">
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-4">
<div class="download-icon"><i class="fa fa-cloud-download fa-2x"></i></div>
<div class="info-label">Download</div>
<div class="content-txt">
<p>Jump-start your automation project with great content from the Ansible community. Galaxy provides pre-packaged units of work known to
Ansible as <a href="http://docs.ansible.com/playbooks_roles.html#roles" target="_blank">roles</a>.</p>
<p>Roles can be dropped into Ansible PlayBooks and immediately put to work. You'll find roles for provisioning infrastructure, deploying
applications, and all of the tasks you do everyday.</p>
<p>Use <a href="/list#/roles">Search</a> to find roles for your project, then download them onto your Ansible host using the
<a href="http://docs.ansible.com/ansible/galaxy.html#the-ansible-galaxy-command-line-tool" target="_blank">ansible-galaxy</a> command that
comes bundled with Ansible.</p>
<p>For example:</p>
<pre>
$ ansible-galaxy install username.rolename
</pre>
</div>
{% contentblock main-downloads %}
</div>
<div class="col-lg-4 col-md-4 col-sm-4">
<div class="share-icon"><i class="fa fa-share-alt fa-2x"></i></div>
<div class="info-label">Share</div>
<div class="content-txt">
<p>Help other Ansible users by sharing the awesome roles you create.</p>
<p>Maybe you have a role for installing and configuring a popular software package, or a role for deploying software built by your company. Whatever
it is, use Galaxy to share it with the community.</p>
<p>Top content authors will be featured on the <a href="/explore#">Explore page</a>, achieving worldwide fame! Or at least fame on
the internet among other developers and sysadmins.</p>
</div>
{% contentblock main-share %}
</div>
<div class="col-lg-4 col-md-4 col-sm-4">
<div id="featured-section">
Expand Down Expand Up @@ -58,9 +38,7 @@
</div>
<div id="featured-blog" class="featured">
<div id="featured-blog-body" class="featured-body">
<span class="upcase title">BLOG:</span>
<a href="https://ansible.com/blog" target="_blank" class="blog-link">Read the latest from The Inside Playbook, and keep up with what's
happening in the Ansible universe.</a>
{% contentblock main-featured-blog %}
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion galaxy/templates/home_title.html
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<h1><span class="ansible-color">Galaxy</span> is your hub for finding, reusing and sharing Ansible content</h1>
{% load contentblock %}
{% contentblock main-title %}

0 comments on commit 0c96633

Please sign in to comment.