fix links to local files in editor
[redakcja.git] / apps / catalogue / management / commands / remove_empty_tags.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 import sys
7 from django.contrib.auth.models import User
8 from optparse import make_option
9
10 from django.core.management import BaseCommand
11
12 from catalogue.models import Book
13 from catalogue.xml_tools import remove_empty_elements
14
15 EXCLUDED_SLUGS = [
16     'aktualizacja-szablonu-8kwie',
17 ]
18
19
20 class Command(BaseCommand):
21     option_list = BaseCommand.option_list + (
22         # make_option('-q', '--quiet', action='store_false', dest='verbose',
23         #     default=True, help='Less output'),
24         # make_option('-d', '--dry-run', action='store_true', dest='dry_run',
25         #     default=False, help="Don't actually touch anything"),
26         make_option(
27             '-u', '--username', dest='username', metavar='USER',
28             help='Assign commits to this user (required, preferably yourself).'),
29     )
30
31     def handle(self, **options):
32         username = options.get('username')
33
34         if username:
35             user = User.objects.get(username=username)
36         else:
37             print 'Please provide a username.'
38             sys.exit(1)
39
40         for book in Book.objects.all():
41             if book.slug in EXCLUDED_SLUGS:
42                 continue
43             print 'processing %s' % book.slug
44             for chunk in book.chunk_set.all():
45                 old_head = chunk.head
46                 src = old_head.materialize()
47                 new_xml = remove_empty_elements(src)
48                 if new_xml:
49                     new_head = chunk.commit(
50                         new_xml,
51                         author=user,
52                         description=u'automatyczne usunięcie pustych znaczników'
53                     )
54                     print 'committed %s (chunk %s)' % (book.slug, chunk.number)
55                     if old_head.publishable:
56                         new_head.set_publishable(True)