I'm trying to format a Date retrieved from MySQL, but while I can print the date as it is I can't access any of the methods, so no strftime() and no day() etc I get a 'not allowed in this context' exception so: <i tal:content="python:item.data"></i> is ok but: <i tal:content="python:item.data.day"></i> causes Zope to ask for authentication Zope 2.7.2, Python 2.3.4 here any hint would be appreciated thanks massimo
--On Montag, 11. Oktober 2004 19:04 Uhr +0200 massimop@users.berlios.de wrote:
I'm trying to format a Date retrieved from MySQL, but while I can print the date as it is I can't access any of the methods, so no strftime() and no day() etc I get a 'not allowed in this context' exception so: <i tal:content="python:item.data"></i> is ok but: <i tal:content="python:item.data.day"></i> causes Zope to ask for authentication
No idea about mysql-python but I assume that is returns date fields from MySQL as DateTime instance to Zope and to your application. So you should be able to use the DateTime API. If 'data' in your example above should be a DateTime instance that you are accessing member variable *instead* of *calling* a DateTime *method*. -aj
On Mon, 2004-10-11 at 19:15 +0200, Andreas Jung wrote:
--On Montag, 11. Oktober 2004 19:04 Uhr +0200 massimop@users.berlios.de wrote:
I'm trying to format a Date retrieved from MySQL, but while I can print the date as it is I can't access any of the methods, so no strftime() and no day() etc I get a 'not allowed in this context' exception so: <i tal:content="python:item.data"></i> is ok but: <i tal:content="python:item.data.day"></i> causes Zope to ask for authentication
No idea about mysql-python but I assume that is returns date fields from MySQL as DateTime instance to Zope and to your application. So you should be able to use the DateTime API. If 'data' in your example above should be a DateTime instance that you are accessing member variable *instead* of *calling* a DateTime *method*.
-aj
I think is a DateTime instance because if I try any of his attributes or methods i get 'not allowed in this context' while if I try with an arbitrary attributes (say item.data.phony) I get Error Type: AttributeError Error Value: 'datetime.datetime' object has no attribute 'phony' so I suppose that item.data has an attribute day (according to DateTime documentation day is an 'Instance attribute') anyway, in case is significant, 'item.data' in the example returns a full specified datetime like: 2004-10-11 19:14:47 ... no idea.... thanks massimo
You could try writing a wee Python script. This one gets the date from the query: for result in context.theQuery(): print result.theDate.strftime('%H:%M, %a %d %b, %Y') return printed Then call the script from the template. Alternatively, you could pass the date string to the Python script. Actually, I call a dtml object from a template, and the dtml calls the script (because the dtml has some extra HTML layout and the python does the date formatting - so the template looks uncomplicated). Cliff massimop@users.berlios.de wrote:
I'm trying to format a Date retrieved from MySQL, but while I can print the date as it is I can't access any of the methods, so no strftime() and no day() etc I get a 'not allowed in this context' exception so: <i tal:content="python:item.data"></i> is ok but: <i tal:content="python:item.data.day"></i> causes Zope to ask for authentication
Zope 2.7.2, Python 2.3.4 here any hint would be appreciated
thanks massimo
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
On Mon, 2004-10-11 at 18:57 +0100, Cliff Ford wrote:
You could try writing a wee Python script. This one gets the date from the query:
for result in context.theQuery(): print result.theDate.strftime('%H:%M, %a %d %b, %Y') return printed
Then call the script from the template.
Alternatively, you could pass the date string to the Python script.
Actually, I call a dtml object from a template, and the dtml calls the script (because the dtml has some extra HTML layout and the python does the date formatting - so the template looks uncomplicated).
Cliff
following your suggestion (if I've understood) I tried this script: ## Script (Python) "formatdate_py" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters=d ##title= ## # Import a standard function, and get the HTML request and response objects. from Products.PythonScripts.standard import html_quote request = container.REQUEST RESPONSE = request.RESPONSE return d.day and then called it from a page template with: <i tal:content="python: here.formatdate_py(item.data)"></i> but it's sadly the same... <<You are not allowed to access 'day' in this context>> and it dosn't change if I pass 'item' to the script an access the field 'data' inside it thanks for your help massimo
Hi, Am Mo, den 11.10.2004 schrieb massimop@users.berlios.de um 20:13:
On Mon, 2004-10-11 at 18:57 +0100, Cliff Ford wrote:
You could try writing a wee Python script. This one gets the date from the query: ... ## Script (Python) "formatdate_py" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters=d ##title= ## # Import a standard function, and get the HTML request and response objects. from Products.PythonScripts.standard import html_quote request = container.REQUEST RESPONSE = request.RESPONSE
return d.day ^^^^ you have to call it anyway. E.g. d.day()
and then called it from a page template with:
<i tal:content="python: here.formatdate_py(item.data)"></i>
but it's sadly the same...
<<You are not allowed to access 'day' in this context>>
and it dosn't change if I pass 'item' to the script an access the field 'data' inside it
Looks a bit like the database adaptor you use has a bug here. Or did you perhaps screw up on permissions a bit? Regards Tino
On Mon, 2004-10-11 at 20:41 +0200, Tino Wildenhain wrote:
Hi,
Am Mo, den 11.10.2004 schrieb massimop@users.berlios.de um 20:13:
On Mon, 2004-10-11 at 18:57 +0100, Cliff Ford wrote:
You could try writing a wee Python script. This one gets the date from the query: ... ## Script (Python) "formatdate_py" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters=d ##title= ## # Import a standard function, and get the HTML request and response objects. from Products.PythonScripts.standard import html_quote request = container.REQUEST RESPONSE = request.RESPONSE
return d.day ^^^^ you have to call it anyway. E.g. d.day() ok anyway it makes no difference in this case I mean, same error
and then called it from a page template with:
<i tal:content="python: here.formatdate_py(item.data)"></i>
but it's sadly the same...
<<You are not allowed to access 'day' in this context>>
and it dosn't change if I pass 'item' to the script an access the field 'data' inside it
Looks a bit like the database adaptor you use has a bug here. Or did you perhaps screw up on permissions a bit? no wonder if it's the case, but where? I can't imagine....
Regards Tino
thanks massimo
Just to be clear that we are on the same wavelength, do this: Add a python script called test from the Add drop down list. Type d in the Parameter List box Put this and only this in the script textarea, replacing completely the default python script: return DateTime(d).strftime('%H:%M, %a %d %b, %Y') Save and test, typing 2004-10-11 in the Test Value box. It should give you the date. Now, in your page template make the call look like this: <i tal:content="python: here.test(d='2004-10-11')"></i> Test the page template. If that works, substitute the typed in date with the value that you are trying to display. If item.data is date string it should just work. If item.data is a more complex object then we are at cross purposes - but it should be because that us what MySQL stores. Cliff massimop@users.berlios.de wrote:
On Mon, 2004-10-11 at 18:57 +0100, Cliff Ford wrote:
You could try writing a wee Python script. This one gets the date from the query:
for result in context.theQuery(): print result.theDate.strftime('%H:%M, %a %d %b, %Y') return printed
Then call the script from the template.
Alternatively, you could pass the date string to the Python script.
Actually, I call a dtml object from a template, and the dtml calls the script (because the dtml has some extra HTML layout and the python does the date formatting - so the template looks uncomplicated).
Cliff
following your suggestion (if I've understood) I tried this script:
## Script (Python) "formatdate_py" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters=d ##title= ## # Import a standard function, and get the HTML request and response objects. from Products.PythonScripts.standard import html_quote request = container.REQUEST RESPONSE = request.RESPONSE
return d.day
and then called it from a page template with:
<i tal:content="python: here.formatdate_py(item.data)"></i>
but it's sadly the same...
<<You are not allowed to access 'day' in this context>>
and it dosn't change if I pass 'item' to the script an access the field 'data' inside it
thanks for your help massimo
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
On Mon, 2004-10-11 at 19:45 +0100, Cliff Ford wrote:
Just to be clear that we are on the same wavelength, do this: ok
Add a python script called test from the Add drop down list.
Type d in the Parameter List box
Put this and only this in the script textarea, replacing completely the default python script:
return DateTime(d).strftime('%H:%M, %a %d %b, %Y')
Save and test, typing 2004-10-11 in the Test Value box. It should give you the date. yes, it does...
Now, in your page template make the call look like this:
<i tal:content="python: here.test(d='2004-10-11')"></i>
Test the page template. If that works, yes
substitute the typed in date with the value that you are trying to display. If item.data is date string it should just work. If item.data is a more complex object then we are at cross purposes - but it should be because that us what MySQL stores.
this way? <i tal:content="python: here.test(d=item.data)"></i> uhm... I get Error Type: TypeError Error Value: int() argument must be a string or a number isn't funny? :)
Cliff
thanks massimo (sorry, I've accidentally answered directly... I'm getting confused:))
This bit of python: return DateTime(d).strftime('%H:%M, %a %d %b, %Y') giving this error:
Error Type: TypeError Error Value: int() argument must be a string or a number
means that your item.data is neither an integer nor a string. You could try to find out what it contains. For example, if you put in this temporary line you should get the content: return d return DateTime(d).strftime('%H:%M, %a %d %b, %Y') Cliff massimop@users.berlios.de wrote:
On Mon, 2004-10-11 at 19:45 +0100, Cliff Ford wrote:
Just to be clear that we are on the same wavelength, do this:
ok
Add a python script called test from the Add drop down list.
Type d in the Parameter List box
Put this and only this in the script textarea, replacing completely the default python script:
return DateTime(d).strftime('%H:%M, %a %d %b, %Y')
Save and test, typing 2004-10-11 in the Test Value box. It should give you the date.
yes, it does...
Now, in your page template make the call look like this:
<i tal:content="python: here.test(d='2004-10-11')"></i>
Test the page template. If that works,
yes
substitute the typed in date with the value that you are trying to display. If item.data is date string it should just work. If item.data is a more complex object then we are at cross purposes - but it should be because that us what MySQL stores.
this way? <i tal:content="python: here.test(d=item.data)"></i>
uhm... I get
Error Type: TypeError Error Value: int() argument must be a string or a number
isn't funny? :)
Cliff
thanks massimo
(sorry, I've accidentally answered directly... I'm getting confused:))
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
On Mon, 2004-10-11 at 20:59 +0100, Cliff Ford wrote:
This bit of python:
return DateTime(d).strftime('%H:%M, %a %d %b, %Y')
giving this error:
Error Type: TypeError Error Value: int() argument must be a string or a number
means that your item.data is neither an integer nor a string. You could try to find out what it contains. For example, if you put in this temporary line you should get the content:
return d this just returns the full date (es. 2004-10-11 19:14:47)
return DateTime(d).strftime('%H:%M, %a %d %b, %Y')
Cliff
thanks for your patience massimo
Try this in the python script: return DateTime(str(d)).strftime('%H:%M, %a %d %b, %Y') It just produces the string representation of object d (to create a DateTime object). If d were already a DateTime object you would use: return d.strftime('%H:%M, %a %d %b, %Y') By way of comfort: I spent a lot of time trying to figure out how to work with dates and have a lot of sympathy with anyone going through the same process. Cliff massimop@users.berlios.de wrote:
On Mon, 2004-10-11 at 20:59 +0100, Cliff Ford wrote:
This bit of python:
return DateTime(d).strftime('%H:%M, %a %d %b, %Y')
giving this error:
Error Type: TypeError Error Value: int() argument must be a string or a number
means that your item.data is neither an integer nor a string. You could try to find out what it contains. For example, if you put in this temporary line you should get the content:
return d
this just returns the full date (es. 2004-10-11 19:14:47)
return DateTime(d).strftime('%H:%M, %a %d %b, %Y')
Cliff
thanks for your patience massimo
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
On Mon, 2004-10-11 at 21:21 +0100, Cliff Ford wrote:
Try this in the python script:
return DateTime(str(d)).strftime('%H:%M, %a %d %b, %Y')
It just produces the string representation of object d (to create a DateTime object). If d were already a DateTime object you would use:
return d.strftime('%H:%M, %a %d %b, %Y')
By way of comfort: I spent a lot of time trying to figure out how to work with dates and have a lot of sympathy with anyone going through the same process.
Cliff
great! it worked! kinda tricky but... thanks again had much fun :) massimo
I am using this in a query to MySql... SELECT Job_id, DATE_FORMAT(Job_Date,"%d %b %Y") as Job_Date FROM Jobs ...etc... This allows me to retrieve Job_Date how ever I need it at the time. The format codes for MySql are similar to strftime in Python. Also, from within a python script I have used: today=DateTime().strftime("%d %b %Y") The DateTime() type should be available in your script environment, and I believe you can pass your data value to DateTime() and it will initialize to a valid Python datetime object. -- Les Ferguson Software developer Waitakere, NZ -- massimop@users.berlios.de wrote:
On Mon, 2004-10-11 at 18:57 +0100, Cliff Ford wrote:
You could try writing a wee Python script. This one gets the date from the query:
for result in context.theQuery(): print result.theDate.strftime('%H:%M, %a %d %b, %Y') return printed
Then call the script from the template.
Alternatively, you could pass the date string to the Python script.
Actually, I call a dtml object from a template, and the dtml calls the script (because the dtml has some extra HTML layout and the python does the date formatting - so the template looks uncomplicated).
Cliff
following your suggestion (if I've understood) I tried this script:
## Script (Python) "formatdate_py" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters=d ##title= ## # Import a standard function, and get the HTML request and response objects. from Products.PythonScripts.standard import html_quote request = container.REQUEST RESPONSE = request.RESPONSE
return d.day
and then called it from a page template with:
<i tal:content="python: here.formatdate_py(item.data)"></i>
but it's sadly the same...
<<You are not allowed to access 'day' in this context>>
and it dosn't change if I pass 'item' to the script an access the field 'data' inside it
thanks for your help massimo
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
On Tue, 2004-10-12 at 07:49 +1300, Les Ferguson wrote:
I am using this in a query to MySql...
SELECT Job_id, DATE_FORMAT(Job_Date,"%d %b %Y") as Job_Date FROM Jobs ...etc...
thanks, this looks like a good workaround (but now it's war, I have to know why I can't get it working the other way...)
This allows me to retrieve Job_Date how ever I need it at the time. The format codes for MySql are similar to strftime in Python. Also, from within a python script I have used:
today=DateTime().strftime("%d %b %Y")
The DateTime() type should be available in your script environment, and I believe you can pass your data value to DateTime() and it will initialize to a valid Python datetime object.
sigh... you can read the result in another post...
-- Les Ferguson Software developer Waitakere, NZ
thanks massimo
participants (5)
-
Andreas Jung -
Cliff Ford -
Les Ferguson -
massimop@users.berlios.de -
Tino Wildenhain