Dexterity: Form Field Multiple-Choice created from Parent not initialized on Object Creation

classic Classic list List threaded Threaded
5 messages Options
andreasma andreasma
Reply | Threaded
Open this post in threaded view
|

Dexterity: Form Field Multiple-Choice created from Parent not initialized on Object Creation

Hi,

I've created a Plone add-on with Dexterity content types. The parent
content type contains a field that is filled with a list (lines) for
categories. The field gets a default value but the site admin could edit
the field and overwrite every value:

    available_category = schema.List(title=_(u"Available Categories"),
        default=['Accounting -Templates',
                 'Agenda-Templates',
                 (...),]
    value_type=schema.TextLine())


I have in my child type the following declaration to get the content
from the parent:


@grok.provider(schema.interfaces.IContextSourceBinder)
def vocabCategories(context):
    category_list = getattr(context.__parent__, 'available_category', [])
    terms = []
    for value in category_list:
        terms.append(SimpleTerm(value,
token=value.encode('unicode_escape'), title=value))
    return SimpleVocabulary(terms)


The form field in the child looks like this:


    dexteritytextindexer.searchable('category_choice')
    category_choice = schema.List(
        title=_(u"Choose your categories"),
        value_type=schema.Choice(source=vocabCategories),
        required=True,
    )

I have spotted this behavior for other such fields in my Dexterity
add-on. Is there a way to trigger the initialization of the field during
the creation of the object?

My add-on project is hosted on github.com:
https://github.com/andreasma/tdf.templateuploadcenter

The both files I refer to are this:
https://github.com/andreasma/tdf.templateuploadcenter/blob/master/src/tdf/templateuploadcenter/tucenter.py
https://github.com/andreasma/tdf.templateuploadcenter/blob/master/src/tdf/templateuploadcenter/tuproject.py

Thanks for any hints
Andreas

--
## Developer LibreOffice
## Freie Office-Suite für Linux, Mac, Windows
## http://LibreOffice.org
## Support the Document Foundation (http://documentfoundation.org)
## Meine Seite: http://www.amantke.de 


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users
Jian Aijun Jian Aijun
Reply | Threaded
Open this post in threaded view
|

Re: Dexterity: Form Field Multiple-Choice created from Parent not initialized on Object Creation

@grok.provider(schema.interfaces.IContextSourceBinder)
def vocabCategories(context):
    category_list = getattr(context.__parent__, 'available_category', [])

def vocabCategories(context):
    category_list = getattr(context, 'available_category', [])

note: here the "context" value may be populated from the "context", For add forms, that’s the containing folder; for other forms, it is normally a content object being edited or displayed.


2013/1/28 Andreas Mantke <[hidden email]>
Hi,

I've created a Plone add-on with Dexterity content types. The parent
content type contains a field that is filled with a list (lines) for
categories. The field gets a default value but the site admin could edit
the field and overwrite every value:

    available_category = schema.List(title=_(u"Available Categories"),
        default=['Accounting -Templates',
                 'Agenda-Templates',
                 (...),]
    value_type=schema.TextLine())


I have in my child type the following declaration to get the content
from the parent:


@grok.provider(schema.interfaces.IContextSourceBinder)
def vocabCategories(context):
    category_list = getattr(context.__parent__, 'available_category', [])
    terms = []
    for value in category_list:
        terms.append(SimpleTerm(value,
token=value.encode('unicode_escape'), title=value))
    return SimpleVocabulary(terms)


The form field in the child looks like this:


    dexteritytextindexer.searchable('category_choice')
    category_choice = schema.List(
        title=_(u"Choose your categories"),
        value_type=schema.Choice(source=vocabCategories),
        required=True,
    )

I have spotted this behavior for other such fields in my Dexterity
add-on. Is there a way to trigger the initialization of the field during
the creation of the object?

My add-on project is hosted on github.com:
https://github.com/andreasma/tdf.templateuploadcenter

The both files I refer to are this:
https://github.com/andreasma/tdf.templateuploadcenter/blob/master/src/tdf/templateuploadcenter/tucenter.py
https://github.com/andreasma/tdf.templateuploadcenter/blob/master/src/tdf/templateuploadcenter/tuproject.py

