This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
/
utils.py
136 lines (106 loc) · 4.63 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
import hashlib
import json
from django.core.cache import cache
import commonware.log
import mkt
from mkt.site.storage_utils import public_storage
from mkt.site.utils import JSONEncoder
from mkt.translations.utils import find_language
log = commonware.log.getLogger('z.webapps')
def get_locale_properties(manifest, property, default_locale=None):
locale_dict = {}
for locale in manifest.get('locales', {}):
if property in manifest['locales'][locale]:
locale_dict[locale] = manifest['locales'][locale][property]
# Add in the default locale name.
default = manifest.get('default_locale') or default_locale
root_property = manifest.get(property)
if default and root_property:
locale_dict[default] = root_property
return locale_dict
def get_supported_locales(manifest):
"""
Returns a list of locales found in the "locales" property of the manifest.
This will convert locales found in the SHORTER_LANGUAGES setting to their
full locale. It will also remove locales not found in AMO_LANGUAGES.
Note: The default_locale is not included.
"""
return sorted(filter(None, map(find_language, set(
manifest.get('locales', {}).keys()))))
def dehydrate_content_rating(rating):
"""
{body.id, rating.id} to translated rating.label.
"""
try:
body = mkt.ratingsbodies.dehydrate_ratings_body(
mkt.ratingsbodies.RATINGS_BODIES[int(rating['body'])])
except TypeError:
# Legacy ES format (bug 943371).
return {}
rating = mkt.ratingsbodies.dehydrate_rating(
body.ratings[int(rating['rating'])])
return rating.label
def dehydrate_content_ratings(content_ratings):
"""Dehydrate an object of content ratings from rating IDs to dict."""
for body in content_ratings or {}:
# Dehydrate all content ratings.
content_ratings[body] = dehydrate_content_rating(content_ratings[body])
return content_ratings
def get_cached_minifest(app_or_langpack, force=False):
"""
Create a "mini" manifest for a packaged app or langpack and cache it (Call
with `force=True` to bypass existing cache).
Note that platform expects name/developer/locales to match the data from
the real manifest in the package, so it needs to be read from the zip file.
Returns a tuple with the minifest contents and the corresponding etag.
"""
cache_prefix = 1 # Change this if you are modifying what enters the cache.
cache_key = '{0}:{1}:{2}:manifest'.format(cache_prefix,
app_or_langpack._meta.model_name,
app_or_langpack.pk)
if not force:
cached_data = cache.get(cache_key)
if cached_data:
return cached_data
sign_if_packaged = getattr(app_or_langpack, 'sign_if_packaged', None)
if sign_if_packaged is None:
# Langpacks are already signed when we generate the manifest and have
# a file_path attribute.
signed_file_path = app_or_langpack.file_path
else:
# sign_if_packaged() will return the signed path. But to call it, we
# need a current version. If we don't have one, return an empty
# manifest, bypassing caching so that when a version does become
# available it can get picked up correctly.
if not app_or_langpack.current_version:
return '{}'
signed_file_path = sign_if_packaged()
manifest = app_or_langpack.get_manifest_json()
package_path = app_or_langpack.get_package_path()
data = {
'size': public_storage.size(signed_file_path),
'package_path': package_path,
}
if hasattr(app_or_langpack, 'current_version'):
data['version'] = app_or_langpack.current_version.version
data['release_notes'] = app_or_langpack.current_version.releasenotes
file_hash = app_or_langpack.current_version.all_files[0].hash
else:
# LangPacks have no version model, the version number is an attribute
# and they don't have release notes.
data['version'] = app_or_langpack.version
# File hash is not stored for langpacks, but file_version changes with
# every new upload so we can use that instead.
file_hash = unicode(app_or_langpack.file_version)
for key in ['developer', 'icons', 'locales', 'name']:
if key in manifest:
data[key] = manifest[key]
data = json.dumps(data, cls=JSONEncoder)
etag = hashlib.sha256()
etag.update(data)
if file_hash:
etag.update(file_hash)
rval = (data, etag.hexdigest())
cache.set(cache_key, rval, None)
return rval