def test_leading_slash(self): with self.settings(WEBAPP_ICON_URL='v', DEBUG=True, SERVE_TMP_PATH=True): eq_(static_url('WEBAPP_ICON_URL'), 'http://testserver/tmp/v') with self.settings(WEBAPP_ICON_URL='/v', DEBUG=True, SERVE_TMP_PATH=True): eq_(static_url('WEBAPP_ICON_URL'), 'http://testserver/tmp/v')
def test_leading_slash(self): with self.settings(ADDON_ICON_URL='v', DEBUG=True, SERVE_TMP_PATH=True): eq_(static_url('ADDON_ICON_URL'), 'http://testserver/tmp/v') with self.settings(ADDON_ICON_URL='/v', DEBUG=True, SERVE_TMP_PATH=True): eq_(static_url('ADDON_ICON_URL'), 'http://testserver/tmp/v')
def test_url(self): with self.settings(WEBAPPS_RECEIPT_URL='/v', SITE_URL='http://f.com'): eq_(static_url('WEBAPPS_RECEIPT_URL'), 'http://f.com/v') with self.settings(DEBUG=True, SERVE_TMP_PATH=True): eq_(static_url('WEBAPPS_RECEIPT_URL'), 'http://testserver/receipt-verifier/') with self.settings(WEBAPPS_RECEIPT_URL='http://f.com'): eq_(static_url('WEBAPPS_RECEIPT_URL'), 'http://f.com')
def test_url(self): with self.settings(WEBAPPS_RECEIPT_URL='/v', SITE_URL='http://f.com'): eq_(static_url('WEBAPPS_RECEIPT_URL'), 'http://f.com/v') with self.settings(DEBUG=True, SERVE_TMP_PATH=True): eq_(static_url('WEBAPPS_RECEIPT_URL'), 'http://testserver/tmp/verify/') with self.settings(WEBAPPS_RECEIPT_URL='http://f.com'): eq_(static_url('WEBAPPS_RECEIPT_URL'), 'http://f.com')
def get_icon_url(base_url_format, obj, size): """ Returns either the icon URL for a given (`obj`, `size`). base_url_format` is a string that will be used for url formatting, see ADDON_ICON_URL for an example. If no icon type if set on the `obj`, then the url for the appropriate default icon for the given `size` will be returned. `obj` needs to implement `icon_type` and `icon_hash` properties for this function to work. Note: does not check size, so it can return 404 URLs if you specify an invalid size. """ # Return default image if no icon_type was stored. if not obj.icon_type: return '%s/default-%s.png' % (static_url('ICONS_DEFAULT_URL'), size) else: # [1] is the whole ID, [2] is the directory. split_id = re.match(r'((\d*?)\d{1,3})$', str(obj.pk)) # If we don't have the icon_hash set to a dummy string ("never"), # when the icon is eventually changed, icon_hash will be updated. suffix = obj.icon_hash or 'never' return base_url_format % (split_id.group(2) or 0, obj.pk, size, suffix)
def create_receipt(webapp, user, uuid, flavour=None, contrib=None): """ Creates a receipt for use in payments. :params app: the app record. :params user: the UserProfile record. :params uuid: a uuid placed in the user field for this purchase. :params flavour: None, developer, inapp, or reviewer - the flavour of receipt. :param: contrib: the Contribution object for the purchase. """ # Unflavo(u)red receipts are for plain ol' vanilla app purchases. assert flavour in (None, 'developer', 'inapp', 'reviewer'), ( 'Invalid flavour: %s' % flavour) time_ = calendar.timegm(time.gmtime()) typ = 'purchase-receipt' storedata = {'id': int(webapp.pk)} # Generate different receipts for reviewers or developers. expiry = time_ + settings.WEBAPPS_RECEIPT_EXPIRY_SECONDS verify = static_url('WEBAPPS_RECEIPT_URL') if flavour == 'inapp': if not contrib: raise ValueError( 'a contribution object is required for in-app receipts') if not contrib.inapp_product: raise ValueError( 'contribution {c} does not link to an in-app product' .format(c=contrib)) storedata['contrib'] = int(contrib.pk) elif flavour in ('developer', 'reviewer'): if not (acl.action_allowed_user(user, 'Apps', 'Review') or webapp.has_author(user)): raise ValueError('User %s is not a reviewer or developer' % user.pk) # Developer and reviewer receipts should expire after 24 hours. expiry = time_ + (60 * 60 * 24) typ = flavour + '-receipt' verify = absolutify(reverse('receipt.verify', args=[webapp.guid])) product = {'storedata': urlencode(storedata), # Packaged and hosted apps should have an origin. If there # isn't one, fallback to the SITE_URL. 'url': webapp.origin or settings.SITE_URL} reissue = absolutify(reverse('receipt.reissue')) receipt = dict(exp=expiry, iat=time_, iss=settings.SITE_URL, nbf=time_, product=product, # TODO: This is temporary until detail pages get added. # TODO: bug 1020997, bug 1020999 detail=absolutify(reissue), # Currently this is a 404. reissue=absolutify(reissue), typ=typ, user={'type': 'directed-identifier', 'value': uuid}, verify=verify) return sign(receipt)
def get_promo_img_url(self, size): if not self.promo_img_hash: return '' return get_promo_img_url(static_url('WEBSITE_PROMO_IMG_URL'), self, size, default_format='website-promo-{size}.png')
def get_icon_url(base_url_format, obj, size, default_format='default-{size}.png'): """ Returns either the icon URL for a given (`obj`, `size`). base_url_format` is a string that will be used for url formatting if we are not using a remote storage, see ADDON_ICON_URL for an example. If no icon type if set on the `obj`, then the url for the appropriate default icon for the given `size` will be returned. `obj` needs to implement `icon_type` and `icon_hash` properties for this function to work. Note: does not check size, so it can return 404 URLs if you specify an invalid size. """ # Return default image if no icon_type was stored. if not obj.icon_type: return '{path}/{name}'.format(path=static_url('ICONS_DEFAULT_URL'), name=default_format.format(size=size)) else: # If we don't have the icon_hash set to a dummy string ("never"), # when the icon is eventually changed, icon_hash will be updated. suffix = obj.icon_hash or 'never' if storage_is_remote(): # We don't care about base_url_format, the storage provides the url # for a given path. We assume AWS_QUERYSTRING_AUTH is False atm. path = '%s/%s-%s.png' % (obj.get_icon_dir(), obj.pk, size) return '%s?modified=%s' % (public_storage.url(path), suffix) # [1] is the whole ID, [2] is the directory. split_id = re.match(r'((\d*?)\d{1,3})$', str(obj.pk)) return base_url_format % (split_id.group(2) or 0, obj.pk, size, suffix)
def get_icon_url(self, size): icon_name = '{icon}-{{size}}.png'.format( icon=DEFAULT_ICONS[self.pk % len(DEFAULT_ICONS)]) return get_icon_url(static_url('WEBSITE_ICON_URL'), self, size, default_format=icon_name)
def test_get_icon_url_bigger_pk(self): website = Website(pk=98765432, icon_type="image/png") if not storage_is_remote(): expected = static_url("WEBSITE_ICON_URL") % (str(website.pk)[:-3], website.pk, 32, "never") else: path = "%s/%s-%s.png" % (website.get_icon_dir(), website.pk, 32) expected = "%s?modified=never" % storage.url(path) assert website.get_icon_url(32).endswith(expected), "Expected %s, got %s" % (expected, website.get_icon_url(32))
def test_get_icon_url_bigger_pk(self): website = Website(pk=98765432, icon_type='image/png') if not storage_is_remote(): expected = (static_url('WEBSITE_ICON_URL') % (str(website.pk)[:-3], website.pk, 32, 'never')) else: path = '%s/%s-%s.png' % (website.get_icon_dir(), website.pk, 32) expected = '%s?modified=never' % public_storage.url(path) assert website.get_icon_url(32).endswith(expected), ( 'Expected %s, got %s' % (expected, website.get_icon_url(32)))
def check_full(self): """ This is the default that verify will use, this will do the entire stack of checks. """ receipt_domain = urlparse(static_url('WEBAPPS_RECEIPT_URL')).netloc try: self.decoded = self.decode() self.check_type('purchase-receipt') self.check_url(receipt_domain) self.check_purchase() except InvalidReceipt, err: return self.invalid(str(err))
def render(self): """ This will output radios as li>img+input. """ output = [] for w in self: value = w.choice_value if value.split('/')[0] == 'icon' or value == '': o = ( ("<li><a href='#' class='%s'><img src='%s/%s-32.png'>" "</a>%s</li>") % ('active' if self.value == w.choice_value else '', static_url('ADDON_ICONS_DEFAULT_URL'), w.choice_label, w)) else: o = "<li class='hide'>%s</li>" % w output.append(o) return mark_safe(u'\n'.join(output))
def render(self): """ This will output radios as li>img+input. """ output = [] for w in self: value = w.choice_value if value.split('/')[0] == 'icon' or value == '': o = (("<li><a href='#' class='%s'><img src='%s/%s-32.png'>" "</a>%s</li>") % ('active' if self.value == w.choice_value else '', static_url('ADDON_ICONS_DEFAULT_URL'), w.choice_label, w)) else: o = "<li class='hide'>%s</li>" % w output.append(o) return mark_safe(u'\n'.join(output))
def url(self): return absolutify( os.path.join(static_url('PRODUCT_ICON_URL'), self._base_path()))
def get_icon_url(self, size): return get_icon_url(static_url('EXTENSION_ICON_URL'), self, size)
def test_get_icon_url_bigger_pk(self): website = Website(pk=98765432, icon_type='image/png') expected = (static_url('WEBSITE_ICON_URL') % (str(website.pk)[:-3], website.pk, 32, 'never')) assert website.get_icon_url(32).endswith(expected), ( 'Expected %s, got %s' % (expected, website.get_icon_url(32)))
def test_get_icon_url(self): website = Website(pk=1, icon_type='image/png') expected = (static_url('WEBSITE_ICON_URL') % ('0', website.pk, 32, 'never')) assert website.get_icon_url(32).endswith(expected), ( 'Expected %s, got %s' % (expected, website.get_icon_url(32)))
def get_icon_url(self, size): return get_icon_url(static_url('WEBSITE_ICON_URL'), self, size)
def url(self): return absolutify(os.path.join(static_url('PRODUCT_ICON_URL'), self._base_path()))