changeset 2612:3e4e78de9cca

tools (date_utils): moved date_parse to common.date_utils, because it can be used in frontends
author Goffi <goffi@goffi.org>
date Thu, 21 Jun 2018 01:21:44 +0200
parents c180ca699e72
children 9680cd958529
files sat/plugins/plugin_exp_events.py sat/plugins/plugin_exp_pubsub_schema.py sat/plugins/plugin_tickets_import_bugzilla.py sat/plugins/plugin_xep_0234.py sat/tools/common/date_utils.py sat/tools/utils.py
diffstat 6 files changed, 21 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_exp_events.py	Thu Jun 21 01:21:44 2018 +0200
+++ b/sat/plugins/plugin_exp_events.py	Thu Jun 21 01:21:44 2018 +0200
@@ -24,6 +24,7 @@
 log = getLogger(__name__)
 from sat.tools import utils
 from sat.tools.common import uri as uri_parse
+from sat.tools.common import date_utils
 from twisted.internet import defer
 from twisted.words.protocols.jabber import jid, error
 from twisted.words.xish import domish
@@ -111,7 +112,7 @@
             raise exceptions.NotFound(_(u"No event with this id has been found"))
 
         try:
-            timestamp = utils.date_parse(next(event_elt.elements(NS_EVENT, "date")))
+            timestamp = date_utils.date_parse(next(event_elt.elements(NS_EVENT, "date")))
         except StopIteration:
             timestamp = -1
 
--- a/sat/plugins/plugin_exp_pubsub_schema.py	Thu Jun 21 01:21:44 2018 +0200
+++ b/sat/plugins/plugin_exp_pubsub_schema.py	Thu Jun 21 01:21:44 2018 +0200
@@ -22,6 +22,7 @@
 from sat.core.constants import Const as C
 from sat.tools import xml_tools
 from sat.tools import utils
+from sat.tools.common import date_utils
 from twisted.words.protocols.jabber import jid
 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
 from twisted.internet import defer
@@ -353,7 +354,7 @@
             return widget_type, args, kwargs
         # we convert XMPP date to timestamp
         try:
-            args[0] = unicode(utils.date_parse(args[0]))
+            args[0] = unicode(date_utils.date_parse(args[0]))
         except Exception as e:
             log.warning(_(u"Can't parse date field: {msg}").format(msg=e))
         return widget_type, args, kwargs
--- a/sat/plugins/plugin_tickets_import_bugzilla.py	Thu Jun 21 01:21:44 2018 +0200
+++ b/sat/plugins/plugin_tickets_import_bugzilla.py	Thu Jun 21 01:21:44 2018 +0200
@@ -26,7 +26,7 @@
 from twisted.internet import defer
 import os.path
 from lxml import etree
-from sat.tools import utils
+from sat.tools.common import date_utils
 
 
 PLUGIN_INFO = {
@@ -68,8 +68,8 @@
         for bug in root.xpath('bug'):
             ticket = {}
             ticket['id'] = bug.findtext('bug_id')
-            ticket['created'] = utils.date_parse(bug.findtext('creation_ts'))
-            ticket['updated'] = utils.date_parse(bug.findtext('delta_ts'))
+            ticket['created'] = date_utils.date_parse(bug.findtext('creation_ts'))
+            ticket['updated'] = date_utils.date_parse(bug.findtext('delta_ts'))
             ticket['title'] = bug.findtext('short_desc')
             reporter_elt = bug.find('reporter')
             ticket['author'] = reporter_elt.get('name')
@@ -103,7 +103,7 @@
                     who = longdesc.find('who')
                     comment = {'id': longdesc.findtext('commentid'),
                                'author_email': who.text,
-                               'published': utils.date_parse(longdesc.findtext('bug_when')),
+                               'published': date_utils.date_parse(longdesc.findtext('bug_when')),
                                'author': who.get('name', who.text),
                                'content': longdesc.findtext('thetext')}
                     comments.append(comment)
--- a/sat/plugins/plugin_xep_0234.py	Thu Jun 21 01:21:44 2018 +0200
+++ b/sat/plugins/plugin_xep_0234.py	Thu Jun 21 01:21:44 2018 +0200
@@ -26,6 +26,7 @@
 from zope.interface import implements
 from sat.tools import utils
 from sat.tools import stream
+from sat.tools.common import date_utils
 import os.path
 from twisted.words.xish import domish
 from twisted.words.protocols.jabber import jid
@@ -210,7 +211,7 @@
             pass
 
         try:
-            file_data[u'modified'] = utils.date_parse(next(file_elt.elements(NS_JINGLE_FT, u'date')))
+            file_data[u'modified'] = date_utils.date_parse(next(file_elt.elements(NS_JINGLE_FT, u'date')))
         except StopIteration:
             pass
 
--- a/sat/tools/common/date_utils.py	Thu Jun 21 01:21:44 2018 +0200
+++ b/sat/tools/common/date_utils.py	Thu Jun 21 01:21:44 2018 +0200
@@ -21,10 +21,21 @@
 
 from sat.core.constants import Const as C
 import datetime
+from dateutil import parser as dateutil_parser
 from babel import dates
+import calendar
 import time
 
 
+def date_parse(value):
+    """Parse a date and return corresponding unix timestamp
+
+    @param value(unicode): date to parse, in any format supported by dateutil.parser
+    @return (int): timestamp
+    """
+    return calendar.timegm(dateutil_parser.parse(unicode(value)).utctimetuple())
+
+
 def date_fmt(timestamp, fmt='short', date_only=False, auto_limit=7, auto_old_fmt='short', auto_new_fmt='relative', locale_str=C.DEFAULT_LOCALE):
     """format date according to locale
 
--- a/sat/tools/utils.py	Thu Jun 21 01:21:44 2018 +0200
+++ b/sat/tools/utils.py	Thu Jun 21 01:21:44 2018 +0200
@@ -25,10 +25,8 @@
 from sat.core.log import getLogger
 log = getLogger(__name__)
 import datetime
-from dateutil import parser as dateutil_parser
 from twisted.python import procutils
 import subprocess
-import calendar
 import time
 import sys
 import random
@@ -91,14 +89,6 @@
     template = u"{}T{}".format(template_date, template_time) if with_time else template_date
     return datetime.datetime.utcfromtimestamp(time.time() if timestamp is None else timestamp).strftime(template)
 
-def date_parse(value):
-    """Parse a date and return corresponding unix timestamp
-
-    @param value(unicode): date to parse, in any format supported by dateutil.parser
-    @return (int): timestamp
-    """
-    return calendar.timegm(dateutil_parser.parse(unicode(value)).utctimetuple())
-
 def generatePassword(vocabulary=None, size=20):
     """Generate a password with random characters.