Re: [Zope3-Users] Implementing a DropdownWidget for a country-code vocabulary

2007-09-17 Thread Jesper Petersen
On 9/17/07, Hermann Himmelbauer <[EMAIL PROTECTED]> wrote:
>
> Am Samstag, 15. September 2007 18:08 schrieb Jesper Petersen:
> > Hello!I'm trying to implement a DropdownWidget for my country list. In
> my
> > app i'd like to have a dropdown menu where a user can choose a country
> (for
> > my main content object, a job). My vocabulary is created from a list of
> > strings, and stored in a local utility:
> >
> >
> > SimpleVocabulary.fromValues( [u'AD', u'AE', 'AF', ...] )
> >
> >
> > But in my dropdown widget i want to display the country name for each
> > option too.. and not just the same value on both the value attribute and
> > inside the element:
> >
> > Andorra
>
> Perhaps I misunderstand your question, but AFAIK vocabulary support this
> scenario. In my application, I also have a country drop-down that is in my
> case filled from a relational database. The vocabulary looks like this:
>
> def landVocabulary(obj):
>"""Get ktoids and ktonrs from session"""
>session = zalchemy.getSession()
>laender = session.query(Land)
>laender_terms = []
>for n, land in enumerate(laender):
>laender_terms.append(vocabulary.SimpleVocabulary.\
> createTerm(land.landid,
>n,
>land.name))
>return vocabulary.SimpleVocabulary(laender_terms)
>
> # Make function a vocabulary factory
> alsoProvides(landVocabulary, IVocabularyFactory)
>
> and in my configuration:
>
>component=".utils.landVocabulary"
>name="landVocabulary"
>/>
>
> Now I can use this vocabulary from any interface.
>
> Best regards,
> Hermann
>

Hello Hermann!Since the country names depend on the locale I don't want to
save the countryname on my object, only the country code. Hence, my utility
contains only country codes, ['AD', 'AF', ...].

If, in my DropdownWidget, I provide a vocabulary on the form [('AD',
'Andorra'), ('Af', 'Afghanistan'), ...] instead of the one in my utility,
the constraint will obviously fail when the form is submitted ('AD' does not
equal ('AD,'Andorra)).

In my case you can say that the countrynames are "dynamic" and merealy a UI
thing, whereas in your app you are retrieving a constant list of
countrynames, right?
Perhaps this should remove any misunderstandings you might have about my
earlier post. (This problem _can_ be solved with z3c.widget but it'd be nice
to get this working on my own)

My current solution is a quick hack: I save a list in
DropdownWidget.territories on the form [('AD', 'Andorra'), ('Af',
'Afghanistan'), ...] and then tal:repeat over it setting the -value
attribute to countrycode and put countryname inside ...

Regards
/Jesper
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Implementing a DropdownWidget for a country-code vocabulary

2007-09-17 Thread Hermann Himmelbauer
Am Samstag, 15. September 2007 18:08 schrieb Jesper Petersen:
> Hello!I'm trying to implement a DropdownWidget for my country list. In my
> app i'd like to have a dropdown menu where a user can choose a country (for
> my main content object, a job). My vocabulary is created from a list of
> strings, and stored in a local utility:
>
>
> SimpleVocabulary.fromValues( [u'AD', u'AE', 'AF', ...] )
>
>
> But in my dropdown widget i want to display the country name for each
> option too.. and not just the same value on both the value attribute and
> inside the element:
>
> Andorra

Perhaps I misunderstand your question, but AFAIK vocabulary support this 
scenario. In my application, I also have a country drop-down that is in my 
case filled from a relational database. The vocabulary looks like this:

def landVocabulary(obj):
"""Get ktoids and ktonrs from session"""
session = zalchemy.getSession()
laender = session.query(Land)
laender_terms = []
for n, land in enumerate(laender):
laender_terms.append(vocabulary.SimpleVocabulary.\
 createTerm(land.landid,
n,
land.name))
return vocabulary.SimpleVocabulary(laender_terms)

# Make function a vocabulary factory
alsoProvides(landVocabulary, IVocabularyFactory)

and in my configuration:



Now I can use this vocabulary from any interface.

Best regards,
Hermann

-- 
[EMAIL PROTECTED]
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Implementing a DropdownWidget for a country-code vocabulary