Thanks for any hints
Andreas

--
## Developer LibreOffice
## Freie Office-Suite für Linux, Mac, Windows
## http://LibreOffice.org
## Support the Document Foundation (http://documentfoundation.org)
## Meine Seite: http://www.amantke.de


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users
Jian Aijun Jian Aijun
Reply | Threaded
Open this post in threaded view
|

Re: Dexterity: Form Field Multiple-Choice created from Parent not initialized on Object Creation

Whole:

@grok.provider(schema.interfaces.IContextSourceBinder)
def vocabCategories(context):
    # For add forms

    # For other forms edited or displayed
    from tdf.templateuploadcenter.tucenter import ITUCenter
    while context is not None and not ITUCenter.providedBy(context):
        #context = aq_parent(aq_inner(context))
        context = context.__parent__

    category_list = []
    if context is not None and context.available_category:
        category_list = context.available_category
    
    terms = []
    for value in category_list:
        terms.append(SimpleTerm(value, token=value.encode('unicode_escape'), title=value))
        
    return SimpleVocabulary(terms)


2013/1/28 Jian Aijun <[hidden email]>
@grok.provider(schema.interfaces.IContextSourceBinder)
def vocabCategories(context):
    category_list = getattr(context.__parent__, 'available_category', [])

def vocabCategories(context):
    category_list = getattr(context, 'available_category', [])

note: here the "context" value may be populated from the "context", For add forms, that’s the containing folder; for other forms, it is normally a content object being edited or displayed.


2013/1/28 Andreas Mantke <[hidden email]>
Hi,

I've created a Plone add-on with Dexterity content types. The parent
content type contains a field that is filled with a list (lines) for
categories. The field gets a default value but the site admin could edit
the field and overwrite every value:

    available_category = schema.List(title=_(u"Available Categories"),
        default=['Accounting -Templates',
                 'Agenda-Templates',
                 (...),]
    value_type=schema.TextLine())


I have in my child type the following declaration to get the content
from the parent:


@grok.provider(schema.interfaces.IContextSourceBinder)
def vocabCategories(context):
    category_list = getattr(context.__parent__, 'available_category', [])
    terms = []
    for value in category_list:
        terms.append(SimpleTerm(value,
token=value.encode('unicode_escape'), title=value))
    return SimpleVocabulary(terms)


The form field in the child looks like this:


    dexteritytextindexer.searchable('category_choice')
    category_choice = schema.List(
        title=_(u"Choose your categories"),
        value_type=schema.Choice(source=vocabCategories),
        required=True,
    )

I have spotted this behavior for other such fields in my Dexterity
add-on. Is there a way to trigger the initialization of the field during
the creation of the object?

My add-on project is hosted on github.com:
https://github.com/andreasma/tdf.templateuploadcenter

The both files I refer to are this:
https://github.com/andreasma/tdf.templateuploadcenter/blob/master/src/tdf/templateuploadcenter/tucenter.py
https://github.com/andreasma/tdf.templateuploadcenter/blob/master/src/tdf/templateuploadcenter/tuproject.py

Thanks for any hints
Andreas

--
## Developer LibreOffice
## Freie Office-Suite für Linux, Mac, Windows
## http://LibreOffice.org
## Support the Document Foundation (http://documentfoundation.org)
## Meine Seite: http://www.amantke.de


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users



------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users
Jian Aijun Jian Aijun
Reply | Threaded
Open this post in threaded view
|

Re: Dexterity: Form Field Multiple-Choice created from Parent not initialized on Object Creation

Whole:

@grok.provider(schema.interfaces.IContextSourceBinder)
def vocabCategories(context):
    # For add forms

    # For other forms edited or displayed
    from tdf.templateuploadcenter.tucenter import ITUCenter
    while context is not None and not ITUCenter.providedBy(context):
        #context = aq_parent(aq_inner(context))
        context = context.__parent__

    category_list = []
    if context is not None and context.available_category:
        category_list = context.available_category

    terms = []
    for value in category_list:
        terms.append(SimpleTerm(value, token=value.encode('unicode_escape'), title=value))
        
    return SimpleVocabulary(terms)


