Dexterity Content Type DateTime Issue & How to modify Content Type Values - dexterity

First of all: I am running a Plonce Plone 4.3 (4305) instance with Dexterity Content Types 2.0.7
My approach is to write a Python Script (added via ZMI) which creates my dexterity content type using the methods invokeFactory(...) or typestool.constructContent(..) described here: http://plone.org/documentation/kb/add-content-programmatically (I've written two scripts which do the same task but use different methods - for learning purposes)
Everything is working fine, except when i try to add a DateTime Object to the constructor of both methods above to create my Content Type. The Date field strangly only updates the day and year values. Because of the restrictions in importing libraries inside python i am stuck (with my current knowledge) with this code:
d = DateTime('12/12/2013')
My script returns the date object after completion which looks like this:
2013/12/12 00:00:00 GMT+1
I've written another small script which outputs the Date value after construction and it gives me the same result (which seems to be correct). The resulting Content Type has its day and year field updated correctly but the month value stays on January and raises the following TypeError upon viewing:
TypeError: int() argument must be a string or a number, not 'instancemethod'
I can fix this by editing the month value manually which is not exactly what i want. I guess it its a minor problem with my DateTime object but i am running out of ideas at this point (overall documentation seems to be a bit scattered). I've tried various date formats inside the DateTime constructor but without luck.
I am also not sure how to modifiy my objects custom field values. Plone seems only to provide setTitle() and setDescription() methods. Maybe someone has a good hint.
Thanky you all in advance,
regards

not sure about the dateTime issue, but check out indexes, indexing and custom index to figure out setting titles and description.
to set your title for instance
#indexer(IFormName)
def titleIndexer(obj):
return obj.valueFromForm
grok.global_adapter(titleIndexer, name="Title")

AFAIK, the problem is that DateTime field for dexterity types needs a datetime object, not a DateTime object.
In the invokeFactory, you should pass a datetime object instead of a DateTime object
>>>date=datetime.datetime(2011,1,1)
>>>myobj=target.invokeFactory(type_name="content_type_name", id=id, date=date)
>>from DateTime import DateTime
>>DateTime().month()
7
>>from datetime import datetime
>>datetime.now().month
7
>>datetime.now().month()
TypeError: 'int' object is not callable
>>> myobj.date
datetime.datetime(2013, 7, 26, 0, 0)
>>> myobj.date.month
7
>>> myobj.date.month()
TypeError: 'int' object is not callable

Related

How to add properties with a specific type into gremlinpython?

Currently i am trying to import via gremlinpython a large graph from igraph programmatically . I am rather new to gremlin and the endpoints i may use it with. The problem i currently face is that a property in a node/edge can have multiple types. (E.g.: -> Bool or None-type | Int, Long, etc..)
I've noticed no error when importing it into this gremlin-server (is this called Apache TinkerGraph-Server? How should i call this?). It seems that the types of same properties can be arbitrary.
However, when using JanusGraph i receive multiple errors:
gremlin_python.driver.protocol.GremlinServerError: 500: Value [XXX] is not an instance of the expected data type for property key [YYY] and cannot be converted. Expected: class <SomeClass>, found: class <SomeOtherClass>
E.g. executing:
conn = DriverRemoteConnection("ws://localhost:8182/gremlin", "g")
remote_graph = traversal().withRemote(conn)
remote_graph.addV().property("test", 10000).next()
remote_graph.addV().property("test", 100000000000000000000000).next() # <- Causes an error on JanusGraph
It is possible for me to cast some properties into other datatypes (Bool/None-Type-> -1,0,1), so i can avoid this error. But i am not sure how i should handle the above provided example. Is there a way to explicitly set the type (for at least numeric types) of a property, so that the server knows to store it e.g. as a Long/BigInt instead of as a Int? Especially since in python3 there is no distincion between long(/bigint) and int anymore.
So specifically is there something like the following?:
E.g. executing:
remote_graph.addV().property("test", 10000).asLong().next()
remote_graph.addV().property("test", 10000, <Type: Long>).next()
Gremlin does have a special class for ensuring a Java Long. You can just do long(10000) given the appropriate import like: from gremlin_python.statics import long