2007-09-15 Thread Jesper Petersen
Hello Adam,
Great, thanks

On 9/15/07, Adam Groszer <[EMAIL PROTECTED]> wrote:
>
>  Hello Jesper,
>
>
> That should be solved already. Look for z3c.widget.country.
>
> svn://svn.zope.org/repos/main/z3c.widget/trunk/src/z3c/widget/country
>
>
> Saturday, September 15, 2007, 6:08:19 PM, you wrote:
>
>
>  >
>
> Hello!
>
> I'm trying to implement a DropdownWidget for my country list. In my app
> i'd like to have a dropdown menu where a user can choose a country (for my
> main content object, a job). My vocabulary is created from a list of
> strings, and stored in a local utility:
>
>
>
>
> SimpleVocabulary.fromValues( [u'AD', u'AE', 'AF', ...] )
>
>
>
>
> But in my dropdown widget i want to display the country name for each
> option too.. and not just the same value on both the value attribute and
> inside the element:
>
>
> Andorra
>
>
> So i send in a different vocabulary.. one that looks like this:
>
>
> [(u'AD', u'Andorra'),...]
>
>
> So i coded this:
>
>
> -
>
>  class CountryDropDown(DropdownWidget):
>
>
>  def __init__(self, field, request):
>
>  #This returns a dict: {'countrycode':'countryname'}
>
>  territories = request.locale.displayNames.territories
>
>
>
>  # Invert the territories dict: keys=names, values=ccodes
>
>  self.territories = sorted(territories.items (),
> key=itemgetter(1))
>
>
>  #not using this any longer..
>
>  #voc = getUtility(IVocabularyFactory, u'Country codes')
>
>
>
>  voc = SimpleVocabulary.fromItems(self.territories)
>
>  super(CountryDropDown, self).__init__(field, voc, request)
>
>
>
>  def textForValue(self, term):
>
>  return term.value
>
> -
>
>
> But it seems like when i choose a country, e.g Andorra, the constraint
> fails. I think it is because my modified vocabulary that i sent in which it
> is now checking constraints against fails.
>
>
> I want the constraints to be checked in my ordinary vocabulary but i want
> to use a different one when populating the dropdown.. any ideas?
>
>
>
> --
>
> Instant Foo - Naturally Flavoured
>
>
>
>
>
> --
>
> Best regards,
>
>  Adammailto:[EMAIL PROTECTED]<[EMAIL PROTECTED]>
>
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Implementing a DropdownWidget for a country-code vocabulary

2007-09-15 Thread Adam Groszer




Hello Jesper,

That should be solved already. Look for z3c.widget.country.
svn://svn.zope.org/repos/main/z3c.widget/trunk/src/z3c/widget/country

Saturday, September 15, 2007, 6:08:19 PM, you wrote:




>


Hello!
I'm trying to implement a DropdownWidget for my country list. In my app i'd like to have a dropdown menu where a user can choose a country (for my main content object, a job). My vocabulary is created from a list of strings, and stored in a local utility: 

 
SimpleVocabulary.fromValues( [u'AD', u'AE', 'AF', ...] )

 
But in my dropdown widget i want to display the country name for each option too.. and not just the same value on both the value attribute and inside the element: 

Andorra

So i send in a different vocabulary.. one that looks like this: 

[(u'AD', u'Andorra'),...]

So i coded this:

-
 class CountryDropDown(DropdownWidget):

     def __init__(self, field, request):
         #This returns a dict: {'countrycode':'countryname'} 
         territories = request.locale.displayNames.territories
 
         # Invert the territories dict: keys=names, values=ccodes
         self.territories = sorted(territories.items (), key=itemgetter(1))

         #not using this any longer..
         #voc = getUtility(IVocabularyFactory, u'Country codes')
       
         voc = SimpleVocabulary.fromItems(self.territories)
         super(CountryDropDown, self).__init__(field, voc, request)
 
     def textForValue(self, term):
         return term.value
-

But it seems like when i choose a country, e.g Andorra, the constraint fails. I think it is because my modified vocabulary that i sent in which it is now checking constraints against fails. 

I want the constraints to be checked in my ordinary vocabulary but i want to use a different one when populating the dropdown.. any ideas?


--
Instant Foo - Naturally Flavoured








-- 
Best regards,
 Adam                            mailto:[EMAIL PROTECTED]



___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users