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(
30 help="Reprocess all media entries")
31 subparser
.add_argument(
33 help="Reprocess media entries in this state"
34 " such as 'failed' or 'processed'")
35 subparser
.add_argument(
37 help="The type of media to be reprocessed such as 'video' or 'image'")
38 subparser
.add_argument(
41 help="The media_entry id(s) you wish to reprocess.")
44 def _set_media_type(args
):
45 if len(args
[0].media_id
) == 1:
46 media_type
= MediaEntry
.query
.filter_by(id=args
[0].media_id
[0])\
47 .first().media_type
.split('.')[-1]
50 args
[0].type = media_type
51 elif args
[0].type != media_type
:
52 raise Exception(_('The type that you set does not match the type'
53 ' of the given media_id.'))
54 elif len(args
[0].media_id
) > 1:
57 for id in args
[0].media_id
:
58 media_types
.append(MediaEntry
.query
.filter_by(id=id).first()\
59 .media_type
.split('.')[-1])
60 for type in media_types
:
61 if media_types
[0] != type:
62 raise Exception((u
'You cannot reprocess different media_types'
63 ' at the same time.'))
66 args
[0].type = media_types
[0]
67 elif args
[0].type != media_types
[0]:
68 raise Exception(_('The type that you set does not match the type'
69 ' of the given media_ids.'))
71 elif not args
[0].type:
72 raise Exception(_('You must provide either a media_id or set the'
76 def _reprocess_all(args
):
78 if args
[0].state
== 'failed':
80 print _('\n Available reprocess actions for all failed' \
81 ' media_entries: \n \t --initial_processing')
84 #TODO reprocess all failed entries
87 raise Exception(_('You must set --type when trying to reprocess'
88 ' all media_entries, unless you set --state'
91 _run_reprocessing(args
)
94 def _run_reprocessing(args
):
96 return hook_handle(('reprocess_action', args
[0].type), args
)
98 return hook_handle(('media_reprocess', args
[0].type), args
)
102 commands_util
.setup_app(args
[0])
104 if not args
[0].state
:
105 args
[0].state
= 'processed'
108 return _reprocess_all(args
)
110 _set_media_type(args
)
112 return _run_reprocessing(args
)