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 fnpdjango.utils.text.slughifi import slughifi
16 from catalogue.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 unknown_users.setdefault(username, 0)
85 unknown_users[username] += 1
89 for fname in redakcja_link.findall(r[-1]):
90 fname = unicode(urllib.unquote(fname), 'utf-8', 'ignore')
91 if fname.endswith('.xml'):
93 fname = fname.replace(' ', '_')
94 fname = slughifi(fname)
96 chunks = Chunk.objects.filter(book__slug=fname)
98 print self.style.ERROR('Unknown book: ' + fname)
99 unknown_books.append(fname)
101 all_chunks += chunks.count()
105 if chunk.user == user:
108 forced.append((chunk, chunk.user, user))
110 print self.style.WARNING(
111 '%s assigned to %s, forcing change to %s.' %
112 (chunk.pretty_name(), chunk.user, user))
114 print self.style.WARNING(
115 '%s assigned to %s not to %s, skipping.' %
116 (chunk.pretty_name(), chunk.user, user))
130 print "Assignments imported from %d/%d tickets to %d/%d relevalt chunks." % (
131 done_tickets, all_tickets, done_chunks, all_chunks)
133 print "%d tickets were unassigned." % empty_users
135 print "%d assignments conficts (%s):" % (
136 len(forced), "changed" if force else "left")
137 for chunk, orig, user in forced:
138 print " %s: \t%s \t-> %s" % (
139 chunk.pretty_name(), orig.username, user.username)
141 print "%d unknown books:" % len(unknown_books)
142 for fname in unknown_books:
145 print "%d unknown users:" % len(unknown_users)
146 for name in unknown_users:
147 print " %s (%d tickets)" % (name, unknown_users[name])
152 transaction.leave_transaction_management()