Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. Schema in ICourseFolder:
  2.  
  3. section = schema.Choice(
  4. title=_(u'Organization, unit or study-subject in Korppi'),
  5. vocabulary=u"jyu.opetusohjelma.Sections",
  6. required=False)
  7.  
  8.  
  9. # Vocabulary for the schema
  10. class SectionsVocabulary(object):
  11. grok.implements(IVocabularyFactory)
  12.  
  13. def __call__(self, context):
  14. terms = []
  15. sections = KorppiQueryUtility().getOrganizations()
  16. for section in sections:
  17. # Section[1] is orgname
  18. terms.append(SimpleVocabulary.createTerm(section[1]))
  19. return SimpleVocabulary(terms)
  20. grok.global_utility(SectionsVocabulary, name=u"jyu.opetusohjelma.Sections")
  21.  
  22.  
  23. def getOrganizations():
  24. query = urllib.urlopen(KORPPI_URL).read()
  25. import json
  26. korppiOrgs = json.loads(query)
  27. # strings in korppiOrgs are unicode
  28.  
  29. organizations = []
  30. for org in korppiOrgs['organisations']:
  31. orgid = org['id']
  32. orgname = org['name'] + ' (ID: %s)' % (orgid)
  33. organizations.append((orgid, orgname.encode('utf8')))
  34. return organizations
  35.  
  36. Gives us UnicodeDecodeError:
  37.  
  38. Module zope.tal.talinterpreter, line 518, in do_optTag
  39. Module zope.tal.talinterpreter, line 513, in no_tag
  40. Module zope.tal.talinterpreter, line 343, in interpret
  41. Module zope.tal.talinterpreter, line 852, in do_condition
  42. Module zope.tal.talinterpreter, line 343, in interpret
  43. Module zope.tal.talinterpreter, line 405, in do_startTag
  44. Module zope.tal.talinterpreter, line 502, in attrAction_tal
  45. UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 12: ordinal not in range(128)
  46.  
  47. If we do not encode, the Vocabulary will crash:
  48. Module Products.Five.schema, line 36, in get
  49. Module jyu.opetusohjelma.vocabularies, line 51, in __call__
  50. Module zope.schema.vocabulary, line 109, in createTerm
  51. Module zope.schema.vocabulary, line 39, in __init__
  52. UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 11: ordinal not in range(128)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement