Exemple #1
0
 def multifeed_handler(self):
     """Handles callback for multiFeedStory form submission."""
     log_fb_request(request)
     facebook.process_request()
     c.is_app_user = facebook.api_client.added
     
     donor_fbid = request.POST.get('donor')
     recipient_fbid = request.POST.get('recipient')
     donation_id = request.POST.get('did')
     
     # look up donation in DB
     session = meta.Session()
     donation = session.query(Donation).get(donation_id)
     
     if donation is None:
         err_obj = {"errorCode": 1, "errorTitle" : "Feed Error", "errorMessage" : "Cannot find donation information."}
         return json.dumps(err_obj)
     
     gifthref = "%s/gift?id=%d" % (c.canvas_url, donation.id)
     
     template_data = {}
     template_data['gift'] = donation.gift.name
     template_data['gifthref'] = gifthref
     template_data['charity'] = donation.charity.name
     template_data['charityhref'] = donation.charity.url
     template_data['comments_xid'] = 'wg-gift.%d' % donation.id
     template_data['images'] = [{'src':h.gift_image_url(donation.gift_id), 'href':gifthref},]
     template_data['user_message'] = 'prompt3'
     
     obj = {'content': {'feed': {'template_id':110890046851, 'user_message':'prompt2', 'template_data':template_data}, 'next_fbjs': 'feed_done();'}, 'method':'multiFeedStory', 'user_message':'prompt1'}
     
     log.debug("multifeed_handler response JSON: %s" % json.dumps(obj))
     return json.dumps(obj)
def publish_stream_item(donor_id, recipient_id, donation):
    charity_name = donation.charity.name
    
    gifthref = "%s/gift?id=%d" % (CANVAS_URL, donation.id)
    if donation.stream_short_msg is None:
        message = 'Here is a meaningful gift.'
    else:
        message = donation.stream_short_msg
    
    properties = {}
    properties['Charity'] = {'text':charity_name, 'href':donation.charity.url}
    
    media = []
    giftimage = {}
    giftimage['type'] = 'image'
    giftimage['src'] = h.gift_image_url(donation.gift_id)
    giftimage['href'] = gifthref
    media.append(giftimage)
    
    attachment = {}
    attachment['name'] = donation.gift.name
    attachment['href'] = gifthref
    attachment['caption'] = 'This donation gift benefits %s.' % charity_name
    attachment['comments_xid'] = 'wg-gift.%d' % donation.id
    attachment['properties'] = properties
    attachment['media'] = media
    
    action_links = []
    send_gift_link = {}
    send_gift_link['text'] = 'Send a gift'
    send_gift_link['href'] = "%s/" % CANVAS_URL
    action_links.append(send_gift_link)
    
    log.debug('Publishing message to stream: %s' % message)
    
    try:
        stream_publish_res = fb.api_client.stream.publish(message=message,
                                                          action_links=simplejson.dumps(action_links),
                                                          attachment=simplejson.dumps(attachment),
                                                          target_id=recipient_id)
        log.debug('Stream.publish response: %r' % stream_publish_res)
        return stream_publish_res
    except FacebookError, err:
        log.debug('Unexpected error calling facebook Stream.publish: ' + str(err))
        return None
def _update_user_fbml(facebook_uid, wg_user):
    received_gifts = user_logic.decorate_with_fb_uid(wg_user.received_gifts, 'donor_id')
    gift_count = len(received_gifts)
    
    if gift_count == 0:
        skinny_content = '<fb:ref handle="profileCommonStyles" /><fb:subtitle>&nbsp;<fb:action href="%s/">Give to a Friend</fb:action></fb:subtitle><div class="no_items">No gifts yet.</div>' % CANVAS_URL
        boxes_content = skinny_content
    else:
        top_gifts = received_gifts[:8]
        
        subtitle_fbml = '<fb:ref handle="profileCommonStyles" /><fb:subtitle><a href="%s/allgifts?uid=%d">%s</a><fb:action href="%s/">Give to a Friend</fb:action></fb:subtitle>' % (CANVAS_URL, facebook_uid, h.plural(gift_count, 'gift', 'gifts'), CANVAS_URL)
        
        gifts_buf = []
        for idx, donation in enumerate(top_gifts):
            if idx == 4:
                gifts_buf.append('<fb:wide>')
            gifts_buf.append('<div class="gift_box"><div class="gift_img"><a href="%s/gift?id=%d" title="%s"><img src="' % (CANVAS_URL, donation.id, donation.gift.name))
            gifts_buf.append(h.gift_image_url(donation.gift_id))
            gifts_buf.append('"></a></div><div class="gift_caption"><span class="caption_from">From:</span> <span class="caption_name"><fb:name uid="%s" firstnameonly="true" useyou="false" ifcantsee="Anon" /></span></div></div>' % donation.fb_uid)
            if idx < 4 and idx % 2 == 1:
                gifts_buf.append('<fb:narrow><div style="height: 1px; font-size: 1px; clear: both;">&nbsp;</div></fb:narrow>')
            if idx % 4 == 3:
                gifts_buf.append('<fb:wide><div style="height: 1px; font-size: 1px; clear: both;">&nbsp;</div></fb:wide>')
        if len(top_gifts) > 4:
            gifts_buf.append('</fb:wide>')
        
        gifts_fbml = ''.join(gifts_buf)
        
        skinny_content = subtitle_fbml + gifts_fbml
        boxes_content = subtitle_fbml + gifts_fbml
        #boxes_content = '<fb:wide>Wide content</fb:wide><fb:narrow>Narrow content</fb:narrow><br>Common content here.'
        #log.debug(boxes_content)
    
    try:
        set_fbml_res = fb.api_client.profile.setFBML(uid=facebook_uid, profile_main=skinny_content, profile=boxes_content)
        log.debug('setFBML response: ' + repr(set_fbml_res))
    except FacebookError, err:
        log.debug('Unexpected error calling facebook.setFBML: ' + str(err))