trac#687: Add unit tests for `redirect` and `redirect_obj`.
[larjonas-mediagoblin.git] / mediagoblin / gmg_commands / deletemedia.py
blobbf50abdef538d6e33339ad4163deed7f3c6370a9
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/>.
17 from __future__ import print_function
18 import sys
20 from mediagoblin.gmg_commands import util as commands_util
23 def parser_setup(subparser):
24 subparser.add_argument('media_ids',
25 help='Comma separated list of media IDs will be deleted.')
28 def deletemedia(args):
29 app = commands_util.setup_app(args)
31 media_ids = set([int(mid) for mid in args.media_ids.split(',') if mid.isdigit()])
32 if not media_ids:
33 print('Can\'t find any valid media ID(s).')
34 sys.exit(1)
35 found_medias = set()
36 filter_ids = app.db.MediaEntry.id.in_(media_ids)
37 medias = app.db.MediaEntry.query.filter(filter_ids).all()
38 for media in medias:
39 found_medias.add(media.id)
40 media.delete()
41 print('Media ID %d has been deleted.' % media.id)
42 for media in media_ids - found_medias:
43 print('Can\'t find a media with ID %d.' % media)
44 print('Done.')
45 sys.exit(0)