1 # -*- coding: utf-8 -*-
4 from optparse import make_option
10 from django.contrib.auth.models import User
11 from django.core.management.base import BaseCommand
12 from django.core.management.color import color_style
13 from django.db import transaction
15 from slughifi import slughifi
16 from wiki.models import Chunk
19 REDMINE_CSV = 'http://redmine.nowoczesnapolska.org.pl/projects/wl-publikacje/issues.csv'
20 REDAKCJA_URL = 'http://redakcja.wolnelektury.pl/documents/'
23 class Command(BaseCommand):
24 option_list = BaseCommand.option_list + (
25 make_option('-r', '--redakcja', dest='redakcja', metavar='URL',
26 help='Base URL of Redakcja documents',
27 default=REDAKCJA_URL),
28 make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
30 make_option('-f', '--force', action='store_true', dest='force', default=False,
31 help='Force assignment overwrite'),
33 help = 'Imports ticket assignments from Redmine.'
34 args = '[redmine-csv-url]'
36 def handle(self, *redmine_csv, **options):
38 self.style = color_style()
40 redakcja = options.get('redakcja')
41 verbose = options.get('verbose')
42 force = options.get('force')
46 print "Using default Redmine CSV URL:", REDMINE_CSV
47 redmine_csv = REDMINE_CSV
49 # Start transaction management.
50 transaction.commit_unless_managed()
51 transaction.enter_transaction_management()
52 transaction.managed(True)
54 redakcja_link = re.compile(re.escape(redakcja) + r'([-_.:?&%/a-zA-Z0-9]*)')
66 print 'Downloading CSV file'
67 for r in csv.reader(urllib2.urlopen(redmine_csv)):
75 print "Empty user, skipping"
79 first_name, last_name = unicode(username, 'utf-8').rsplit(u' ', 1)
81 user = User.objects.get(first_name=first_name, last_name=last_name)
82 except User.DoesNotExist:
83 print self.style.ERROR('Unknown user: ' + username)
84 print "'%s' '%s'" % (first_name, last_name)
90 for fname in redakcja_link.findall(r[-1]):
91 fname = unicode(urllib.unquote(fname), 'utf-8', 'ignore')
92 if fname.endswith('.xml'):
94 fname = fname.replace(' ', '_')
95 fname = slughifi(fname)
97 chunks = Chunk.objects.filter(book__slug=fname)
99 print self.style.ERROR('Unknown book: ' + fname)
102 all_chunks += chunks.count()
106 if chunk.user == user:
111 print self.style.WARNING(
112 '%s assigned to %s, forcing change to %s.' %
113 (chunk.pretty_name(), chunk.user, user))
115 print self.style.WARNING(
116 '%s assigned to %s not to %s, skipping.' %
117 (chunk.pretty_name(), chunk.user, user))
131 print "Done %d/%d tickets, assigned %d/%d book chunks." % (
132 done_tickets, all_tickets, done_chunks, all_chunks)
133 print "%d tickets unassigned, for %d chunks assignment differed." % (
135 print "Unrecognized: %d books, %d users." % (
136 unknown_books, unknown_users)
141 transaction.leave_transaction_management()