@@ -0,0 +1,2442 @@
+# -*- coding: utf-8 -*-%0A#%0A# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.%0A# Copyright %C2%A9 Fundacja Nowoczesna Polska. See NOTICE for more information.%0A#%0A%0Aimport sys%0Afrom django.contrib.auth.models import User%0Afrom lxml import etree%0Afrom optparse import make_option%0A%0Afrom django.core.management import BaseCommand%0A%0Afrom catalogue.models import Book%0Afrom librarian import DCNS%0A%0A%0Aclass Command(BaseCommand):%0A    option_list = BaseCommand.option_list + (%0A        # make_option('-q', '--quiet', action='store_false', dest='verbose',%0A        #     default=True, help='Less output'),%0A        # make_option('-d', '--dry-run', action='store_true', dest='dry_run',%0A        #     default=False, help=%22Don't actually touch anything%22),%0A        make_option(%0A            '-u', '--username', dest='username', metavar='USER',%0A            help='Assign commits to this user (required, preferably yourself).'),%0A    )%0A    args = 'exclude_file'%0A%0A    def handle(self, exclude_file, **options):%0A        username = options.get('username')%0A%0A        if username:%0A            user = User.objects.get(username=username)%0A        else:%0A            print 'Please provide a username.'%0A            sys.exit(1)%0A%0A        excluded_slugs = %5Bline.strip() for line in open(exclude_file, 'rb') if line.strip()%5D%0A        books = Book.objects.exclude(slug__in=excluded_slugs)%0A%0A        for book in books:%0A            if not book.is_published():%0A                continue%0A            print 'processing %25s' %25 book.slug%0A            chunk = book.chunk_set.first()%0A            old_head = chunk.head%0A            src = old_head.materialize()%0A            tree = etree.fromstring(src)%0A            audience_nodes = tree.findall('.//' + DCNS(%22audience%22))%0A            if not audience_nodes:%0A                print '%25s has no audience, skipping' %25 book.slug%0A                continue%0A%0A            for node in audience_nodes:%0A                node.getparent().remove(node)%0A%0A            chunk.commit(%0A                etree.tostring(tree, encoding=unicode),%0A                author=user,%0A                description='automatyczne skasowanie audience',%0A                publishable=old_head.publishable%0A            )%0A            print 'committed %25s' %25 book.slug%0A            if not old_head.publishable:%0A                print 'Warning: %25s not publishable, last head: %25s, %25s' %25 (%0A                    book.slug, old_head.author.username, old_head.description%5B:40%5D.replace('%5Cn', ' '))%0A

add command to remove unneeded audience tags