Commit bd3dfee9 by Matt Drayer

Merge pull request #9629 from edx/asadiqbal08/SOL-1159

SOL-1159 Remove hash cert asset file name
parents 9aba4a72 17f10d21
......@@ -49,6 +49,7 @@ from datetime import datetime
import json
import logging
import uuid
import os
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
......@@ -730,6 +731,21 @@ class CertificateTemplate(TimeStampedModel):
unique_together = (('organization_id', 'course_key', 'mode'),)
def template_assets_path(instance, filename):
"""
Delete the file if it already exist and returns the certificate template asset file path.
:param instance: CertificateTemplateAsset object
:param filename: file to upload
:return path: path of asset file e.g. certificate_template_assets/1/filename
"""
name = os.path.join('certificate_template_assets', str(instance.id), filename)
fullname = os.path.join(settings.MEDIA_ROOT, name)
if os.path.exists(fullname):
os.remove(fullname)
return name
class CertificateTemplateAsset(TimeStampedModel):
"""A set of assets to be used in custom web certificate templates.
......@@ -745,10 +761,20 @@ class CertificateTemplateAsset(TimeStampedModel):
)
asset = models.FileField(
max_length=255,
upload_to='certificate_template_assets',
upload_to=template_assets_path,
help_text=_(u'Asset file. It could be an image or css file.'),
)
def save(self, *args, **kwargs):
"""save the certificate template asset """
if self.pk is None:
asset_image = self.asset
self.asset = None
super(CertificateTemplateAsset, self).save(*args, **kwargs)
self.asset = asset_image
super(CertificateTemplateAsset, self).save(*args, **kwargs)
def __unicode__(self):
return u'%s' % (self.asset.url, ) # pylint: disable=no-member
......
......@@ -2,6 +2,7 @@
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.files.images import ImageFile
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
from django.test.utils import override_settings
from nose.plugins.attrib import attr
......@@ -12,6 +13,7 @@ from certificates.models import (
ExampleCertificate,
ExampleCertificateSet,
CertificateHtmlViewConfiguration,
CertificateTemplateAsset,
BadgeImageConfiguration)
FEATURES_INVALID_FILE_PATH = settings.FEATURES.copy()
......@@ -204,3 +206,31 @@ class BadgeImageConfigurationTest(TestCase):
ValidationError,
BadgeImageConfiguration(mode='test2', icon=self.get_image('good'), default=True).full_clean
)
@attr('shard_1')
class CertificateTemplateAssetTest(TestCase):
"""
Test Assets are uploading/saving successfully for CertificateTemplateAsset.
"""
def test_asset_file_saving_with_actual_name(self):
"""
Verify that asset file is saving with actual name, No hash tag should be appended with the asset filename.
"""
CertificateTemplateAsset(description='test description', asset=SimpleUploadedFile(
'picture1.jpg',
'these are the file contents!')).save()
certificate_template_asset = CertificateTemplateAsset.objects.get(id=1)
self.assertEqual(certificate_template_asset.asset, 'certificate_template_assets/1/picture1.jpg')
# Now save asset with same file again, New file will be uploaded after deleting the old one with the same name.
certificate_template_asset.asset = SimpleUploadedFile('picture1.jpg', 'file contents')
certificate_template_asset.save()
self.assertEqual(certificate_template_asset.asset, 'certificate_template_assets/1/picture1.jpg')
# Now replace the asset with another file
certificate_template_asset.asset = SimpleUploadedFile('picture2.jpg', 'file contents')
certificate_template_asset.save()
certificate_template_asset = CertificateTemplateAsset.objects.get(id=1)
self.assertEqual(certificate_template_asset.asset, 'certificate_template_assets/1/picture2.jpg')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment