Rearrange source to src dir.
[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 from optparse import make_option
11
12 from django.core.management import BaseCommand
13
14 from catalogue.models import Book
15 from librarian import DCNS
16
17
18 class Command(BaseCommand):
19     option_list = BaseCommand.option_list + (
20         # make_option('-q', '--quiet', action='store_false', dest='verbose',
21         #     default=True, help='Less output'),
22         # make_option('-d', '--dry-run', action='store_true', dest='dry_run',
23         #     default=False, help="Don't actually touch anything"),
24         make_option(
25             '-u', '--username', dest='username', metavar='USER',
26             help='Assign commits to this user (required, preferably yourself).'),
27     )
28     args = 'exclude_file'
29
30     def handle(self, exclude_file, **options):
31         username = options.get('username')
32
33         if username:
34             user = User.objects.get(username=username)
35         else:
36             print 'Please provide a username.'
37             sys.exit(1)
38
39         excluded_slugs = [line.strip() for line in open(exclude_file, 'rb') if line.strip()]
40         books = Book.objects.exclude(slug__in=excluded_slugs)
41
42         for book in books:
43             if not book.is_published():
44                 continue
45             print 'processing %s' % book.slug
46             chunk = book.chunk_set.first()
47             old_head = chunk.head
48             src = old_head.materialize()
49             tree = etree.fromstring(src)
50             audience_nodes = tree.findall('.//' + DCNS("audience"))
51             if not audience_nodes:
52                 print '%s has no audience, skipping' % book.slug
53                 continue
54
55             for node in audience_nodes:
56                 node.getparent().remove(node)
57
58             chunk.commit(
59                 etree.tostring(tree, encoding=unicode),
60                 author=user,
61                 description='automatyczne skasowanie audience',
62                 publishable=old_head.publishable
63             )
64             print 'committed %s' % book.slug
65             if not old_head.publishable:
66                 print 'Warning: %s not publishable, last head: %s, %s' % (
67                     book.slug, old_head.author.username, old_head.description[:40].replace('\n', ' '))