Exemplo n.º 1
0
    def get(self):

        username = urllib.unquote(cgi.escape(self.request.path[6:]))  ## Get the username in the URL string such as /user/1234
        ViewUser = None
        
        try:
            ## After URL style changed, Now won't allow username in URL, only accept id in URL.
            
            ## Get this user.
            q = db.GqlQuery("SELECT * FROM tarsusaUser WHERE userid = :1 LIMIT 1", int(username))
            ViewUser = q.get()

            if ViewUser == None:
                q = tarsusaUser.get_by_id(int(username))
                ViewUser = q
        except:
            self.redirect('/')

        UserNickName = '访客'
        outputStringUserAvatar = ''

        UserNonPrivateItemsList = ''
        UserFriends = []

        if ViewUser != None:
        
            ## Preparation
            ## Will be useed
            if ViewUser.avatar:
                outputStringUserAvatar = "<img src='/image?avatar=" + str(ViewUser.key().id()) + "' width=64 height=64>"
            else:
                outputStringUserAvatar = "<img src='/img/default_avatar.jpg' width=64 height=64>"
                
            outputStringUserMainPageTitle = ViewUser.dispname + "公开的项目".decode("utf-8")

            #-------------------------------------
            if not self.chk_login():
            
                #Not a login user, show only the public items.
                UserNickName = "访客"
                logictag_OneoftheFriendsViewThisPage = False
                CurrentUserIsOneofViewUsersFriends = False
                statusUserFriends = 'NotLogin'
                #Above tag will be recognized by template
                ViewedUserIsOneofCurrentUsersFriends = False

                #Check Whether there is usermainPage_publicitems_anony
                cachedUserMainPagePublicItemsAnony = memcache.get_item("mainpage_publicitems_anony", ViewUser.key().id())
                if cachedUserMainPagePublicItemsAnony is not None:
                    UserNonPrivateItemsList = cachedUserMainPagePublicItemsAnony
                else:
                    UserNonPrivateItemsList = tarsusaCore.get_UserNonPrivateItems(ViewUser.key().id(), 'public')
                

                template_values = {
                    'PrefixCSSdir': "../",
                    
                    'ViewedUserNickName': ViewUser.dispname,

                    'UserAvatarImage': outputStringUserAvatar,
                    
                    'statusViewedUserFriends': statusUserFriends,   
                    'UserJoinInDate': datetime.datetime.date(ViewUser.datejoinin),
                    'UserWebsite': ViewUser.website,
                    'UserMainPageUserTitle': outputStringUserMainPageTitle,
                    'UserNonPrivateItemsList': UserNonPrivateItemsList,
                    
                    'outputFeed': True,
                    'outputFeedTitle': ViewUser.dispname,
                    'outputFeedURL': "/user/" + str(ViewUser.key().id()) + "/feed",
                }

            
            else:               
                #User Login.
                
                #Check Whether CurrerentUser is one of ViewUser's friends
                UserNickName = ViewUser.dispname
                CurrentUser = self.get_user_db()

                CurrentUserIsOneofViewUsersFriends = False

                for each_Friend_key in ViewUser.friends:
                    if each_Friend_key == CurrentUser.key():
                        CurrentUserIsOneofViewUsersFriends = True
                        logictag_OneoftheFriendsViewThisPage = True

                ## Check whether the ViewedUser is a friend of CurrentUser.
                ## For AddUserAsFriend button.
                ViewedUserIsOneofCurrentUsersFriends = False

                for each_Friend_key in CurrentUser.friends:
                    if each_Friend_key == ViewUser.key():
                        ViewedUserIsOneofCurrentUsersFriends = True
            
                # Get user friend list
                cachedUserMainPageFriends = memcache.get_item("mainpage_friends", ViewUser.key().id())
                if cachedUserMainPageFriends is not None:
                    UserFriends = cachedUserMainPageFriends
                else:
                    # This is shown to all logged in users.
                    #Check This Users Friends.
                    
                    UserFriends = tarsusaCore.get_UserFriends(ViewUser.key().id())
                    
                    #set cache item
                    memcache.set_item("mainpage_friends", UserFriends, ViewUser.key().id())
                    
                #----------------------------------------               
                if ViewedUserIsOneofCurrentUsersFriends == True:
                    #Check Whether there is usermainpage_publicitems
                    cachedUserMainPagePublicItems = memcache.get_item("mainpage_publicitems", ViewUser.key().id())
                    if cachedUserMainPagePublicItems is not None:
                        UserNonPrivateItemsList = cachedUserMainPagePublicItems
                    else:
                        UserNonPrivateItemsList = tarsusaCore.get_UserNonPrivateItems(ViewUser.key().id(), 'publicOnlyforFriends')
                        #about the property, it pass this one but actually it's going to digg publicwithPublicforFriends.
                        
                        #set cache item
                        memcache.set_item("mainpage_publicitems", UserNonPrivateItemsList, ViewUser.key().id())

                else:
                    #CurrentUser is not one of ViewUser's friends.

                    #Check Whether there is usermainPage_publicitems_anony
                    cachedUserMainPagePublicItemsAnony = memcache.get_item("mainpage_publicitems_anony", ViewUser.key().id())
                    if cachedUserMainPagePublicItemsAnony is not None:
                        UserNonPrivateItemsList = cachedUserMainPagePublicItemsAnony
                    else:
                        UserNonPrivateItemsList = tarsusaCore.get_UserNonPrivateItems(ViewUser.key().id(), 'public')

                        #set cache item
                        memcache.set_item("mainpage_publicitems_anony", UserNonPrivateItemsList, ViewUser.key().id())

                template_values = {
                    'PrefixCSSdir': "../",
                    
                    'UserLoggedIn': 'Logged In',

                    'UserID': CurrentUser.key().id(), #This indicates the UserSettingPage Link on the topright of the Page, so it should be CurrentUser

                    'ViewedUserNickName': UserNickName,
                    'UserNickName': CurrentUser.dispname,
                    'ViewedUser': ViewUser,

                    'ViewedUserFriends': UserFriends,   

                    'UserAvatarImage': outputStringUserAvatar,
                    
                    'UserJoinInDate': datetime.datetime.date(ViewUser.datejoinin),
                    'UserWebsite': ViewUser.website,
                    'UserMainPageUserTitle': outputStringUserMainPageTitle,
                
                    'ViewedUserIsOneofCurrentUsersFriends': ViewedUserIsOneofCurrentUsersFriends,
                    'UserNonPrivateItemsList': UserNonPrivateItemsList,

                    'outputFeed': True,
                    'outputFeedTitle': ViewUser.dispname,
                    'outputFeedURL': "/user/" + str(ViewUser.key().id()) + "/feed",
                }
            

            path = os.path.join(os.path.dirname(__file__), 'pages/usermainpage.html')
            
            self.response.out.write(template.render(path, template_values))

        else:
            #self.write('not found this user and any items')
            
            # Prompt 'Can not found this user, URL style have been changed since Dec.X 2008, Some of the old external links are invalid now.
            # But We offer you another options, You may check whether these Users, may be one of them is whom you are looking for.
            # Better UE idea!

            outputStringUserMainPageTitle = 'not found this user and any items'
            outputStringRoutineLog = 'None'
            self.error(404)
            self.redirect('/')  
Exemplo n.º 2
0
	def test_tarsusaCore_getuserNonPrivateItems(self):
		NonPrivateItems = tarsusaCore.get_UserNonPrivateItems(userid=1)