def test_commands_static_dump_errors(self): from ptah.view.commands import StaticCommand StaticCommand._include = ('ptah', self.__class__.__module__) view.static('tests', 'ptah.view.tests:static/dir2') _out = sys.stdout file = os.path.join(self.dir, 'file') f = open(file, 'w') f.write(' ') f.close() sys.argv[:] = ['paste', 'static', '-d %s'%file] out = StringIO() sys.stdout = out try: run() except SystemExit: pass sys.stdout = _out val = out.getvalue() self.assertTrue('Output path is not directory.' in val)
def test_static_register_url(self): from ptah.view import resources resources.STATIC.url = 'http://ptah.org/static' view.static('testsurl', 'ptah.view.tests:static/dir1') self._init_ptah() self.assertEquals( view.static_url('tests', 'styles.css', self.request), 'http://ptah.org/static/tests/styles.css')
def test_static_wired(self): # something strange can happen, info can be removed # from registry before ptah init (tests for example) view.static('tests', 'ptah.view.tests:static/dir1') self.assertTrue('tests' in resources.registry) del resources.registry['tests'] self._init_ptah() self.assertTrue('tests' in resources.registry)
def test_base_static_url(self): view.static('tests2', 'ptah.view.tests:static/dir1') self._init_ptah() request = self._makeRequest() base = View(None, request) self.assertEquals( base.static_url('tests2', 'styles.css'), 'http://localhost:8080/static/tests2/styles.css')
def test_library_include_resource(self): view.static('tests2', 'ptah.view.tests:static/dir1') view.library( 'test-lib', path='style.css', resource='tests2', type='css') self._init_ptah() request = self._makeRequest() view.include(request, 'test-lib') self.assertEqual( view.render_includes(request), '<link type="text/css" rel="stylesheet" href="http://localhost:8080/static/tests2/style.css" />')
def test_static_register(self): view.static('tests', 'ptah.view.tests:static/dir1') self._init_ptah() request = self._makeRequest() self._setRequest(request) self.assertEquals( view.static_url('tests', 'styles.css', request), 'http://localhost:8080/static/tests/styles.css') self.assertEquals( view.static_url('tests', '', request), 'http://localhost:8080/static/tests')
def test_commands_static_dump(self): view.static('tests', 'ptah.view.tests:static/dir2') dir = os.path.join(self.dir, 'subdir') sys.argv[:] = ['paste', 'static', '-d %s'%dir] try: run() except SystemExit: pass val = self.out.getvalue() self.assertTrue("* Coping from 'ptah.view.tests'" in val) files = os.listdir(os.path.join(dir, 'tests')) files.sort() self.assertTrue(files == ['style.css', 'text.txt'])
def test_library_simple_css(self): view.static('tests', 'ptah.view.tests:static/dir1') view.library( 'test-lib', path='style.css', resource='tests', type='css') self._init_ptah() from ptah.view.library import LIBRARY_ID lib = config.get_cfg_storage(LIBRARY_ID)['test-lib'] self.assertEqual(lib.name, 'test-lib') self.assertEqual(len(lib.entries), 1) self.assertEqual(len(lib.entries), 1) self.assertTrue('style.css' in lib.entries[0].paths) self.assertEqual( repr(lib), '<ptah.view.library.Library "test-lib">')
def test_commands_static_dump_skipping_existing(self): view.static('tests', 'ptah.view.tests:static/dir2') os.makedirs(os.path.join(self.dir, 'tests')) file = os.path.join(self.dir, 'tests', 'text.txt') f = open(file, 'wb') f.write('test existing file') f.close() sys.argv[:] = ['paste', 'static', '-d %s'%self.dir] try: run() except SystemExit: pass val = self.out.getvalue() self.assertTrue("skipping ../ptah.view.tests/text.txt" in val) self.assertTrue('test existing file' == open(file, 'r').read())
def test_commands_static_list(self): view.static('tests', 'ptah.view.tests:static') _out = sys.stdout sys.argv[:] = ['paste', 'static', '-l'] out = StringIO() sys.stdout = out try: print run() except SystemExit: pass sys.stdout = _out val = out.getvalue() self.assertTrue('* tests' in val) self.assertTrue('by: ptah.view.tests' in val)
""" views """ import os.path from webob import Response from ptah import view from pyramid.httpexceptions import HTTPFound from pyramid.security import authenticated_userid from root import IPloudApplicationRoot view.static("ploud", "ploud.frontend:assets") class FaviconView(view.View): view.pview(route="frontend-favicon", layout=None) icon_path = os.path.join(os.path.dirname(__file__), "assets", "_img-ui", "favicon.ico") def render(self): response = self.request.response response.content_type = "image/x-icon" return open(self.icon_path).read() class RobotsView(view.View): view.pview(route="frontend-robots", layout=None) robots_path = os.path.join(os.path.dirname(__file__), "assets", "robots.txt") def render(self): response = self.request.response response.content_type = "text/plain"
""" static assets """ from ptah import view view.static('jquery', 'ptah:static/jquery') view.static('bootstrap', 'ptah:static/bootstrap') view.static('tiny_mce', 'ptah:static/tiny_mce') # jQuery library view.library( 'jquery', path="jquery-1.6.4.min.js", resource="jquery", type="js") view.library( 'jquery-ui', path="jquery-ui-1.8.16.min.js", type="js", resource="jquery", require="jquery") view.library( 'jquery-ui', path="jquery-ui.css", resource="jquery", type='css') # Bootstrap css view.library( 'bootstrap', path="bootstrap-1.4.0.min.css",
from datetime import datetime import ptah from ptah import view, form, cms from pyramid.httpexceptions import HTTPFound, HTTPNotFound from myapp301 import settings from myapp301.app import ApplicationRoot view.static('myapp301', 'myapp301:static') # access this view by going to http://localhost:8080/myview.html # add myapp301.band=my band to .ini file, restart and render this view. @view.pview('myview.html', context = ApplicationRoot) def my_view(request): data = {'context' : request.root, 'happy' : settings.myapp301.happy, 'favband' : settings.myapp301.band, 'now' : datetime.now()} return str(data) class DefaultContentView(form.DisplayForm): view.pview( context = cms.Content, permission = cms.View, template=view.template("templates/contentview.pt")) @property def fields(self): return self.context.__type__.fieldset
""" TineMCE field """ from ptah import view, form # TinyMCE view.static( 'tiny_mce', 'ptah.cmsapp:static/tiny_mce') view.library( "tiny_mce", resource="tiny_mce", path=('tiny_mce.js', 'jquery.tinymce.js'), type="js", require='jquery') class TinymceField(form.TextAreaField): __doc__ = u'TinyMCE Text Area input widget' form.field('tinymce') klass = u'tinymce-widget' width = '400px' height = '300px' theme = "advanced" tmpl_input = view.template( "ptah.cmsapp:templates/tinymce_input.pt")