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