diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 4f90167..67cbf24 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -3473,16 +3473,17 @@ def set_custom_key_fields(session, project, fields, types, data): if types[idx] != "list": # Only Lists use data, strip it otherwise data[idx] = None + if key in current_keys: issuekey = current_keys[key] issuekey.key_type = types[idx] - issuekey.key_data = data[idx] + issuekey.data = data[idx] else: issuekey = model.IssueKeys( project_id=project.id, name=key, key_type=types[idx], - key_data=data[idx] + data=data[idx] ) session.add(issuekey) diff --git a/pagure/lib/model.py b/pagure/lib/model.py index 79516d7..d3dde2d 100644 --- a/pagure/lib/model.py +++ b/pagure/lib/model.py @@ -999,6 +999,48 @@ class IssueKeys(BASE): 'issue_keys', cascade="delete, delete-orphan", single_parent=True) ) + @property + def data(self): + ''' Return the list of items ''' + items = {} + if self.key_data: + items = json.loads(self.key_data) + else: + return None + return items['list'] + + @property + def data_string(self): + ''' Take the list from key_data and convert it to a string. + This is used by the project settings custom field section. ''' + + if self.key_data: + mylist = json.loads(self.key_data) + else: + return "" + + return_str = "" + first_item = True + if mylist: + for item in mylist['list']: + if first_item: + return_str = item + first_item = False + else: + return_str = return_str + ", " + item + return return_str + + @data.setter + def data(self, list_str): + ''' Ensures the list items are properly saved. ''' + if list_str is None: + self.key_data = None + else: + list_list = [item.strip() for item in list_str.split(',')] + list_data = {} + list_data['list'] = list_list + self.key_data = json.dumps(list_data) + class IssueValues(BASE): """ Stores the values of the custom keys set by project on issues. diff --git a/pagure/templates/issue.html b/pagure/templates/issue.html index 7864e24..bcb3ccd 100644 --- a/pagure/templates/issue.html +++ b/pagure/templates/issue.html @@ -386,7 +386,7 @@ {% if field.key_type == 'list' %} -
+
-
+
+ value={% if field.data is none %}""{% else %}"{{ field.data_string }}"{% endif %} class="form-control"/>
{% endfor %} diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py index f33c8ea..6ef7579 100644 --- a/tests/test_pagure_flask_api_issue.py +++ b/tests/test_pagure_flask_api_issue.py @@ -1902,18 +1902,18 @@ class PagureFlaskApiIssuetests(tests.Modeltests): self.session, repo, ['bugzilla', 'upstream', 'reviewstatus'], ['link', 'boolean', 'list'], - ['unused data for non-list type', '', 'ack, nack, needs review']) + ['unused data for non-list type', '', 'ack, nack , needs review']) self.session.commit() self.assertEqual(msg, 'List of custom fields updated') # Check the project custom fields were correctly set for key in repo.issue_keys: # Check that the bugzilla field correctly had its data removed - if key.name == "bugzilla" and key.key_data != "": + if key.name == "bugzilla" and key.data is not None: assert False # Check that the reviewstatus list field still has its list if (key.name == "reviewstatus" and - key.key_data != 'ack, nack, needs review'): + key.data_string != 'ack, nack, needs review'): assert False # No value specified while we try to create the field