Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Migrate ASRSnippets to new template system + icon gallery
###
# To be run within a django shell plus
###
import os
import re
import hashlib
import base64
import json
import io
from PIL import Image
from django.core.files import File
import tempfile
ImageCache = {}
def get_image_from_base64(data, id, field):
if not data:
return
type, encoded = data.split(',', 1)
decoded = base64.b64decode(encoded)
if 'svg' in type:
# Extract PNGs wrapped in SVGs
if b'data:img/png' in decoded:
decoded = base64.b64decode(
re.search(b'xlink:href="data:img/png;base64,(.+?)"', decoded).groups()[0])
img = Image.open(io.BytesIO(decoded))
# Use Inkscape to convert the images. Python based libraries didn't cut it.
else:
try:
with tempfile.NamedTemporaryFile(suffix='.svg', delete=False) as fp:
fp.write(decoded)
os.system(
'inkscape {} -e /tmp/foo.png --without-gui > /dev/null 2>&1'.format(fp.name))
img = Image.open('/tmp/foo.png')
except:
print ('failed to load svg', id, field)
return None
else:
img = Image.open(io.BytesIO(decoded))
blob = io.BytesIO()
img.save(blob, 'PNG')
return blob
for snippet in ASRSnippet.objects.filter(for_qa=False).reverse():
data = json.loads(snippet.data)
if snippet.template.code_name == 'eoy_snippet':
template = FundraisingTemplate()
elif snippet.template.code_name == 'fxa_signup_snippet':
template = FxASignupTemplate()
elif snippet.template.code_name == 'newsletter_snippet':
template = NewsletterTemplate()
elif snippet.template.code_name == 'send_to_device_snippet':
template = SendToDeviceTemplate()
elif snippet.template.code_name == 'simple_snippet':
template = SimpleTemplate()
for key, value in data.items():
if 'icon' in key:
# this is an icon, extract and save it to fs
icon = Icon(
creator=snippet.creator,
name='{} #{}'.format(key, snippet.id),
width=0,
height=0,
)
imgdata = get_image_from_base64(value, snippet.id, key)
if imgdata:
imgdata.seek(0)
hash = hashlib.md5(imgdata.read()).hexdigest()
if hash in ImageCache:
icon = ImageCache[hash]
else:
icon.image.save('foo.png', File(imgdata), save=False)
icon.save()
ImageCache[hash] = icon
setattr(template, key, icon)
else:
setattr(template, key, value)
template.snippet = snippet
try:
template.save()
except Exception as exp:
print('Cannot parse {}'.format(snippet.id))
continue
def set_default(template, attr, value):
current_value = getattr(template, attr)
if not current_value:
setattr(template, attr, value)
for snippet in ASRSnippet.objects.filter(for_qa=False).reverse():
if not getattr(snippet, 'template_ng', None):
print('Cannot parse {}'.format(snippet.id))
continue
template = snippet.template_ng
set_default(template, 'block_button_text', 'Remove this')
if isinstance(template, FxASignupTemplate):
set_default(template, 'scene1_button_label', 'Learn more')
set_default(template, 'scene2_email_placeholder_text', 'Your email here')
set_default(template, 'scene2_button_label', 'Sign me up')
set_default(template, 'scene2_dismiss_button_text', 'Dismiss')
elif isinstance(template, FundraisingTemplate):
set_default(template, 'monthly_checkbox_label_text', 'Make my donation monthly')
set_default(template, 'locale', 'en-US')
set_default(template, 'currency_code', 'usd')
set_default(template, 'selected_button', 'donation_amount_second')
set_default(template, 'highlight_color', '#FFE900')
elif isinstance(template, NewsletterTemplate):
set_default(template, 'scene1_button_label', 'Learn more')
set_default(template, 'scene2_newsletter', 'mozilla-foundation')
set_default(template, 'locale', 'en-US')
set_default(template, 'scene2_email_placeholder_text', 'Your email here')
set_default(template, 'scene2_button_label', 'Sign Me Up')
set_default(template, 'scene2_dismiss_button_text', 'Dismiss')
elif isinstance(template, SendToDeviceTemplate):
set_default(template, 'scene1_button_label', 'Learn more')
set_default(template, 'scene2_button_label', 'Send')
set_default(template, 'scene2_input_placeholder', 'Your email here')
set_default(template, 'scene2_dismiss_button_text', 'Dismiss')
set_default(template, 'country', 'us')
set_default(template, 'locale', 'en-US')
template.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.