2851.2.1
by Guilherme Salgado
ShipIt Reports, take 1 |
1 |
#!/usr/bin/python
|
2 |
# Copyright 2005 Canonical Ltd. All rights reserved.
|
|
3 |
||
4 |
"""Script to generate reports with data from ShipIt orders."""
|
|
5 |
||
6 |
import _pythonpath |
|
7 |
||
8 |
from datetime import datetime, date |
|
9 |
import optparse |
|
10 |
import sys |
|
11 |
||
12 |
from zope.component import getUtility |
|
13 |
||
14 |
import pytz |
|
15 |
||
16 |
from canonical.config import config |
|
17 |
from canonical.uuid import generate_uuid |
|
18 |
from canonical.lp import initZopeless |
|
19 |
from canonical.launchpad.scripts import ( |
|
20 |
execute_zcml_for_scripts, logger, logger_options) |
|
21 |
from canonical.launchpad.interfaces import ( |
|
22 |
ILibraryFileAliasSet, IShippingRequestSet, IShipItReportSet) |
|
23 |
||
24 |
||
25 |
def _createLibraryFileAlias(csv_file, basename): |
|
26 |
"""Create and return a LibraryFileAlias containing the given csv file.
|
|
27 |
|
|
28 |
The filename is generated using the given basename, the current date
|
|
29 |
and a random string, in order for it to not be guessable.
|
|
30 |
"""
|
|
31 |
fileset = getUtility(ILibraryFileAliasSet) |
|
32 |
csv_file.seek(0) |
|
33 |
now = datetime.now(pytz.timezone('UTC')) |
|
34 |
filename = ('%s-%s-%s.csv' |
|
35 |
% (basename, now.strftime('%y-%m-%d'), generate_uuid())) |
|
36 |
return fileset.create( |
|
37 |
name=filename, size=len(csv_file.getvalue()), file=csv_file, |
|
38 |
contentType='text/plain') |
|
39 |
||
40 |
||
41 |
def main(argv): |
|
42 |
parser = optparse.OptionParser() |
|
43 |
# Add the verbose/quiet options.
|
|
44 |
logger_options(parser) |
|
45 |
||
46 |
options, args = parser.parse_args(argv[1:]) |
|
47 |
logger_obj = logger(options, 'shipit-reports') |
|
48 |
logger_obj.info('Generating ShipIt reports') |
|
49 |
||
50 |
ztm = initZopeless(implicitBegin=False) |
|
51 |
execute_zcml_for_scripts() |
|
52 |
requestset = getUtility(IShippingRequestSet) |
|
53 |
reportset = getUtility(IShipItReportSet) |
|
54 |
||
55 |
ztm.begin() |
|
56 |
csv_file = requestset.generateCountryBasedReport() |
|
57 |
reportset.new(_createLibraryFileAlias(csv_file, 'OrdersByCountry')) |
|
58 |
||
59 |
csv_file = requestset.generateShipmentSizeBasedReport() |
|
60 |
reportset.new(_createLibraryFileAlias(csv_file, 'OrdersBySize')) |
|
61 |
||
62 |
# XXX: For now this will be hardcoded as the date when we opened
|
|
63 |
# ShipItNG. In future we'll have UI to specify a start/end date
|
|
64 |
# for every report and then we'll be able to remove this.
|
|
65 |
# -- Guilherme Salgado, 2005-11-24
|
|
66 |
start_date = date(2005, 9, 14) |
|
67 |
end_date = date.today() |
|
68 |
csv_file = requestset.generateWeekBasedReport(start_date, end_date) |
|
69 |
reportset.new(_createLibraryFileAlias(csv_file, 'OrdersByWeek')) |
|
70 |
ztm.commit() |
|
71 |
||
72 |
logger_obj.info('Done.') |
|
73 |
return 0 |
|
74 |
||
75 |
||
76 |
if __name__ == '__main__': |
|
77 |
sys.exit(main(sys.argv)) |
|
78 |