# CCS CS Lab Facebook Utilities # # Douglas Thrift # # $Id$ from ccscslab.facebook.models import Publication, TemplateBundle, User from ccscslab.main.models import UserProfile import cgi from datetime import datetime, timedelta from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.db.models import Q import facebook import os.path import pytz import threading callback = settings.CCSCSLAB_CALLBACK + '/facebook' callback_secure = settings.CCSCSLAB_CALLBACK_SECURE + '/facebook' css_js = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'templates', 'facebook')) root = 'http://apps.facebook.com/' + settings.CCSCSLAB_FACEBOOK_ROOT _facebook = facebook.Facebook(settings.FACEBOOK_API_KEY, settings.FACEBOOK_SECRET_KEY) def get_facebook(): try: __facebook = facebook.djangofb.get_facebook_client() if __facebook.session_key is not None and __facebook.uid is not None: return __facebook except ImproperlyConfigured: pass return _facebook def publish(template_bundle, *args, **kwargs): _facebook = get_facebook() if Publication.objects.filter(user__id = _facebook.uid, published__gte = datetime.utcnow() - timedelta(days = 1)).count() < 10: Publication.objects.create(user_id = _facebook.uid, published = datetime.utcnow()) return _facebook.feed.publishUserAction(template_bundle_id = TemplateBundle.objects.get(name = template_bundle).bundle_id, *args, **kwargs) def set_user(id = settings.FACEBOOK_USER_ID): try: user = User.objects.get(Q(id = id), Q(session_expiry__gt = datetime.utcnow()) | Q(session_expiry = datetime.utcfromtimestamp(0))) _facebook.uid = user.id _facebook.session_key = '%s-%u' % (user.session, user.id) _facebook.session_key_expires = user.session_expiry return True except User.DoesNotExist: return False set_user() _local = threading.local() def timezone(): while True: try: return _local.timezone except AttributeError: def timezone(): _facebook = get_facebook() utcoffset = timedelta(hours = int(_facebook.fql.query('select timezone from user where uid = %s' % _facebook.uid)[0]['timezone'])) for timezone in pytz.common_timezones: timezone = pytz.timezone(timezone) if utcoffset == datetime.now(timezone).utcoffset(): return timezone # XXX: something has gone horribly wrong, hooray! return pytz.timezone(settings.CCSCSLAB_TIMEZONE) _local.timezone = timezone() def update_profile(user): _facebook = get_facebook() info = [ {'field': 'Account', 'items': [{'label': user.person.name, 'link': user.get_facebook_url()}]}, {'field': 'Shell', 'items': [{'label': user.person.shell, 'link': user.person.get_shell_url()}]}, ] try: userprofile = user.person.userprofile_set.get() email = userprofile.user.email if email is not None: info.append({'field': 'Email', 'items': [{'label': email, 'link': 'mailto:' + email}]}) for screenname in userprofile.screenname_set.all(): items = {'label': screenname.name} url = screenname.get_absolute_url() if url is not None: items['link'] = url info.append({'field': screenname.get_service_display(), 'items': [items]}) phone = userprofile.phone if phone is not None: info.append({'field': 'Phone', 'items': [{'label': phone}]}) website = userprofile.website if website is not None: info.append({'field': 'Website', 'items': [{'label': website, 'link': website}]}) except UserProfile.DoesNotExist: pass #_facebook.profile.setInfo(uid = user.id, title = 'CCS CS Lab', type = 1, info_fields = info) def profile(info_): profile = '
' for info in info_: item = info['items'][0] profile += '
' + cgi.escape(info['field']) + ':
' if 'link' in item: profile += '%s' % (cgi.escape(item['link'], True), cgi.escape(item['label'])) else: profile += cgi.escape(item['label']) profile += '
' profile += '
' return profile #_facebook.profile.setFBML(uid = user.id, profile = profile(info), profile_main = profile(info[:2]))