Librarian in regular requirements.
[redakcja.git] / apps / catalogue / management / commands / __init__.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 import sys
7 from optparse import make_option
8 from django.contrib.auth.models import User
9 from django.core.management.base import BaseCommand
10 from catalogue.models import Book
11
12
13 class XmlUpdaterCommand(BaseCommand):
14     """Base class for creating massive XML-updating commands.
15
16     In a subclass, provide an XmlUpdater class in the `updater' attribute.
17     """
18     option_list = BaseCommand.option_list + (
19         make_option('-q', '--quiet', action='store_false', dest='verbose',
20             default=True, help='Less output'),
21         make_option('-d', '--dry-run', action='store_true', dest='dry_run',
22             default=False, help="Don't actually touch anything"),
23         make_option('-u', '--username', dest='username', metavar='USER',
24             help='Assign commits to this user (required, preferably yourself).'),
25     )
26     args = "[slug]..."
27
28     def handle(self, *args, **options):
29         verbose = options.get('verbose')
30         dry_run = options.get('dry_run')
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         books = Book.objects.filter(slug__in=args) if args else None
40
41         updater = self.updater()
42         updater.run(user, verbose=verbose, dry_run=dry_run, books=books)
43         updater.print_results()