2013-03-31 06:09:25 +04:00
|
|
|
import json
|
2013-04-03 02:39:39 +04:00
|
|
|
import posixpath
|
2013-03-31 06:09:25 +04:00
|
|
|
import re
|
|
|
|
from time import time
|
2013-05-20 21:35:27 +04:00
|
|
|
from wsgiref.handlers import format_date_time
|
2013-03-31 06:09:25 +04:00
|
|
|
|
2015-12-17 09:59:16 +03:00
|
|
|
from olympia.constants import base
|
2013-03-31 06:09:25 +04:00
|
|
|
|
2018-07-03 20:20:06 +03:00
|
|
|
from services.utils import (
|
|
|
|
get_cdn_url, log_configure, log_exception, mypool, settings,
|
|
|
|
user_media_url)
|
2013-03-31 06:09:25 +04:00
|
|
|
|
2013-05-20 21:35:27 +04:00
|
|
|
# This has to be imported after the settings (utils).
|
|
|
|
from django_statsd.clients import statsd
|
|
|
|
|
2013-03-31 06:09:25 +04:00
|
|
|
|
2017-10-05 15:48:52 +03:00
|
|
|
# Configure the log.
|
|
|
|
log_configure()
|
|
|
|
|
|
|
|
|
2013-03-31 06:09:25 +04:00
|
|
|
class ThemeUpdate(object):
|
|
|
|
|
|
|
|
def __init__(self, locale, id_, qs=None):
|
2013-04-29 00:33:08 +04:00
|
|
|
self.from_gp = qs == 'src=gp'
|
2018-07-03 20:20:06 +03:00
|
|
|
self.addon_id = id_
|
|
|
|
self.cursor = mypool.connect().cursor()
|
|
|
|
|
|
|
|
def get_headers(self, length):
|
|
|
|
return [('Cache-Control', 'public, max-age=86400'),
|
|
|
|
('Content-Length', str(length)),
|
|
|
|
('Content-Type', 'application/json'),
|
|
|
|
('Expires', format_date_time(time() + 86400)),
|
|
|
|
('Last-Modified', format_date_time(time()))]
|
|
|
|
|
|
|
|
|
|
|
|
class MigratedUpdate(ThemeUpdate):
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
if hasattr(self, 'row'):
|
|
|
|
return self.row
|
|
|
|
|
|
|
|
primary_key = (
|
|
|
|
'getpersonas_id' if self.from_gp else 'lightweight_theme_id')
|
|
|
|
|
|
|
|
"""sql from:
|
|
|
|
MigratedLWT.objects.filter(lightweight_theme_id=xxx).values_list(
|
|
|
|
'static_theme_id',
|
|
|
|
'static_theme___current_version__files__filename',
|
|
|
|
'static_theme___current_version__files__hash').query"""
|
|
|
|
|
|
|
|
sql = """
|
|
|
|
SELECT `migrated_personas`.`static_theme_id`,
|
|
|
|
`files`.`filename`,
|
|
|
|
`files`.`hash`
|
|
|
|
FROM `migrated_personas`
|
|
|
|
INNER JOIN `addons` T3 ON (
|
|
|
|
`migrated_personas`.`static_theme_id` = T3.`id` )
|
|
|
|
LEFT OUTER JOIN `versions` ON (
|
|
|
|
T3.`current_version` = `versions`.`id` )
|
|
|
|
LEFT OUTER JOIN `files` ON (
|
|
|
|
`versions`.`id` = `files`.`version_id` )
|
|
|
|
WHERE `migrated_personas`.{primary_key}=%(id)s
|
|
|
|
""".format(primary_key=primary_key)
|
|
|
|
self.cursor.execute(sql, {'id': self.addon_id})
|
|
|
|
row = self.cursor.fetchone()
|
|
|
|
self.data = (
|
|
|
|
dict(zip(('stheme_id', 'filename', 'hash'), row)) if row else {})
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_migrated(self):
|
|
|
|
return bool(self.get_data())
|
|
|
|
|
|
|
|
def get_json(self):
|
|
|
|
if self.get_data():
|
|
|
|
response = {
|
|
|
|
"converted_theme": {
|
|
|
|
"url": get_cdn_url(self.data['stheme_id'], self.data),
|
|
|
|
"hash": self.data['hash']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return json.dumps(response)
|
|
|
|
|
|
|
|
|
|
|
|
class LWThemeUpdate(ThemeUpdate):
|
|
|
|
|
|
|
|
def __init__(self, locale, id_, qs=None):
|
|
|
|
super(LWThemeUpdate, self).__init__(locale, id_, qs)
|
2013-03-31 06:09:25 +04:00
|
|
|
self.data = {
|
|
|
|
'locale': locale,
|
|
|
|
'id': id_,
|
|
|
|
# If we came from getpersonas.com, then look up by `persona_id`.
|
|
|
|
# Otherwise, look up `addon_id`.
|
2013-04-29 00:33:08 +04:00
|
|
|
'primary_key': 'persona_id' if self.from_gp else 'addon_id',
|
2013-03-31 06:09:25 +04:00
|
|
|
'atype': base.ADDON_PERSONA,
|
|
|
|
'row': {}
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_update(self):
|
|
|
|
"""
|
|
|
|
TODO:
|
|
|
|
|
|
|
|
* When themes have versions let's not use
|
|
|
|
`personas.approve` as a `modified` timestamp. Either set this
|
|
|
|
during theme approval, or let's keep a hash of the header and
|
|
|
|
footer.
|
|
|
|
|
|
|
|
* Do a join on `addons_users` to get the actual correct user.
|
|
|
|
We're cheating and setting `personas.display_username` during
|
|
|
|
submission/management heh. But `personas.author` and
|
|
|
|
`personas.display_username` are not what we want.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
sql = """
|
2013-07-09 01:40:26 +04:00
|
|
|
SELECT p.persona_id, a.id, a.slug, v.version,
|
2013-03-31 06:09:25 +04:00
|
|
|
t_name.localized_string AS name,
|
|
|
|
t_desc.localized_string AS description,
|
|
|
|
p.display_username, p.header,
|
|
|
|
p.footer, p.accentcolor, p.textcolor,
|
2018-03-01 15:27:59 +03:00
|
|
|
UNIX_TIMESTAMP(a.modified) AS modified,
|
|
|
|
p.checksum
|
2013-03-31 06:09:25 +04:00
|
|
|
FROM addons AS a
|
|
|
|
LEFT JOIN personas AS p ON p.addon_id=a.id
|
2013-07-09 01:40:26 +04:00
|
|
|
LEFT JOIN versions AS v ON a.current_version=v.id
|
2013-03-31 06:09:25 +04:00
|
|
|
LEFT JOIN translations AS t_name
|
|
|
|
ON t_name.id=a.name AND t_name.locale=%(locale)s
|
|
|
|
LEFT JOIN translations AS t_desc
|
|
|
|
ON t_desc.id=a.summary AND t_desc.locale=%(locale)s
|
|
|
|
WHERE p.{primary_key}=%(id)s AND
|
2013-04-03 02:39:39 +04:00
|
|
|
a.addontype_id=%(atype)s AND a.status=4 AND a.inactive=0
|
2013-03-31 06:09:25 +04:00
|
|
|
""".format(primary_key=self.data['primary_key'])
|
|
|
|
|
|
|
|
self.cursor.execute(sql, self.data)
|
|
|
|
row = self.cursor.fetchone()
|
|
|
|
|
2017-10-05 15:48:52 +03:00
|
|
|
def row_to_dict(row):
|
|
|
|
return dict(zip((
|
|
|
|
'persona_id', 'addon_id', 'slug', 'current_version', 'name',
|
|
|
|
'description', 'username', 'header', 'footer', 'accentcolor',
|
2018-03-01 15:27:59 +03:00
|
|
|
'textcolor', 'modified', 'checksum'),
|
2017-10-05 15:48:52 +03:00
|
|
|
list(row)))
|
2013-03-31 06:09:25 +04:00
|
|
|
|
|
|
|
if row:
|
|
|
|
self.data['row'] = row_to_dict(row)
|
|
|
|
|
|
|
|
# Fall back to `en-US` if the name was null for our locale.
|
|
|
|
# TODO: Write smarter SQL and don't rerun the whole query.
|
|
|
|
if not self.data['row']['name']:
|
|
|
|
self.data['locale'] = 'en-US'
|
|
|
|
self.cursor.execute(sql, self.data)
|
|
|
|
row = self.cursor.fetchone()
|
|
|
|
if row:
|
|
|
|
self.data['row'] = row_to_dict(row)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_json(self):
|
|
|
|
if not self.get_update():
|
|
|
|
# Persona not found.
|
|
|
|
return
|
|
|
|
|
|
|
|
row = self.data['row']
|
|
|
|
accent = row.get('accentcolor')
|
|
|
|
text = row.get('textcolor')
|
2013-04-29 00:33:08 +04:00
|
|
|
|
|
|
|
id_ = str(row[self.data['primary_key']])
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'id': id_,
|
2013-03-31 06:09:25 +04:00
|
|
|
'name': row.get('name'),
|
|
|
|
'description': row.get('description'),
|
|
|
|
# TODO: Change this to be `addons_users.user.username`.
|
|
|
|
'author': row.get('username'),
|
|
|
|
# TODO: Change this to be `addons_users.user.display_name`.
|
|
|
|
'username': row.get('username'),
|
|
|
|
'headerURL': self.image_url(row['header']),
|
2018-01-31 22:24:02 +03:00
|
|
|
'footerURL': (
|
|
|
|
# Footer can be blank, return '' if that's the case.
|
|
|
|
self.image_url(row['footer']) if row['footer'] else ''),
|
2013-04-05 11:51:29 +04:00
|
|
|
'detailURL': self.locale_url(settings.SITE_URL,
|
|
|
|
'/addon/%s/' % row['slug']),
|
2013-03-31 06:09:25 +04:00
|
|
|
'previewURL': self.image_url('preview.png'),
|
|
|
|
'iconURL': self.image_url('icon.png'),
|
|
|
|
'accentcolor': '#%s' % accent if accent else None,
|
|
|
|
'textcolor': '#%s' % text if text else None,
|
2013-04-05 11:51:29 +04:00
|
|
|
'updateURL': self.locale_url(settings.VAMO_URL,
|
2013-04-29 00:33:08 +04:00
|
|
|
'/themes/update-check/' + id_),
|
2013-04-25 20:17:50 +04:00
|
|
|
# 04-25-2013: Bumped for GP migration so we get new `updateURL`s.
|
2013-07-09 01:40:26 +04:00
|
|
|
'version': row.get('current_version', 0)
|
2013-04-29 00:33:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# If this theme was originally installed from getpersonas.com,
|
|
|
|
# we have to use the `<persona_id>?src=gp` version of the `updateURL`.
|
|
|
|
if self.from_gp:
|
|
|
|
data['updateURL'] += '?src=gp'
|
|
|
|
|
|
|
|
return json.dumps(data)
|
2013-03-31 06:09:25 +04:00
|
|
|
|
|
|
|
def image_url(self, filename):
|
|
|
|
row = self.data['row']
|
|
|
|
|
|
|
|
# Special cased for non-AMO-uploaded themes imported from getpersonas.
|
|
|
|
if row['persona_id'] != 0:
|
|
|
|
if filename == 'preview.png':
|
|
|
|
filename = 'preview.jpg'
|
|
|
|
elif filename == 'icon.png':
|
|
|
|
filename = 'preview_small.jpg'
|
|
|
|
|
2014-08-19 22:48:58 +04:00
|
|
|
image_url = posixpath.join(user_media_url('addons'),
|
2013-04-03 02:39:39 +04:00
|
|
|
str(row['addon_id']), filename or '')
|
2018-03-01 15:27:59 +03:00
|
|
|
if row['checksum']:
|
|
|
|
modified = row['checksum'][:8]
|
|
|
|
elif row['modified']:
|
|
|
|
modified = int(row['modified'])
|
|
|
|
else:
|
|
|
|
modified = 0
|
|
|
|
return '%s?modified=%s' % (image_url, modified)
|
2013-03-31 06:09:25 +04:00
|
|
|
|
2013-04-05 11:51:29 +04:00
|
|
|
def locale_url(self, domain, url):
|
|
|
|
return '%s/%s%s' % (domain, self.data.get('locale', 'en-US'), url)
|
2013-03-31 06:09:25 +04:00
|
|
|
|
|
|
|
|
|
|
|
url_re = re.compile('(?P<locale>.+)?/themes/update-check/(?P<id>\d+)$')
|
|
|
|
|
|
|
|
|
|
|
|
def application(environ, start_response):
|
|
|
|
"""
|
|
|
|
Developing locally?
|
|
|
|
|
|
|
|
gunicorn -b 0.0.0.0:7000 -w 12 -k sync -t 90 --max-requests 5000 \
|
|
|
|
-n gunicorn-theme_update services.wsgi.theme_update:application
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
status = '200 OK'
|
|
|
|
with statsd.timer('services.theme_update'):
|
|
|
|
try:
|
|
|
|
locale, id_ = url_re.match(environ['PATH_INFO']).groups()
|
|
|
|
locale = (locale or 'en-US').lstrip('/')
|
|
|
|
id_ = int(id_)
|
|
|
|
except AttributeError: # URL path incorrect.
|
|
|
|
start_response('404 Not Found', [])
|
|
|
|
return ['']
|
|
|
|
|
|
|
|
try:
|
2018-07-03 20:20:06 +03:00
|
|
|
query_string = environ.get('QUERY_STRING')
|
|
|
|
update = MigratedUpdate(locale, id_, query_string)
|
|
|
|
if not update.is_migrated:
|
|
|
|
update = LWThemeUpdate(locale, id_, query_string)
|
2013-03-31 06:09:25 +04:00
|
|
|
output = update.get_json()
|
|
|
|
if not output:
|
|
|
|
start_response('404 Not Found', [])
|
|
|
|
return ['']
|
|
|
|
start_response(status, update.get_headers(len(output)))
|
2017-10-30 12:54:02 +03:00
|
|
|
except Exception:
|
2015-12-30 01:17:54 +03:00
|
|
|
log_exception(environ['PATH_INFO'])
|
2013-03-31 06:09:25 +04:00
|
|
|
raise
|
|
|
|
|
|
|
|
return [output]
|