deletemedia.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 __future__ import print_function
  17. import sys
  18. from mediagoblin.gmg_commands import util as commands_util
  19. def parser_setup(subparser):
  20. subparser.add_argument('media_ids',
  21. help='Comma separated list of media IDs will be deleted.')
  22. def deletemedia(args):
  23. app = commands_util.setup_app(args)
  24. media_ids = set([int(mid) for mid in args.media_ids.split(',') if mid.isdigit()])
  25. if not media_ids:
  26. print('Can\'t find any valid media ID(s).')
  27. sys.exit(1)
  28. found_medias = set()
  29. filter_ids = app.db.MediaEntry.id.in_(media_ids)
  30. medias = app.db.MediaEntry.query.filter(filter_ids).all()
  31. for media in medias:
  32. found_medias.add(media.id)
  33. media.delete()
  34. print('Media ID %d has been deleted.' % media.id)
  35. for media in media_ids - found_medias:
  36. print('Can\'t find a media with ID %d.' % media)
  37. print('Done.')
  38. sys.exit(0)