示例#1
0
文件: admin.py 项目: hallmark/we-give
 def touch_fbml(self):
     uid = request.params.get('uid')
     if uid is None:
         return 'Please specify uid'
     import wegive.model.meta as meta
     from wegive.model import UserPersona, SocialNetwork
     import wegive.logic.facebook_platform as fb_logic
     network = meta.Session.query(SocialNetwork).filter_by(name=u'Facebook').one()
     userpersona_q = meta.Session.query(UserPersona)
     fb_userpersona = userpersona_q.filter_by(network_id=network.id).filter_by(network_user_id=uid).first()
     if fb_userpersona is None:
         return 'No record of FB user %s' % uid
     
     fb_logic.update_user_fbml_by_userpersona(fb_userpersona)
     return 'done'
示例#2
0
    def index(self):
        realstart = start = time.time()
        
        log_fb_request(request)
        log.debug('time to log request: %.3f ms' % ((time.time() - start)*1000.0))
        
        #
        # TODOs for canvas page:
        #  - if not added, show message: "To send a gift, add this application"
        #  - if not added but received gift(s), show gifts and message "Add application and to profile to display gifts"
        #  - if added, and there are pending gifts, show these
        #
        
        current_user = None
        start = time.time()
        facebook.process_request()
        log.debug('time to do facebook.process_request(): %.3f ms' % ((time.time() - start)*1000.0))
        c.is_app_user = facebook.api_client.added
        if facebook.user:
            log.debug('user: %s' % facebook.user)
            current_user = facebook.user
        elif facebook.canvas_user:
            log.debug('canvas_user: %s' % facebook.canvas_user)
            current_user = facebook.canvas_user

        c.just_installed = (request.GET.get('installed') == '1')
        c.gift_count = None
        
        if current_user:
            start = time.time()

            """ Removing unnecessary Facebook API calls
            # TODO: need to handle "URLError: urlopen error" exceptions thrown from api calls
            info = facebook.api_client.users.getInfo([current_user], ['name', 'first_name', 'last_name', 'pic_square', 'locale'])[0]
            log.debug('name: %s, pic: %s, locale: %s' % (info['name'], info['pic_square'], info['locale']) )
            friends = facebook.api_client.friends.get(uid=current_user)
            friends = facebook.api_client.users.getInfo(friends, ['uid', 'name', 'pic_square', 'locale'])
            c.friends = friends
            log.debug('time to make facebook API calls: %.3f ms' % ((time.time() - start)*1000.0))
            """
            
            # TODO: we cannot store the first_name, last_name - the user needs to provide it to us
            # TODO: need a way to map existing Users to Facebook users
            
            start = time.time()
            session = meta.Session()
            
            # Look up current_user in UserPersona table
            #   if exists:
            #     - update 'is_app_user' status?
            #   if not exists:
            #     - add new row in User table (even if it ends up being a dummy)
            #     - add new row in UserPersona table
            fb_user = user_logic.get_fb_userpersona(session, current_user, create_if_missing=True)
            
            # update is_app_user if persisted value is inaccurate
            if fb_user.is_app_user != facebook.api_client.added:
                fb_user.is_app_user = facebook.api_client.added

            session.flush()
            
            if c.just_installed:
                c.gift_count = len(fb_user.user.received_gifts)
            
                fbml_start = time.time()
                if fb_user.is_app_user and not fb_logic.has_profile_fbml(current_user):
                    ALLOW_FBML_INIT_ON_FIRST_VISIT = True
                    if ALLOW_FBML_INIT_ON_FIRST_VISIT:
                        fb_logic.update_user_fbml_by_userpersona(fb_user)
                    else:
                        log.debug("Updating profile FBML on canvas-page views is currently de-fanged, so that I can properly test handling users w/o FBML!")
            
                log.debug('time for has_profile_fbml call: %.3f ms' % ((time.time() - fbml_start)*1000.0))
            
            # will commit new user if call to get_fb_userpersona added row
            session.commit()
        
        # query DB for list of gifts
        gifts_mkey = 'Cols.gifts-page1'
        mc_start = time.time()
        gifts = g.mc.get(gifts_mkey)
        log.debug('time for memcached calls: %.3f ms' % ((time.time() - mc_start)*1000.0))
        if gifts:
            log.debug('got gifts from memcached!')
            c.gifts = gifts
        else:
            gift_q = meta.Session.query(Gift)
            c.gifts = gift_q.filter_by(for_sale=True).order_by(Gift.created)[:18]
            if g.mc.set(gifts_mkey, c.gifts, time=86400):
                log.debug('stored gifts in memcached!')
            else:
                log.debug('unable to store gifts in memcached.  make sure memcached server is running!')
        
        # get list of charities that have registered thru CBUI as payment recipients
        c.charities = self._get_active_charities()
        
        c.preselected_charity_id = None
        c.preselected_charity = None
        if len(c.charities) == 1:
            # TODO: temporary logic until we have more than 1 charity
            c.preselected_charity_id = c.charities[0].id
            c.preselected_charity = c.charities[0]
        else:
            co_param = request.params.get('co')
            if co_param is not None:
                for charity in c.charities:
                    if charity.short_code == co_param:
                        c.preselected_charity_id = charity.id
                        c.preselected_charity = charity
                        break
        log.debug('preselected_charity_id: %s' % c.preselected_charity_id)
        
        log.debug('time for all DB calls: %.3f ms' % ((time.time() - start)*1000.0))
        
        c.form_uuid = uuid.uuid1().hex
        
        # for (offset, item) in enumerate(c.gifts):
        #    do something on item and offset
        # or: list comprehension: [c * i for (i, c) in enumerate(c.gifts)]
        
        log.debug('total time: %.3f ms' % ((time.time() - realstart)*1000.0))
        
        ext_perms = request.params.get('fb_sig_ext_perms', '').split(',')
        c.has_publish_stream = ('publish_stream' in ext_perms)
        c.has_email_perm = ('email' in ext_perms)
        if 'publish_stream' in ext_perms:
            c.show_prompt_perm = False
        elif current_user is not None:
            # show link for Tracy and me
            c.show_prompt_perm = (int(current_user) == 541265766 or int(current_user) == 1004760)
        else:
            c.show_prompt_perm = False
        
        return render('/facebook/index.tmpl')