1- import random
2-
31from PyQt6 import QtCore , QtWidgets
42
5- from ...api import errors
6-
73from ..dbview import FilterChain
84from ..tools import ShowWaitCursor
95from ..api import get_ckan_api
@@ -20,7 +16,14 @@ def __init__(self, *args, **kwargs):
2016 """Filter chain with user-related features"""
2117 super (FilterChainMyData , self ).__init__ (* args , ** kwargs )
2218
23- # Enable the "add to collection tool box"
19+ # Enable "create collection" toolbutton
20+ self .fw_collections .pushButton_custom .setText (
21+ "Create new collection..." )
22+ self .fw_collections .pushButton_custom .setVisible (True )
23+ self .fw_collections .pushButton_custom .clicked .connect (
24+ self .on_create_collection )
25+
26+ # Enable the "add to collection" toolbutton
2427 self .fw_datasets .pushButton_custom .setText (
2528 "Add selected datasets to a collection..." )
2629 self .fw_datasets .pushButton_custom .setVisible (True )
@@ -42,8 +45,6 @@ def choose_collaborator(self, what_for=None):
4245 # Fetch a list of users
4346 with ShowWaitCursor ():
4447 api = get_ckan_api ()
45- # TODO: let the user search for collaborators and add them to a
46- # memoized list.
4748 users = api .get ("user_autocomplete" , q = "" , limit = 100 )
4849
4950 for_what_for = f" for { what_for } "
@@ -93,7 +94,8 @@ def on_add_datasets_to_collection(self):
9394 "Select a collection" ,
9495 f"Please choose a collection for "
9596 f"{ len (dataset_ids )} datasets." ,
96- [f"{ i } : { g ['display_name' ]} " for i , g in enumerate (grps )],
97+ [f"{ i } : { g ['display_name' ]} ({ g ['name' ]} )"
98+ for i , g in enumerate (grps )],
9799 0 , # current index
98100 False , # editable
99101 )
@@ -106,22 +108,15 @@ def on_add_datasets_to_collection(self):
106108 "Create a collection" ,
107109 "Type the name of a collection to create" ,
108110 )
109- # create a valid name
110- name = "" .join (
111- [c .lower () if c .isalnum () else "-" for c in text ])
112- name = name .strip ("-" )
113- for ii in range (10 ):
114- try :
115- collection = api .post ("group_create" ,
116- {"title" : text .strip (),
117- "name" : name ,
118- })
119- except errors .APIConflictError :
120- name = name + random .choice ("abcdefghijkm0123456789" )
121- else :
122- break
123- else :
124- raise ValueError ("Could not create collection" )
111+ if ok :
112+ # create a valid name
113+ name = "" .join (
114+ [c .lower () if c .isalnum () else "-" for c in text ])
115+ name = name .strip ("-" )
116+ # create the collection
117+ collection = api .require_collection (name = name ,
118+ title = text ,
119+ exist_ok = False )
125120 if ok :
126121 with ShowWaitCursor ():
127122 # add all datasets to that collection
@@ -137,6 +132,29 @@ def on_add_datasets_to_collection(self):
137132 self .added_datasets_to_collection .emit (collection ,
138133 dataset_ids )
139134
135+ @QtCore .pyqtSlot ()
136+ def on_create_collection (self ):
137+ text , ok = QtWidgets .QInputDialog .getText (
138+ self ,
139+ "Create a collection" ,
140+ "Type the name of a collection to create" ,
141+ )
142+ if ok :
143+ api = get_ckan_api ()
144+ # create a valid name
145+ name = "" .join (
146+ [c .lower () if c .isalnum () else "-" for c in text ])
147+ name = name .strip ("-" )
148+ collection = api .require_collection (name = name ,
149+ title = text ,
150+ exist_ok = False )
151+ QtWidgets .QMessageBox .information (
152+ self ,
153+ "Collection created" ,
154+ f"You may now add datasets to "
155+ f"collection '{ collection ['name' ]} '." ,
156+ )
157+
140158 @QtCore .pyqtSlot (str , str )
141159 def on_remove_item (self , id_type , identifier ):
142160 """Remove a dataset from a collection"""
0 commit comments