Why does AliceBundle Fixture DateTime give me an unexpected value?

I'm currently working on a project in which I create fixtures with Alice-bundle to run tests to make sure my API-endpoints work properly. Everything works fine, except for the DateTime properties.
No matter what string I pass it, eg: <dateTime('2019-09-23 14:00:00')>, it always gives me the wrong date and time, usually something like: 1998-10-25T14:29:45+01:00.
Even using <dateTime('now')> does not work -- it gives me some pre-2000s date & time as well, while that's exactly what some examples I'd found do use.
A fixture could look something like this:
Path\To\Task\Entity:
my_task:
title: 'My tasks'
description: 'These are all the tasks just for me!!!'
startsAt: <dateTime('now')>
endsAt: <dateTime('now')>
createdBy: '#some_higher_user'
Ideally I just want to pass it a string so I can define both a Date and Time and make sure it works properly, in the right format.
And help would be appreciated.
Looking here https://github.com/nelmio/alice/blob/master/doc/advanced-guide.md#functions we read:
function can be a Faker or a PHP native (or registered in the global scope) function.
So I would recommend trying a PHP native function that creates a \DateTime object
<date_create_from_format ( 'Y-m-d H:i:s' , '2019-09-23 14:00:00')>
// or
<date_create('now')>
That's how it works. The <dateTime()> function takes an argument called $max. So what it does is create a date between a starting date (not sure which one, probably something in the 1900 range or so) and that $max argument.
I guess you will want to use <dateTimeBetween()> which takes a startDate and an endDate to create a fake date between them. So I suppose if startDate = endDate, then you'll get the desired fixed date.
Take a look at fzaninotto/Faker library documentation. It's the library used by AliceBundle to actually fake data. There you can see what DateTime related functions you can use.

Vaadin Datefield doesn't recognize weeks in SimpleDateTime formats

