Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backported utils.generate_unique_id() method #2572

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Breaking changes:

New features:

- *add item here*
- Ported generateUniqueId skin script to utils.generate_unique_id() method.
https://github.com/plone/Products.CMFPlone/issues/1801
[reinhardt]

Bug fixes:

Expand Down
25 changes: 25 additions & 0 deletions Products/CMFPlone/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from zope.interface import alsoProvides
from plone.subrequest.interfaces import ISubRequest

import re
import unittest


Expand Down Expand Up @@ -147,6 +148,30 @@ def physicalPathFromURL(self, url):
ctx.vh_root = '/approot/PloneSite/folder/SubSite'
self.assertEqual(get_top_site_from_url(ctx, req).id, 'SubSite')

def test_generate_unique_id_no_clash(self):
from Products.CMFPlone.utils import generate_unique_id

ids = [generate_unique_id('some_type') for i in range(20)]
self.assertEqual(
len(ids),
len(set(ids)),
)

def test_generate_unique_id_format(self):
from Products.CMFPlone.utils import generate_unique_id

id_pattern = re.compile('[0-9]{4}-[0-9]{2}-[0-9]{2}\.[0-9]{10}')
id1 = generate_unique_id(None)
id2 = generate_unique_id(None)
self.assertIsNotNone(
id_pattern.match(id1),
msg='ID does not have expected format: {0}'.format(id1),
)
self.assertIsNotNone(
id_pattern.match(id2),
msg='ID does not have expected format: {0}'.format(id2),
)


class LogoTests(PloneTestCase.PloneTestCase):

Expand Down
14 changes: 14 additions & 0 deletions Products/CMFPlone/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from Products.CMFPlone import PloneMessageFactory as _
from Products.CMFPlone.interfaces.controlpanel import IImagingSchema
from Products.CMFPlone.interfaces.siteroot import IPloneSiteRoot
from random import random
from types import ClassType
from urlparse import urlparse
from webdav.interfaces import IWriteLock
Expand Down Expand Up @@ -798,3 +799,16 @@ def _safe_format(inst, method):
as we do in CMFPlone/__init__.py.
"""
return SafeFormatter(inst).safe_format


def generate_unique_id(type_name):
now = DateTime()
time = '%s.%s' % (now.strftime('%Y-%m-%d'), str(now.millis())[7:])
rand = str(random())[2:6]
prefix = ''

if type_name is not None:
prefix = type_name.replace(' ', '_') + '.'
prefix = prefix.lower()

return prefix + time + rand