Skip to content
Permalink
Browse files
Accept 'now' in post-list date conditions
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Mar 7, 2017
1 parent c7dd69f commit 467f9fe8c19a32fedf60448b9149f0239d71167d
Showing with 15 additions and 7 deletions.
  1. +1 −0 CHANGES.txt
  2. +1 −1 docs/manual.txt
  3. +10 −4 nikola/packages/datecond/__init__.py
  4. +3 −2 nikola/plugins/compile/rest/post_list.py
@@ -4,6 +4,7 @@ New in master
Features
--------

* Accept ``now`` in post-list date conditions
* Add ``RSS_COPYRIGHT``, ``RSS_COPYRIGHT_PLAIN``, and
``RSS_COPYRIGHT_FORMATS`` options in conf.py which can be disabled
by specifying ``copyright_=False`` to ``generic_rss_renderer``, or
@@ -2354,7 +2354,7 @@ The following options are recognized:
* clause: attribute comparison_operator value (spaces optional)
* attribute: year, month, day, hour, month, second, weekday, isoweekday; or empty for full datetime
* comparison_operator: == != <= >= < >
* value: integer or dateutil-compatible date input
* value: integer, 'now' or dateutil-compatible date input

* ``tags`` : string [, string...]
Filter posts to show only posts having at least one of the ``tags``.
@@ -1,7 +1,6 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Date Conditionals (datecond)
# Version 0.1.2
# Date Conditionals v0.1.6
# Copyright © 2015-2017, Chris Warrick.
# All rights reserved.
#
@@ -36,6 +35,7 @@
"""Date range parser."""

from __future__ import print_function, unicode_literals
import datetime
import dateutil.parser
import re
import operator
@@ -54,7 +54,7 @@
}


def date_in_range(date_range, date, debug=True):
def date_in_range(date_range, date, debug=False, now=None):
"""Check if date is in the range specified.
Format:
@@ -63,7 +63,10 @@ def date_in_range(date_range, date, debug=True):
* attribute: year, month, day, hour, month, second, weekday, isoweekday
or empty for full datetime
* comparison_operator: == != <= >= < >
* value: integer or dateutil-compatible date input
* value: integer, 'now' or dateutil-compatible date input
The optional `now` parameter can be used to provide a specific `now` value
(if none is provided, datetime.datetime.now() is used).
"""
out = True

@@ -76,6 +79,9 @@ def date_in_range(date_range, date, debug=True):
elif attribute:
left = getattr(date, attribute)
right = int(value)
elif value == 'now':
left = date
right = now or datetime.datetime.now()
else:
left = date
right = dateutil.parser.parse(value)
@@ -99,7 +99,7 @@ class PostList(Directive):
* clause: attribute comparison_operator value (spaces optional)
* attribute: year, month, day, hour, month, second, weekday, isoweekday; or empty for full datetime
* comparison_operator: == != <= >= < >
* value: integer or dateutil-compatible date input
* value: integer, 'now' or dateutil-compatible date input
``tags`` : string [, string...]
Filter posts to show only posts having at least one of the ``tags``.
@@ -273,7 +273,8 @@ def _do_post_list(start=None, stop=None, reverse=False, tags=None, require_all_t
filtered_timeline = natsort.natsorted(filtered_timeline, key=lambda post: post.meta[lang][sort], alg=natsort.ns.F | natsort.ns.IC)

if date:
filtered_timeline = [p for p in filtered_timeline if date_in_range(date, p.date)]
_now = utils.current_time()
filtered_timeline = [p for p in filtered_timeline if date_in_range(date, p.date, now=_now)]

for post in filtered_timeline[start:stop:step]:
if slugs:

0 comments on commit 467f9fe

Please sign in to comment.