Commit 17bd328d authored by Tristan Cavelier's avatar Tristan Cavelier

erp5_discussion: change reference generation behavior on forum thread creation

Previous behavior was to generate reference this way : `sanitized title` if reference was not already existing else `random "-" sanitized title`.
Now it generates `sanitized title "-" random` with:
- `random` at the end to maximize search engine optimization;
- `random` every time present to prevent creating document with the same reference by mistake.

+ update tests
parent aa98df88
......@@ -27,17 +27,15 @@ membership_criterion_category_list = context.getMembershipCriterionCategoryList(
multimembership_criterion_base_category_list = context.getMultimembershipCriterionBaseCategoryList()
reference = context.Base_generateReferenceFromString(title)
existing_document = context.getDocumentValue(reference)
existing_web_section_list = portal.portal_catalog(id=reference, portal_type=['Web Site', 'Web Section'])
existing_module_list = portal.portal_catalog(id=reference, parent_uid=portal.getUid())
if existing_document is not None \
or len(existing_web_section_list) \
or len(existing_module_list):
# if there are other document or any tarversal objects (module, web section)
# which ID or reference duplicates just add some random part
# so we can distinguish)
reference = '%s-%s' %(context.Base_generateRandomString(), reference)
random_string_length = 10
while True:
random_reference = "%s-%s" % (reference, context.Base_generateRandomString(string_length=random_string_length))
if context.restrictedTraverse(random_reference, None) is None:
# object does not already exist (module, web site, web section, action, bound method, ...)
break
random_string_length += 1
reference = random_reference
category_list = []
create_kw = dict(title = title,
......
......@@ -111,6 +111,7 @@ class TestERP5Discussion(ERP5TypeTestCase):
Create a disucssion thread
"""
portal = self.portal
discussion_thread_id_set = set(portal.discussion_thread_module.objectIds())
# create web sections & set predicates
group1 = portal.portal_categories.group.newContent(portal_type='Category',
......@@ -122,8 +123,10 @@ class TestERP5Discussion(ERP5TypeTestCase):
self.tic()
web_section1.WebSection_createNewDiscussionThread('test1-new', 'test1 body')
discussion_thread = [x for x in self.portal.discussion_thread_module.objectValues() \
if x.getReference()=='test1-new'][0]
discussion_thread, = [x for x in self.portal.discussion_thread_module.objectValues() \
if x.getId() not in discussion_thread_id_set]
discussion_thread_id_set.add(discussion_thread.getId())
self.assertTrue(discussion_thread.getReference().startswith("test1-new-"))
# not indexed yet
self.assertSameSet([], web_section1.WebSection_getDiscussionThreadList())
......@@ -142,8 +145,10 @@ class TestERP5Discussion(ERP5TypeTestCase):
# check attachment creation
file = makeFileUpload('TEST-en-002.doc')
web_section1.WebSection_createNewDiscussionThread('test1-new-with-attachment', 'test1 body', file=file)
discussion_thread = [x for x in self.portal.discussion_thread_module.objectValues() \
if x.getReference()=='test1-new-with-attachment'][0]
discussion_thread, = [x for x in self.portal.discussion_thread_module.objectValues() \
if x.getId() not in discussion_thread_id_set]
discussion_thread_id_set.add(discussion_thread.getId())
self.assertTrue(discussion_thread.getReference().startswith("test1-new-with-attachment-"))
self.tic()
discussion_post = discussion_thread.contentValues(filter={'portal_type': 'Discussion Post'})[0]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment