1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 from mediagoblin
.db
.models
import MediaEntry
17 from mediagoblin
.gmg_commands
import util
as commands_util
18 from mediagoblin
.tools
.translate
import lazy_pass_to_ugettext
as _
19 from mediagoblin
.tools
.pluginapi
import hook_handle
22 def reprocess_parser_setup(subparser
):
23 subparser
.add_argument(
26 help="List available actions for a given media entry")
27 subparser
.add_argument(
29 help="Reprocess media entries in this state"
30 " such as 'failed' or 'processed'")
31 subparser
.add_argument(
33 help="The type of media to be reprocessed such as 'video' or 'image'")
34 subparser
.add_argument(
37 help="The media_entry id(s) you wish to reprocess.")
40 def _set_media_type(args
):
42 if len(args
[0].media_id
) == 1:
43 media_type
= MediaEntry
.query
.filter_by(id=args
[0].media_id
[0])\
44 .first().media_type
.split('.')[-1]
47 args
[0].type = media_type
48 elif args
[0].type != media_type
:
49 raise Exception(_('The --type that you set does not match the type'
50 ' of the given media_id.'))
51 elif len(args
[0].media_id
) > 1:
54 for id in args
[0].media_id
:
55 media_types
.append(MediaEntry
.query
.filter_by(id=id).first()
56 .media_type
.split('.')[-1])
57 for type in media_types
:
58 if media_types
[0] != type:
59 raise Exception((u
'You cannot reprocess different media_types'
60 ' at the same time.'))
63 args
[0].type = media_types
[0]
64 elif args
[0].type != media_types
[0]:
65 raise Exception(_('The --type that you set does not match the type'
66 ' of the given media_ids.'))
69 def _reprocess_all(args
):
71 if args
[0].state
== 'failed':
73 print _('\n Available reprocess actions for all failed'
74 ' media_entries: \n \t --initial_processing')
77 #TODO reprocess all failed entries
80 raise Exception(_('You must set --type when trying to reprocess'
81 ' all media_entries, unless you set --state'
84 _run_reprocessing(args
)
87 def _run_reprocessing(args
):
89 if args
[0].state
== 'failed':
90 print _('\n Available reprocess actions for all failed'
91 ' media_entries: \n \t --initial_processing')
93 result
= hook_handle(('reprocess_action', args
[0].type), args
)
95 print _('Sorry there is no available reprocessing for {}'
96 ' entries in the {} state'.format(args
[0].type,
99 return hook_handle(('media_reprocess', args
[0].type), args
)
102 def _set_media_state(args
):
104 if len(args
[0].media_id
) == 1:
105 args
[0].state
= MediaEntry
.query
.filter_by(id=args
[0].media_id
[0])\
108 elif len(args
[0].media_id
) > 1:
111 for id in args
[0].media_id
:
112 media_states
.append(MediaEntry
.query
.filter_by(id=id).first()
114 for state
in media_states
:
115 if state
!= media_states
[0]:
116 raise Exception(_('You can only reprocess media that is in the'
119 args
[0].state
= media_states
[0]
121 if not args
[0].state
:
122 args
[0].state
= 'processed'
126 commands_util
.setup_app(args
[0])
128 _set_media_state(args
)
129 _set_media_type(args
)
131 if not args
[0].media_id
:
132 return _reprocess_all(args
)
134 return _run_reprocessing(args
)