Skip to content

Commit 467f9fe

Browse files
committedMar 7, 2017
Accept 'now' in post-list date conditions
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
1 parent c7dd69f commit 467f9fe

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed
 

‎CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ New in master
44
Features
55
--------
66

7+
* Accept ``now`` in post-list date conditions
78
* Add ``RSS_COPYRIGHT``, ``RSS_COPYRIGHT_PLAIN``, and
89
``RSS_COPYRIGHT_FORMATS`` options in conf.py which can be disabled
910
by specifying ``copyright_=False`` to ``generic_rss_renderer``, or

‎docs/manual.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,7 @@ The following options are recognized:
23542354
* clause: attribute comparison_operator value (spaces optional)
23552355
* attribute: year, month, day, hour, month, second, weekday, isoweekday; or empty for full datetime
23562356
* comparison_operator: == != <= >= < >
2357-
* value: integer or dateutil-compatible date input
2357+
* value: integer, 'now' or dateutil-compatible date input
23582358

23592359
* ``tags`` : string [, string...]
23602360
Filter posts to show only posts having at least one of the ``tags``.

‎nikola/packages/datecond/__init__.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python
22
# -*- encoding: utf-8 -*-
3-
# Date Conditionals (datecond)
4-
# Version 0.1.2
3+
# Date Conditionals v0.1.6
54
# Copyright © 2015-2017, Chris Warrick.
65
# All rights reserved.
76
#
@@ -36,6 +35,7 @@
3635
"""Date range parser."""
3736

3837
from __future__ import print_function, unicode_literals
38+
import datetime
3939
import dateutil.parser
4040
import re
4141
import operator
@@ -54,7 +54,7 @@
5454
}
5555

5656

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

@@ -76,6 +79,9 @@ def date_in_range(date_range, date, debug=True):
7679
elif attribute:
7780
left = getattr(date, attribute)
7881
right = int(value)
82+
elif value == 'now':
83+
left = date
84+
right = now or datetime.datetime.now()
7985
else:
8086
left = date
8187
right = dateutil.parser.parse(value)

‎nikola/plugins/compile/rest/post_list.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class PostList(Directive):
9999
* clause: attribute comparison_operator value (spaces optional)
100100
* attribute: year, month, day, hour, month, second, weekday, isoweekday; or empty for full datetime
101101
* comparison_operator: == != <= >= < >
102-
* value: integer or dateutil-compatible date input
102+
* value: integer, 'now' or dateutil-compatible date input
103103
104104
``tags`` : string [, string...]
105105
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
273273
filtered_timeline = natsort.natsorted(filtered_timeline, key=lambda post: post.meta[lang][sort], alg=natsort.ns.F | natsort.ns.IC)
274274

275275
if date:
276-
filtered_timeline = [p for p in filtered_timeline if date_in_range(date, p.date)]
276+
_now = utils.current_time()
277+
filtered_timeline = [p for p in filtered_timeline if date_in_range(date, p.date, now=_now)]
277278

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

0 commit comments

Comments
 (0)
Failed to load comments.