-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker_views.py
173 lines (140 loc) · 6.25 KB
/
tracker_views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- coding: UTF-8 -*-
# Copyright (C) 2007 Luis Arturo Belmar-Letelier <[email protected]>
# Copyright (C) 2007 Sylvain Taverne <[email protected]>
# Copyright (C) 2007-2008 Henry Obein <[email protected]>
# Copyright (C) 2007-2008 Hervé Cauwelier <[email protected]>
# Copyright (C) 2007-2008 Juan David Ibáñez Palomar <[email protected]>
# Copyright (C) 2007-2008 Nicolas Deram <[email protected]>
# Copyright (C) 2009 Armel Fortun <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from the Standard Library
from time import strftime
from tempfile import mkdtemp
from subprocess import call
from zipfile import ZipFile
# Import from itools
from itools.core import guess_extension
from itools.gettext import MSG
from itools.fs import vfs
from itools.uri import encode_query, get_uri_name
from itools.fs import FileName
from itools.web import BaseView, ERROR, STLView
from itools.xml import XMLParser
from itools.handlers import File as FileHandler
# Import from ikaaro
from ikaaro.views import ContextMenu
from ikaaro.views import CompositeView
from ikaaro.tracker.tracker_views import Tracker_View, StoreSearchMenu
from ikaaro.tracker.tracker_views import TrackerViewMenu
from ikaaro.file import Image, Video
class Tchacker_ViewMenu(TrackerViewMenu):
title = MSG(u'Advanced')
def get_items(self, resource, context):
# Keep the query parameters
schema = context.view.get_query_schema()
params = encode_query(context.query, schema)
return [
{'title': MSG(u'Download "Last Att." images as one Zip'),
'href': ';zip?%s' % params}
] + TrackerViewMenu.get_items(self, resource, context)
class Tchacker_ViewTop(STLView):
template = '/ui/tchacker/tchacker_view_top.xml.en'
class Tchacker_ViewBottom(Tracker_View):
access = 'is_allowed_to_view'
title = MSG(u'View')
icon = 'view.png'
# XXX
#table_template = '/ui/tchacker/browse_table.xml'
#context.styles.append('/ui/tchacker/tracker.css')
def get_item_value(self, resource, context, item, column):
# Last Attachement
if column == 'issue_last_attachment':
attach_name = item.issue_last_attachment
if attach_name is None:
return None
attach = resource.get_resource('%s/%s' % (item.name, attach_name))
#print item.name, attach, isinstance(attach, Video)
if isinstance(attach, Image) is True:
img_template = '<img src="./%s/%s/;thumb?width=256&height=256"/>'
return XMLParser(img_template % (item.name, attach_name))
elif isinstance(attach, Video) is True:
img_template = '<img src="./%s/thumb_%s/;thumb?width=256&height=256"/>'
return XMLParser(img_template % (item.name, attach_name))
else:
return None
# Last Author
elif column == 'issue_last_author':
user_id = item.issue_last_author
user = resource.get_resource('/users/%s' % user_id, soft=True)
if user is None:
return None
return user.get_title()
return Tracker_View.get_item_value(self, resource, context, item,
column)
def get_table_columns(self, resource, context):
table_columns = Tracker_View.get_table_columns(self, resource, context)
# Insert the last attachement row's title in the table
table_columns.insert(2, ('issue_last_attachment', 'Last Attach.'))
table_columns.insert(11, ('issue_last_author', 'Last Auth.'))
return table_columns
class Tchacker_View(CompositeView):
access = 'is_allowed_to_view'
subviews = [Tchacker_ViewTop(),
Tchacker_ViewBottom()]
context_menus = [StoreSearchMenu(),
Tchacker_ViewMenu()]
def GET(self, resource, context):
context.scripts.append('/ui/tchacker/tracker.js')
context.styles.append('/ui/tchacker/tracker.css')
return CompositeView.GET(self, resource, context)
class Tracker_Zip_Img(Tchacker_ViewBottom):
access = 'is_allowed_to_view'
title = MSG(u'Zip Last Images')
def GET(self, resource, context):
items = self.get_items(resource, context)
issues = self.sort_and_batch(resource, context, items)
# Get path of all attachment to add in zip
dirname = '%s/zip' % mkdtemp('zip', 'ikaaro')
zip = ZipFile(dirname, 'w')
for issue in issues:
attachment_name = issue.issue_last_attachment
attachment = resource.get_resource('%s/%s' % (issue.name, attachment_name))
attachment_uri = attachment.handler.uri[7:]
# Get extension
mimetype = attachment.get_content_type()
ext = guess_extension(mimetype)[1:]
#attachment_name = 'issue_%s_%s' % (issue.name, get_uri_name(attachment_uri))
attachment_name = '%s_%s.%s' % (issue.name, issue.title, ext)
#print attachment_name, attachment_uri
zip.write(attachment_uri, attachment_name)
zip.close()
# Create zip
# Build zipname
name = "LastAttachedImages"
now = strftime("%y%d%m%H%M")
#pprint("%s" % now)
zipname = "%s_%s_%s.zip" % (resource.name, name, now)
file = open(dirname)
try:
data = file.read()
finally:
file.close()
vfs.remove(dirname)
# Return the zip
response = context.response
response.set_header('Content-Type', 'application/zip')
response.set_header('Content-Disposition',
'attachment; filename=%s' % zipname)
return data