1. Marc-Alexandre Chan
  2. DailyPromptBot

Commits

Marc-Alexandre Chan  committed c8b2864

Added support for db set_posted to update post time. Updated events accordingly.

Fixes issue 3.

  • Participants
  • Parent commits f346b59
  • Branches dev-minibot

Comments (0)

Files changed (2)

File minibot/db.py

View file
  • Ignore whitespace
         self.post_time = post_time
         self.r_post_id = None
 
-    def set_posted(self, post_id):
-        """ Set the prompt entry as posted and store the post id. Note that
-        post_time is not updated—consider calling Prompt.queue() or setting
-        post_time if the value needs to be set first. """
+    def set_posted(self, post_id, post_time):
+        """ Set the prompt entry as posted and store the post id and time.
+        ``post_id`` is a string; ``post_time`` is a ``datetime`` object (in UTC)
+        or a UNIX timestamp (float). """
         self.status = self.STATUS_POSTED
         self.r_post_id = post_id
+        try:
+            self.post_time = datetime.utcfromtimestamp(post_time)
+        except ValueError:
+            self.post_time = post_time
 
     def __repr__(self):
         """ Return a human-readable representation of the Prompt row. """
     def get_status_string(self):
         return self.STATUS_TEXT[self.status]
 
-    def set_posted(self, post_id):
-        """ Sets status as posted and records post data. """
-        if not isinstance(post_id, basestring):
-            raise ValueError('post_id must be a string')
+    def set_posted(self, post_id, post_time):
+        """ Sets status as posted and records post id and time.
+        ``post_id`` is a string; ``post_time`` is a ``datetime`` object or a
+        UNIX timestamp (float). """
         self.status = self.STATUS_ACTIVE
         self.r_post_id = post_id
+        try:
+            self.post_time = datetime.utcfromtimestamp(post_time)
+        except ValueError:
+            self.post_time = post_time
 
     def set_archived(self):
         """ Sets status as archived. """

File minibot/events.py

View file
  • Ignore whitespace
         db.close()
 
     def end(self):
-        db.close()
+        self.res['dbsession'].close()
 
     def handle_exception(self, e):
         self.res['dbsession'].rollback()
                 ''.join(["Prompt ", self.id, " not found."]))
 
         r_sub = reddit.submit(target, self._get_title(), self._get_text())
-        prompt.set_posted(r_sub.id)
+        prompt.set_posted(r_sub.id, r_sub.created_utc)
         self.log.info("%s: Posted prompt %d to /r/%s", classname(self),
             prompt.id, target)
         db.commit()
 
         r_post = reddit.submit(target, self._get_title(), self._get_text())
 
-        thread.set_posted(r_post.id)
+        thread.set_posted(r_post.id, r_post.created_utc)
         self._archive_older_threads()
         self._add_link_to_last_thread()
         self.log.info("%s: Posted suggestion thread %d to /r/%s",