Beispiel #1
0
    def add_about(self, uid):
        """This method adds about info for the logged in user

        Args:
            uid: The user id
        """
        if self.user and uid == str(self.user.key().id()):
            about = self.request.get('about')
            if about:
                self.user.about = about
                UserEntity.commit_user(self.user)

        self.redirect('/')
Beispiel #2
0
    def like_post(self, post):
        """This method adds likes for the given post

        Args:
            post: The post
        """
        if not self.user:
            return self.redirect('/login')

        # Check if the user has already liked this post
        pid = post.key().id()
        if not self.user.check_likes(pid):
            self.user.liked_posts.append(pid)
            UserEntity.commit_user(self.user)
        elif post:
            return self.redirect('/{0}'.format(pid))
        self.redirect('/')
Beispiel #3
0
    def unlike_post(self, post):
        """This method reduces likes for the post with given post id

        Args:
            pid: The post id
        """
        if not self.user:
            return self.redirect('/login')

        # Check if the user has already unliked this post
        pid = post.key().id()
        if self.user.check_likes(pid):
            self.user.liked_posts.remove(pid)
            UserEntity.commit_user(self.user)
        elif post:
            return self.redirect('/{0}'.format(pid))

        self.redirect('/')
Beispiel #4
0
    def initialize(self, *args, **kwargs):
        """This method overrides RequestHandler initialize method to read cookie value

        Args:
            *args: The args
            **kwargs: The keyworded args
        """
        webapp2.RequestHandler.initialize(self, *args, **kwargs)
        uid = self.read_secure_cookie('user-id')
        self.user = uid and UserEntity.by_id(int(uid))
Beispiel #5
0
    def post(self):
        """This method posts to the register page"""
        username = self.request.get('username')
        password = self.request.get('password')
        verify = self.request.get('verify')
        email = self.request.get('email')

        params = dict(username=username,
                      email=email)
        have_error = False

        if not RegisterHandler.valid_username(username):
            params['error_username'] = "******"
            have_error = True

        if not RegisterHandler.valid_password(password):
            params['error_password'] = "******"
            have_error = True
        elif password != verify:
            params['error_verify'] = "Your passwords didn't match."
            have_error = True

        if not RegisterHandler.valid_email(email):
            params['error_email'] = "That's not a valid email."
            have_error = True

        # In case of error re-render the register page
        if have_error:
            self.render('register.html', **params)
        else:
            if UserEntity.by_name(username):
                self.render(
                    'register.html', error_username="******")
            else:
                # Create hash for password for security
                hashed_password = SecurityProvider.make_secure_password(
                    username, password)
                new_user = UserEntity.register(
                    username, hashed_password, email)
                UserEntity.commit_user(new_user)
                self.create_login_cookie(new_user)
                self.redirect('/welcome')
Beispiel #6
0
    def login(cls, name, password):
        """This function checks given username and password and returns the user if it exists

        Args:
            name: The user name
            password: The password

        Returns:
            Success: The user entity
            Failure: None
        """
        user = UserEntity.by_name(name)

        # If user exists, check the password with saved hash
        if user and SecurityProvider.check_secure_password(
                name, password, user.password):
            return user
Beispiel #7
0
    def about_user(self, *args):
        """This method gets the about page for a given user

        Args:
            args[0]: The username(unused, added for more readability of route)
            args[1]: The user id
        """
        user_for_about = len(args) == 2 and UserEntity.by_id(int(args[1]))
        if user_for_about:
            user_posts = UserInfoHandler.get_all_posts(
                user_for_about.key().id())
            if not user_posts:
                user_posts = []
            return self.render('user_info.html',
                               userA=user_for_about,
                               user_posts=user_posts)

        self.redirect("/")
Beispiel #8
0
 def likes(self):
     """Returns the number likes for given post"""
     return UserEntity.gql("WHERE liked_posts = :1",
                           self.key().id()).count()