22
33from django .conf import settings
44
5+ import caching .base as caching
6+ import jinja2
7+ import waffle
58from jingo import register , env
69from jingo_minify import helpers as jingo_minify_helpers
7- import jinja2
810from tower import ugettext as _
9- import waffle
1011
1112from access import acl
1213from amo .helpers import urlparams
1314from amo .urlresolvers import reverse , get_outgoing_url
1415from amo .utils import JSONEncoder
1516from translations .helpers import truncate
17+ from versions .compare import version_int as vint
18+
19+ import mkt
1620
1721
1822@jinja2 .contextfunction
@@ -56,11 +60,11 @@ def no_results():
5660
5761@jinja2 .contextfunction
5862@register .function
59- def market_button (context , product , receipt_type = None ):
63+ def market_button (context , product , receipt_type = None , classes = None ):
6064 request = context ['request' ]
6165 if product .is_webapp ():
6266 purchased = False
63- classes = ['button' , 'product' ]
67+ classes = ( classes or []) + ['button' , 'product' ]
6468 data_attrs = {'manifest_url' : product .get_manifest_url (),
6569 'is_packaged' : json .dumps (product .is_packaged )}
6670
@@ -75,13 +79,6 @@ def market_button(context, product, receipt_type=None):
7579 request .check_ownership (product , require_author = True )):
7680 purchased = True
7781
78- classes .append ('premium' )
79- if waffle .switch_is_active ('disabled-payments' ) or not request .GAIA :
80- classes .append ('disabled' )
81-
82- if not request .MOBILE and not 'disabled' in classes :
83- classes .append ('disabled' )
84-
8582 if purchased :
8683 label = _ ('Install' )
8784 else :
@@ -185,7 +182,8 @@ def product_as_dict_theme(request, product):
185182def market_tile (context , product , link = True , src = '' ):
186183 request = context ['request' ]
187184 if product .is_webapp ():
188- classes = ['product' , 'mkt-tile' ]
185+ classes = []
186+ notices = []
189187 purchased = (request .amo_user and
190188 product .pk in request .amo_user .purchase_ids ())
191189
@@ -202,10 +200,47 @@ def market_tile(context, product, link=True, src=''):
202200 'manifest_url' : product .get_manifest_url (),
203201 'src' : src
204202 }
203+
204+ ua = request .META .get ('HTTP_USER_AGENT' , '' )
205+ need_firefox , need_upgrade = check_firefox (ua )
206+
205207 if product .is_premium () and product .premium :
206208 classes .append ('premium' )
209+
210+ if waffle .switch_is_active ('disabled-payments' ):
211+ notices .append (_ ('This app is temporarily unavailable for '
212+ 'purchase.' ))
213+ elif not request .GAIA :
214+ notices .append (_ ('This app is available for purchase only '
215+ 'on Firefox OS.' ))
216+
217+ if product .is_packaged and not request .GAIA :
218+ notices .append (_ ('This app is available only on Firefox OS.' ))
219+
220+ if not request .MOBILE :
221+ notices .append (_ ('This app is available only on Firefox for '
222+ 'Android and Firefox OS.' ))
223+ if need_firefox :
224+ if request .MOBILE :
225+ url = ('https://www.mozilla.org/en-US/mobile/android-download'
226+ '.html' )
227+ notices .append (_ ('To use this app, <a href="{url}" '
228+ 'target="_blank">download and install '
229+ 'Firefox for Android</a>.' ).format (url = url ))
230+ # TODO: Enable when we allow installs on desktop again.
231+ #else:
232+ # url = 'https://www.mozilla.org/en-US/firefox/'
233+ # notices.append(_('To use this app, <a href="{url}" '
234+ # 'target="_blank">download and install '
235+ # 'Firefox</a>.').format(url=url))
236+ elif need_upgrade :
237+ notices .append (_ ('To use this app, upgrade Firefox.' ))
238+
239+ if notices :
240+ classes += ['bad' , 'disabled' ]
241+
207242 c = dict (request = request , product = product , data_attrs = data_attrs ,
208- classes = ' ' . join ( classes ) , link = link )
243+ classes = classes , link = link , notices = notices [: 1 ] )
209244 t = env .get_template ('site/tiles/app.html' )
210245 return jinja2 .Markup (t .render (c ))
211246
@@ -354,3 +389,37 @@ def get_login_link(context, to=None):
354389 if to == url :
355390 to = None
356391 return urlparams (url , to = to )
392+
393+
394+ @register .function
395+ def check_firefox (ua ):
396+ return caching .cached (lambda : _check_firefox (ua ), 'check_firefox:%s' % ua )
397+
398+
399+ def _check_firefox (ua ):
400+ need_firefox , need_upgrade = True , True
401+
402+ for ua_res , min_version in mkt .platforms .APP_PLATFORMS :
403+ for ua_re in ua_res :
404+ match = ua_re .search (ua )
405+ if match :
406+ v = match .groups ()[0 ]
407+
408+ # If we found a version at all, then this is Firefox.
409+ need_firefox = False
410+
411+ # If we found a matching version, then we can install apps!
412+ need_upgrade = vint (v ) < min_version
413+
414+ return need_firefox , need_upgrade
415+
416+
417+ @register .function
418+ @jinja2 .contextfunction
419+ def allow_installs (context ):
420+ """
421+ This will return of a boolean of whether we're on a version of
422+ Firefox that supports `navigator.mozApps`.
423+ """
424+ ua = context ['request' ].META .get ('HTTP_USER_AGENT' , '' )
425+ return check_firefox (ua ) == (False , False )
0 commit comments