| 1 | ## |
|---|
| 2 | ## Utilities for sharing items and displaying shared items. |
|---|
| 3 | ## HACK: these should probably go in the web helper object. |
|---|
| 4 | ## |
|---|
| 5 | |
|---|
| 6 | <%! from galaxy import model %> |
|---|
| 7 | |
|---|
| 8 | ## Get display name for a class. |
|---|
| 9 | <%def name="get_class_display_name( a_class )"> |
|---|
| 10 | <% |
|---|
| 11 | ## Start with exceptions, end with default. |
|---|
| 12 | if a_class is model.StoredWorkflow: |
|---|
| 13 | return "Workflow" |
|---|
| 14 | else: |
|---|
| 15 | return a_class.__name__ |
|---|
| 16 | %> |
|---|
| 17 | </%def> |
|---|
| 18 | |
|---|
| 19 | <%def name="get_item_name( item )"> |
|---|
| 20 | <% |
|---|
| 21 | # Start with exceptions, end with default. |
|---|
| 22 | if type( item ) is model.Page: |
|---|
| 23 | item_name = item.title |
|---|
| 24 | elif type( item ) is model.Visualization: |
|---|
| 25 | item_name = item.title |
|---|
| 26 | elif hasattr( item, 'get_display_name'): |
|---|
| 27 | item_name = item.get_display_name() |
|---|
| 28 | else: |
|---|
| 29 | item_name = item.name |
|---|
| 30 | |
|---|
| 31 | # Encode in unicode. |
|---|
| 32 | if type( item_name ) is str: |
|---|
| 33 | item_name = unicode( item_name, 'utf-8' ) |
|---|
| 34 | return item_name |
|---|
| 35 | %> |
|---|
| 36 | </%def> |
|---|
| 37 | |
|---|
| 38 | ## Get plural display name for a class. |
|---|
| 39 | <%def name="get_class_plural_display_name( a_class )"> |
|---|
| 40 | <% |
|---|
| 41 | # Start with exceptions, end with default. |
|---|
| 42 | if a_class is model.History: |
|---|
| 43 | return "Histories" |
|---|
| 44 | elif a_class is model.FormDefinitionCurrent: |
|---|
| 45 | return "Forms" |
|---|
| 46 | else: |
|---|
| 47 | return get_class_display_name( a_class ) + "s" |
|---|
| 48 | %> |
|---|
| 49 | </%def> |
|---|
| 50 | |
|---|
| 51 | ## Get display name for a class. |
|---|
| 52 | <%def name="get_class_display_name( a_class )"> |
|---|
| 53 | <% |
|---|
| 54 | ## Start with exceptions, end with default. |
|---|
| 55 | if a_class is model.StoredWorkflow: |
|---|
| 56 | return "Workflow" |
|---|
| 57 | elif a_class is model.HistoryDatasetAssociation: |
|---|
| 58 | return "Dataset" |
|---|
| 59 | else: |
|---|
| 60 | return a_class.__name__ |
|---|
| 61 | %> |
|---|
| 62 | </%def> |
|---|
| 63 | |
|---|
| 64 | ## Get plural term for item. |
|---|
| 65 | <%def name="get_item_plural( item )"> |
|---|
| 66 | <% return get_class_plural( item.__class__ ) %> |
|---|
| 67 | </%def> |
|---|
| 68 | |
|---|
| 69 | ## Get plural term for class. |
|---|
| 70 | <%def name="get_class_plural( a_class )"> |
|---|
| 71 | <% |
|---|
| 72 | if a_class == model.History: |
|---|
| 73 | class_plural = "Histories" |
|---|
| 74 | elif a_class == model.StoredWorkflow: |
|---|
| 75 | class_plural = "Workflows" |
|---|
| 76 | elif a_class == model.Page: |
|---|
| 77 | class_plural = "Pages" |
|---|
| 78 | elif a_class == model.Library: |
|---|
| 79 | class_plural = "Libraries" |
|---|
| 80 | elif a_class == model.HistoryDatasetAssociation: |
|---|
| 81 | class_plural = "Datasets" |
|---|
| 82 | elif a_class == model.SampleDataset: |
|---|
| 83 | class_plural = "Sample Datasets" |
|---|
| 84 | elif a_class == model.FormDefinitionCurrent: |
|---|
| 85 | class_plural = "Forms" |
|---|
| 86 | elif a_class == model.RequestType: |
|---|
| 87 | class_plural = "sequencer configurations" |
|---|
| 88 | else: |
|---|
| 89 | class_plural = a_class.__name__ + "s" |
|---|
| 90 | return class_plural |
|---|
| 91 | %> |
|---|
| 92 | </%def> |
|---|
| 93 | |
|---|
| 94 | ## Returns the controller name for an item based on its class. |
|---|
| 95 | <%def name="get_controller_name( item )"> |
|---|
| 96 | <% |
|---|
| 97 | if isinstance( item, model.History ): |
|---|
| 98 | return "history" |
|---|
| 99 | elif isinstance( item, model.StoredWorkflow ): |
|---|
| 100 | return "workflow" |
|---|
| 101 | elif isinstance( item, model.HistoryDatasetAssociation ): |
|---|
| 102 | return "dataset" |
|---|
| 103 | elif isinstance( item, model.Page ): |
|---|
| 104 | return "page" |
|---|
| 105 | elif isinstance( item, model.Visualization ): |
|---|
| 106 | return "visualization" |
|---|
| 107 | %> |
|---|
| 108 | </%def> |
|---|
| 109 | |
|---|
| 110 | ## Returns item user/owner. |
|---|
| 111 | <%def name="get_item_user( item )"> |
|---|
| 112 | <% |
|---|
| 113 | # Exceptions first, default last. |
|---|
| 114 | if isinstance( item, model.HistoryDatasetAssociation ): |
|---|
| 115 | return item.history.user |
|---|
| 116 | else: |
|---|
| 117 | return item.user |
|---|
| 118 | %> |
|---|
| 119 | </%def> |
|---|
| 120 | |
|---|
| 121 | ## Returns item slug. |
|---|
| 122 | <%def name="get_item_slug( item )"> |
|---|
| 123 | <% |
|---|
| 124 | # Exceptions first, default last. |
|---|
| 125 | if isinstance( item, model.HistoryDatasetAssociation ): |
|---|
| 126 | return trans.security.encode_id( item.id ) |
|---|
| 127 | else: |
|---|
| 128 | return item.slug |
|---|
| 129 | %> |
|---|
| 130 | </%def> |
|---|
| 131 | |
|---|
| 132 | ## Return a link to view a history. |
|---|
| 133 | <%def name="get_history_link( history, qualify=False )"> |
|---|
| 134 | %if history.slug and history.user.username: |
|---|
| 135 | <% return h.url_for( controller='/history', action='display_by_username_and_slug', username=history.user.username, slug=history.slug, qualified=qualify ) %> |
|---|
| 136 | %else: |
|---|
| 137 | <% return h.url_for( controller='/history', action='view', id=trans.security.encode_id( history.id ), qualified=qualify ) %> |
|---|
| 138 | %endif |
|---|
| 139 | </%def> |
|---|
| 140 | |
|---|
| 141 | ## Render message. |
|---|
| 142 | <%def name="render_message( message, status )"> |
|---|
| 143 | %if message: |
|---|
| 144 | <p> |
|---|
| 145 | <div class="${status}message transient-message">${util.restore_text( message )}</div> |
|---|
| 146 | <div style="clear: both"></div> |
|---|
| 147 | </p> |
|---|
| 148 | %endif |
|---|
| 149 | </%def> |
|---|
| 150 | |
|---|