1 # -*- coding: utf-8 -*-
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.
8 from django.contrib.auth.models import User
11 from django.core.management import BaseCommand
13 from catalogue.models import Book
14 from librarian import DCNS
17 class Command(BaseCommand):
20 def add_arguments(self, parser):
22 '-u', '--username', dest='username', metavar='USER',
23 help='Assign commits to this user (required, preferably yourself).')
25 def handle(self, exclude_file, **options):
26 username = options.get('username')
29 user = User.objects.get(username=username)
31 print('Please provide a username.')
34 excluded_slugs = [line.strip() for line in open(exclude_file, 'rb') if line.strip()]
35 books = Book.objects.exclude(slug__in=excluded_slugs)
38 if not book.is_published():
40 print('processing %s' % book.slug)
41 chunk = book.chunk_set.first()
43 src = old_head.materialize()
44 tree = etree.fromstring(src)
45 audience_nodes = tree.findall('.//' + DCNS("audience"))
46 if not audience_nodes:
47 print('%s has no audience, skipping' % book.slug)
50 for node in audience_nodes:
51 node.getparent().remove(node)
54 etree.tostring(tree, encoding='unicode'),
56 description='automatyczne skasowanie audience',
57 publishable=old_head.publishable
59 print('committed %s' % book.slug)
60 if not old_head.publishable:
61 print('Warning: %s not publishable, last head: %s, %s' % (
62 book.slug, old_head.author.username, old_head.description[:40].replace('\n', ' ')))