Commit 25ff6c76 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_crm: Limit ticket creations per project

    Dont use cache to check ticket creation is closed.
    Use countResults to not require len() over the list.
parent fae625b7
portal = context.getPortalObject() portal = context.getPortalObject()
if portal.ERP5Site_isSupportRequestCreationClosed():
# Stop process alarm if there are too many tickets
return
monitor_enabled_category = portal.restrictedTraverse( monitor_enabled_category = portal.restrictedTraverse(
"portal_categories/monitor_scope/enabled", None) "portal_categories/monitor_scope/enabled", None)
......
portal = context.getPortalObject() portal = context.getPortalObject()
if portal.ERP5Site_isSupportRequestCreationClosed():
# Stop verification if there are too much tickets
return
portal.portal_catalog.searchAndActivate( portal.portal_catalog.searchAndActivate(
portal_type='Instance Tree', portal_type='Instance Tree',
validation_state='validated', validation_state='validated',
......
portal = context.getPortalObject() portal = context.getPortalObject()
if portal.ERP5Site_isSupportRequestCreationClosed():
# Stop process alarm if there are too many tickets
return
monitor_enabled_category = portal.restrictedTraverse( monitor_enabled_category = portal.restrictedTraverse(
"portal_categories/monitor_scope/enabled", None) "portal_categories/monitor_scope/enabled", None)
......
from DateTime import DateTime from DateTime import DateTime
portal = context.getPortalObject() portal = context.getPortalObject()
if (context.getMonitorScope() == "disabled") or \ if (context.getMonitorScope() == "disabled"):
portal.ERP5Site_isSupportRequestCreationClosed(): return
project = context.getFollowUpValue()
if project.Project_isSupportRequestCreationClosed():
return return
software_installation_list = portal.portal_catalog( software_installation_list = portal.portal_catalog(
......
from DateTime import DateTime from DateTime import DateTime
portal = context.getPortalObject() portal = context.getPortalObject()
if (context.getMonitorScope() == "disabled") or \ if (context.getMonitorScope() == "disabled"):
portal.ERP5Site_isSupportRequestCreationClosed(): return
project = context.getFollowUpValue()
if project.Project_isSupportRequestCreationClosed():
return return
reference = context.getReference() reference = context.getReference()
...@@ -76,8 +79,6 @@ if not should_notify: ...@@ -76,8 +79,6 @@ if not should_notify:
context.getTitle(), context.getReference(), last_contact) context.getTitle(), context.getReference(), last_contact)
if should_notify: if should_notify:
project = context.getFollowUpValue()
support_request = project.Project_createSupportRequestWithCausality( support_request = project.Project_createSupportRequestWithCausality(
ticket_title, ticket_title,
description, description,
......
from Products.ERP5Type.Cache import CachingMethod
portal = context.getPortalObject()
def isSupportRequestCreationClosed(destination_decision=None):
limit = int(portal.portal_preferences.getPreferredSupportRequestCreationLimit(5))
kw = {
'limit': limit,
'portal_type': 'Support Request',
'simulation_state': ["validated", "submitted"],
'resource__uid': portal.service_module.slapos_crm_monitoring.getUid()
}
if destination_decision:
kw['destination_decision__uid'] = context.restrictedTraverse(
destination_decision).getUid()
support_request_amount_list = context.portal_catalog(**kw)
return limit <= len(support_request_amount_list)
return CachingMethod(isSupportRequestCreationClosed,
"isSupportRequestCreationClosed",
cache_factory="erp5_content_short")(destination_decision=destination_decision)
...@@ -4,8 +4,8 @@ from erp5.component.module.DateUtils import addToDate ...@@ -4,8 +4,8 @@ from erp5.component.module.DateUtils import addToDate
instance_tree = context instance_tree = context
portal = context.getPortalObject() portal = context.getPortalObject()
if portal.ERP5Site_isSupportRequestCreationClosed(): project = context.getFollowUpValue()
# Stop ticket creation if project.Project_isSupportRequestCreationClosed():
return return
date_check_limit = addToDate(DateTime(), to_add={'hour': -1}) date_check_limit = addToDate(DateTime(), to_add={'hour': -1})
...@@ -49,7 +49,6 @@ for instance in software_instance_list: ...@@ -49,7 +49,6 @@ for instance in software_instance_list:
else: else:
error_message = "No message!" error_message = "No message!"
project = context.getFollowUpValue()
support_request = project.Project_createSupportRequestWithCausality( support_request = project.Project_createSupportRequestWithCausality(
ticket_title, ticket_title,
description, description,
......
portal = context.getPortalObject()
assert context.getPortalType() == 'Project', "context is not a project"
limit = int(portal.portal_preferences.getPreferredSupportRequestCreationLimit(5))
kw = {
'limit': limit,
'portal_type': 'Support Request',
'simulation_state': ["validated", "submitted", "suspended"],
'source_project__uid': context.getUid(),
'resource__uid': portal.service_module.slapos_crm_monitoring.getUid()
}
return limit <= int(portal.portal_catalog.countResults(**kw)[0][0])
...@@ -50,11 +50,11 @@ ...@@ -50,11 +50,11 @@
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <key> <string>_params</string> </key>
<value> <string>destination_decision=None, **kw</string> </value> <value> <string>**kw</string> </value>
</item> </item>
<item> <item>
<key> <string>id</string> </key> <key> <string>id</string> </key>
<value> <string>ERP5Site_isSupportRequestCreationClosed</string> </value> <value> <string>Project_isSupportRequestCreationClosed</string> </value>
</item> </item>
</dictionary> </dictionary>
</pickle> </pickle>
......
...@@ -205,93 +205,69 @@ class TestSlapOSisSupportRequestCreationClosed(TestCRMSkinsMixin): ...@@ -205,93 +205,69 @@ class TestSlapOSisSupportRequestCreationClosed(TestCRMSkinsMixin):
def afterSetUp(self): def afterSetUp(self):
TestCRMSkinsMixin.afterSetUp(self) TestCRMSkinsMixin.afterSetUp(self)
self.project = self.addProject() self.project = self.addProject()
self._cancelTestSupportRequestList() self.other_project = self.addProject()
self.clearCache() # ensure it is set to 5
def test_ERP5Site_isSupportRequestCreationClosed(self):
person = self.makePerson(self.project, user=0)
other_person = self.makePerson(self.project, user=0)
url = person.getRelativeUrl()
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed(url))
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed())
self.portal.portal_preferences.slapos_default_system_preference\ self.portal.portal_preferences.slapos_default_system_preference\
.setPreferredSupportRequestCreationLimit(5) .setPreferredSupportRequestCreationLimit(5)
self.clearCache()
def newSupportRequest(): def newDummySupportRequest(self,
sr = self.portal.support_request_module.newContent(\ resource="service_module/slapos_crm_monitoring",
title="Test Support Request POIUY", state='validated'):
resource="service_module/slapos_crm_monitoring", sr = self.portal.support_request_module.newContent(\
destination_decision=url) title="Test isSupportRequestCreationClosed %s" % self.generateNewId(),
resource=resource,
source_project_value=self.project)
if state == 'validated':
sr.validate() sr.validate()
sr.immediateReindexObject() elif state == 'suspended':
newSupportRequest()
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed(url))
newSupportRequest()
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed(url))
newSupportRequest()
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed(url))
newSupportRequest()
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed(url))
newSupportRequest()
# It hit cache
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed(url))
self.clearCache()
self.assertTrue(self.portal.ERP5Site_isSupportRequestCreationClosed(url))
self.assertTrue(self.portal.ERP5Site_isSupportRequestCreationClosed())
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed(
other_person.getRelativeUrl()))
def test_ERP5Site_isSupportRequestCreationClosed_suspended_state(self):
person = self.makePerson(self.project, user=0)
url = person.getRelativeUrl()
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed(url))
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed())
def newSupportRequest():
sr = self.portal.support_request_module.newContent(\
title="Test Support Request POIUY",
resource="service_module/slapos_crm_monitoring",
destination_decision=url)
sr.validate() sr.validate()
sr.suspend() sr.suspend()
sr.immediateReindexObject() elif state == 'submited':
# Create five tickets, the limit of ticket creation sr.submit()
newSupportRequest() sr.immediateReindexObject()
newSupportRequest()
newSupportRequest() def test_Project_isSupportRequestCreationClosed(self, state='validated'):
newSupportRequest() self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
newSupportRequest() self.newDummySupportRequest(state=state)
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed(url)) self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed()) self.newDummySupportRequest(state=state)
self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.newDummySupportRequest(state=state)
def test_ERP5Site_isSupportRequestCreationClosed_nonmonitoring(self): self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
person = self.makePerson(self.project, user=0) self.newDummySupportRequest(state=state)
url = person.getRelativeUrl() self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed(url)) self.newDummySupportRequest(state=state)
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed()) self.assertTrue(self.project.Project_isSupportRequestCreationClosed())
self.newDummySupportRequest(state=state)
def newSupportRequest(): self.assertTrue(self.project.Project_isSupportRequestCreationClosed())
sr = self.portal.support_request_module.newContent(\ # it dont close another project
title="Test Support Request POIUY", self.assertFalse(self.other_project.Project_isSupportRequestCreationClosed())
destination_decision=url)
sr.validate() def test_Project_isSupportRequestCreationClosed_submited_state(self):
sr.immediateReindexObject() self.test_Project_isSupportRequestCreationClosed(state='submited')
# Create five tickets, the limit of ticket creation def test_Project_isSupportRequestCreationClosed_suspended_state(self):
newSupportRequest() self.test_Project_isSupportRequestCreationClosed(state='suspended')
newSupportRequest()
newSupportRequest() def test_Project_isSupportRequestCreationClosed_nonmonitoring(self):
newSupportRequest() self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
newSupportRequest() self.newDummySupportRequest(resource='')
self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed(url)) self.newDummySupportRequest(resource='')
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed()) self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.newDummySupportRequest(resource='')
self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.newDummySupportRequest(resource='')
self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.newDummySupportRequest(resource='')
self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.newDummySupportRequest(resource='')
self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.newDummySupportRequest(resource='')
self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
# it dont close another project
self.assertFalse(self.other_project.Project_isSupportRequestCreationClosed())
class TestSlapOSHasError(SlapOSTestCaseMixin): class TestSlapOSHasError(SlapOSTestCaseMixin):
......
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