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()
if portal.ERP5Site_isSupportRequestCreationClosed():
# Stop process alarm if there are too many tickets
return
monitor_enabled_category = portal.restrictedTraverse(
"portal_categories/monitor_scope/enabled", None)
......
portal = context.getPortalObject()
if portal.ERP5Site_isSupportRequestCreationClosed():
# Stop verification if there are too much tickets
return
portal.portal_catalog.searchAndActivate(
portal_type='Instance Tree',
validation_state='validated',
......
portal = context.getPortalObject()
if portal.ERP5Site_isSupportRequestCreationClosed():
# Stop process alarm if there are too many tickets
return
monitor_enabled_category = portal.restrictedTraverse(
"portal_categories/monitor_scope/enabled", None)
......
from DateTime import DateTime
portal = context.getPortalObject()
if (context.getMonitorScope() == "disabled") or \
portal.ERP5Site_isSupportRequestCreationClosed():
if (context.getMonitorScope() == "disabled"):
return
project = context.getFollowUpValue()
if project.Project_isSupportRequestCreationClosed():
return
software_installation_list = portal.portal_catalog(
......
from DateTime import DateTime
portal = context.getPortalObject()
if (context.getMonitorScope() == "disabled") or \
portal.ERP5Site_isSupportRequestCreationClosed():
if (context.getMonitorScope() == "disabled"):
return
project = context.getFollowUpValue()
if project.Project_isSupportRequestCreationClosed():
return
reference = context.getReference()
......@@ -76,8 +79,6 @@ if not should_notify:
context.getTitle(), context.getReference(), last_contact)
if should_notify:
project = context.getFollowUpValue()
support_request = project.Project_createSupportRequestWithCausality(
ticket_title,
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
instance_tree = context
portal = context.getPortalObject()
if portal.ERP5Site_isSupportRequestCreationClosed():
# Stop ticket creation
project = context.getFollowUpValue()
if project.Project_isSupportRequestCreationClosed():
return
date_check_limit = addToDate(DateTime(), to_add={'hour': -1})
......@@ -49,7 +49,6 @@ for instance in software_instance_list:
else:
error_message = "No message!"
project = context.getFollowUpValue()
support_request = project.Project_createSupportRequestWithCausality(
ticket_title,
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 @@
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>destination_decision=None, **kw</string> </value>
<value> <string>**kw</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>ERP5Site_isSupportRequestCreationClosed</string> </value>
<value> <string>Project_isSupportRequestCreationClosed</string> </value>
</item>
</dictionary>
</pickle>
......
......@@ -205,93 +205,69 @@ class TestSlapOSisSupportRequestCreationClosed(TestCRMSkinsMixin):
def afterSetUp(self):
TestCRMSkinsMixin.afterSetUp(self)
self.project = self.addProject()
self._cancelTestSupportRequestList()
self.clearCache()
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.other_project = self.addProject()
# ensure it is set to 5
self.portal.portal_preferences.slapos_default_system_preference\
.setPreferredSupportRequestCreationLimit(5)
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.immediateReindexObject()
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",
def newDummySupportRequest(self,
resource="service_module/slapos_crm_monitoring",
destination_decision=url)
sr.validate()
sr.suspend()
sr.immediateReindexObject()
# Create five tickets, the limit of ticket creation
newSupportRequest()
newSupportRequest()
newSupportRequest()
newSupportRequest()
newSupportRequest()
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed(url))
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed())
def test_ERP5Site_isSupportRequestCreationClosed_nonmonitoring(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():
state='validated'):
sr = self.portal.support_request_module.newContent(\
title="Test Support Request POIUY",
destination_decision=url)
title="Test isSupportRequestCreationClosed %s" % self.generateNewId(),
resource=resource,
source_project_value=self.project)
if state == 'validated':
sr.validate()
elif state == 'suspended':
sr.validate()
sr.suspend()
elif state == 'submited':
sr.submit()
sr.immediateReindexObject()
# Create five tickets, the limit of ticket creation
newSupportRequest()
newSupportRequest()
newSupportRequest()
newSupportRequest()
newSupportRequest()
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed(url))
self.assertFalse(self.portal.ERP5Site_isSupportRequestCreationClosed())
def test_Project_isSupportRequestCreationClosed(self, state='validated'):
self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.newDummySupportRequest(state=state)
self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.newDummySupportRequest(state=state)
self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.newDummySupportRequest(state=state)
self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.newDummySupportRequest(state=state)
self.assertFalse(self.project.Project_isSupportRequestCreationClosed())
self.newDummySupportRequest(state=state)
self.assertTrue(self.project.Project_isSupportRequestCreationClosed())
self.newDummySupportRequest(state=state)
self.assertTrue(self.project.Project_isSupportRequestCreationClosed())
# it dont close another project
self.assertFalse(self.other_project.Project_isSupportRequestCreationClosed())
def test_Project_isSupportRequestCreationClosed_submited_state(self):
self.test_Project_isSupportRequestCreationClosed(state='submited')
def test_Project_isSupportRequestCreationClosed_suspended_state(self):
self.test_Project_isSupportRequestCreationClosed(state='suspended')
def test_Project_isSupportRequestCreationClosed_nonmonitoring(self):
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())
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):
......
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