示例#1
0
    def post(self):
        self.response.headers['Content-Type'] = 'text/html'
        if self.request.get('button') == 'Follow':

            id = self.request.get('id')
            following_user_key = ndb.Key('MyUser', id)
            following_user = following_user_key.get()
            his_follower = Follow.query(Follow.user == following_user)

            user = users.get_current_user()
            my_user_key = ndb.Key('MyUser', user.user_id())
            my_user = my_user_key.get()
            my_following = Follow.query(Follow.user == my_user)

            for i in his_follower:
                for j in i.followers:
                    if j.user_id != my_user.user_id:
                        i.followers.append(my_user)
                        i.put()
            for i in my_following:
                for j in i.following:
                    if j.user_id != following_user:
                        i.following.append(following_user)
                        i.put()

            self.redirect('/')

        if self.request.get('button') == 'Unfollow':

            id = self.request.get('id')
            following_user_key = ndb.Key('MyUser', id)
            following_user = following_user_key.get()
            his_follower = Follow.query(Follow.user == following_user)

            user = users.get_current_user()
            my_user_key = ndb.Key('MyUser', user.user_id())
            my_user = my_user_key.get()
            my_following = Follow.query(Follow.user == my_user)

            for i in his_follower:
                for j in i.followers:
                    if j.email_address == my_user.email_address:
                        i.followers.remove(j)
                i.put()
            for i in my_following:
                for j in i.following:
                    if j.email_address == following_user.email_address:
                        i.following.remove(j)
                i.put()

            self.redirect('/')
        if self.request.get('button') == 'Return to main page':
            self.redirect('/')
示例#2
0
    def get(self):
        self.response.headers['Content-Type'] = 'text/html'

        user = users.get_current_user()  #boolean
        id = self.request.get('id')
        myuser_key = ndb.Key('MyUser', id)
        myuser = myuser_key.get()

        my_follow = Follow.query()

        my_post = MyPost.query(
            MyPost.creator == myuser).order(-MyPost.creation_time)

        list_comment = ListComment.query()

        template_values = {
            'user': user,
            'my_post': my_post,
            'my_follow': my_follow,
            'myuser': myuser,
            'id': id,
            'list_comment': list_comment,
        }

        template = JINJA_ENVIRONMENT.get_template('myprofil.html')
        self.response.write(template.render(template_values))
示例#3
0
    def get(self):
        self.response.headers['Content-Type'] = 'text/html'

        user = users.get_current_user()  #boolean
        id = self.request.get('id')
        myuser_key = ndb.Key('MyUser', id)
        myuser = myuser_key.get()

        my_follow = Follow.query(Follow.user == myuser)

        template_values = {
            'user': user,
            'my_follow': my_follow,
            'myuser': myuser,
            'id': id,
        }

        template = JINJA_ENVIRONMENT.get_template('myfollower.html')
        self.response.write(template.render(template_values))
示例#4
0
    def get(self):
        self.response.headers['Content-Type'] = 'text/html'

        url = ''
        url_string = ''

        user = users.get_current_user() #boolean

        if user:
            url = users.create_logout_url(self.request.uri)
            url_string = 'logout'
            myuser_key = ndb.Key('MyUser', user.user_id()) #generate a key for MyUser type
            myuser = myuser_key.get()
            my_follow = Follow.query(Follow.user == myuser)
            if myuser == None: # if we find the key or not here not
                myuser = MyUser(id=user.user_id(), email_address = user.email(), user_id = user.user_id())
                myfollow = Follow(user=myuser)
                myuser.put() #commit myuser in datastore
                myfollow.put()

            key = ndb.Key('ListUser', 'default')
            list_user = key.get()
            if list_user == None:
                list_user = ListUser(id='default')
                list_user.put()

            my_following_post = MyPost.query()
            my_post = MyPost.query(MyPost.creator == myuser)
            list_comment = ListComment.query()

            list_post = ListPost.query(ListPost.user == myuser)
            if list_post.count() == 0:
                my_list_post = ListPost(user=myuser)
                for i in my_following_post:
                    for j in my_follow:
                        for k in j.following:
                            if i.creator == k:
                                my_list_post.list_post.append(i)
                for i in my_post:
                    my_list_post.list_post.append(i)
                my_list_post.list_post = sorted(my_list_post.list_post, key=lambda post: post.creation_time, reverse=True)
                if len(my_list_post.list_post) > 50:
                    my_list_post.list_post = my_list_post.list_post[0:50]
                my_list_post.put()
            else:
                for i in list_post:
                    i.key.delete()
                my_list_post = ListPost(user=myuser)
                for i in my_following_post:
                    for j in my_follow:
                        for k in j.following:
                            if i.creator == k:
                                my_list_post.list_post.append(i)
                for i in my_post:
                    my_list_post.list_post.append(i)
                my_list_post.list_post = sorted(my_list_post.list_post, key=lambda post: post.creation_time, reverse=True)
                if len(my_list_post.list_post) > 50:
                    my_list_post.list_post = my_list_post.list_post[0:50]
                my_list_post.put()


            template_values = {
                'url' : url,
                'url_string' : url_string,
                'user' : user,
                'upload_url' : blobstore.create_upload_url('/upload'),
                'list_user' : list_user,
                'my_following_post': my_following_post,
                'my_follow': my_follow,
                'list_post': list_post,
                'list_comment': list_comment,
            }


        else:
            url = users.create_login_url(self.request.uri)
            url_string = 'login'

            template_values = {
                'url' : url,
                'url_string' : url_string,
            }

        template = JINJA_ENVIRONMENT.get_template('main.html')
        self.response.write(template.render(template_values))