类似Django的基于日期的归档,带有Flask和SqlAlchemy

类似Django的基于日期的归档,带有Flask和SqlAlchemy,sqlalchemy,flask,Sqlalchemy,Flask,我将重温python和web开发。我以前用过Django,但已经有一段时间了。Flask+SqlAlchemy对我来说是全新的,但我喜欢它给我的控制 出发;下面的代码在我的dev服务器上工作得很好。但我仍然觉得它并不像它可能的那么小和高效。我想知道是否有人构建了类似的解决方案。现在,我正试图找到一种方法来使用单个查询并将关键字参数格式化到其中。此外,我认为围绕函数构建一个类以使其更易于重用可能对我很有用 以下是基于日期构造查询的函数: def live_post_filter(year=None

我将重温python和web开发。我以前用过Django,但已经有一段时间了。Flask+SqlAlchemy对我来说是全新的,但我喜欢它给我的控制

出发;下面的代码在我的dev服务器上工作得很好。但我仍然觉得它并不像它可能的那么小和高效。我想知道是否有人构建了类似的解决方案。现在,我正试图找到一种方法来使用单个查询并将关键字参数格式化到其中。此外,我认为围绕函数构建一个类以使其更易于重用可能对我很有用

以下是基于日期构造查询的函数:

def live_post_filter(year=None, month=None, day=None):
    """ Query to filter only published Posts exluding drafts 
    Takes additional arguments to filter by year, month and day
    """
    live = Post.query.filter(Post.status == Post.LIVE_STATUS).order_by(Post.pub_date.desc())

    if year and month and day:
        queryset = live.filter(extract('year', Post.pub_date) == year,
                           extract('month', Post.pub_date) == month,
                           extract('day', Post.pub_date) == day).all()
    elif year and month:
        queryset = live.filter(extract('year', Post.pub_date) == year,
                           extract('month', Post.pub_date) == month).all()
    elif year:
        queryset = live.filter(extract('year', Post.pub_date) == year).all()
    else:
        queryset = live.all()

    return queryset
以下是我如何从视图调用上述函数:

@mod.route('/api/get_posts/', methods = ['GET'])
@mod.route('/api/get_posts/<year>/<month>/<day>/', methods = ['GET'])
@mod.route('/api/get_posts/<year>/<month>/', methods = ['GET'])
@mod.route('/api/get_posts/<year>/', methods = ['GET'])
def get_posts(year=None, month=None, day=None):
    posts = live_post_filter(year=year, month=month, day=day)
    postlist = []
    if request.method == 'GET':
    # do stuff 
@mod.route('/api/get_posts/',methods=['get']))
@mod.route('/api/get_posts//',methods=['get'])
@mod.route('/api/get_posts//',methods=['get'])
@mod.route('/api/get_posts/',methods=['get'])
def get_posts(年=无,月=无,日=无):
帖子=实时帖子过滤器(年=年,月=月,日=日)
postlist=[]
如果request.method==“GET”:
#做事

如上所述,所有这些感觉都很笨拙,如果有任何建议,我将不胜感激。

使用
extract
按日期过滤组件对我来说似乎很奇怪。我将创建一个辅助函数,该函数返回
参数的日期范围:

def get_date_range(year=None, month=None, day=None):
    from_date = None
    to_date = None
    if year and month and day:
        from_date = datetime(year, month, day)
        to_date = from_date
    elif year and month:
        from_date = datetime(year, month, 1)
        month += 1
        if month > 12:
            month = 1
            year += 1
        to_date = datetime(year, month, 1)
    elif year:
        from_date = datetime(year, 1, 1)
        to_date = datetime(year + 1, 1, 1)
    return from_date, to_date
然后,查询功能变得更加简单:

def live_post_filter(year=None, month=None, day=None):
    """ Query to filter only published Posts exluding drafts 
    Takes additional arguments to filter by year, month and day
    """
    live = Post.query.filter(Post.status == Post.LIVE_STATUS).order_by(Post.pub_date.desc())
    from_date, to_date = get_date_range(year, month, day)
    if from_date and to_date:
        live = live.filter(Post.pub_date >= from_date, Post.pub_date < to_date)
    return live.all()
def live_post_过滤器(年=无,月=无,日=无):
“”“查询以仅筛选排除草稿的已发布帖子
接受按年、月和日筛选的附加参数
"""
live=Post.query.filter(Post.status==Post.live\u status).order\u by(Post.pub\u date.desc())
从日期到日期=获取日期范围(年、月、日)
如果从年月日到年月日:
live=live.filter(Post.pub_date>=from_date,Post.pub_date
一些提示:您只需要一个
route
调用,因为您可以使用
defaults
关键字参数在未提供任何内容时提供默认值
GET
是默认方法。您可以通过转换将路由限制为有效值。因此,您的所有路由装饰器都可以替换为以下内容:
@mod.route('/api/get_posts//',defaults={'year':None,'month':None,'day':None})
如果我只使用单个装饰器,那么出于某种原因,只有端点可以工作。我还不知道为什么……谢谢,这很好用。以后可能会用结果更新我的答案。