Fixes
[redakcja.git] / src / catalogue / management / commands / prune_audience.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
7 import sys
8 from django.contrib.auth.models import User
9 from lxml import etree
10
11 from django.core.management import BaseCommand
12
13 from catalogue.models import Book
14 from librarian import DCNS
15
16
17 class Command(BaseCommand):
18     args = 'exclude_file'
19
20     def add_arguments(self, parser):
21         parser.add_argument(
22             '-u', '--username', dest='username', metavar='USER',
23             help='Assign commits to this user (required, preferably yourself).')
24
25     def handle(self, exclude_file, **options):
26         username = options.get('username')
27
28         if username:
29             user = User.objects.get(username=username)
30         else:
31             print('Please provide a username.')
32             sys.exit(1)
33
34         excluded_slugs = [line.strip() for line in open(exclude_file, 'rb') if line.strip()]
35         books = Book.objects.exclude(slug__in=excluded_slugs)
36
37         for book in books:
38             if not book.is_published():
39                 continue
40             print('processing %s' % book.slug)
41             chunk = book.chunk_set.first()
42             old_head = chunk.head
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)
48                 continue
49
50             for node in audience_nodes:
51                 node.getparent().remove(node)
52
53             chunk.commit(
54                 etree.tostring(tree, encoding='unicode'),
55                 author=user,
56                 description='automatyczne skasowanie audience',
57                 publishable=old_head.publishable
58             )
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', ' ')))