Estimated costs for collections.
[redakcja.git] / src / documents / management / commands / prune_audience.py
1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 import sys
5 from django.contrib.auth.models import User
6 from lxml import etree
7
8 from django.core.management import BaseCommand
9
10 from documents.models import Book
11 from librarian import DCNS
12
13
14 class Command(BaseCommand):
15     args = 'exclude_file'
16
17     def add_arguments(self, parser):
18         parser.add_argument(
19             '-u', '--username', dest='username', metavar='USER',
20             help='Assign commits to this user (required, preferably yourself).')
21
22     def handle(self, exclude_file, **options):
23         username = options.get('username')
24
25         if username:
26             user = User.objects.get(username=username)
27         else:
28             print('Please provide a username.')
29             sys.exit(1)
30
31         excluded_slugs = [line.strip() for line in open(exclude_file, 'rb') if line.strip()]
32         books = Book.objects.exclude(slug__in=excluded_slugs)
33
34         for book in books:
35             if not book.is_published():
36                 continue
37             print('processing %s' % book.slug)
38             chunk = book.chunk_set.first()
39             old_head = chunk.head
40             src = old_head.materialize()
41             tree = etree.fromstring(src)
42             audience_nodes = tree.findall('.//' + DCNS("audience"))
43             if not audience_nodes:
44                 print('%s has no audience, skipping' % book.slug)
45                 continue
46
47             for node in audience_nodes:
48                 node.getparent().remove(node)
49
50             chunk.commit(
51                 etree.tostring(tree, encoding='unicode'),
52                 author=user,
53                 description='automatyczne skasowanie audience',
54                 publishable=old_head.publishable
55             )
56             print('committed %s' % book.slug)
57             if not old_head.publishable:
58                 print('Warning: %s not publishable, last head: %s, %s' % (
59                     book.slug, old_head.author.username, old_head.description[:40].replace('\n', ' ')))