support editable as result in find

This commit is contained in:
j 2016-08-23 15:24:37 +02:00
parent 2aa2a76cb5
commit 83c2e25aac
2 changed files with 19 additions and 1 deletions

View file

@ -573,6 +573,7 @@ class Item(models.Model):
} }
if self.user: if self.user:
i['user'] = self.user.username i['user'] = self.user.username
i['groups'] = [g.name for g in self.groups.all()]
i.update(self.external_data) i.update(self.external_data)
i.update(self.data) i.update(self.data)
if settings.USE_IMDB: if settings.USE_IMDB:

View file

@ -230,11 +230,25 @@ def find(request, data):
'numberofannotations', 'numberoffiles', 'numberofdocuments' 'numberofannotations', 'numberoffiles', 'numberofdocuments'
} }
groups = {g.name for g in request.user.groups.all()}
def is_editable(item):
if request.user.is_anonymous():
return False
if request.user.profile.capability('canEditMetadata') or \
request.user.is_staff or \
item.get('user') == request.user.username or \
item.get('groups', []) & groups:
return True
return False
def only_p_sums(m): def only_p_sums(m):
r = {} r = {}
for p in _p: for p in _p:
if p == 'accessed': if p == 'accessed':
r[p] = m.sort.accessed or '' r[p] = m.sort.accessed or ''
elif p == 'editable':
r[p] = is_editable(m.json)
elif p in sort_keys: elif p in sort_keys:
r[p] = getattr(m.sort, p) r[p] = getattr(m.sort, p)
else: else:
@ -249,6 +263,9 @@ def find(request, data):
if not isinstance(m, dict): if not isinstance(m, dict):
m = json.loads(m, object_hook=oxdjango.fields.from_json) m = json.loads(m, object_hook=oxdjango.fields.from_json)
for p in _p: for p in _p:
if p == 'editable':
r[p] = is_editable(m)
else:
r[p] = m.get(p) r[p] = m.get(p)
if 'clip_qs' in query: if 'clip_qs' in query:
r['clips'] = get_clips(query['clip_qs'].filter(item__public_id=m['id'])) r['clips'] = get_clips(query['clip_qs'].filter(item__public_id=m['id']))