45177497b47c14d1c6b9b43220a3b3e2a92025d2
[redakcja.git] / apps / wiki / management / commands / assign_from_redmine.py
1 # -*- coding: utf-8 -*-
2
3 import csv
4 from optparse import make_option
5 import re
6 import sys
7 import urllib
8 import urllib2
9
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
14
15 from slughifi import slughifi
16 from wiki.models import Chunk
17
18
19 REDMINE_CSV = 'http://redmine.nowoczesnapolska.org.pl/projects/wl-publikacje/issues.csv'
20 REDAKCJA_URL = 'http://redakcja.wolnelektury.pl/documents/'
21
22
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,
29             help='Less output'),
30         make_option('-f', '--force', action='store_true', dest='force', default=False,
31             help='Force assignment overwrite'),
32     )
33     help = 'Imports ticket assignments from Redmine.'
34     args = '[redmine-csv-url]'
35
36     def handle(self, *redmine_csv, **options):
37
38         self.style = color_style()
39
40         redakcja = options.get('redakcja')
41         verbose = options.get('verbose')
42         force = options.get('force')
43
44         if not redmine_csv:
45             if verbose:
46                 print "Using default Redmine CSV URL:", REDMINE_CSV
47             redmine_csv = REDMINE_CSV
48
49         # Start transaction management.
50         transaction.commit_unless_managed()
51         transaction.enter_transaction_management()
52         transaction.managed(True)
53
54         redakcja_link = re.compile(re.escape(redakcja) + r'([-_.:?&%/a-zA-Z0-9]*)')
55
56         all_tickets = 0
57         all_chunks = 0
58         done_tickets = 0
59         done_chunks = 0
60         empty_users = 0
61         unknown_users = 0
62         unknown_books = 0
63         forced = 0
64
65         if verbose:
66             print 'Downloading CSV file'
67         for r in csv.reader(urllib2.urlopen(redmine_csv)):
68             if r[0] == '#':
69                 continue
70             all_tickets += 1
71
72             username = r[6]
73             if not username:
74                 if verbose:
75                     print "Empty user, skipping"
76                 empty_users += 1
77                 continue
78
79             first_name, last_name = unicode(username, 'utf-8').rsplit(u' ', 1)
80             try:
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)
85                 print type(last_name)
86                 unknown_users += 1
87                 continue
88
89             ticket_done = False
90             for fname in redakcja_link.findall(r[-1]):
91                 fname = unicode(urllib.unquote(fname), 'utf-8', 'ignore')
92                 if fname.endswith('.xml'):
93                     fname = fname[:-4]
94                 fname = fname.replace(' ', '_')
95                 fname = slughifi(fname)
96
97                 chunks = Chunk.objects.filter(book__slug=fname)
98                 if not chunks:
99                     print self.style.ERROR('Unknown book: ' + fname)
100                     unknown_books += 1
101                     continue
102                 all_chunks += chunks.count()
103
104                 for chunk in chunks:
105                     if chunk.user:
106                         if chunk.user == user:
107                             continue
108                         else:
109                             forced += 1
110                             if force:
111                                 print self.style.WARNING(
112                                     '%s assigned to %s, forcing change to %s.' %
113                                     (chunk.pretty_name(), chunk.user, user))
114                             else:
115                                 print self.style.WARNING(
116                                     '%s assigned to %s not to %s, skipping.' %
117                                     (chunk.pretty_name(), chunk.user, user))
118                                 continue
119                     chunk.user = user
120                     chunk.save()
121                     ticket_done = True
122                     done_chunks += 1
123
124             if ticket_done:
125                 done_tickets += 1
126
127
128         # Print results
129         print
130         print "Results:"
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." % (
134                 empty_users, forced)
135         print "Unrecognized: %d books, %d users." % (
136                 unknown_books, unknown_users)
137         print
138
139
140         transaction.commit()
141         transaction.leave_transaction_management()
142