Repository: getnikola/nikola

Issue:
new_post_date_path uses today's date, instead of rrule's date
If a new post is created with `new_post` and a schedule rule is present, the rule is not taken into account when `new_post_date_path` is present. Thus, for example, one can have a structure like 2017/01/30 and a post dated 2017-02-05.


Discussion:

Resolving patch:
diff --git a/nikola/plugins/command/new_post.py b/nikola/plugins/command/new_post.py
--- a/nikola/plugins/command/new_post.py
+++ b/nikola/plugins/command/new_post.py
@@ -111,7 +111,7 @@ def get_date(schedule=False, rule=None, last_date=None, tz=None, iso8601=False):
         else:
             tz_str = ' UTC'
 
-    return date.strftime('%Y-%m-%d %H:%M:%S') + tz_str
+    return (date.strftime('%Y-%m-%d %H:%M:%S') + tz_str, date)
 
 
 class CommandNewPost(Command):
@@ -325,7 +325,7 @@ def _execute(self, options, args):
         self.site.scan_posts()
         timeline = self.site.timeline
         last_date = None if not timeline else timeline[0].date
-        date = get_date(schedule, rule, last_date, self.site.tzinfo, self.site.config['FORCE_ISO8601'])
+        date, dateobj = get_date(schedule, rule, last_date, self.site.tzinfo, self.site.config['FORCE_ISO8601'])
         data = {
             'title': title,
             'slug': slug,
@@ -341,7 +341,7 @@ def _execute(self, options, args):
             suffix = pattern[1:]
             output_path = os.path.dirname(entry[0])
             if date_path_auto or date_path_opt:
-                output_path += os.sep + datetime.datetime.now(self.site.tzinfo).strftime(date_path_format)
+                output_path += os.sep + dateobj.strftime(date_path_format)
 
             txt_path = os.path.join(output_path, slug + suffix)
             meta_path = os.path.join(output_path, slug + ".meta")


Test patch:
diff --git a/tests/test_scheduling.py b/tests/test_scheduling.py
--- a/tests/test_scheduling.py
+++ b/tests/test_scheduling.py
@@ -52,80 +52,80 @@ def test_get_date(self):
 
         # NOW does not match rule #########################################
         # No last date
-        expected = TODAY.replace(day=23).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_FR, tz=UTC))
-        self.assertEqual(expected, get_date(True, RULE_FR, tz=UTC))
+        expected = TODAY.replace(day=23)
+        self.assertEqual(expected, get_date(True, RULE_FR, tz=UTC)[1])
+        self.assertEqual(expected, get_date(True, RULE_FR, tz=UTC)[1])
 
         # Last date in the past; doesn't match rule
         date = TODAY.replace(hour=7)
-        expected = TODAY.replace(day=23, hour=7).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC))
+        expected = TODAY.replace(day=23, hour=7)
+        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC)[1])
 
         # Last date in the future; doesn't match rule
         date = TODAY.replace(day=24, hour=7)
-        expected = TODAY.replace(day=30, hour=7).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC))
+        expected = TODAY.replace(day=30, hour=7)
+        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC)[1])
 
         # Last date in the past; matches rule
         date = TODAY.replace(day=16, hour=8)
-        expected = TODAY.replace(day=23, hour=8).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC))
+        expected = TODAY.replace(day=23, hour=8)
+        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC)[1])
 
         # Last date in the future; matches rule
         date = TODAY.replace(day=23, hour=18)
-        expected = TODAY.replace(day=30, hour=18).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC))
+        expected = TODAY.replace(day=30, hour=18)
+        self.assertEqual(expected, get_date(True, RULE_FR, date, tz=UTC)[1])
 
         # NOW matches rule ################################################
         # Not scheduling should return NOW
-        self.assertEqual(NOW, get_date(False, RULE_TH, tz=UTC))
+        self.assertEqual(_NOW, get_date(False, RULE_TH, tz=UTC)[1])
         # No last date
-        self.assertEqual(NOW, get_date(True, RULE_TH, tz=UTC))
-        self.assertEqual(NOW, get_date(True, RULE_TH, tz=UTC))
+        self.assertEqual(_NOW, get_date(True, RULE_TH, tz=UTC)[1])
+        self.assertEqual(_NOW, get_date(True, RULE_TH, tz=UTC)[1])
 
         # Last date in the past; doesn't match rule
         # Corresponding time has already passed, today
         date = TODAY.replace(day=21, hour=7)
-        expected = TODAY.replace(day=29, hour=7).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
+        expected = TODAY.replace(day=29, hour=7)
+        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC)[1])
         # Corresponding time has not passed today
         date = TODAY.replace(day=21, hour=18)
-        expected = TODAY.replace(day=22, hour=18).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
+        expected = TODAY.replace(day=22, hour=18)
+        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC)[1])
 
         # Last date in the future; doesn't match rule
         # Corresponding time has already passed, today
         date = TODAY.replace(day=24, hour=7)
-        expected = TODAY.replace(day=29, hour=7).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
+        expected = TODAY.replace(day=29, hour=7)
+        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC)[1])
         # Corresponding time has not passed today
         date = TODAY.replace(day=24, hour=18)
-        expected = TODAY.replace(day=29, hour=18).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
+        expected = TODAY.replace(day=29, hour=18)
+        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC)[1])
 
         # Last date in the past; matches rule
         # Corresponding time has already passed, today
         date = TODAY.replace(day=15, hour=7)
-        expected = TODAY.replace(day=29, hour=7).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
+        expected = TODAY.replace(day=29, hour=7)
+        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC)[1])
         # Corresponding time has already passed, today; rule specifies HOUR
         date = TODAY.replace(day=15, hour=7)
-        expected = TODAY.replace(day=29, hour=9).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_TH + ';BYHOUR=9', date, tz=UTC))
+        expected = TODAY.replace(day=29, hour=9)
+        self.assertEqual(expected, get_date(True, RULE_TH + ';BYHOUR=9', date, tz=UTC)[1])
         # Corresponding time has not passed today
         date = TODAY.replace(day=15, hour=18)
-        expected = TODAY.replace(day=22, hour=18).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
+        expected = TODAY.replace(day=22, hour=18)
+        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC)[1])
 
         # Last date in the future; matches rule
         # Corresponding time has already passed, today
         date = TODAY.replace(day=29, hour=7)
-        expected = TODAY.replace(day=5, month=9, hour=7).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
+        expected = TODAY.replace(day=5, month=9, hour=7)
+        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC)[1])
         # Corresponding time has not passed today
         date = TODAY.replace(day=22, hour=18)
-        expected = TODAY.replace(day=29, hour=18).strftime(FMT)
-        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC))
+        expected = TODAY.replace(day=29, hour=18)
+        self.assertEqual(expected, get_date(True, RULE_TH, date, tz=UTC)[1])
 
 if __name__ == '__main__':
     import unittest