def to_dict(self): item = {} item['id'] = self.id item['title'] = self.title item['created_at'] = self.human_created_at item['updated_at'] = self.human_updated_at item['timestamp'] = self.created_at.timestamp() if self.parent: item['parent_id'] = self.parent.id else: item['parent_id'] = '' item['ctype'] = 'folder' tags = [] for tag in self.tags.all(): tags.append(tag.to_dict()) item['tags'] = tags kvstore = [] for kv in self.kv.all(): kvstore.append(kv.to_dict()) item['metadata'] = {} item['metadata']['kvstore'] = kvstore item['metadata']['currency_formats'] = get_currency_formats() item['metadata']['date_formats'] = get_date_formats() item['metadata']['numeric_formats'] = get_numeric_formats() item['metadata']['kv_types'] = get_kv_types() return item
def metadata(request, model, id): """ model can be either node or page. Respectively id will be the 'id' of either node or page. """ if model == 'node': _Klass = BaseTreeNode else: _Klass = Page try: item = _Klass.objects.get(id=id) except _Klass.DoesNotExist: raise Http404("Node does not exists") kvstore = [] if request.method == 'GET': for kv in item.kv.all(): kvstore.append(kv.to_dict()) else: kv_data = json.loads(request.body) if 'kvstore' in kv_data: if isinstance(kv_data['kvstore'], list): item.kv.update(kv_data['kvstore']) return HttpResponse(json.dumps({ 'kvstore': kvstore, 'currency_formats': get_currency_formats(), 'date_formats': get_date_formats(), 'numeric_formats': get_numeric_formats(), 'kv_types': get_kv_types() }), content_type="application/json")
def metadata(request, model, id): """ model can be either node or page. Respectively id will be the 'id' of either node or page. E.g. POST /metadata/page/55 # will update metadata for page id=55 POST /metadata/node/40 # will update metadata for node id=40 """ if model == 'node': _Klass = BaseTreeNode else: _Klass = Page try: item = _Klass.objects.get(id=id) except _Klass.DoesNotExist: raise Http404("Node does not exists") kvstore = [] if request.method == 'GET': for kv in item.kv.all(): kvstore.append(kv.to_dict()) else: if isinstance(item, BaseTreeNode): node = item else: node = item.document if request.user.has_perm(Access.PERM_WRITE, node): kv_data = json.loads(request.body) if 'kvstore' in kv_data: if isinstance(kv_data['kvstore'], list): item.kv.update(_sanitize_kvstore_list(kv_data['kvstore'])) else: return HttpResponseForbidden() return HttpResponse(json.dumps({ 'kvstore': kvstore, 'currency_formats': get_currency_formats(), 'date_formats': get_date_formats(), 'numeric_formats': get_numeric_formats(), 'kv_types': get_kv_types() }), content_type="application/json")
def metadata(request, model, id): """ model can be either node or page. Respectively id will be the 'id' of either node or page. E.g. POST /metadata/page/55 # will update metadata for page id=55 POST /metadata/node/40 # will update metadata for node id=40 """ if model == 'node': _Klass = BaseTreeNode # if queried node is a document, then instead # of document's metadata - metadata of first page # of respective document will be returned. node = BaseTreeNode.objects.get(id=id) if node.is_document(): # will return first page # i.e. page with lowerest page.number # attribute _Klass = Page id = node.pages.first().id else: _Klass = Page try: item = _Klass.objects.get(id=id) except _Klass.DoesNotExist: raise Http404("Node does not exists") kvstore = [] if request.method == 'GET': for kv in item.kv.all(): kvstore.append(kv.to_dict()) else: # Note that for a document metadata can be updated in two ways: # # 1) POST /metadata/node/:id - request issued from document browser # :id is the ``id`` of respective node # 2) POST /metadata/page/:id i.e. - request issued from document # viewer. :id in this case is ``id`` of first page of the # document. # # For case 1) the key/value will be applied on the document and # propagated to the first page while for 2) the update is applied to # the page and not propagated as page models are leaf elements. # # This slight inconsistency works because document metadata is not used # at all. Instead what user thinks is document's metadata is actually # metadata of the first page of the document. if isinstance(item, BaseTreeNode): node = item else: node = item.document if request.user.has_perm(Access.PERM_WRITE, node): kv_data = json.loads(request.body) if 'kvstore' in kv_data: if isinstance(kv_data['kvstore'], list): item.kv.update(sanitize_kvstore_list(kv_data['kvstore'])) else: return HttpResponseForbidden() return HttpResponse(json.dumps({ 'kvstore': kvstore, 'currency_formats': get_currency_formats(), 'date_formats': get_date_formats(), 'numeric_formats': get_numeric_formats(), 'kv_types': get_kv_types() }), content_type="application/json")