1 # -*- coding: utf-8 -*-
4 from optparse import make_option
9 from django.contrib.auth.models import User
10 from django.core.management.base import BaseCommand
11 from django.core.management.color import color_style
12 from django.db import transaction
14 from slughifi import slughifi
15 from catalogue.models import Chunk
18 REDMINE_CSV = 'http://redmine.nowoczesnapolska.org.pl/projects/wl-publikacje/issues.csv'
19 REDAKCJA_URL = 'http://redakcja.wolnelektury.pl/documents/'
22 class Command(BaseCommand):
23 option_list = BaseCommand.option_list + (
25 '-r', '--redakcja', dest='redakcja', metavar='URL',
26 help='Base URL of Redakcja documents',
27 default=REDAKCJA_URL),
29 '-q', '--quiet', action='store_false', dest='verbose', default=True,
32 '-f', '--force', action='store_true', dest='force', default=False,
33 help='Force assignment overwrite'),
35 help = 'Imports ticket assignments from Redmine.'
36 args = '[redmine-csv-url]'
38 def handle(self, *redmine_csv, **options):
40 self.style = color_style()
42 redakcja = options.get('redakcja')
43 verbose = options.get('verbose')
44 force = options.get('force')
48 print "Using default Redmine CSV URL:", REDMINE_CSV
49 redmine_csv = REDMINE_CSV
51 # Start transaction management.
52 transaction.commit_unless_managed()
53 transaction.enter_transaction_management()
54 transaction.managed(True)
56 redakcja_link = re.compile(re.escape(redakcja) + r'([-_.:?&%/a-zA-Z0-9]*)')
68 print 'Downloading CSV file'
69 for r in csv.reader(urllib2.urlopen(redmine_csv)):
77 print "Empty user, skipping"
81 first_name, last_name = unicode(username, 'utf-8').rsplit(u' ', 1)
83 user = User.objects.get(first_name=first_name, last_name=last_name)
84 except User.DoesNotExist:
85 print self.style.ERROR('Unknown user: ' + username)
86 unknown_users.setdefault(username, 0)
87 unknown_users[username] += 1
91 for fname in redakcja_link.findall(r[-1]):
92 fname = unicode(urllib.unquote(fname), 'utf-8', 'ignore')
93 if fname.endswith('.xml'):
95 fname = fname.replace(' ', '_')
96 fname = slughifi(fname)
98 chunks = Chunk.objects.filter(book__slug=fname)
100 print self.style.ERROR('Unknown book: ' + fname)
101 unknown_books.append(fname)
103 all_chunks += chunks.count()
107 if chunk.user == user:
110 forced.append((chunk, chunk.user, user))
112 print self.style.WARNING(
113 '%s assigned to %s, forcing change to %s.' %
114 (chunk.pretty_name(), chunk.user, user))
116 print self.style.WARNING(
117 '%s assigned to %s not to %s, skipping.' %
118 (chunk.pretty_name(), chunk.user, user))
131 print "Assignments imported from %d/%d tickets to %d/%d relevalt chunks." % (
132 done_tickets, all_tickets, done_chunks, all_chunks)
134 print "%d tickets were unassigned." % empty_users
136 print "%d assignments conficts (%s):" % (
137 len(forced), "changed" if force else "left")
138 for chunk, orig, user in forced:
139 print " %s: \t%s \t-> %s" % (
140 chunk.pretty_name(), orig.username, user.username)
142 print "%d unknown books:" % len(unknown_books)
143 for fname in unknown_books:
146 print "%d unknown users:" % len(unknown_users)
147 for name in unknown_users:
148 print " %s (%d tickets)" % (name, unknown_users[name])
152 transaction.leave_transaction_management()