diff --git a/common/djangoapps/pipeline_mako/templates/static_content.html b/common/djangoapps/pipeline_mako/templates/static_content.html index 88686c4e07..f3c5ee4a51 100644 --- a/common/djangoapps/pipeline_mako/templates/static_content.html +++ b/common/djangoapps/pipeline_mako/templates/static_content.html @@ -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 name='certificate_asset_url(slug)'><% +try: + url = get_asset_url_by_slug(slug) +except: + url = '' +%>${url} + <%def name='css(group, raw=False)'> <% rtl_group = '{}-rtl'.format(group) diff --git a/lms/djangoapps/certificates/admin.py b/lms/djangoapps/certificates/admin.py index 923b412752..bd15df2faf 100644 --- a/lms/djangoapps/certificates/admin.py +++ b/lms/djangoapps/certificates/admin.py @@ -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): diff --git a/lms/djangoapps/certificates/api.py b/lms/djangoapps/certificates/api.py index 8904ab707e..6e27b96dd2 100644 --- a/lms/djangoapps/certificates/api.py +++ b/lms/djangoapps/certificates/api.py @@ -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 diff --git a/lms/djangoapps/certificates/migrations/0006_certificatetemplateasset_asset_slug.py b/lms/djangoapps/certificates/migrations/0006_certificatetemplateasset_asset_slug.py new file mode 100644 index 0000000000..a4b2105c79 --- /dev/null +++ b/lms/djangoapps/certificates/migrations/0006_certificatetemplateasset_asset_slug.py @@ -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), + ), + ] diff --git a/lms/djangoapps/certificates/models.py b/lms/djangoapps/certificates/models.py index e85b72d942..e9b1eabba7 100644 --- a/lms/djangoapps/certificates/models.py +++ b/lms/djangoapps/certificates/models.py @@ -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 """ diff --git a/lms/djangoapps/certificates/tests/test_webview_views.py b/lms/djangoapps/certificates/tests/test_webview_views.py index d25ad0e643..0c8056ab4d 100644 --- a/lms/djangoapps/certificates/tests/test_webview_views.py +++ b/lms/djangoapps/certificates/tests/test_webview_views.py @@ -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'/> lang: ${LANGUAGE_CODE} @@ -145,6 +147,7 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase): mode: ${course_mode} ${accomplishment_copy_course_description} ${twitter_url} + """ @@ -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, '') + + 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, ''.format( + settings.MEDIA_URL + ) + )