Skip to content

Commit

Permalink
Change field in behaviors: they are now relation fields to be correct…
Browse files Browse the repository at this point in the history
…ly serialized in plone.restapi
  • Loading branch information
cekk authored and thet committed May 7, 2021
1 parent d34187a commit 37f9698
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 96 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Changelog
- Fallback to default event field value in accessor, if location reference is not set.
[cekk]

- Change field in behaviors: they are now relation fields to be correctly serialized in plone.restapi.
[cekk]


4.1 (2020-07-10)
----------------
Expand Down
23 changes: 11 additions & 12 deletions collective/venue/behaviors.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from zope import schema
from zope.interface import provider
from zope.schema.interfaces import IContextAwareDefaultFactory
from z3c.relationfield.schema import RelationChoice


@provider(IContextAwareDefaultFactory)
Expand All @@ -25,25 +26,24 @@ def default_location(context):
@provider(IFormFieldProvider)
class ILocation(model.Schema, IVenueEnabled, IDXEvent):

location_uid = schema.Choice(
location_ref = RelationChoice(
title=_(u'label_event_location', default=u'Location'),
description=_(
u'description_event_location', default=u'Select a location.'
),
required=False,
missing_value='',
defaultFactory=default_location,
# defaultFactory=default_location,
vocabulary='plone.app.vocabularies.Catalog',
)
form.widget(
'location_uid',
'location_ref',
RelatedItemsFieldWidget,
vocabulary='plone.app.vocabularies.Catalog',
pattern_options={
'selectableTypes': ['Venue'],
'basePath': get_base_path,
# 'basePath': get_base_path,
},
)

location_notes = schema.Text(
title=_(u'label_event_location_notes', default=u'Location notes'),
description=_(
Expand All @@ -56,7 +56,7 @@ class ILocation(model.Schema, IVenueEnabled, IDXEvent):
directives.fieldset(
'venue',
label=_(u'fieldset_venue'),
fields=['location_uid', 'location_notes'],
fields=['location_ref', 'location_notes'],
)


Expand All @@ -73,18 +73,17 @@ def default_organizer(context):
@provider(IFormFieldProvider)
class IOrganizer(model.Schema, IVenueEnabled):

organizer_uid = schema.Choice(
organizer_ref = RelationChoice(
title=_(u'label_event_organizer', default=u'Organizer'),
description=_(
u'description_event_organizer', default=u'Select an organizer.'
),
required=False,
missing_value='',
defaultFactory=default_organizer,
# defaultFactory=default_organizer,
vocabulary='plone.app.vocabularies.Catalog',
)
form.widget(
'organizer_uid',
'organizer_ref',
RelatedItemsFieldWidget,
pattern_options={
'selectableTypes': ['Venue'],
Expand All @@ -104,6 +103,6 @@ class IOrganizer(model.Schema, IVenueEnabled):
directives.fieldset(
'venue',
label=_(u'fieldset_venue'),
fields=['organizer_uid', 'organizer_notes'],
fields=['organizer_ref', 'organizer_notes'],
order=10,
)
10 changes: 9 additions & 1 deletion collective/venue/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,15 @@
handler="collective.venue.upgrades.upgrade_registry"
profile="collective.venue:base"
/>


<genericsetup:upgradeStep
title="Migrate fields"
description="Migrate reference fields."
source="5"
destination="6"
handler="collective.venue.upgrades.upgrade_5_to_6"
profile="collective.venue:base"
/>

<utility
factory=".setuphandlers.HiddenProfiles"
Expand Down
14 changes: 8 additions & 6 deletions collective/venue/eventaccessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ def default_location(self):
@property
def location(self):
context = self.context
location_ref = ILocation(context, None)
location_behavior = ILocation(context, None)
if not location_behavior:
return self.default_location
location_ref = location_behavior.location_ref
if not location_ref:
return self.default_location
location_uid = location_ref.location_uid
if not location_uid:
location_notes = location_behavior.location_notes
location = location_ref.to_object
if not location:
return self.default_location
location_notes = location_ref.location_notes
location = uuidToObject(location_uid)

meta_basic = IBasic(location, None)
add = IAddress(location, None)
Expand All @@ -64,7 +66,7 @@ def location(self):
if site_path not in location_path:
# location in different site - cannot directly open it
location_url = u'{0}/@@venue_view?uid={1}'.format(
site.absolute_url(), location_uid
site.absolute_url(), location.UID()
)

country = get_pycountry_name(add.country)
Expand Down
15 changes: 7 additions & 8 deletions collective/venue/geolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@


def get_location_ref(obj):
location_ref = ILocation(obj, None)
if location_ref:
location_uid = location_ref.location_uid
return uuidToObject(location_uid)
location_behavior = ILocation(obj, None)
if location_behavior:
location_ref = location_behavior.location_ref
if location_ref:
return location_ref.to_object
return None


@indexer(ILocation)
Expand All @@ -30,7 +32,6 @@ def longitude(obj):
@adapter(ILocation)
@implementer(IGeoJSONProperties)
class GeoJSONProperties(object):

def __init__(self, context):
self.context = context

Expand All @@ -44,9 +45,7 @@ def popup(self):
<header><a href="{0}">{1}</a></header>
<p>{2}</p>
""".format(
self.context.absolute_url(),
self.context.title,
location.title,
self.context.absolute_url(), self.context.title, location.title
)

@property
Expand Down
48 changes: 27 additions & 21 deletions collective/venue/icalexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,25 @@ def location(self):
if not ILocation.providedBy(self.context):
return super(VenueICalendarEventComponent, self).location

ref = ILocation(self.context)
item = uuidToObject(ref.location_uid)
location_behavior = ILocation(self.context)
location_ref = location_behavior.location_ref
if location_ref:
item = location_ref.to_object
else:
item = None

ret = None
if item:
basic = IBasic(item, None)
locationstring = utils.join_nonempty([
basic.title,
utils.get_venue_address_string(item)
], sep=u', ')
locationstring = utils.join_nonempty(
[basic.title, utils.get_venue_address_string(item)], sep=u', '
)
ret = {
'value': locationstring,
'parameters': {'altrep': item.absolute_url()}
'parameters': {'altrep': item.absolute_url()},
}
else:
ret = {'value': ref.location_notes}
ret = {'value': location_behavior.location_notes}

return ret

Expand All @@ -48,19 +51,21 @@ def contact(self):
if not IOrganizer.providedBy(self.context):
return super(VenueICalendarEventComponent, self).contact

ref = IOrganizer(self.context)
item = uuidToObject(ref.organizer_uid)
organizer_behavior = IOrganizer(self.context)
organizer_ref = organizer_behavior.organizer_ref
if not organizer_ref:
return None
item = organizer_ref.to_object

ret = None
if item:
basic = IBasic(item, None)
retstring = utils.join_nonempty([
basic.title,
utils.get_venue_contact_string(item)
], sep=u', ')
retstring = utils.join_nonempty(
[basic.title, utils.get_venue_contact_string(item)], sep=u', '
)
ret = {'value': retstring}
else:
ret = {'value': ref.organizer_notes}
ret = {'value': organizer_behavior.organizer_notes}

return ret

Expand All @@ -69,15 +74,16 @@ def geo(self):
if not ILocation.providedBy(self.context):
return super(VenueICalendarEventComponent, self).geo

ref = ILocation(self.context)
item = uuidToObject(ref.location_uid)
location_behavior = ILocation(self.context)
location_ref = location_behavior.location_ref
if location_ref:
item = location_ref.to_object
else:
item = None

if not IGeolocatable.providedBy(item):
return super(VenueICalendarEventComponent, self).geo

geo = IGeolocatable(item, None)
ret = (
geo.geolocation.latitude,
geo.geolocation.longitude
)
ret = (geo.geolocation.latitude, geo.geolocation.longitude)
return {'value': ret}
43 changes: 26 additions & 17 deletions collective/venue/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,41 @@ def _concat_and_utf8(*args):
matter if input was unicode or str.
Taken from ``plone.app.contenttypes.indexers``
"""
result = ''
result = ""
for value in args:
if six.PY2 and isinstance(value, six.text_type):
value = value.encode('utf-8', 'replace')
value = value.encode("utf-8", "replace")
if value:
result = ' '.join((result, value))
result = " ".join((result, value))
return result


# Index lat/lng of ILocation behavior providing objects like Events.
# IGeolocatable (which venue objects provide) are already indexed in
# collective.geolocationbehavior


@indexer(ILocation)
def latitude(obj):
if not obj.location_uid:
raise AttributeError('no location')
venue = uuidToObject(obj.location_uid)
location_behavior = ILocation(obj)
location_ref = location_behavior.location_ref
if not location_ref:
raise AttributeError("no location")
venue = location_ref.to_object
if not venue:
raise AttributeError("no location")
return venue.geolocation.latitude


@indexer(ILocation)
def longitude(obj):
if not obj.location_uid:
raise AttributeError('no location')
venue = uuidToObject(obj.location_uid)
location_behavior = ILocation(obj)
location_ref = location_behavior.location_ref
if not location_ref:
raise AttributeError("no location")
venue = location_ref.to_object
if not venue:
raise AttributeError("no location")
return venue.geolocation.longitude


Expand All @@ -51,20 +60,20 @@ def searchable_text_indexer(obj):
address = address_idx(obj)() # returns DelegatingIndexer callable
meta_basic = IBasic(obj)
venue = IVenue(obj)
notes = venue.notes and venue.notes.output_relative_to(obj) or u''
notes = venue.notes and venue.notes.output_relative_to(obj) or u""
if notes:
transforms = getToolByName(obj, 'portal_transforms')
body_plain = transforms.convertTo(
'text/plain',
notes,
mimetype='text/html',
).getData().strip()
transforms = getToolByName(obj, "portal_transforms")
body_plain = (
transforms.convertTo("text/plain", notes, mimetype="text/html")
.getData()
.strip()
)
notes = body_plain
parts = [
safe_unicode(address),
safe_unicode(meta_basic.title),
safe_unicode(meta_basic.description),
safe_unicode(notes)
safe_unicode(notes),
]
ret = _concat_and_utf8(*parts)
return ret
Loading

0 comments on commit 37f9698

Please sign in to comment.