def testParseFieldDefRequest_Empty(self): post_data = fake.PostData() parsed = field_helpers.ParseFieldDefRequest(post_data, self.config) self.assertEqual('', parsed.field_name) self.assertEqual(None, parsed.field_type_str) self.assertEqual(None, parsed.min_value) self.assertEqual(None, parsed.max_value) self.assertEqual(None, parsed.regex) self.assertFalse(parsed.needs_member) self.assertEqual('', parsed.needs_perm) self.assertEqual('', parsed.grants_perm) self.assertEqual(0, parsed.notify_on) self.assertFalse(parsed.is_required) self.assertFalse(parsed.is_niche) self.assertFalse(parsed.is_multivalued) self.assertEqual('', parsed.field_docstring) self.assertEqual('', parsed.choices_text) self.assertEqual('', parsed.applicable_type) self.assertEqual('', parsed.applicable_predicate) unchanged_labels = [(label_def.label, label_def.label_docstring, label_def.deprecated) for label_def in self.config.well_known_labels] self.assertEqual(unchanged_labels, parsed.revised_labels) self.assertEqual('', parsed.approvers_str) self.assertEqual('', parsed.survey) self.assertEqual('', parsed.parent_approval_name) self.assertFalse(parsed.is_phase_field)
def testParseFieldDefRequest_PreventPhaseApprovals(self): post_data = fake.PostData( field_type=['approval_type'], is_phase_field=['on'], ) parsed = field_helpers.ParseFieldDefRequest(post_data, self.config) self.assertEqual('approval_type', parsed.field_type_str) self.assertFalse(parsed.is_phase_field)
def testParseFieldDefRequest_Normal(self): post_data = fake.PostData( name=['somefield'], field_type=['INT_TYPE'], min_value=['11'], max_value=['99'], regex=['.*'], needs_member=['Yes'], needs_perm=['Commit'], grants_perm=['View'], notify_on=['any_comment'], importance=['required'], is_multivalued=['Yes'], docstring=['It is just some field'], choices=['Hot = Lots of activity\nCold = Not much activity'], applicable_type=['Defect'], approver_names=['*****@*****.**'], survey=['Are there UX changes?'], parent_approval_name=['UIReview'], is_phase_field=['on'], ) parsed = field_helpers.ParseFieldDefRequest(post_data, self.config) self.assertEqual('somefield', parsed.field_name) self.assertEqual('INT_TYPE', parsed.field_type_str) self.assertEqual(11, parsed.min_value) self.assertEqual(99, parsed.max_value) self.assertEqual('.*', parsed.regex) self.assertTrue(parsed.needs_member) self.assertEqual('Commit', parsed.needs_perm) self.assertEqual('View', parsed.grants_perm) self.assertEqual(1, parsed.notify_on) self.assertTrue(parsed.is_required) self.assertFalse(parsed.is_niche) self.assertTrue(parsed.is_multivalued) self.assertEqual('It is just some field', parsed.field_docstring) self.assertEqual('Hot = Lots of activity\nCold = Not much activity', parsed.choices_text) self.assertEqual('Defect', parsed.applicable_type) self.assertEqual('', parsed.applicable_predicate) unchanged_labels = [(label_def.label, label_def.label_docstring, label_def.deprecated) for label_def in self.config.well_known_labels] new_labels = [('somefield-Hot', 'Lots of activity', False), ('somefield-Cold', 'Not much activity', False)] self.assertEqual(unchanged_labels + new_labels, parsed.revised_labels) self.assertEqual('*****@*****.**', parsed.approvers_str) self.assertEqual('Are there UX changes?', parsed.survey) self.assertEqual('UIReview', parsed.parent_approval_name) self.assertTrue(parsed.is_phase_field)
def _ProcessEditField(self, mr, post_data, config, field_def): """The user wants to edit this field definition.""" # TODO(jrobbins): future feature: editable field names parsed = field_helpers.ParseFieldDefRequest(post_data, config) admin_ids, _admin_str = tracker_helpers.ParseAdminUsers( mr.cnxn, post_data['admin_names'], self.services.user) # TODO(jrobbins): bounce on validation errors self.services.config.UpdateFieldDef( mr.cnxn, mr.project_id, field_def.field_id, applicable_type=parsed.applicable_type, applicable_predicate=parsed.applicable_predicate, is_required=parsed.is_required, is_niche=parsed.is_niche, min_value=parsed.min_value, max_value=parsed.max_value, regex=parsed.regex, needs_member=parsed.needs_member, needs_perm=parsed.needs_perm, grants_perm=parsed.grants_perm, notify_on=parsed.notify_on, is_multivalued=parsed.is_multivalued, date_action=parsed.date_action_str, docstring=parsed.field_docstring, admin_ids=admin_ids) self.services.config.UpdateConfig( mr.cnxn, mr.project, well_known_labels=parsed.revised_labels)
def ProcessFormData(self, mr, post_data): """Validate and store the contents of the issues tracker admin page. Args: mr: commonly used info parsed from the request. post_data: HTML form data from the request. Returns: String URL to redirect the user to, or None if response was already sent. """ config = self.services.config.GetProjectConfig(mr.cnxn, mr.project_id) parsed = field_helpers.ParseFieldDefRequest(post_data, config) if not tracker_constants.FIELD_NAME_RE.match(parsed.field_name): mr.errors.field_name = 'Invalid field name' field_name_error_msg = FieldNameErrorMessage(parsed.field_name, config) if field_name_error_msg: mr.errors.field_name = field_name_error_msg if (parsed.min_value is not None and parsed.max_value is not None and parsed.min_value > parsed.max_value): mr.errors.min_value = 'Minimum value must be less than maximum.' if parsed.regex: try: re.compile(parsed.regex) except re.error: mr.errors.regex = 'Invalid regular expression.' admin_ids, admin_str = tracker_helpers.ParseAdminUsers( mr.cnxn, post_data['admin_names'], self.services.user) if parsed.field_type_str == 'approval_type': if parsed.approvers_str: approver_ids_dict = self.services.user.LookupUserIDs( mr.cnxn, re.split('[,;\s]+', parsed.approvers_str), autocreate=True) approver_ids = list(set(approver_ids_dict.values())) else: mr.errors.approvers = 'Please provide at least one default approver.' if mr.errors.AnyErrors(): self.PleaseCorrect( mr, initial_field_name=parsed.field_name, initial_type=parsed.field_type_str, initial_parent_approval_name=parsed.parent_approval_name, initial_field_docstring=parsed.field_docstring, initial_applicable_type=parsed.applicable_type, initial_applicable_predicate=parsed.applicable_predicate, initial_needs_member=ezt.boolean(parsed.needs_member), initial_needs_perm=parsed.needs_perm, initial_importance=parsed.importance, initial_is_multivalued=ezt.boolean(parsed.is_multivalued), initial_grants_perm=parsed.grants_perm, initial_notify_on=parsed.notify_on, initial_date_action=parsed.date_action_str, initial_choices=parsed.choices_text, initial_approvers=parsed.approvers_str, initial_survey=parsed.survey, initial_is_phase_field=parsed.is_phase_field, initial_admins=admin_str, ) return approval_id = None if parsed.parent_approval_name and ( parsed.field_type_str != 'approval_type'): approval_fd = tracker_bizobj.FindFieldDef( parsed.parent_approval_name, config) if approval_fd: approval_id = approval_fd.field_id field_id = self.services.config.CreateFieldDef( mr.cnxn, mr.project_id, parsed.field_name, parsed.field_type_str, parsed.applicable_type, parsed.applicable_predicate, parsed.is_required, parsed.is_niche, parsed.is_multivalued, parsed.min_value, parsed.max_value, parsed.regex, parsed.needs_member, parsed.needs_perm, parsed.grants_perm, parsed.notify_on, parsed.date_action_str, parsed.field_docstring, admin_ids, approval_id, parsed.is_phase_field) if parsed.field_type_str == 'approval_type': revised_approvals = field_helpers.ReviseApprovals( field_id, approver_ids, parsed.survey, config) self.services.config.UpdateConfig( mr.cnxn, mr.project, approval_defs=revised_approvals) if parsed.field_type_str == 'enum_type': self.services.config.UpdateConfig( mr.cnxn, mr.project, well_known_labels=parsed.revised_labels) return framework_helpers.FormatAbsoluteURL( mr, urls.ADMIN_LABELS, saved=1, ts=int(time.time()))
def _ProcessEditField(self, mr, post_data, config, field_def): """The user wants to edit this field definition.""" # TODO(jrobbins): future feature: editable field names parsed = field_helpers.ParseFieldDefRequest(post_data, config) if (parsed.min_value is not None and parsed.max_value is not None and parsed.min_value > parsed.max_value): mr.errors.min_value = 'Minimum value must be less than maximum.' if parsed.regex: try: re.compile(parsed.regex) except re.error: mr.errors.regex = 'Invalid regular expression.' admin_ids, admin_str = tracker_helpers.ParseAdminUsers( mr.cnxn, post_data['admin_names'], self.services.user) if field_def.field_type == tracker_pb2.FieldTypes.APPROVAL_TYPE: if parsed.approvers_str: approver_ids_dict = self.services.user.LookupUserIDs( mr.cnxn, re.split('[,;\s]+', parsed.approvers_str), autocreate=True) approver_ids = list(set(approver_ids_dict.values())) else: mr.errors.approvers = 'Please provide at least one default approver.' if mr.errors.AnyErrors(): new_field_def = field_helpers.ReviseFieldDefFromParsed( parsed, field_def) new_field_def_view = tracker_views.FieldDefView( new_field_def, config) self.PleaseCorrect(mr, field_def=new_field_def_view, initial_applicable_type=parsed.applicable_type, initial_choices=parsed.choices_text, initial_admins=admin_str, initial_approvers=parsed.approvers_str) return self.services.config.UpdateFieldDef( mr.cnxn, mr.project_id, field_def.field_id, applicable_type=parsed.applicable_type, applicable_predicate=parsed.applicable_predicate, is_required=parsed.is_required, is_niche=parsed.is_niche, min_value=parsed.min_value, max_value=parsed.max_value, regex=parsed.regex, needs_member=parsed.needs_member, needs_perm=parsed.needs_perm, grants_perm=parsed.grants_perm, notify_on=parsed.notify_on, is_multivalued=parsed.is_multivalued, date_action=parsed.date_action_str, docstring=parsed.field_docstring, admin_ids=admin_ids) if field_def.field_type == tracker_pb2.FieldTypes.APPROVAL_TYPE: approval_defs = field_helpers.ReviseApprovals( field_def.field_id, approver_ids, parsed.survey, config) self.services.config.UpdateConfig(mr.cnxn, mr.project, approval_defs=approval_defs) if field_def.field_type == tracker_pb2.FieldTypes.ENUM_TYPE: self.services.config.UpdateConfig( mr.cnxn, mr.project, well_known_labels=parsed.revised_labels) return framework_helpers.FormatAbsoluteURL(mr, urls.FIELD_DETAIL, field=field_def.field_name, saved=1, ts=int(time.time()))