Add url and provision fore remote_path in static files
This commit is contained in:
parent
656dfad25e
commit
a82b848a1a
|
@ -11,7 +11,8 @@
|
|||
import os
|
||||
import base64
|
||||
|
||||
from nereid.helpers import slugify, send_file
|
||||
from nereid.helpers import slugify, send_file, url_for
|
||||
from nereid.globals import _request_ctx_stack
|
||||
from werkzeug import abort
|
||||
|
||||
from trytond.model import ModelSQL, ModelView, fields
|
||||
|
@ -97,16 +98,28 @@ class NereidStaticFile(ModelSQL, ModelView):
|
|||
folder = fields.Many2One(
|
||||
'nereid.static.folder', 'Folder', select=True, required=True
|
||||
)
|
||||
type = fields.Selection([
|
||||
('local', 'Local File'),
|
||||
('remote', 'Remote File'),
|
||||
], 'File Type')
|
||||
|
||||
# This function field returns the field contents. This is useful if the
|
||||
# field is going to be displayed on the clients.
|
||||
#: URL of the remote file if the :attr:`type` is remote
|
||||
remote_path = fields.Char('Remote File', select=True, translate=True)
|
||||
|
||||
#: This function field returns the field contents. This is useful if the
|
||||
#: field is going to be displayed on the clients.
|
||||
file_binary = fields.Function(
|
||||
fields.Binary('File'), 'get_file_binary', 'set_file_binary'
|
||||
)
|
||||
|
||||
# Full path to the file in the filesystem
|
||||
#: Full path to the file in the filesystem
|
||||
file_path = fields.Function(fields.Char('File Path'), 'get_file_path',)
|
||||
|
||||
#: URL that can be used to idenfity the resource. Note that the value
|
||||
#: of this field is available only when called within a request context.
|
||||
#: In other words the URL is valid only when called in a nereid request.
|
||||
url = fields.Function(fields.Char('URL'), 'get_url')
|
||||
|
||||
def __init__(self):
|
||||
super(NereidStaticFile, self).__init__()
|
||||
self._constraints += [
|
||||
|
@ -122,6 +135,27 @@ class NereidStaticFile(ModelSQL, ModelView):
|
|||
(2) file name contains '/'""",
|
||||
})
|
||||
|
||||
def default_type(self):
|
||||
return 'local'
|
||||
|
||||
def get_url(self, ids, name):
|
||||
"""Return the url if within an active request context or return
|
||||
False values
|
||||
"""
|
||||
res = {}.fromkeys(ids, False)
|
||||
if _request_ctx_stack.top is None:
|
||||
return res
|
||||
|
||||
for f in self.browse(ids):
|
||||
if f.type == 'local':
|
||||
res[f.id] = url_for(
|
||||
'nereid.static.file.send_static_file',
|
||||
folder=f.folder.folder_name, name=f.name
|
||||
)
|
||||
elif f.type == 'remote':
|
||||
res[f.id] = f.remote_path
|
||||
return res
|
||||
|
||||
def get_nereid_base_path(self):
|
||||
"""
|
||||
Returns base path for nereid, where all the static files would be
|
||||
|
|
|
@ -7,7 +7,9 @@
|
|||
:copyright: (c) 2012 by Openlabs Technologies & Consulting (P) LTD
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import new
|
||||
import base64
|
||||
import functools
|
||||
import unittest2 as unittest
|
||||
|
||||
from trytond.config import CONFIG
|
||||
|
@ -16,6 +18,7 @@ CONFIG.options['data_path'] = '/tmp/temp_tryton_data/'
|
|||
|
||||
from trytond.modules import register_classes
|
||||
register_classes()
|
||||
from nereid import render_template
|
||||
from nereid.testing import testing_proxy, TestCase
|
||||
from trytond.transaction import Transaction
|
||||
|
||||
|
@ -39,6 +42,15 @@ class TestStaticFile(TestCase):
|
|||
application_user = 1, guest_user = cls.guest_user
|
||||
)
|
||||
|
||||
# Create a homepage template
|
||||
testing_proxy.create_template(
|
||||
'home.jinja',
|
||||
'''
|
||||
{% set static_file = static_file_obj.browse(static_file_id) %}
|
||||
{{ static_file.url }}
|
||||
''', cls.site
|
||||
)
|
||||
|
||||
txn.cursor.commit()
|
||||
|
||||
def get_app(self, **options):
|
||||
|
@ -51,6 +63,7 @@ class TestStaticFile(TestCase):
|
|||
def setUp(self):
|
||||
self.static_folder_obj = testing_proxy.pool.get('nereid.static.folder')
|
||||
self.static_file_obj = testing_proxy.pool.get('nereid.static.file')
|
||||
self.website_obj = testing_proxy.pool.get('nereid.website')
|
||||
|
||||
def test_000_view(self):
|
||||
from trytond.tests.test_tryton import test_view
|
||||
|
@ -85,6 +98,71 @@ class TestStaticFile(TestCase):
|
|||
self.assertEqual(rv.headers['Content-Type'], 'image/png')
|
||||
self.assertEqual(rv.status_code, 200)
|
||||
|
||||
def test_0020_static_file_url(self):
|
||||
with Transaction().start(testing_proxy.db_name, 1, None) as txn:
|
||||
file_id, = self.static_file_obj.search([], limit=1)
|
||||
file = self.static_file_obj.browse(file_id)
|
||||
self.assertFalse(file.url)
|
||||
|
||||
app = self.get_app()
|
||||
with app.test_client() as c:
|
||||
# Patch the home page method
|
||||
def home_func(self, file_id):
|
||||
static_file_obj = self.pool.get('nereid.static.file')
|
||||
return render_template(
|
||||
'home.jinja',
|
||||
static_file_obj=static_file_obj,
|
||||
static_file_id=file_id,
|
||||
)
|
||||
home_func = functools.partial(home_func, file_id=file_id)
|
||||
c.application.view_functions[
|
||||
'nereid.website.home'] = new.instancemethod(
|
||||
home_func, self.website_obj
|
||||
)
|
||||
self.website_obj.home = new.instancemethod(
|
||||
home_func, self.website_obj
|
||||
)
|
||||
rv = c.get('/en_US/')
|
||||
self.assertTrue('/en_US/static-file/test/test.png' in rv.data)
|
||||
self.assertEqual(rv.status_code, 200)
|
||||
|
||||
def test_0030_static_file_remote_url(self):
|
||||
with Transaction().start(testing_proxy.db_name, 1, None) as txn:
|
||||
folder_id, = self.static_folder_obj.search([])
|
||||
file_id = self.static_file_obj.create({
|
||||
'name': 'remote.png',
|
||||
'folder': folder_id,
|
||||
'type': 'remote',
|
||||
'remote_path': 'http://openlabs.co.in/logo.png',
|
||||
})
|
||||
file = self.static_file_obj.browse(file_id)
|
||||
self.assertFalse(file.url)
|
||||
txn.cursor.commit()
|
||||
|
||||
app = self.get_app()
|
||||
with app.test_client() as c:
|
||||
# Patch the home page method
|
||||
def home_func(self, file_id):
|
||||
static_file_obj = self.pool.get('nereid.static.file')
|
||||
return render_template(
|
||||
'home.jinja',
|
||||
static_file_obj=static_file_obj,
|
||||
static_file_id=file_id,
|
||||
)
|
||||
home_func = functools.partial(home_func, file_id=file_id)
|
||||
c.application.view_functions[
|
||||
'nereid.website.home'] = new.instancemethod(
|
||||
home_func, self.website_obj
|
||||
)
|
||||
self.website_obj.home = new.instancemethod(
|
||||
home_func, self.website_obj
|
||||
)
|
||||
rv = c.get('/en_US/')
|
||||
self.assertTrue(
|
||||
'http://openlabs.co.in/logo.png' in rv.data
|
||||
)
|
||||
self.assertEqual(rv.status_code, 200)
|
||||
|
||||
|
||||
def suite():
|
||||
"Nereid test suite"
|
||||
|
|
Loading…
Reference in New Issue