/
subfolder.py
92 lines (76 loc) · 3.35 KB
/
subfolder.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
"""
mediatum - a multimedia content repository
Copyright (C) 2007 Arne Seifert <seiferta@in.tum.de>
Copyright (C) 2007 Matthias Kramm <kramm@in.tum.de>
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 logging
from utils.utils import dec_entry_log
from schema.schema import getMetaType
from core.translation import lang
from core.transition import current_user
from core import httpstatus
from core import Node
from core import db
q = db.query
logg = logging.getLogger(__name__)
@dec_entry_log
def getContent(req, ids):
user = current_user
language = lang(req)
node = q(Node).get(ids[0])
if "sort" in user.hidden_edit_functions or not node.has_write_access():
req.setStatus(httpstatus.HTTP_FORBIDDEN)
return req.getTAL("web/edit/edit.html", {}, macro="access_error")
logg.info("%s sorting subfolders of node %s (%s, %s): %s", user.login_name, node.id, node.name, node.type, req.params)
if "order" in req.params: # do reorder
ids = req.params.get('order').split(',')
children = []
for n in ids:
child = q(Node).get(n)
child.orderpos = ids.index(n)
children.append(child)
db.session.commit()
req.writeTAL('web/edit/modules/subfolder.html', {'nodelist': children, "language": language, "csrf": req.csrf_token.current_token}, macro="ordered_list")
return ""
elif "sortdirection" in req.params: # do automatic re-order
sorted_children = node.container_children.order_by(Node.name).all()
if req.params.get("sortdirection", "up") != "up":
sorted_children.reverse()
for position, child in enumerate(sorted_children, start=1):
child.orderpos = position
db.session.commit()
req.writeTAL('web/edit/modules/subfolder.html', {'nodelist': sorted_children, "language": language, "csrf": req.csrf_token.current_token}, macro="ordered_list")
return ""
nodelist = []
attributes = []
fields = {}
i = 0
for child in list(node.container_children.sort_by_orderpos()):
i += 1 # count container children
nodelist.append(child)
if getMetaType(child.schema):
for field in getMetaType(child.schema).getMetaFields():
if not field in fields.keys():
fields[field] = 0
fields[field] += 1
for field in fields:
if i == fields[field]:
attributes.append(field)
ctx = {
"node": node,
"nodelist": nodelist,
"sortattributes": sorted(attributes, lambda x, y: cmp(x.getLabel().lower(), y.getLabel().lower())),
"language": language,
"csrf": req.csrf_token.current_token
}
return req.getTAL("web/edit/modules/subfolder.html", ctx, macro="edit_subfolder")