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.
5 from django.contrib.auth.models import User
8 from django.core.management import BaseCommand
10 from catalogue.models import Book
11 from librarian import DCNS
14 class Command(BaseCommand):
17 def add_arguments(self, parser):
19 '-u', '--username', dest='username', metavar='USER',
20 help='Assign commits to this user (required, preferably yourself).')
22 def handle(self, exclude_file, **options):
23 username = options.get('username')
26 user = User.objects.get(username=username)
28 print('Please provide a username.')
31 excluded_slugs = [line.strip() for line in open(exclude_file, 'rb') if line.strip()]
32 books = Book.objects.exclude(slug__in=excluded_slugs)
35 if not book.is_published():
37 print('processing %s' % book.slug)
38 chunk = book.chunk_set.first()
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)
47 for node in audience_nodes:
48 node.getparent().remove(node)
51 etree.tostring(tree, encoding='unicode'),
53 description='automatyczne skasowanie audience',
54 publishable=old_head.publishable
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', ' ')))