2013/1/28 Jian Aijun <[hidden email]>
Whole:

@grok.provider(schema.interfaces.IContextSourceBinder)
def vocabCategories(context):
    # For add forms

    # For other forms edited or displayed
    from tdf.templateuploadcenter.tucenter import ITUCenter
    while context is not None and not ITUCenter.providedBy(context):
        #context = aq_parent(aq_inner(context))
        context = context.__parent__

    category_list = []
    if context is not None and context.available_category:
        category_list = context.available_category
    
    terms = []
    for value in category_list:
        terms.append(SimpleTerm(value, token=value.encode('unicode_escape'), title=value))
        
    return SimpleVocabulary(terms)


2013/1/28 Jian Aijun <[hidden email]>
@grok.provider(schema.interfaces.IContextSourceBinder)
def vocabCategories(context):
    category_list = getattr(context.__parent__, 'available_category', [])

def vocabCategories(context):
    category_list = getattr(context, 'available_category', [])

note: here the "context" value may be populated from the "context", For add forms, that’s the containing folder; for other forms, it is normally a content object being edited or displayed.


2013/1/28 Andreas Mantke <[hidden email]>
Hi,

I've created a Plone add-on with Dexterity content types. The parent
content type contains a field that is filled with a list (lines) for
categories. The field gets a default value but the site admin could edit
the field and overwrite every value:

    available_category = schema.List(title=_(u"Available Categories"),
        default=['Accounting -Templates',
                 'Agenda-Templates',
                 (...),]
    value_type=schema.TextLine())


I have in my child type the following declaration to get the content
from the parent:


@grok.provider(schema.interfaces.IContextSourceBinder)
def vocabCategories(context):
    category_list = getattr(context.__parent__, 'available_category', [])
    terms = []
    for value in category_list:
        terms.append(SimpleTerm(value,
token=value.encode('unicode_escape'), title=value))
    return SimpleVocabulary(terms)


The form field in the child looks like this:


    dexteritytextindexer.searchable('category_choice')
    category_choice = schema.List(
        title=_(u"Choose your categories"),
        value_type=schema.Choice(source=vocabCategories),
        required=True,
    )

I have spotted this behavior for other such fields in my Dexterity
add-on. Is there a way to trigger the initialization of the field during
the creation of the object?

My add-on project is hosted on github.com:
https://github.com/andreasma/tdf.templateuploadcenter

The both files I refer to are this:
https://github.com/andreasma/tdf.templateuploadcenter/blob/master/src/tdf/templateuploadcenter/tucenter.py
https://github.com/andreasma/tdf.templateuploadcenter/blob/master/src/tdf/templateuploadcenter/tuproject.py

Thanks for any hints
Andreas

--
## Developer LibreOffice
## Freie Office-Suite für Linux, Mac, Windows
## http://LibreOffice.org
## Support the Document Foundation (http://documentfoundation.org)
## Meine Seite: http://www.amantke.de


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users




------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users
andreasma andreasma
Reply | Threaded
Open this post in threaded view
|

Re: Dexterity: Form Field Multiple-Choice created from Parent not initialized on Object Creation

In reply to this post by Jian Aijun
Hi Jian,

Am 28.01.2013 16:37, schrieb Jian Aijun:
Whole:

@grok.provider(schema.interfaces.IContextSourceBinder)
def vocabCategories(context):
    # For add forms

    # For other forms edited or displayed
    from tdf.templateuploadcenter.tucenter import ITUCenter
    while context is not None and not ITUCenter.providedBy(context):
        #context = aq_parent(aq_inner(context))
        context = context.__parent__

    category_list = []
    if context is not None and context.available_category:
        category_list = context.available_category
    
    terms = []
    for value in category_list:
        terms.append(SimpleTerm(value, token=value.encode('unicode_escape'), title=value))
        
    return SimpleVocabulary(terms)



thank you very much. Worked perfect.

Best regards,
Andreas
-- 
## Developer LibreOffice
## Freie Office-Suite für Linux, Mac, Windows
## http://LibreOffice.org
## Support the Document Foundation (http://documentfoundation.org)
## Meine Seite: http://www.amantke.de 

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
Plone-Users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/plone-users