1 | import sys |
---|
2 | from galaxy import model, util |
---|
3 | from galaxy.web.base.controller import * |
---|
4 | from galaxy.web.framework.helpers import time_ago, iff, grids |
---|
5 | from galaxy.model.orm import * |
---|
6 | # Older py compatibility |
---|
7 | try: |
---|
8 | set() |
---|
9 | except: |
---|
10 | from sets import Set as set |
---|
11 | |
---|
12 | import logging |
---|
13 | log = logging.getLogger( __name__ ) |
---|
14 | |
---|
15 | class LibraryListGrid( grids.Grid ): |
---|
16 | class NameColumn( grids.TextColumn ): |
---|
17 | def get_value( self, trans, grid, library ): |
---|
18 | return library.name |
---|
19 | class DescriptionColumn( grids.TextColumn ): |
---|
20 | def get_value( self, trans, grid, library ): |
---|
21 | if library.description: |
---|
22 | return library.description |
---|
23 | return '' |
---|
24 | class StatusColumn( grids.GridColumn ): |
---|
25 | def get_value( self, trans, grid, library ): |
---|
26 | if library.purged: |
---|
27 | return "purged" |
---|
28 | elif library.deleted: |
---|
29 | return "deleted" |
---|
30 | return "" |
---|
31 | # Grid definition |
---|
32 | title = "Data Libraries" |
---|
33 | model_class = model.Library |
---|
34 | template='/admin/library/grid.mako' |
---|
35 | default_sort_key = "name" |
---|
36 | columns = [ |
---|
37 | NameColumn( "Name", |
---|
38 | key="name", |
---|
39 | model_class=model.Library, |
---|
40 | link=( lambda library: dict( operation="browse", id=library.id ) ), |
---|
41 | attach_popup=False, |
---|
42 | filterable="advanced" ), |
---|
43 | DescriptionColumn( "Description", |
---|
44 | key="description", |
---|
45 | model_class=model.Library, |
---|
46 | attach_popup=False, |
---|
47 | filterable="advanced" ), |
---|
48 | grids.GridColumn( "Created", key="create_time", format=time_ago ), |
---|
49 | grids.GridColumn( "Last Updated", key="update_time", format=time_ago ), |
---|
50 | StatusColumn( "Status", attach_popup=False ), |
---|
51 | # Columns that are valid for filtering but are not visible. |
---|
52 | grids.DeletedColumn( "Deleted", key="deleted", visible=False, filterable="advanced" ) |
---|
53 | ] |
---|
54 | columns.append( grids.MulticolFilterColumn( "Search", |
---|
55 | cols_to_filter=[ columns[0], columns[1] ], |
---|
56 | key="free-text-search", |
---|
57 | visible=False, |
---|
58 | filterable="standard" ) ) |
---|
59 | global_actions = [ |
---|
60 | grids.GridAction( "Create new data library", dict( controller='library_admin', action='create_library' ) ) |
---|
61 | ] |
---|
62 | standard_filters = [ |
---|
63 | grids.GridColumnFilter( "Active", args=dict( deleted=False ) ), |
---|
64 | grids.GridColumnFilter( "Deleted", args=dict( deleted=True, purged=False ) ), |
---|
65 | grids.GridColumnFilter( "Purged", args=dict( purged=True ) ), |
---|
66 | grids.GridColumnFilter( "All", args=dict( deleted='All' ) ) |
---|
67 | ] |
---|
68 | default_filter = dict( name="All", description="All", deleted="False", purged="False" ) |
---|
69 | num_rows_per_page = 50 |
---|
70 | preserve_state = False |
---|
71 | use_paging = True |
---|
72 | |
---|
73 | class LibraryAdmin( BaseController ): |
---|
74 | |
---|
75 | library_list_grid = LibraryListGrid() |
---|
76 | |
---|
77 | @web.expose |
---|
78 | @web.require_admin |
---|
79 | def browse_libraries( self, trans, **kwd ): |
---|
80 | if 'operation' in kwd: |
---|
81 | operation = kwd['operation'].lower() |
---|
82 | if operation == "browse": |
---|
83 | return trans.response.send_redirect( web.url_for( controller='library_common', |
---|
84 | action='browse_library', |
---|
85 | cntrller='library_admin', |
---|
86 | **kwd ) ) |
---|
87 | # Render the list view |
---|
88 | return self.library_list_grid( trans, **kwd ) |
---|
89 | @web.expose |
---|
90 | @web.require_admin |
---|
91 | def create_library( self, trans, **kwd ): |
---|
92 | params = util.Params( kwd ) |
---|
93 | message = util.restore_text( params.get( 'message', '' ) ) |
---|
94 | status = params.get( 'status', 'done' ) |
---|
95 | if params.get( 'create_library_button', False ): |
---|
96 | name = util.restore_text( params.get( 'name', 'No name' ) ) |
---|
97 | description = util.restore_text( params.get( 'description', '' ) ) |
---|
98 | synopsis = util.restore_text( params.get( 'synopsis', '' ) ) |
---|
99 | if synopsis in [ 'None', None ]: |
---|
100 | synopsis = '' |
---|
101 | library = trans.app.model.Library( name=name, description=description, synopsis=synopsis ) |
---|
102 | root_folder = trans.app.model.LibraryFolder( name=name, description='' ) |
---|
103 | library.root_folder = root_folder |
---|
104 | trans.sa_session.add_all( ( library, root_folder ) ) |
---|
105 | trans.sa_session.flush() |
---|
106 | message = "The new library named '%s' has been created" % library.name |
---|
107 | return trans.response.send_redirect( web.url_for( controller='library_common', |
---|
108 | action='browse_library', |
---|
109 | cntrller='library_admin', |
---|
110 | id=trans.security.encode_id( library.id ), |
---|
111 | message=util.sanitize_text( message ), |
---|
112 | status='done' ) ) |
---|
113 | return trans.fill_template( '/admin/library/new_library.mako', message=message, status=status ) |
---|
114 | @web.expose |
---|
115 | @web.require_admin |
---|
116 | def purge_library( self, trans, **kwd ): |
---|
117 | # TODO: change this function to purge_library_item, behaving similar to delete_library_item |
---|
118 | # assuming we want the ability to purge libraries. |
---|
119 | # This function is currently only used by the functional tests. |
---|
120 | params = util.Params( kwd ) |
---|
121 | library = trans.sa_session.query( trans.app.model.Library ).get( trans.security.decode_id( params.id ) ) |
---|
122 | def purge_folder( library_folder ): |
---|
123 | for lf in library_folder.folders: |
---|
124 | purge_folder( lf ) |
---|
125 | trans.sa_session.refresh( library_folder ) |
---|
126 | for library_dataset in library_folder.datasets: |
---|
127 | trans.sa_session.refresh( library_dataset ) |
---|
128 | ldda = library_dataset.library_dataset_dataset_association |
---|
129 | if ldda: |
---|
130 | trans.sa_session.refresh( ldda ) |
---|
131 | dataset = ldda.dataset |
---|
132 | trans.sa_session.refresh( dataset ) |
---|
133 | # If the dataset is not associated with any additional undeleted folders, then we can delete it. |
---|
134 | # We don't set dataset.purged to True here because the cleanup_datasets script will do that for |
---|
135 | # us, as well as removing the file from disk. |
---|
136 | #if not dataset.deleted and len( dataset.active_library_associations ) <= 1: # This is our current ldda |
---|
137 | dataset.deleted = True |
---|
138 | ldda.deleted = True |
---|
139 | trans.sa_session.add_all( ( dataset, ldda ) ) |
---|
140 | library_dataset.deleted = True |
---|
141 | trans.sa_session.add( library_dataset ) |
---|
142 | library_folder.deleted = True |
---|
143 | library_folder.purged = True |
---|
144 | trans.sa_session.add( library_folder ) |
---|
145 | trans.sa_session.flush() |
---|
146 | if not library.deleted: |
---|
147 | message = "Library '%s' has not been marked deleted, so it cannot be purged" % ( library.name ) |
---|
148 | return trans.response.send_redirect( web.url_for( controller='library_admin', |
---|
149 | action='browse_libraries', |
---|
150 | message=util.sanitize_text( message ), |
---|
151 | status='error' ) ) |
---|
152 | else: |
---|
153 | purge_folder( library.root_folder ) |
---|
154 | library.purged = True |
---|
155 | trans.sa_session.add( library ) |
---|
156 | trans.sa_session.flush() |
---|
157 | message = "Library '%s' and all of its contents have been purged, datasets will be removed from disk via the cleanup_datasets script" % library.name |
---|
158 | return trans.response.send_redirect( web.url_for( controller='library_admin', |
---|
159 | action='browse_libraries', |
---|
160 | message=util.sanitize_text( message ), |
---|
161 | status='done' ) ) |
---|