# -*- coding: utf-8
# Copyright 2007 by Vahur Rebas
from Products.Archetypes.public import BaseSchema, Schema
from Products.Archetypes.public import StringField, LinesField, FileField, TextField
from Products.Archetypes.public import LinesWidget, TextAreaWidget, RichWidget, StringWidget, FileWidget
from Products.Archetypes.public import BaseContent, registerType
from Globals import InitializeClass
from Products.CMFCore.utils import getToolByName
from Common import CommonMix as Common
from Widgets import TagsWidget, TagsField, TrackbackWidget, TrackbacksMadeToWidget, TrackbackLinkWidget, DictField, HelpWidget, RichWidget
from AccessControl import ClassSecurityInfo, Unauthorized
from config import PROJECT_NAME
import urllib
from urllib2 import URLError
schema = BaseSchema + Schema((
TextField('body',
accessor='getDescription',
default_content_type = 'text/html',
default_output_type = 'text/html',
allowable_content_types = ('text/html',),
widget = TextAreaWidget(
label = 'Description',
label_msgid='label_video_description',
description = 'You can describe your photoalbum here.',
description_msgid='desc_video_body',
i18n_domain="eportfolio"
),
),
# StringField('keyword',
# accessor="getKeyword",
# searchable = 1,
# index = 'KeywordIndex:schema',
# widget=StringWidget(
# label="Keyword",
# label_msgid='label_keyword',
# i18n_domain="eportfolio"
# ),
# ),
# StringField('setId',
# accessor="getSetId",
# searchable = 1,
# widget=StringWidget(
# label="Set id",
# label_msgid='label_set_id',
# description = 'Copy set id from flickr',
# description_msgid='desc_set_id',
# i18n_domain="eportfolio"
# ),
# ),
))
class Photoalbum(BaseContent, Common):
""" photos from flickr or nagi """
meta_type = "Photoalbum"
archetype_name = "Photoalbum"
global_allow = 1
security = ClassSecurityInfo()
schema = schema
actions = (
{
'id':'view',
'name':'View',
'action':'string:${object_url}/photoalbum_view',
'permissions': ('View',),
},
{
'id':'edit',
'name':'Edit',
'action':'string:${object_url}/photoalbum_edit',
'permissions': ('Modify portal content',),
},
{
'id':'metadata',
'name':'Properties',
'action':'string:${object_url}/base_metadata',
'permissions': ('Manage Portal',),
},
{
'id':'delete',
'name':'Delete',
'action':'string:${object_url}/delete_confirmation',
'permissions': ('Modify portal content',),
},
{
'id':'competencies',
'name':'Associate with competency',
'action':'string:${object_url}/add_reference_to_competency?uid=${object/UID}',
'permissions': ('Modify portal content',),
},
)
def showEditableBorder(self, **args):
return 0
def setAlbum(self, REQUEST):
""" """
title = REQUEST.get('title')
description = REQUEST.get('description')
self.setTitle(title)
self.setDescription(description)
service = REQUEST.get('photo_service')
self.setService(service)
if service == 'flickr':
self.setFlickrImages(REQUEST)
if service == 'nagi':
self.setNagiImages(REQUEST)
REQUEST.RESPONSE.redirect('photoalbum_view')
def setFlickrImages(self, REQUEST):
username = REQUEST.get('username')
self.setFlickrUsername(username)
album_id = REQUEST.get('album')
self.setFlickrAlbumId(album_id)
user_id = REQUEST.get('flickr_user_id')
self.setAlbumUrl('http://www.flickr.com/photos/'+user_id+'/sets/'+str(album_id))
url = "http://www.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=3b123c3da3c5bd4ec58a33c0c411d821&photoset_id="+str(album_id)
import urllib2
try:
f = urllib2.urlopen(url)
except URLError:
f = ''
print 'URLError'
return []
html = f.read()
print 'List:', html
import xml.dom.minidom
dom = xml.dom.minidom.parseString(html)
photoset = dom.getElementsByTagName("photoset")
owner = photoset[0].getAttribute("owner")
photos = dom.getElementsByTagName("photo")
images = []
for photo in photos:
pic_info = {}
photoId = photo.getAttribute("id")
secret = photo.getAttribute("secret")
server = photo.getAttribute("server")
farm = photo.getAttribute("farm")
pic_info['medium_img'] = 'http://farm'+str(farm)+'.static.flickr.com/'+str(server)+'/'+str(photoId)+'_'+str(secret)+'.jpg'
pic_info['square_img'] = 'http://farm'+str(farm)+'.static.flickr.com/'+str(server)+'/'+str(photoId)+'_'+str(secret)+'_s.jpg'
pic_info['img_href'] = 'http://www.flickr.com/photos/'+str(owner)+'/'+str(photoId)
images.append(pic_info)
self.flickrimages = images
def setNagiImages(self, REQUEST):
username = REQUEST.get('nagi_username').decode('utf-8')
self.setNagiUsername(username)
set_id = REQUEST.get('nagi_set_id')
self.setNagiAlbumId(set_id)
self.setAlbumUrl('http://nagi.ee/photos/'+urllib.quote_plus(username)+'/sets/'+set_id)
size = 'sq' #sq - square 75x75, t - tiny 100x100, s - small 250x250, m - medium 500x500, l - large 800x800, o - original
stream = '\n'
stream += '\n'
self.nagiimages = stream
def getImages(self):
service = self.getService()
if service == 'nagi':
return getattr(self, 'nagiimages', '')
if service == 'flickr':
print 'fimages', self.flickrimages
return getattr(self, 'flickrimages', [])
def setService(self, service):
self.service = service
def getService(self):
#return REQUEST.get('photo_service', 'flickr')
return getattr(self, 'service', 'flickr')
def setTitle(self, title):
self.title = title
def getTitle(self):
return getattr(self, 'title', '')
def setDescription(self, desc):
self.description = desc
def getDescription(self):
return getattr(self, 'description', '')
def setNagiAlbumId(self, setid):
self.nagi_setid = setid
def getNagiAlbumId(self):
return getattr(self, 'nagi_setid', '')
def setNagiUsername(self, uname):
self.nagi_username = uname
def getNagiUsername(self):
return getattr(self, 'nagi_username', '')
def setFlickrAlbumId(self, setid):
self.flickr_setid = setid
def getFlickrAlbumId(self):
return getattr(self, 'flickr_setid', '')
def setFlickrUsername(self, uname):
self.flickr_username = uname
def getFlickrUsername(self):
return getattr(self, 'flickr_username', '')
def deleteAlbum(self,album_id):
""" delete photoalbum """
if hasattr(self,album_id):
self._delObject(album_id)
def getListOfSets(self, REQUEST):
""" get flickr sets by username """
set_list = []
username = self.getFlickrUsername()
if not username:
username = REQUEST.get('username')
if username == '':
return 'Insert username'
if username == None:
return ''
import urllib
url = "http://www.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key=3b123c3da3c5bd4ec58a33c0c411d821&username="+urllib.quote_plus(username)
print 'URL', url
import urllib2
try:
f = urllib2.urlopen(url)
except URLError:
print 'URLError'
return ''
html = f.read()
print 'HTMl', html
import xml.dom.minidom
dom = xml.dom.minidom.parseString(html)
user = dom.getElementsByTagName("user")
print 'User:', user
if user == []:
error = dom.getElementsByTagName("err")
error_msg = error[0].getAttribute("msg")
return str(error_msg)
user_id = user[0].getAttribute("id")
photosets_url = "http://www.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=3b123c3da3c5bd4ec58a33c0c411d821&user_id="+str(user_id)
try:
f = urllib2.urlopen(photosets_url)
except URLError:
f = ''
print 'URLError'
return ''
html = f.read()
print 'List:', html
dom = xml.dom.minidom.parseString(html)
sets = dom.getElementsByTagName("photoset")
for set in sets:
set_id = set.getAttribute("id")
title = set.getElementsByTagName("title")
set_title = title[0].childNodes[0].data
set_list.append([set_id,set_title])
return [user_id, set_list]
def setAlbumUrl(self, url):
self.album_url = url
def getAlbumUrl(self):
return self.album_url
def goBack(self, REQUEST):
REQUEST.RESPONSE.redirect(self.absolute_url())
def addPhotoalbum(self, REQUEST):
""" change photoalbum """
self.setAlbum(REQUEST)
return self.REQUEST.RESPONSE.redirect(self.absolute_url())
registerType(Photoalbum, PROJECT_NAME)