added a slug field to reference template asset using this field
removed print statement removed unnecessary status code assertion
This commit is contained in:
@@ -5,6 +5,7 @@ from django.utils.translation import get_language_bidi
|
||||
from mako.exceptions import TemplateLookupException
|
||||
|
||||
from microsite_configuration import microsite
|
||||
from certificates.api import get_asset_url_by_slug
|
||||
%>
|
||||
|
||||
<%def name='url(file, raw=False)'><%
|
||||
@@ -14,6 +15,13 @@ except:
|
||||
url = file
|
||||
%>${url}${"?raw" if raw else ""}</%def>
|
||||
|
||||
<%def name='certificate_asset_url(slug)'><%
|
||||
try:
|
||||
url = get_asset_url_by_slug(slug)
|
||||
except:
|
||||
url = ''
|
||||
%>${url}</%def>
|
||||
|
||||
<%def name='css(group, raw=False)'>
|
||||
<%
|
||||
rtl_group = '{}-rtl'.format(group)
|
||||
|
||||
@@ -45,7 +45,8 @@ class CertificateTemplateAssetAdmin(admin.ModelAdmin):
|
||||
"""
|
||||
Django admin customizations for CertificateTemplateAsset model
|
||||
"""
|
||||
list_display = ('description', '__unicode__')
|
||||
list_display = ('description', 'asset_slug',)
|
||||
prepopulated_fields = {"asset_slug": ("description",)}
|
||||
|
||||
|
||||
class GeneratedCertificateAdmin(admin.ModelAdmin):
|
||||
|
||||
@@ -26,6 +26,7 @@ from certificates.models import (
|
||||
ExampleCertificateSet,
|
||||
GeneratedCertificate,
|
||||
CertificateTemplate,
|
||||
CertificateTemplateAsset,
|
||||
)
|
||||
from certificates.queue import XQueueCertInterface
|
||||
|
||||
@@ -477,3 +478,16 @@ def emit_certificate_event(event_name, user, course_id, course=None, event_data=
|
||||
|
||||
with tracker.get_tracker().context(event_name, context):
|
||||
tracker.emit(event_name, event_data)
|
||||
|
||||
|
||||
def get_asset_url_by_slug(asset_slug):
|
||||
"""
|
||||
Returns certificate template asset url for given asset_slug.
|
||||
"""
|
||||
asset_url = ''
|
||||
try:
|
||||
template_asset = CertificateTemplateAsset.objects.get(asset_slug=asset_slug)
|
||||
asset_url = template_asset.asset.url
|
||||
except CertificateTemplateAsset.DoesNotExist:
|
||||
pass
|
||||
return asset_url
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('certificates', '0005_auto_20151208_0801'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='certificatetemplateasset',
|
||||
name='asset_slug',
|
||||
field=models.SlugField(help_text="Asset's unique slug. We can reference the asset in templates using this value.", max_length=255, unique=True, null=True),
|
||||
),
|
||||
]
|
||||
@@ -945,6 +945,12 @@ class CertificateTemplateAsset(TimeStampedModel):
|
||||
upload_to=template_assets_path,
|
||||
help_text=_(u'Asset file. It could be an image or css file.'),
|
||||
)
|
||||
asset_slug = models.SlugField(
|
||||
max_length=255,
|
||||
unique=True,
|
||||
null=True,
|
||||
help_text=_(u'Asset\'s unique slug. We can reference the asset in templates using this value.'),
|
||||
)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
"""save the certificate template asset """
|
||||
|
||||
@@ -26,7 +26,8 @@ from certificates.models import (
|
||||
CertificateStatuses,
|
||||
CertificateSocialNetworks,
|
||||
CertificateTemplate,
|
||||
CertificateHtmlViewConfiguration
|
||||
CertificateHtmlViewConfiguration,
|
||||
CertificateTemplateAsset,
|
||||
)
|
||||
|
||||
from certificates.tests.factories import (
|
||||
@@ -138,6 +139,7 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase):
|
||||
Creates a custom certificate template entry in DB.
|
||||
"""
|
||||
template_html = """
|
||||
<%namespace name='static' file='static_content.html'/>
|
||||
<html>
|
||||
<body>
|
||||
lang: ${LANGUAGE_CODE}
|
||||
@@ -145,6 +147,7 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase):
|
||||
mode: ${course_mode}
|
||||
${accomplishment_copy_course_description}
|
||||
${twitter_url}
|
||||
<img class="custom-logo" src="${static.certificate_asset_url('custom-logo')}" />
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
@@ -824,3 +827,38 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase):
|
||||
else:
|
||||
self.assertContains(response, "Tweet this Accomplishment")
|
||||
self.assertContains(response, 'https://twitter.com/intent/tweet')
|
||||
|
||||
@override_settings(FEATURES=FEATURES_WITH_CUSTOM_CERTS_ENABLED)
|
||||
def test_certificate_asset_by_slug(self):
|
||||
"""
|
||||
Tests certificate template asset display by slug using static.certificate_asset_url method.
|
||||
"""
|
||||
self._add_course_certificates(count=1, signatory_count=2)
|
||||
self._create_custom_template(mode='honor')
|
||||
test_url = get_certificate_url(
|
||||
user_id=self.user.id,
|
||||
course_id=unicode(self.course.id)
|
||||
)
|
||||
|
||||
# render certificate without template asset
|
||||
with patch('certificates.api.get_course_organizations') as mock_get_orgs:
|
||||
mock_get_orgs.return_value = []
|
||||
response = self.client.get(test_url)
|
||||
self.assertContains(response, '<img class="custom-logo" src="" />')
|
||||
|
||||
template_asset = CertificateTemplateAsset(
|
||||
description='custom logo',
|
||||
asset='certificate_template_assets/32/test_logo.png',
|
||||
asset_slug='custom-logo',
|
||||
)
|
||||
template_asset.save()
|
||||
|
||||
# render certificate with template asset
|
||||
with patch('certificates.api.get_course_organizations') as mock_get_orgs:
|
||||
mock_get_orgs.return_value = []
|
||||
response = self.client.get(test_url)
|
||||
self.assertContains(
|
||||
response, '<img class="custom-logo" src="{}certificate_template_assets/32/test_logo.png" />'.format(
|
||||
settings.MEDIA_URL
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user