Skip to content

Commit

Permalink
- add file parameter to manage_addPythonScript (fixes #45)
Browse files Browse the repository at this point in the history
  • Loading branch information
dataflake committed Jun 3, 2020
1 parent 2c534bc commit fc69e80
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Expand Up @@ -3,23 +3,30 @@ Changelog

4.12 (unreleased)
-----------------

- add a ``file`` parameter to factory function ``manage_addPythonScript``
(`#45 <https://github.com/zopefoundation/Products.PythonScripts/issues/45>`_)

- fix ``TypeError`` when updating an existing script from a file
(`#43 <https://github.com/zopefoundation/Products.PythonScripts/issues/43>`_)


4.11 (2020-02-11)
-----------------

- fix ``PUT`` issues with string encoding


4.10 (2020-02-11)
-----------------

- override ``manage_DAVget`` to get correct editable sources
(`#40 <https://github.com/zopefoundation/Products.PythonScripts/issues/40>`_)


4.9 (2019-10-09)
----------------

- prevent ResourceWarning/Error by closing default contents file
(`#39 <https://github.com/zopefoundation/Products.PythonScripts/issues/39>`_)

Expand Down
19 changes: 11 additions & 8 deletions src/Products/PythonScripts/PythonScript.py
Expand Up @@ -80,22 +80,25 @@
_marker = [] # Create a new marker object


def manage_addPythonScript(self, id, title='', REQUEST=None, submit=None):
def manage_addPythonScript(self, id, title='', file=None, REQUEST=None,
submit=None):
"""Add a Python script to a folder.
"""
id = str(id)
id = self._setObject(id, PythonScript(id))
pyscript = self._getOb(id)
if title:
pyscript.ZPythonScript_setTitle(title)

file = file or (REQUEST and REQUEST.form.get('file'))
if hasattr(file, 'read'):
file = file.read()
if not file:
with open(_default_file) as fp:
file = fp.read()
pyscript.write(file)

if REQUEST is not None:
file = REQUEST.form.get('file', '')
if not isinstance(file, str):
file = file.read()
if not file:
with open(_default_file) as fp:
file = fp.read()
pyscript.write(file)
try:
u = self.DestinationURL()
except Exception:
Expand Down
76 changes: 76 additions & 0 deletions src/Products/PythonScripts/tests/testPythonScript.py
Expand Up @@ -12,6 +12,7 @@
##############################################################################
import codecs
import contextlib
import io
import os
import sys
import unittest
Expand All @@ -22,6 +23,7 @@
import Zope2
from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManagement import noSecurityManager
from OFS.Folder import Folder
from Testing.makerequest import makerequest
from Testing.testbrowser import Browser
from Testing.ZopeTestCase import FunctionalTestCase
Expand Down Expand Up @@ -51,6 +53,13 @@ def readf(name):
return f.read()


class DummyFolder(Folder):
""" Stitch in an implementation for getPhysicalPath """

def getPhysicalPath(self):
return ()


class PythonScriptTestBase(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -304,6 +313,73 @@ def test_write(self):
ps.write(u'return 1')
self.assertEqual(ps.body(), 'return 1\n')

def test_factory(self):
from Products.PythonScripts.PythonScript import manage_addPythonScript

# Only passing the id
container = DummyFolder('container')
manage_addPythonScript(container, 'testing')
self.assertEqual(container.testing.getId(), 'testing')
self.assertEqual(container.testing.title, '')
self.assertIn('# Example code:', container.testing.body())
self.assertEqual(container.testing.params(), '')

# Passing id and a title
container = DummyFolder('container')
manage_addPythonScript(container, 'testing', title='This is a title')
self.assertEqual(container.testing.getId(), 'testing')
self.assertEqual(container.testing.title, 'This is a title')
self.assertIn('# Example code:', container.testing.body())
self.assertEqual(container.testing.params(), '')

# Passing id, title and a request that has no file
container = makerequest(DummyFolder('container'))
container.REQUEST.form = {}
manage_addPythonScript(container, 'testing', title='This is a title',
REQUEST=container.REQUEST)
self.assertEqual(container.testing.getId(), 'testing')
self.assertEqual(container.testing.title, 'This is a title')
self.assertIn('# Example code:', container.testing.body())
self.assertEqual(container.testing.params(), '')

# Passing id, title and a request ith a file string
container = makerequest(DummyFolder('container'))
container.REQUEST.form = {'file': 'return 1'}
manage_addPythonScript(container, 'testing', title='This is a title',
REQUEST=container.REQUEST)
self.assertEqual(container.testing.getId(), 'testing')
self.assertEqual(container.testing.title, 'This is a title')
self.assertEqual(container.testing.body(), 'return 1\n')
self.assertEqual(container.testing.params(), '')

# Passing id, title and a request with a file object
container = makerequest(DummyFolder('container'))
container.REQUEST.form = {'file': io.BytesIO(b'return 1')}
manage_addPythonScript(container, 'testing', title='This is a title',
REQUEST=container.REQUEST)
self.assertEqual(container.testing.getId(), 'testing')
self.assertEqual(container.testing.title, 'This is a title')
self.assertEqual(container.testing.body(), 'return 1\n')
self.assertEqual(container.testing.params(), '')

# Passing id, title and a file string
container = makerequest(DummyFolder('container'))
manage_addPythonScript(container, 'testing', title='This is a title',
file=b'return 1')
self.assertEqual(container.testing.getId(), 'testing')
self.assertEqual(container.testing.title, 'This is a title')
self.assertEqual(container.testing.body(), 'return 1\n')
self.assertEqual(container.testing.params(), '')

# Passing id, title and a file object
container = makerequest(DummyFolder('container'))
manage_addPythonScript(container, 'testing', title='This is a title',
file=io.BytesIO(b'return 1'))
self.assertEqual(container.testing.getId(), 'testing')
self.assertEqual(container.testing.title, 'This is a title')
self.assertEqual(container.testing.body(), 'return 1\n')
self.assertEqual(container.testing.params(), '')


class TestPythonScriptErrors(PythonScriptTestBase):

Expand Down

0 comments on commit fc69e80

Please sign in to comment.