def list_posts(self, order, start, end):
        """
        Lists posts in a period of time, ordering the results according to the constraints defined by the parameters of the function
        
        Args:
            order (datetime): How the posts must be ordered. Options are 'ups' (number of upvotes) or 'comments' number of comments
            start (datetime): The start of the period 
            end (datetime): The end of the period 
                
        Returns:
            [List(Post)]: List of Posts ordered accordingly
        """

        try:
            order_clause = DbPost.comments.desc(
            ) if order == 'comments' else DbPost.ups.desc()
            db_posts = DbPost.select().where(DbPost.created.between(
                start, end)).order_by(order_clause)
            return [
                Post(title=p.title,
                     ups=p.ups,
                     comments=p.comments,
                     created=p.created,
                     author=p.author.name) for p in db_posts
            ]
        except Exception as e:
            raise Exception('error listing post')
Exemplo n.º 2
0
    def execute(self, request):
        response = Response()
        try:
            for post in [p['data'] for p in request.endpoint_data['data']['children']]:
                user_id = self.user_repo.add_user(User(name = post['author'], ups = post['ups'], comments = post['num_comments']))
                created_time = datetime.fromtimestamp(post['created_utc'], tz = pytz.timezone(request.timezone))
                self.post_repo.add_post(Post(title = post['title'], ups = post['ups'], comments = post['num_comments'], created = created_time, author = user_id))
            return response

        except Exception as e:
            response.add_exception_error(e)
            return response