Esempio n. 1
0
def get_timestamp(context, formatting=None):
    """Get timestamp matching context modification date"""
    if formatting == 'iso':
        format_func = datetime.isoformat
    else:
        format_func = datetime.timestamp
    zdc = IZopeDublinCore(context, None)
    if zdc is not None:
        return format_func(tztime(zdc.modified))
    return format_func(tztime(datetime.utcnow()))
Esempio n. 2
0
 def check_history(self, duration, length):
     """Check history container contents"""
     if duration:
         now = tztime(datetime.utcnow())
         for key in list(self.keys()):
             if (now - tztime(self[key].date)).days > duration:
                 del self[key]
     if length and (len(self) > length):
         keys = sorted(self.keys(), reverse=True)[:length]
         for key in list(self.keys()):
             if key not in keys:
                 del self[key]
Esempio n. 3
0
 def get_metas(self):
     """Metas getter"""
     config = IZMIConfiguration(self.request.root, None)
     if (config is not None) and (config.favicon is not None):
         icon = config.favicon
         icon_url = absolute_url(icon, self.request)
         icon_size = icon.get_image_size()[0]  # pylint: disable=no-member
         dc = IZopeDublinCore(icon)  # pylint: disable=invalid-name
         timestamp = datetime.timestamp(tztime(dc.modified))
         for size in (180, 144, 114, 72, 32, 16):
             if icon_size >= size:
                 yield LinkMeta(
                     'apple-touch-icon',
                     type=icon.content_type,  # pylint: disable=no-member
                     href=THUMB_HREF_STR.format(icon_url, size, size,
                                                timestamp),
                     sizes='{0}x{0}'.format(size))
         for size in (128, 124, 32):
             if icon_size >= size:
                 yield LinkMeta(
                     'icon',
                     type=icon.content_type,  # pylint: disable=no-member
                     href=THUMB_HREF_STR.format(icon_url, size, size,
                                                timestamp),
                     sizes='{0}x{0}'.format(size))
         yield LinkMeta('shortcut-icon',
                        type=icon.content_type,
                        href=icon_url)  # pylint: disable=no-member
Esempio n. 4
0
 def get_trigger(self, task):
     """Get loop interval trigger"""
     if not self.marker_interface.providedBy(task):
         raise Exception(
             _("Task is not configured for loop-style scheduling!"))
     info = self.schema(task, None)
     if info is None:
         return None
     return IntervalTrigger(
         weeks=info.weeks,
         days=info.days,
         hours=info.hours,
         minutes=info.minutes,
         seconds=info.seconds,
         start_date=tztime(date_to_datetime(info.start_date)),
         end_date=tztime(date_to_datetime(info.end_date)))
Esempio n. 5
0
 def _log_report(report, message, add_timestamp=True, level=logging.INFO):
     """Execution log report"""
     if isinstance(message, bytes):
         message = message.decode()
     if add_timestamp:
         message = '{0} - {1}'.format(tztime(datetime.utcnow()).strftime('%c'), message)
     if report is not None:
         report.write(message + '\n')
     LOGGER.log(level, message)
Esempio n. 6
0
 def get_trigger(self, task):
     """Get date trigger"""
     if not self.marker_interface.providedBy(task):
         raise Exception(
             _("Task is not configured for date-style scheduling!"))
     info = self.schema(task, None)
     if info is None:
         return None
     return DateTrigger(run_date=tztime(date_to_datetime(info.start_date)))
Esempio n. 7
0
 def _log_exception(report, message=None):
     """Exception log report"""
     if isinstance(message, bytes):
         message = message.decode()
     message = '{0} - {1}'.format(tztime(datetime.utcnow()).strftime('%c'),
                                  message or 'An error occurred') + '\n\n'
     if report is not None:
         report.write(message)
         report.write(traceback.format_exc() + '\n')
     LOGGER.exception(message)
Esempio n. 8
0
 def publication(self):
     """Publication label used to display workflow publication state"""
     request = check_request()
     translate = request.localizer.translate
     sm = get_utility(ISecurityManager)  # pylint: disable=invalid-name
     return translate(_('{date} by {principal}')).format(
         date=format_datetime(tztime(
             IWorkflowPublicationInfo(self).publication_date),
                              request=request),
         principal=translate(sm.get_principal(self.publisher).title))
Esempio n. 9
0
 def get_trigger(self, task):
     """Get cron-like task trigger"""
     if not self.marker_interface.providedBy(task):
         raise Exception(
             _("Task is not configured for cron-style scheduling!"))
     info = self.schema(task, None)
     if info is None:
         return None
     return CronTrigger(year=info.year or '*',
                        month=info.month or '*',
                        day=info.day or '*',
                        week=info.week or '*',
                        day_of_week=info.day_of_week or '*',
                        hour=info.hour or '*',
                        minute=info.minute or '*',
                        second=info.second or '0',
                        start_date=tztime(date_to_datetime(
                            info.start_date)),
                        end_date=tztime(date_to_datetime(info.end_date)))
Esempio n. 10
0
 def is_published(self, check_parent=True):
     """Check if content is published"""
     # check is parent is also published...
     if check_parent:
         parent = get_parent(self.__parent__,
                             IWorkflowPublicationSupport,
                             allow_context=False)
         if (parent is not None
             ) and not IWorkflowPublicationInfo(parent).is_published():
             return False
     # associated workflow?
     workflow = IWorkflow(self.__parent__, None)
     if (workflow is not None) and not workflow.visible_states:
         return False
     # check content versions
     versions = IWorkflowVersions(self.__parent__, None)
     if (versions is not None) and not versions.get_versions(
             workflow.visible_states):
         return False
     now = tztime(datetime.utcnow())
     return (self.publication_effective_date is not None) and \
            (self.publication_effective_date <= now) and \
            ((self.publication_expiration_date is None) or
             (self.publication_expiration_date >= now))
Esempio n. 11
0
def format_date(value, format_string=EXT_DATE_FORMAT, request=None):
    """Format given date with the given format

    :param datetime value: the value to format
    :param str format_string: a format string to use by `strftime` function
    :param request: the request from which to extract localization info for translation
    :return: str; input datetime converted to given format

    >>> from datetime import datetime
    >>> from pyams_utils.date import format_date, SH_DATE_FORMAT
    >>> value = datetime(2016, 11, 15, 10, 13, 12)
    >>> format_date(value)
    'on 15/11/2016'
    >>> format_date(value, SH_DATE_FORMAT)
    '15/11/2016'
    >>> format_date(None)
    '--'
    """
    if not value:
        return '--'
    if request is None:
        request = check_request()
    localizer = request.localizer
    return datetime.strftime(tztime(value), localizer.translate(format_string))
Esempio n. 12
0
 def to_field_value(self, value):
     if not value:
         return None
     return tztime(parse_date(value))
Esempio n. 13
0
 def to_widget_value(self, value):
     if not value:
         return None
     return tztime(value).isoformat()