-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
634 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1 @@ | ||
# -*- coding: utf-8 -*- | ||
from plone import api | ||
from plone.dexterity.browser.view import DefaultView | ||
from plone.memoize.instance import memoizedproperty | ||
from sc.photogallery.utils import PhotoGalleryMixin | ||
|
||
|
||
class View(DefaultView, PhotoGalleryMixin): | ||
|
||
"""Slideshow view for Photo Gallery content type.""" | ||
|
||
def id(self): | ||
return id(self) | ||
|
||
@memoizedproperty | ||
def results(self): | ||
return self.context.listFolderContents() | ||
|
||
@memoizedproperty | ||
def is_empty(self): | ||
return len(self.results) == 0 | ||
|
||
def image(self, obj, scale='large'): | ||
"""Return an image scale if the item has an image field. | ||
:param obj: [required] | ||
:type obj: content type object | ||
:param scale: the scale to be used | ||
:type scale: string | ||
""" | ||
scales = obj.restrictedTraverse('@@images') | ||
return scales.scale('image', scale) | ||
|
||
def localized_time(self, obj, long_format=False): | ||
"""Return the object time in a user-friendly way. | ||
:param item: [required] | ||
:type item: content type object | ||
:param long_format: show long date format if True | ||
:type scale: string | ||
""" | ||
return api.portal.get_localized_time(obj.Date(), long_format) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# -*- coding: utf-8 -*- | ||
from plone import api | ||
from plone.api.exc import InvalidParameterError | ||
from plone.dexterity.browser.view import DefaultView | ||
from plone.memoize import forever | ||
from plone.memoize.instance import memoizedproperty | ||
from sc.photogallery.config import HAS_ZIPEXPORT | ||
from sc.photogallery.interfaces import IPhotoGallerySettings | ||
from sc.photogallery.utils import PhotoGalleryMixin | ||
from zope.component import getMultiAdapter | ||
|
||
import os | ||
|
||
if HAS_ZIPEXPORT: | ||
from ftw.zipexport.generation import ZipGenerator | ||
from ftw.zipexport.interfaces import IZipRepresentation | ||
|
||
|
||
def last_modified(context): | ||
# HACK: We don't care with recursion | ||
objects = context.objectValues() | ||
# Take all mofied dates in seconds since epoch | ||
modified = [int(obj.modified().strftime('%s')) | ||
for obj in objects] | ||
modified.append(int(context.modified().strftime('%s'))) | ||
modified.sort() | ||
# return the most recent modification | ||
return modified[-1] | ||
|
||
|
||
class View(DefaultView, PhotoGalleryMixin): | ||
|
||
"""Slideshow view for Photo Gallery content type.""" | ||
|
||
def id(self): | ||
return id(self) | ||
|
||
@memoizedproperty | ||
def results(self): | ||
return self.context.listFolderContents() | ||
|
||
@memoizedproperty | ||
def is_empty(self): | ||
return len(self.results) == 0 | ||
|
||
def image(self, obj, scale='large'): | ||
"""Return an image scale if the item has an image field. | ||
:param obj: [required] | ||
:type obj: content type object | ||
:param scale: the scale to be used | ||
:type scale: string | ||
""" | ||
scales = obj.restrictedTraverse('@@images') | ||
return scales.scale('image', scale) | ||
|
||
def localized_time(self, obj, long_format=False): | ||
"""Return the object time in a user-friendly way. | ||
:param item: [required] | ||
:type item: content type object | ||
:param long_format: show long date format if True | ||
:type scale: string | ||
""" | ||
return api.portal.get_localized_time(obj.Date(), long_format) | ||
|
||
def can_download(self): | ||
download = False | ||
try: | ||
record = IPhotoGallerySettings.__identifier__ + '.download' | ||
download = api.portal.get_registry_record(record) | ||
except InvalidParameterError: | ||
pass | ||
|
||
return download | ||
|
||
def img_size(self, item): | ||
return '{0:.1f} MB'.format(item.size() / float(1024 * 1024)) | ||
|
||
def can_zipexport(self): | ||
return HAS_ZIPEXPORT | ||
|
||
def last_modified(self): | ||
return last_modified(self.context) | ||
|
||
def zip_url(self): | ||
last_modified = self.last_modified() | ||
base_url = self.context.absolute_url() | ||
url = '%s/@@zip/%s/%s.zip' % (base_url, | ||
str(last_modified), | ||
self.context.getId()) | ||
return url | ||
|
||
@forever.memoize | ||
def _zip_size(self, last_modified=None): | ||
if not HAS_ZIPEXPORT: | ||
return '{0:.1f} MB'.format(0) | ||
with ZipGenerator() as generator: | ||
for obj in [self.context, ]: | ||
repre = getMultiAdapter((obj, self.request), | ||
interface=IZipRepresentation) | ||
for path, pointer in repre.get_files(): | ||
generator.add_file(path, pointer) | ||
zip_file = generator.generate() | ||
size = os.stat(zip_file.name).st_size | ||
return '{0:.1f} MB'.format(size / float(1024 * 1024)) | ||
|
||
def zip_size(self): | ||
last_modified = self.last_modified() | ||
return self._zip_size(last_modified) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# -*- coding: utf-8 -*- | ||
from Products.statusmessages.interfaces import IStatusMessage | ||
from sc.photogallery import _ | ||
from sc.photogallery.config import HAS_ZIPEXPORT | ||
from sc.photogallery.browser.view import last_modified | ||
from zope.component import getMultiAdapter | ||
from zope.publisher.browser import BrowserPage | ||
from ZPublisher.Iterators import filestream_iterator | ||
|
||
import os | ||
|
||
if HAS_ZIPEXPORT: | ||
from ftw.zipexport.generation import ZipGenerator | ||
from ftw.zipexport.interfaces import IZipRepresentation | ||
|
||
|
||
class ZipView(BrowserPage): | ||
'''Export PhotoGallery as zip package''' | ||
|
||
def __init__(self, context, request): | ||
self.context = context | ||
self.request = request | ||
self._path = [] | ||
|
||
@property | ||
def traverse_subpath(self): | ||
return self._path | ||
|
||
def publishTraverse(self, request, name): | ||
self._path.append(name) | ||
return self | ||
|
||
def _subpath(self): | ||
return getattr(self, 'traverse_subpath', []) | ||
|
||
def last_modified(self): | ||
return last_modified(self.context) | ||
|
||
@property | ||
def filename(self): | ||
base_url = self.context.absolute_url() | ||
subpath = self._subpath() | ||
last_modified = self.last_modified() | ||
if not ((len(subpath) > 1) and (str(last_modified) == subpath[0])): | ||
url = '%s/@@zip/%s/%s.zip' % (base_url, | ||
str(last_modified), | ||
self.context.getId()) | ||
return self.request.response.redirect(url) | ||
filename = subpath[1].encode('utf-8') | ||
return filename | ||
|
||
def zip_selected(self, objects): | ||
if not HAS_ZIPEXPORT: | ||
return None | ||
response = self.request.response | ||
with ZipGenerator() as generator: | ||
for obj in objects: | ||
repre = getMultiAdapter((obj, self.request), | ||
interface=IZipRepresentation) | ||
for path, pointer in repre.get_files(): | ||
generator.add_file(path, pointer) | ||
# check if zip has files | ||
if generator.is_empty: | ||
messages = IStatusMessage(self.request) | ||
messages.add( | ||
_('statmsg_content_not_supported', | ||
default=u'Zip export is not supported on the selected content.'), | ||
type=u'error' | ||
) | ||
self.request.response.redirect(self.context.absolute_url()) | ||
return | ||
|
||
zip_file = generator.generate() | ||
response.setHeader('Content-Disposition', | ||
'inline; filename="%s"' % self.filename) | ||
response.setHeader('Content-type', | ||
'application/zip') | ||
response.setHeader('Content-Length', | ||
os.stat(zip_file.name).st_size) | ||
|
||
return filestream_iterator(zip_file.name, 'rb') | ||
|
||
def __call__(self): | ||
filename = self.filename | ||
if HAS_ZIPEXPORT and filename: | ||
return self.zip_selected([self.context]) | ||
else: | ||
messages = IStatusMessage(self.request) | ||
messages.add( | ||
_(u'Not supported operation'), | ||
type=u'error' | ||
) | ||
self.request.response.redirect(self.context.absolute_url()) | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.