def validate(self, album): settings = Data(self.view) if settings.gallery_type != 'picasa': return username = settings.picasa_username if empty(album): raise zope.schema.ValidationError( _(u"label_validate_picasa_ablum_empty", default=u"You must specify a picasa album."), True ) if empty(username): # do not continue validation until they have a valid username return adapter = getGalleryAdapter(self.context, self.request, gallery_type=settings.gallery_type) found = adapter.get_album_name(name=album, user=username) if found is None: raise zope.schema.ValidationError( _(u"label_validate_picasa_find_album", default=u"Could not find album."), True )
def validate(self, username): super(FlickrUsernameValidator, self).validate(username) settings = Data(self.view) if settings.gallery_type != 'flickr': return if empty(username): raise zope.schema.ValidationError( _(u"label_validate_flickr_specify_username", default=u"You must specify a username."), True ) try: adapter = getGalleryAdapter(self.context, self.request, settings.gallery_type) flickr_userid = adapter.get_flickr_user_id(username) if empty(flickr_userid): raise zope.schema.ValidationError( _(u"label_validate_flickr_user", default=u"Could not find flickr user."), True ) except: raise zope.schema.ValidationError(_(u"label_validate_flickr_user", default=u"Could not find flickr user."), True )
def validate(self, photoset): super(FlickrSetValidator, self).validate(photoset) settings = Data(self.view) if settings.gallery_type != 'flickr': return if empty(photoset): raise zope.schema.ValidationError( _(u"label_validate_flickr_specify_user", default=u"You must specify a flickr set to use."), True ) try: adapter = getGalleryAdapter(self.context, self.request, settings.gallery_type) userid = adapter.get_flickr_user_id(settings.flickr_username) flickr_photosetid = adapter.get_flickr_photoset_id(photoset, userid) if empty(flickr_photosetid): raise zope.schema.ValidationError( _(u"label_validate_flickr_find_set", default="Could not find flickr set."), True ) except: raise zope.schema.ValidationError( _(u"label_validate_flickr_find_set", default="Could not find flickr set."), True )
def ThumbVocabulary(context): image_terms = [ SimpleTerm('tile', 'tile', _(u"label_tile", default=u"tile")), SimpleTerm('thumb', 'thumb', _(u"label_thumb", default=u"thumb")), SimpleTerm('mini', 'mini', _(u"label_mini", default=u"mini")), SimpleTerm('preview', 'preview', _(u"label_preview", default=u"preview")) ] site = getSite() portal_properties = getToolByName(site, 'portal_properties', None) # these are only working for plone so everything should be OK here try: #Plone 5 sizes = api.portal.get_registry_record('plone.allowed_sizes') except: #Plone 4 portal_properties = api.portal.get_tool(name='portal_properties') if 'imaging_properties' in portal_properties.objectIds(): sizes = portal_properties.imaging_properties.getProperty('allowed_sizes') terms = [SimpleTerm(value=format_size(pair), token=format_size(pair), title=pair) for pair in sizes if not format_size(pair) in ['icon', 'tile', 'listing', 'mini', 'preview', 'thumb', 'large']] image_terms = image_terms + terms return SimpleVocabulary(image_terms)
def SizeVocabulary(context): image_terms = [ SimpleTerm('small', 'small', _(u"label_size_small", default=u'Small')), SimpleTerm('medium', 'medium', _(u"label_size_medium", default=u'Medium')), SimpleTerm('large', 'large', _(u"label_size_large", default=u'Large')) ] site = getSite() portal_properties = getToolByName(site, 'portal_properties', None) # here we add the custom image sizes, we skip the small ones and # preview, large since they are already added. # if we add them back, be sure to do it in basic.py, too # we also need to test if gallery_type == 'basic': # dont think this is right, it might be the overall seting if IGallerySettings['gallery_type'].default == 'basic': try: #Plone 5 sizes = api.portal.get_registry_record('plone.allowed_sizes') except: #Plone 4 portal_properties = api.portal.get_tool(name='portal_properties') if 'imaging_properties' in portal_properties.objectIds(): sizes = portal_properties.imaging_properties.getProperty('allowed_sizes') terms = [SimpleTerm(value=format_size(pair), token=format_size(pair), title=pair) for pair in sizes if not format_size(pair) in ['icon', 'tile', 'listing', 'mini', 'preview', 'thumb', 'large', 'small', 'medium']] image_terms = image_terms + terms return SimpleVocabulary(image_terms)
class GalleryEditForm(base.EditForm): form_fields = form.Fields(IGalleryPortlet) form_fields['gallery'].custom_widget = UberSelectionWidget label = _(u"gallery_portlet_edit_form_title", default=u"Edit Gallery Portlet") description = _(u"gallery_portlet_edit_form_description", default=u"This portlet allows you to show gallery images in a " u"portlet.")
class GalleryEditForm(base.EditForm): schema = IGalleryPortlet label = _(u"gallery_portlet_edit_form_title", default=u"Edit Gallery Portlet") description = _( u"gallery_portlet_edit_form_description", default=u"This portlet allows you to show gallery images in a " u"portlet.")
class GalleryAddForm(base.AddForm): schema = IGalleryPortlet label = _(u"gallery_portlet_add_form_title", default=u"Add Gallery") description = _(u"gallery_portlet_add_form_description", default=u"This portlet allows you to show gallery" u"images in a portlet.") def create(self, data): return GalleryAssignment(**data)
class GalleryAddForm(base.AddForm): form_fields = form.Fields(IGalleryPortlet) form_fields['gallery'].custom_widget = UberSelectionWidget label = _(u"gallery_portlet_add_form_title", default=u"Add Gallery") description = _(u"gallery_portlet_add_form_description", default=u"This portlet allows you to show gallery" u"images in a portlet.") def create(self, data): return GalleryAssignment(**data)
class IFlickrGallerySettings(IBaseSettings): flickr_username = schema.TextLine( title=_(u"label_flickr_username", default=u"flickr username"), description=_(u"description_flickr_username", default=u"The username/id of your flickr account. " u"(*flickr* gallery type)"), required=False) flickr_set = schema.TextLine(title=_(u"label_flickr_set", default="Flickr Set"), description=_( u"description_flickr_set", default=u"Name/id of your flickr set." u"(*flickr* gallery type)"), required=False)
class IPicasaGallerySettings(IBaseSettings): picasa_username = schema.TextLine( title=_(u"label_picasa_username", default=u"GMail address"), description=_(u"description_picasa_username", default=u"GMail address of who this album belongs to. " u"(*Picasa* gallery type)"), required=False) picasa_album = schema.TextLine( title=_(u"label_picasa_album", default=u"Picasa Album"), description=_(u"description_picasa_album", default=u"Name of your picasa web album. " u"This will be the qualified name you'll see in " u"the address bar or the full length name of the " u"album. (*Picasa* gallery type)"), required=False)
class FlickrAdapter(BaseAdapter): implements(IFlickrAdapter, IGalleryAdapter) schema = IFlickrGallerySettings name = u"flickr" description = _(u"label_flickr_gallery_type", default=u"Flickr") sizes = { 'small': { 'width': 500, 'height': 375 }, 'medium': { 'width': 640, 'height': 480 }, 'large': { 'width': 1024, 'height': 768 }, 'thumb': { 'width': 72, 'height': 72 }, 'flickr': { 'small': '_m', 'medium': '', 'large': '_b' } } def assemble_image_information(self, image): return { 'image_url': self.get_large_photo_url(image), 'thumb_url': self.get_mini_photo_url(image), 'link': self.get_photo_link(image), 'title': image.get('title'), 'description': "" } def get_flickr_user_id(self, username=None): if username is None: username = self.settings.flickr_username username = username.strip() try: return self.flickr.people_findByUsername( username=username).find('user').get('nsid') except: try: return self.flickr.people_getInfo( user_id=username).find('person').get('nsid') except Exception, inst: self.log_error(Exception, inst, "Can't find filckr user id") return None
def ThumbVocabulary(context): image_terms = [ SimpleTerm("tile", "tile", _(u"label_tile", default=u"tile")), SimpleTerm("thumb", "thumb", _(u"label_thumb", default=u"thumb")), SimpleTerm("mini", "mini", _(u"label_mini", default=u"mini")), SimpleTerm("preview", "preview", _(u"label_preview", default=u"preview")), ] site = getSite() portal_properties = getToolByName(site, "portal_properties", None) # these are only working for plone so everything should be OK here if "imaging_properties" in portal_properties.objectIds(): sizes = portal_properties.imaging_properties.getProperty("allowed_sizes") terms = [ SimpleTerm(value=format_size(pair), token=format_size(pair), title=pair) for pair in sizes if not format_size(pair) in ["icon", "tile", "listing", "mini", "preview", "thumb", "large"] ] image_terms = image_terms + terms return SimpleVocabulary(image_terms)
def validate(self, username): settings = Data(self.view) if settings.gallery_type != 'picasa': return if empty(username): raise zope.schema.ValidationError( _(u"label_validate_picasa_specify_username", default=u"You must specify a picasa username."), True )
def SizeVocabulary(context): image_terms = [ SimpleTerm("small", "small", _(u"label_size_small", default=u"Small")), SimpleTerm("medium", "medium", _(u"label_size_medium", default=u"Medium")), SimpleTerm("large", "large", _(u"label_size_large", default=u"Large")), ] site = getSite() portal_properties = getToolByName(site, "portal_properties", None) # here we add the custom image sizes, we skip the small ones and # preview, large since they are already added. # if we add them back, be sure to do it in basic.py, too # we also need to test if gallery_type == 'basic': # dont think this is right, it might be the overall seting if IGallerySettings["gallery_type"].default == "basic": if "imaging_properties" in portal_properties.objectIds(): sizes = portal_properties.imaging_properties.getProperty("allowed_sizes") terms = [ SimpleTerm(value=format_size(pair), token=format_size(pair), title=pair) for pair in sizes if not format_size(pair) in ["icon", "tile", "listing", "mini", "preview", "thumb", "large"] ] image_terms = image_terms + terms return SimpleVocabulary(image_terms)
def SizeVocabulary(context): # PTG default image_terms = [ SimpleTerm('ptg_default', 'ptg_default', _(u"label_ptg_default", default=u'PTG default')), ] # Append Flickr size array from __init__.py from __init__ import FlickrAdapter sizes = FlickrAdapter.sizes['flickr'].keys() sizes.sort() for s in sizes: image_terms.append(SimpleTerm(s, s, s)) return SimpleVocabulary(image_terms)
class IGallerySettings(Interface): gallery_type = schema.Choice( title=_(u"label_gallery_type", default=u"Type"), description=_( u"description_gallery_type", default=u"Select the type of gallery you want this to be. " u"If you select something other than default, you " u"must fill out the information in the corresponding " u"tab for that gallery type."), vocabulary="collective.plonetruegallery.GalleryTypeVocabulary", default="basic") display_type = schema.Choice( title=_(u"label_gallery_display_type", default=u"Gallery Display Type"), description=_(u"label_gallery_display_type_description", default=u"Choose the method in which the " u"gallery should be displayed"), default="galleria", vocabulary="collective.plonetruegallery.DisplayTypes") # the specific options for the gallery types will be added # dynamcially in the form size = schema.Choice( title=_(u"label_gallery_size", default=u"Size"), description=_( u"description_gallery_size", default=u"The actual sizes used can vary depending on the " u"gallery type that is used since different services " u"have different size constraints. " u"Only 'Small, Medium and Large' work with Flickr and Picasa"), default='large', vocabulary="collective.plonetruegallery.SizeVocabulary", ) thumb_size = schema.Choice( title=_(u"label_thumb_size", default=u"Thumbnail image size"), description=_(u"description_thumb_size", default=u"The size of thumbnail images. " u"Will only work with plone image gallery type."), default='thumb', vocabulary="collective.plonetruegallery.ThumbVocabulary", ) # the options for the display type will also be added dynamically timed = schema.Bool(title=_(u"label_timed", default=u"Timed?"), description=_( u"description_timed", default=u"Should this gallery automatically " u"change images for the user?"), default=True) copyright = schema.Bool( title=_(u"label_copyright", default=u"Copyright notice?"), description=_(u"description_copyright", default=u"Should copyright notices be attached " u"to each image?"), default=False) delay = schema.Int( title=_(u"label_delay", default=u"Delay"), description=_( u"description_delay", default=u"If slide show is timed, the delay sets " u"how long before the next image is shown in miliseconds."), default=5000, required=True) duration = schema.Int( title=_(u"label_image_change_duration", default=u"Change Duration"), description=_(u"description_fade_in_duration", default=u"The amount of time the change effect should " u"take in miliseconds."), default=500, required=True) show_subgalleries = schema.Bool( title=_(u"label_show_subgalleries", default=u"Show Sub-Galleries?"), description=_(u"description_show_subgalleries", default=u"If you select this option, previews for all " u"nested galleries will show up below this gallery."), default=True) random_subgallery_lead = schema.Bool( title=_(u"label_random_subgallery_lead", default=u"Show random lead image for subgalleries?"), description=_( u"desciption_random_subgallery_lead", default= u"If you select this option, the lead images for the subgalleries will be chosen at random. " u"Otherwise the first image is chosen."), default=True) batch_size = schema.Int( title=_(u"label_batch_size", default=u"Batch Size"), description=_(u"description_batch_size", default=u"The amount of images shown in one page. " u"This is not used for all display types."), default=50, required=True) enable_bodytext = schema.Bool( title=_(u"label_enable_bodytext", default=u"Enable bodytext"), description=_( u"description_bodytext", default= u"This is only used for customized templates to show body text"), default=False) custom_stylesheet = schema.TextLine( title=_(u"label_custom_stylesheet", default=u"Custom stylesheet"), description= _(u"description_custom_stylesheet", default= u"Relative path from the root of the site where the stylesheet " u"will be referenced from. This will be included after the display " u"gallery type's styles and is a good entry point for customization." ), default=u"", required=False)
class IGalleryPortlet(IPortletDataProvider): show_title = schema.Bool( title=_(u"gallery_portlet_show_title_title", default=u"Show Title?"), description=_(u"gallery_portlet_show_title_description", default=u"Check to show the title of the gallery in the portlet"), default=True) gallery = schema.Choice( title=_(u"gallery_portlet_gallery_title", default=u"Gallery"), description=_(u"gallery_portlet_gallery_description", default=u"The gallery to show in this portlet."), source=GallerySearchabelTextSourceBinder(), required=True) mini = schema.Bool( title=_(u'gallery_portlet_mini', default=u"Mini gallery"), description=_(u'gallery_portlet_mini_desc', default=u"If false, the actual full gallery will render " u"in an iframe and will follow the width and " u"height settings"), default=True) width = schema.Int( title=_(u"gallery_portlet_width_title", default=u"Width in pixels"), description=_(u"gallery_portlet_width_description", default=u"The width of the image in the portlet"), required=True, default=200) height = schema.Int( title=_(u"gallery_portlet_height_title", default=u"Height in pixels"), description=_(u"gallery_portlet_height_description", default=u"The height of the image in the portlet." u"If 0, no height is set."), required=True, default=0) timed = schema.Bool( title=_(u"gallery_portlet_timed_title", default=u"Timed"), description=_(u"gallery_portlet_timed_description", default=u"Do you want the gallery to be timed?"), required=True, default=True) hide_controls = schema.Bool( title=_(u"gallery_portlet_hide_controls", default="Hide Controls"), description=_(u"gallery_portlet_hide_controls_description", default="Hide portlet gallery controls"), required=False, default=False)
class IGallerySettings(Interface): gallery_type = schema.Choice( title=_(u"label_gallery_type", default=u"Type"), description=_( u"description_gallery_type", default=u"Select the type of gallery you want this to be. " u"If you select something other than default, you " u"must fill out the information in the corresponding " u"tab for that gallery type."), vocabulary="collective.plonetruegallery.GalleryTypeVocabulary", default="basic") display_type = schema.Choice( title=_(u"label_gallery_display_type", default=u"Gallery Display Type"), description=_(u"label_gallery_display_type_description", default=u"Choose the method in which the " u"gallery should be displayed"), default="galleria", vocabulary="collective.plonetruegallery.DisplayTypes") # the specific options for the gallery types will be added # dynamcially in the form size = schema.Choice( title=_(u"label_gallery_size", default=u"Size"), description=_( u"description_gallery_size", default=u"The actual sizes used can vary depending on the " u"gallery type that is used since different services " u"have different size constraints."), default='medium', vocabulary=SimpleVocabulary([ SimpleTerm('small', 'small', _(u"label_size_small", default=u'Small')), SimpleTerm('medium', 'medium', _(u"label_size_medium", default=u'Medium')), SimpleTerm('large', 'large', _(u"label_size_large", default=u'Large')) ])) thumb_size = schema.Choice( title=_(u"label_thumb_size", default=u"Thumbnail image size"), description=_(u"description_thumb_size", default=u"The size of thumbnail images. " u"Will only work with plone image gallery type."), default='thumb', vocabulary=SimpleVocabulary([ SimpleTerm('tile', 'tile', _(u"label_tile", default=u"tile")), SimpleTerm('thumb', 'thumb', _(u"label_thumb", default=u"thumb")), SimpleTerm('mini', 'mini', _(u"label_mini", default=u"mini")), SimpleTerm('preview', 'preview', _(u"label_preview", default=u"preview")), ])) # the options for the display type will also be added dynamically timed = schema.Bool(title=_(u"label_timed", default=u"Timed?"), description=_( u"description_timed", default=u"Should this gallery automatically " u"change images for the user?"), default=True) delay = schema.Int( title=_(u"label_delay", default=u"Delay"), description=_( u"description_delay", default=u"If slide show is timed, the delay sets " u"how long before the next image is shown in miliseconds."), default=5000, required=True) duration = schema.Int( title=_(u"label_image_change_duration", default=u"Change Duration"), description=_(u"description_fade_in_duration", default=u"The amount of time the change effect should " u"take in miliseconds."), default=500, required=True) show_subgalleries = schema.Bool( title=_(u"label_show_subgalleries", default=u"Show Sub-Galleries?"), description=_(u"description_show_subgalleries", default=u"If you select this option, previews for all " u"nested galleries will show up below this gallery."), default=True) batch_size = schema.Int( title=_(u"label_batch_size", default=u"Batch Size"), description=_(u"description_batch_size", default=u"The amount of images shown in one page. " u"This is not used for all display types."), default=50, required=True)
class PicasaAdapter(BaseAdapter): implements(IPicasaAdapter, IGalleryAdapter) schema = IPicasaGallerySettings name = u"picasa" description = _(u"label_picasa_gallery_type", default=u"Picasa Web Albums") sizes = { 'small': { 'width': 320, 'height': 320 }, 'medium': { 'width': 576, 'height': 576 }, 'large': { 'width': 800, 'height': 800 }, 'thumb': { 'width': 72, 'height': 72 } } def get_gd_client(self): uid = self.gallery.UID() if uid in GDATA: return GDATA[uid] else: self.gd_client = gdata.photos.service.PhotosService() return GDATA[uid] def set_gd_client(self, value): GDATA[self.gallery.UID()] = value gd_client = property(get_gd_client, set_gd_client) def assemble_image_information(self, image): return { 'image_url': image.content.src, 'thumb_url': image.media.thumbnail[0].url, 'link': image.content.src, 'title': image.title.text, 'description': image.summary.text or '' } def get_album_name(self, name=None, user=None): if name is None: name = self.settings.picasa_album name = name.strip() if user is None: user = self.settings.picasa_username user = user.strip() feed = self.gd_client.GetUserFeed(user=user) for entry in feed.entry: if entry.name.text.decode("utf-8") == name or \ entry.title.text.decode("utf-8") == name: return entry.name.text return None def feed(self): gd_client = self.gd_client try: url = DATA_FEED_URL % (self.settings.picasa_username, self.get_album_name(), self.sizes[self.settings.size]['width'], self.sizes['thumb']['width']) feed = gd_client.GetFeed(url) return feed except GooglePhotosException, inst: #Do not show anything if connection failed self.log_error(GooglePhotosException, inst, "Error getting photo feed") return None
class BasicAdapter(BaseAdapter): implements(IBasicAdapter, IGalleryAdapter) name = u"basic" description = _(u"label_default_gallery_type", default=u"Use Plone To Manage Images") schema = IBasicGallerySettings cook_delay = 0 # since some default sizes Plone has are rather small, # let's setup a mechanism to upgrade sizes. minimum_sizes = { 'small': { 'width': 320, 'height': 320, 'next_scale': 'preview' }, 'medium': { 'width': 576, 'height': 576, 'next_scale': 'large' } } @property @memoize_contextless def size_map(self): image_sizes = { 'small': 'mini', 'medium': 'preview', 'large': 'large', 'thumb': 'tile' } # Here we try to get the custom sizes # we skip some scales, since they are already 'taken' try: from plone.app.imaging.utils import getAllowedSizes all_sizes = getAllowedSizes() for scale_name in all_sizes.keys(): if scale_name not in [ 'small', 'medium', 'mini', 'preview', 'thumb', 'tile', 'large' ]: image_sizes[str(scale_name)] = str(scale_name) except (ImportError, AttributeError): # plone 3 without plone.app.blob... We still have defaults... pass return image_sizes @property @memoize_contextless def _inverted_size_map(self): return dict([(v, k) for (k, v) in self.size_map.iteritems()]) @property @memoize_contextless def sizes(self): if has_pai: from plone.app.imaging.utils import getAllowedSizes # user has plone.app.imaging installed, use # these image size settings _allowed_sizes = getAllowedSizes() allowed_sizes = {} for scale_name, sizes in _allowed_sizes.items(): width, height = sizes if scale_name not in self._inverted_size_map: continue size_name = self._inverted_size_map[scale_name] allowed_sizes[size_name] = {'width': width, 'height': height} if size_name in self.minimum_sizes: if width < self.minimum_sizes[size_name]['width']: allowed_sizes[size_name]['width'] = \ self.minimum_sizes[size_name]['width'] if height < self.minimum_sizes[size_name]['height']: allowed_sizes[size_name]['height'] = \ self.minimum_sizes[size_name]['height'] self.size_map[size_name] = \ self.minimum_sizes[size_name]['next_scale'] return allowed_sizes else: from Products.ATContentTypes.content.image import ATImageSchema return { 'small': { 'width': 320, 'height': 320 }, 'medium': { 'width': 576, 'height': 576 }, 'large': { 'width': ATImageSchema['image'].sizes['large'][0], 'height': ATImageSchema['image'].sizes['large'][1] }, 'thumb': { 'width': ATImageSchema['image'].sizes['tile'][0], 'height': ATImageSchema['image'].sizes['tile'][1] } } def retrieve_images(self): adapter = getMultiAdapter((self.gallery, self), IImageInformationRetriever) return adapter.getImageInformation() def cook(self): """ do not cook anything since we don't have a cook_delay on this type anyway """ pass @property @memoize def cooked_images(self): return self.retrieve_images()
class GallerySettingsForm(group.GroupForm, form.EditForm): """ The page that holds all the gallery settings """ fields = field.Fields(INothing) groups = [MainSettingsGroup] label = _(u'heading_gallery_settings_form', default=u'Gallery') description = _(u'description_gallery_settings_form', default=u'Configure the parameters for this gallery.') successMessage = _(u'successMessage_gallery_settings_form', default=u'Gallery Settings Saved.') noChangesMessage = _( u'noChangesMessage_gallery_settings_form', default=u'There are no changes in the Gallery settings.') def add_fields_to_group(self, type_, groupname): group = None for g in self.groups: if groupname == g.label: group = g if group is None: g = plonegroup.GroupFactory(groupname, field.Fields(type_.schema)) self.groups.append(g) else: fields = field.Fields(type_.schema) toadd = [] for f in fields._data_values: if f.__name__ not in group.fields.keys(): toadd.append(f) group.fields = field.Fields(group.fields, *toadd) def update(self): gallerytypes = getAllGalleryTypes() displaytypes = getAllDisplayTypes() for t in gallerytypes: if len(t.schema.names()) > 0: self.add_fields_to_group(t, t.name) for t in displaytypes: if len(t.schema.names()) > 0: self.add_fields_to_group(t, t.name) super(GallerySettingsForm, self).update() def set_status_message(self, settings, has_changes): display_type = getDisplayType(settings.display_type) msg = has_changes and self.successMessage or self.noChangesMessage msg = zope.i18n.translate(msg) if display_type.userWarning is not None: self.status = "%s %s" % ( msg, zope.i18n.translate(display_type.userWarning)) else: self.status = msg @button.buttonAndHandler(_('Apply'), name='apply') def handleApply(self, action): data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return changes = self.applyChanges(data) settings = GallerySettings(self.context) has_changes = False if changes: settings = GallerySettings(self.context) settings.last_cooked_time_in_seconds = 0 has_changes = True self.set_status_message(settings, has_changes) return self.request.response.redirect(self.context.absolute_url())
class MainSettingsGroup(plonegroup.Group): fields = field.Fields(IGallerySettings) label = _(u"Main")
class BaseAdapter(object): implements(IGalleryAdapter) adapts(IGallery, IDefaultBrowserLayer) sizes = {} settings = None schema = None name = u"base" description = _(u"label_base_gallery_type", default=u"base: this isn't actually a gallery type. " u"Think abstract class here...") cook_delay = 24 * 60 * 60 # will update once a day def __init__(self, gallery, request): self.gallery = gallery self.request = request self.settings = GallerySettings(self.gallery, interfaces=[self.schema]) if self.time_to_cook(): self.cook() @property @memoize def subgalleries(self): return self.get_subgalleries() def get_subgalleries(self, **kwargs): catalog = getToolByName(self.gallery, 'portal_catalog') gallery_path = self.gallery.getPhysicalPath() results = catalog.searchResults( path='/'.join(gallery_path), object_provides=IGallery.__identifier__, **kwargs ) uid = IUUID(self.gallery, None) if uid is None: uid = self.gallery.UID() def afilter(i): """prevent same object and multiple nested galleries""" return i.UID != uid and \ len(i.getPath().split('/')) == len(gallery_path) + 1 and \ getMultiAdapter( (i.getObject(), self.request), name='plonetruegallery_util' ).enabled() return filter(afilter, results) @property def contains_sub_galleries(self): return len(self.subgalleries) > 0 def time_to_cook(self): return (self.settings.last_cooked_time_in_seconds + self.cook_delay) \ <= (time.time()) def log_error(self, ex='', inst='', msg=""): LOG('collective.plonetruegallery', INFO, "%s adapter, gallery is %s\n%s\n%s\n%s" % (self.name, str(self.gallery), msg, ex, inst)) def cook(self): self.settings.cooked_images = self.retrieve_images() self.settings.last_cooked_time_in_seconds = time.time() def get_random_image(self): if len(self.cooked_images) > 0: return self.cooked_images[random.randint(0, self.number_of_images - 1)] else: return {} def get_first_image(self): if len(self.cooked_images) > 0: return self.cooked_images[0] else: return {} @property def number_of_images(self): return len(self.cooked_images) def retrieve_images(self): raise Exception("Not implemented") @property def cooked_images(self): return self.settings.cooked_images