make media_id an optional argument
[mediagoblin.git] / mediagoblin / gmg_commands / reprocess.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
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.
8 #
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.
13 #
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
20
21
22 def reprocess_parser_setup(subparser):
23 subparser.add_argument(
24 '--available', '-a',
25 action="store_true",
26 help="List available actions for a given media entry")
27 subparser.add_argument(
28 '--state', '-s',
29 help="Reprocess media entries in this state"
30 " such as 'failed' or 'processed'")
31 subparser.add_argument(
32 '--type', '-t',
33 help="The type of media to be reprocessed such as 'video' or 'image'")
34 subparser.add_argument(
35 '--media_id',
36 nargs='*',
37 help="The media_entry id(s) you wish to reprocess.")
38
39
40 def _set_media_type(args):
41 if args[0].media_id:
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]
45
46 if not args[0].type:
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:
52 media_types = []
53
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.'))
61
62 if not args[0].type:
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.'))
67
68
69 def _reprocess_all(args):
70 if not args[0].type:
71 if args[0].state == 'failed':
72 if args[0].available:
73 print _('\n Available reprocess actions for all failed'
74 ' media_entries: \n \t --initial_processing')
75 return
76 else:
77 #TODO reprocess all failed entries
78 pass
79 else:
80 raise Exception(_('You must set --type when trying to reprocess'
81 ' all media_entries, unless you set --state'
82 ' to "failed".'))
83
84 _run_reprocessing(args)
85
86
87 def _run_reprocessing(args):
88 if args[0].available:
89 if args[0].state == 'failed':
90 print _('\n Available reprocess actions for all failed'
91 ' media_entries: \n \t --initial_processing')
92 else:
93 result = hook_handle(('reprocess_action', args[0].type), args)
94 if not result:
95 print _('Sorry there is no available reprocessing for {}'
96 ' entries in the {} state'.format(args[0].type,
97 args[0].state))
98 else:
99 return hook_handle(('media_reprocess', args[0].type), args)
100
101
102 def _set_media_state(args):
103 if args[0].media_id:
104 if len(args[0].media_id) == 1:
105 args[0].state = MediaEntry.query.filter_by(id=args[0].media_id[0])\
106 .first().state
107
108 elif len(args[0].media_id) > 1:
109 media_states = []
110
111 for id in args[0].media_id:
112 media_states.append(MediaEntry.query.filter_by(id=id).first()
113 .state)
114 for state in media_states:
115 if state != media_states[0]:
116 raise Exception(_('You can only reprocess media that is in the'
117 ' same state.'))
118
119 args[0].state = media_states[0]
120
121 if not args[0].state:
122 args[0].state = 'processed'
123
124
125 def reprocess(args):
126 commands_util.setup_app(args[0])
127
128 _set_media_state(args)
129 _set_media_type(args)
130
131 if not args[0].media_id:
132 return _reprocess_all(args)
133
134 return _run_reprocessing(args)