add command option for regenerating all thumbnails
[mediagoblin.git] / mediagoblin / gmg_commands / reprocess.py
CommitLineData
c3071480
RE
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/>.
99b34c4c
RE
16from mediagoblin.db.models import MediaEntry
17from mediagoblin.gmg_commands import util as commands_util
18from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
6fc8aaf6 19from mediagoblin.tools.pluginapi import hook_handle
c3071480
RE
20
21
22def 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")
c3071480
RE
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(
842ba305 35 '--media_id',
c3071480
RE
36 nargs='*',
37 help="The media_entry id(s) you wish to reprocess.")
065db047
RE
38 subparser.add_argument(
39 '--thumbnails',
40 action="store_true",
41 help="Regenerate thumbnails for all processed media")
c3071480
RE
42
43
99b34c4c 44def _set_media_type(args):
842ba305
RE
45 if args[0].media_id:
46 if len(args[0].media_id) == 1:
47 media_type = MediaEntry.query.filter_by(id=args[0].media_id[0])\
48 .first().media_type.split('.')[-1]
49
50 if not args[0].type:
51 args[0].type = media_type
52 elif args[0].type != media_type:
53 raise Exception(_('The --type that you set does not match the type'
54 ' of the given media_id.'))
55 elif len(args[0].media_id) > 1:
56 media_types = []
57
58 for id in args[0].media_id:
59 media_types.append(MediaEntry.query.filter_by(id=id).first()
60 .media_type.split('.')[-1])
61 for type in media_types:
62 if media_types[0] != type:
63 raise Exception((u'You cannot reprocess different media_types'
64 ' at the same time.'))
65
66 if not args[0].type:
67 args[0].type = media_types[0]
68 elif args[0].type != media_types[0]:
69 raise Exception(_('The --type that you set does not match the type'
70 ' of the given media_ids.'))
99b34c4c 71
99b34c4c 72
6fc8aaf6
RE
73def _reprocess_all(args):
74 if not args[0].type:
065db047
RE
75 if args[0].thumbnails:
76 if args[0].available:
77 print _('Available options for regenerating all processed'
78 ' media thumbnails: \n'
79 '\t --size: max_width max_height'
80 ' (defaults to config specs)')
81 else:
82 #TODO regenerate all thumbnails
83 pass
84
85 elif args[0].state == 'failed':
6fc8aaf6 86 if args[0].available:
4a36407d
RE
87 print _('\n Available reprocess actions for all failed'
88 ' media_entries: \n \t --initial_processing')
6fc8aaf6
RE
89 else:
90 #TODO reprocess all failed entries
91 pass
065db047 92
6fc8aaf6
RE
93 else:
94 raise Exception(_('You must set --type when trying to reprocess'
95 ' all media_entries, unless you set --state'
96 ' to "failed".'))
97
065db047
RE
98 else:
99 _run_reprocessing(args)
7c1f6a6a
RE
100
101
102def _run_reprocessing(args):
6fc8aaf6 103 if args[0].available:
11a99d78
RE
104 if args[0].state == 'failed':
105 print _('\n Available reprocess actions for all failed'
106 ' media_entries: \n \t --initial_processing')
107 else:
108 result = hook_handle(('reprocess_action', args[0].type), args)
109 if not result:
110 print _('Sorry there is no available reprocessing for {}'
111 ' entries in the {} state'.format(args[0].type,
112 args[0].state))
6fc8aaf6
RE
113 else:
114 return hook_handle(('media_reprocess', args[0].type), args)
115
116
243756e0 117def _set_media_state(args):
842ba305
RE
118 if args[0].media_id:
119 if len(args[0].media_id) == 1:
120 args[0].state = MediaEntry.query.filter_by(id=args[0].media_id[0])\
121 .first().state
243756e0 122
842ba305
RE
123 elif len(args[0].media_id) > 1:
124 media_states = []
99b34c4c 125
842ba305
RE
126 for id in args[0].media_id:
127 media_states.append(MediaEntry.query.filter_by(id=id).first()
128 .state)
129 for state in media_states:
130 if state != media_states[0]:
131 raise Exception(_('You can only reprocess media that is in the'
132 ' same state.'))
243756e0 133
842ba305 134 args[0].state = media_states[0]
243756e0 135
842ba305 136 if not args[0].state:
6fc8aaf6
RE
137 args[0].state = 'processed'
138
6fc8aaf6 139
243756e0
RE
140def reprocess(args):
141 commands_util.setup_app(args[0])
142
143 _set_media_state(args)
99b34c4c 144 _set_media_type(args)
7c1f6a6a 145
243756e0
RE
146 if not args[0].media_id:
147 return _reprocess_all(args)
148
7c1f6a6a 149 return _run_reprocessing(args)