My core problem is that I want to show the year's week number and the year itself in a Vaadin date-field. For example the user picks the 6th February 2018. The date-field should format its value like "06 2018".
As I understood the documentation of Vaadin 7.7.13, this should easily possible with:
public class CalendarWeekField extends DateField {
public CalendarWeekField() {
super();
this.setLocale(UI.getCurrent().getLocale());
this.setDateFormat("ww yyyy");
this.setShowISOWeekNumbers(true);
}
Please recognize the line this.setDateFormat("ww yyyy"); since it contains the actual "magic".
The date and time are normally displayed according to the default
format for the current locale (see Locale). You can specify a custom
format with setDateFormat(). It takes a format string that follows the
format of the SimpleDateFormat in Java.
However, following the above example, the output of the DateField is only " 2018".
What do I need to consider to get the year's week numbers displayed?
Consulted documentations:
Java SimpleDateFormat
Vaadin Date and Time Input with DateField - Date and Time Format
Vaadin 7.7.13 API Documentation DateField - setDateFormat(...)
I would call this a bug, your code is fine. You can even "fiddle around" with their live sampler (hit properties -> date format), the field excepts most date patterns but ww (and apparently some timezone-properties). File a bug report maybe?
Maybe you can walk around it by applying a change listener and using .setText() yourself? You would not need to extend the DateField in that case (actually there is no need to extend the field in the first place, simply create and set your properties).

Typo3 6.2.15 DateTime off by 1 hour

I have a model with a DateTime column named "fromDate":
/**
* fromDate
*
* #var \DateTime
*/
protected $fromDate;
now I need this date to do some calculations, but when I try outputting it, it seems to be off exactly 1 hour.
echo $model->getFromDate()->format('Y-m-d H:i:s');
This returns 2016-02-11 01:00:00 instead of 2016-02-11 00:00:00
I checked my server timezone, date_default_timezone_get() returns "Europe/Berlin" (which is correct). I tried to change the typo3 serverTimeZone (I tried 0 and 1, but doesn't change anything).
If I look at the database with phpmyadmin, the entry is "2016-02-11 00:00:00"
What am I missing, why is this happening? Any hints? Because I feel like I'm going insane, a huge calculation script is based on the correctness of $fromDate ... Any help would be greatly appreciated. Thank you!
Try something like
$myDate = $model->getFromDate();
$myDate->setTimezone(new \DateTimeZone('UTC'));
or whatever time zone you use in your raw database table value, if not UTC.
In TYPO3 6.2, at least, doing a findAll() on your repository begins a series of calls -- to \TYPO3\CMS\Extbase\Persistence\Repository->createQuery(), and afterward to \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult->rewind(). rewind() calls initialize(), which calls \TYPO3\CMS\Extbase\Persistence\Generic\Mapper->map(). map() calls mapSingleRow(), which then calls thawProperties() in the same class.
TYPO3\CMS\Extbase\Persistence\Generic\Mapper->thawProperties() has a "switch case" code section based on each property data type. If the type is DateTime, thawProperties() calls mapDateTime() in the same class.
mapDateTime() assumes "native date/datetime values are stored in UTC", so it applies the 'UTC' DateTimeZone to the raw value. But then, mapDateTime() applies the DateTimeZone from the PHP date_default_timezone_get() function, and returns the altered date and timezone property values to thawProperties() and eventually to your Extbase extension.
You can find details in the TYPO3 6.2 API reference for mapDateTime() and its definition in DataMapper.php.
My answer is related to this StackOverflow answer, this TYPO3 Forum thread, and this TYPO3 Forum thread.
This is a problem in the DateTime handling of Extbase, which was recently fixed for TYPO3 7.6 and 8 (dates were not interpreted as UTC). We won’t backport the fix for 6.2 officially, but you can of course apply it yourself.
If you don’t want to patch the core, you can also create an extension class for the relevant part (DataMapper) and override the method (mapDateTime()).

XPages: create current Datetime in ssjs and save to a Datetime Notes form field

The only way i have found to store date in a Datetime field in a notes form is this:
theDoc2.replaceItemValue("lastAccess",session.createDateTime("Today"));
But this only creates a Date, not DateTime. Also, i dont want to create a static time like "Today 12" but i want the current datetime dynamicly.
Using this i get an error (Exception occurred calling method NotesDocument.replaceItemValue(string, Date) null):
theDoc2.replaceItemValue("lastAccess",#Now());
and using this, the form field changes from Date/Time to Text data type and i want to keep Date/Time type:
theDoc2.replaceItemValue("lastAccess",#Now().toLocaleString);
Any ideas?
Just gave it a try:
as you wrote, .replaceItemValue("fieldName", #Now()) throws an error.
However, I got it to work with
.replaceItemValue("fieldName", session.createDateTime(#Now()))
In that case the value is stored in the Notes field as Time/Date with all necessary components as in
17.01.2014 12:45:51 CET
From what I can see, difference between the two is that #Now() returns a Date data type, whereas session.createDateTime() returns a NotesDateTime object
On the other hand, for me it's also working with your original method:
session.createDateTime("Today")
Don't know what's causing the prob on your side; do you have an editable represantion of the field on you xpage? If so, does it have some kind of converter enabled which could do some filtering during submit?
i will answer my own question as i found a way. Please comment if you think it is not correct or best practice...
theDoc2.replaceItemValue("lastAccess",session.createDateTime("Today"+#Now().toLocaleTimeString()));
A little late, but I had the same problem, but this method resolved it:
DateTime datumtijd = session.createDateTime("Today");
datumtijd.setNow(); //!!!!!!
System.out.println((datumtijd).toString());
Hope it helps :)

Resources