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
 
  10 from optparse import make_option
 
  12 from django.core.management import BaseCommand
 
  14 from catalogue.models import Book
 
  15 from librarian import DCNS
 
  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"),
 
  25             '-u', '--username', dest='username', metavar='USER',
 
  26             help='Assign commits to this user (required, preferably yourself).'),
 
  30     def handle(self, exclude_file, **options):
 
  31         username = options.get('username')
 
  34             user = User.objects.get(username=username)
 
  36             print 'Please provide a username.'
 
  39         excluded_slugs = [line.strip() for line in open(exclude_file, 'rb') if line.strip()]
 
  40         books = Book.objects.exclude(slug__in=excluded_slugs)
 
  43             if not book.is_published():
 
  45             print 'processing %s' % book.slug
 
  46             chunk = book.chunk_set.first()
 
  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
 
  55             for node in audience_nodes:
 
  56                 node.getparent().remove(node)
 
  59                 etree.tostring(tree, encoding=unicode),
 
  61                 description='automatyczne skasowanie audience',
 
  62                 publishable=old_head.publishable
 
  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', ' '))