def get_node_path(request): if request.method == 'POST': data = json.loads(request.body) try: topic = ContentNode.objects.prefetch_related('children').get( node_id=data['topic_id'], tree_id=data['tree_id']) if topic.kind_id != content_kinds.TOPIC: node = ContentNode.objects.prefetch_related('files')\ .prefetch_related('assessment_items')\ .prefetch_related('tags').get(node_id=data['topic_id'], tree_id=data['tree_id']) nodes = node.get_ancestors(ascending=True) else: node = data['node_id'] and ContentNode.objects.prefetch_related('files')\ .prefetch_related('assessment_items')\ .prefetch_related('tags').get(node_id=data['node_id'], tree_id=data['tree_id']) nodes = topic.get_ancestors(include_self=True, ascending=True) return HttpResponse( json.dumps({ 'path': JSONRenderer().render( ContentNodeSerializer(nodes, many=True).data), 'node': node and JSONRenderer().render( ContentNodeEditSerializer(node).data), 'parent_node_id': topic.kind_id != content_kinds.TOPIC and node.parent and node.parent.node_id })) except ObjectDoesNotExist: return HttpResponseNotFound( "Invalid URL: the referenced content does not exist in this channel." )
def get_node_path(request, topic_id, tree_id, node_id): try: topic = ContentNode.objects.prefetch_related('children').get( node_id__startswith=topic_id, tree_id=tree_id) if topic.kind_id != content_kinds.TOPIC: node = ContentNode.objects.prefetch_related( 'files', 'assessment_items', 'tags').get(node_id__startswith=topic_id, tree_id=tree_id) nodes = node.get_ancestors(ascending=True) else: node = node_id and ContentNode.objects.prefetch_related( 'files', 'assessment_items', 'tags').get( node_id__startswith=node_id, tree_id=tree_id) nodes = topic.get_ancestors(include_self=True, ascending=True) return HttpResponse( json.dumps({ 'path': JSONRenderer().render( ContentNodeSerializer(nodes, many=True).data), 'node': node and JSONRenderer().render( ContentNodeEditSerializer(node).data), 'parent_node_id': topic.kind_id != content_kinds.TOPIC and node.parent and node.parent.node_id })) except ObjectDoesNotExist: return HttpResponseNotFound( "Invalid URL: the referenced content does not exist in this channel." )
def file_create(request): if request.method == 'POST': original_filename, ext = os.path.splitext( request.FILES.values()[0]._name) size = request.FILES.values()[0]._size presets = FormatPreset.objects.filter( allowed_formats__extension__contains=ext[1:].lower()) kind = presets.first().kind preferences = json.loads(request.user.preferences) author = preferences.get('author') or "" license = License.objects.filter( license_name=preferences.get('license')).first( ) # Use filter/first in case preference hasn't been set license_id = license.pk if license else settings.DEFAULT_LICENSE new_node = ContentNode( title=original_filename, kind=kind, license_id=license_id, author=author, copyright_holder=preferences.get('copyright_holder')) if license.license_name == licenses.SPECIAL_PERMISSIONS: new_node.license_description = preferences.get( 'license_description') new_node.save() file_object = File(file_on_disk=DjFile(request.FILES.values()[0]), file_format_id=ext[1:].lower(), original_filename=request.FILES.values()[0]._name, contentnode=new_node, file_size=size) file_object.save() if kind.pk == content_kinds.VIDEO: file_object.preset_id = guess_video_preset_by_resolution( str(file_object.file_on_disk)) elif presets.filter(supplementary=False).count() == 1: file_object.preset = presets.filter(supplementary=False).first() file_object.save() try: if preferences.get('auto_derive_video_thumbnail') and new_node.kind_id == content_kinds.VIDEO \ or preferences.get('auto_derive_audio_thumbnail') and new_node.kind_id == content_kinds.AUDIO \ or preferences.get('auto_derive_html5_thumbnail') and new_node.kind_id == content_kinds.HTML5 \ or preferences.get('auto_derive_document_thumbnail') and new_node.kind_id == content_kinds.DOCUMENT: generate_thumbnail_from_node(new_node, set_node=True) except Exception: pass return HttpResponse( json.dumps({ "success": True, "node": JSONRenderer().render( ContentNodeEditSerializer(new_node).data) }))
def create_new_node(request): if request.method == 'POST': data = json.loads(request.body) license = License.objects.filter( license_name=data.get('license_name')).first( ) # Use filter/first in case preference hasn't been set license_id = license.pk if license else settings.DEFAULT_LICENSE new_node = ContentNode.objects.create( kind_id=data.get('kind'), title=data.get('title'), author=data.get('author'), copyright_holder=data.get('copyright_holder'), license_id=license_id, license_description=data.get('license_description')) return HttpResponse(JSONRenderer().render( ContentNodeEditSerializer(new_node).data))
def create_new_node(request): data = request.data license = License.objects.filter(license_name=data.get('license_name')).first() # Use filter/first in case preference hasn't been set license_id = license.pk if license else settings.DEFAULT_LICENSE new_node = ContentNode.objects.create( kind_id=data.get('kind'), title=data.get('title'), author=data.get('author'), aggregator=data.get('aggregator'), provider=data.get('provider'), copyright_holder=data.get('copyright_holder'), license_id=license_id, license_description=data.get('license_description'), parent_id=settings.ORPHANAGE_ROOT_ID, ) return Response(ContentNodeEditSerializer(new_node).data)
def create_new_node(request): if request.method != 'POST': return HttpResponseBadRequest( "Only POST requests are allowed on this endpoint.") data = json.loads(request.body) license = License.objects.filter( license_name=data.get('license_name')).first( ) # Use filter/first in case preference hasn't been set license_id = license.pk if license else settings.DEFAULT_LICENSE new_node = ContentNode.objects.create( kind_id=data.get('kind'), title=data.get('title'), author=data.get('author'), aggregator=data.get('aggregator'), provider=data.get('provider'), copyright_holder=data.get('copyright_holder'), license_id=license_id, license_description=data.get('license_description'), parent_id=settings.ORPHANAGE_ROOT_ID, ) return HttpResponse(JSONRenderer().render( ContentNodeEditSerializer(new_node).data))
def get_nodes_by_ids_complete(request): if request.method == 'POST': nodes = ContentNode.objects.prefetch_related('children').prefetch_related('files')\ .prefetch_related('assessment_items').prefetch_related('tags').filter(pk__in=json.loads(request.body)) return HttpResponse(JSONRenderer().render( ContentNodeEditSerializer(nodes, many=True).data))
def file_create(request): if request.method == 'POST': original_filename, ext = os.path.splitext( request.FILES.values()[0]._name) size = request.FILES.values()[0]._size contentfile = DjFile(request.FILES.values()[0]) checksum = get_hash(contentfile) request.user.check_space(size, checksum) presets = FormatPreset.objects.filter( allowed_formats__extension__contains=ext[1:].lower()) kind = presets.first().kind preferences = json.loads(request.META.get('HTTP_PREFERENCES')) author = preferences.get('author') or "" license = License.objects.filter( license_name=preferences.get('license')).first( ) # Use filter/first in case preference hasn't been set license_id = license.pk if license else None new_node = ContentNode( title=original_filename, kind=kind, license_id=license_id, author=author, copyright_holder=preferences.get('copyright_holder'), ) if license and license.is_custom: new_node.license_description = preferences.get( 'license_description') new_node.save() file_object = File( file_on_disk=contentfile, checksum=checksum, file_format_id=ext[1:].lower(), original_filename=request.FILES.values()[0]._name, contentnode=new_node, file_size=size, uploaded_by=request.user, ) file_object.save() if kind.pk == content_kinds.VIDEO: file_object.preset_id = guess_video_preset_by_resolution( str(file_object.file_on_disk)) elif presets.filter(supplementary=False).count() == 1: file_object.preset = presets.filter(supplementary=False).first() file_object.save() thumbnail = None try: if preferences.get('auto_derive_video_thumbnail') and new_node.kind_id == content_kinds.VIDEO \ or preferences.get('auto_derive_audio_thumbnail') and new_node.kind_id == content_kinds.AUDIO \ or preferences.get('auto_derive_html5_thumbnail') and new_node.kind_id == content_kinds.HTML5 \ or preferences.get('auto_derive_document_thumbnail') and new_node.kind_id == content_kinds.DOCUMENT: thumbnail = generate_thumbnail_from_node(new_node, set_node=True) request.user.check_space(thumbnail.file_size, thumbnail.checksum) except Exception: if thumbnail: thumbnail.delete() return HttpResponse( json.dumps({ "success": True, "node": JSONRenderer().render( ContentNodeEditSerializer(new_node).data) }))
def get_nodes_by_ids_complete(request, ids): nodes = ContentNode.objects.prefetch_related( 'children', 'files', 'assessment_items', 'tags').filter(pk__in=ids.split(",")) serializer = ContentNodeEditSerializer(nodes, many=True) return Response(serializer.data)
def file_create(request): if request.method != 'POST': return HttpResponseBadRequest( "Only POST requests are allowed on this endpoint.") original_filename, ext = os.path.splitext(request.FILES.values()[0]._name) size = request.FILES.values()[0]._size contentfile = DjFile(request.FILES.values()[0]) checksum = get_hash(contentfile) request.user.check_space(size, checksum) presets = FormatPreset.objects.filter( allowed_formats__extension__contains=ext[1:].lower()) kind = presets.first().kind preferences = json.loads( request.POST.get('content_defaults', None) or "{}") license = License.objects.filter( license_name=preferences.get('license')).first( ) # Use filter/first in case preference hasn't been set license_id = license.pk if license else None new_node = ContentNode( title=original_filename, kind=kind, license_id=license_id, author=preferences.get('author') or "", aggregator=preferences.get('aggregator') or "", provider=preferences.get('provider') or "", copyright_holder=preferences.get('copyright_holder'), parent_id=settings.ORPHANAGE_ROOT_ID, ) if license and license.is_custom: new_node.license_description = preferences.get('license_description') # The orphanage is not an actual tree but just a long list of items. with ContentNode.objects.disable_mptt_updates(): new_node.save() file_object = File( file_on_disk=contentfile, checksum=checksum, file_format_id=ext[1:].lower(), original_filename=request.FILES.values()[0]._name, contentnode=new_node, file_size=size, uploaded_by=request.user, ) file_object.save() if kind.pk == content_kinds.VIDEO: file_object.preset_id = guess_video_preset_by_resolution( str(file_object.file_on_disk)) elif presets.filter(supplementary=False).count() == 1: file_object.preset = presets.filter(supplementary=False).first() file_object.save() thumbnail = None try: if preferences.get('auto_derive_video_thumbnail') and new_node.kind_id == content_kinds.VIDEO \ or preferences.get('auto_derive_audio_thumbnail') and new_node.kind_id == content_kinds.AUDIO \ or preferences.get('auto_derive_html5_thumbnail') and new_node.kind_id == content_kinds.HTML5 \ or preferences.get('auto_derive_document_thumbnail') and new_node.kind_id == content_kinds.DOCUMENT: thumbnail = generate_thumbnail_from_node(new_node, set_node=True) request.user.check_space(thumbnail.file_size, thumbnail.checksum) except Exception: if thumbnail: thumbnail.delete() return HttpResponse( json.dumps({ "success": True, "node": JSONRenderer().render(ContentNodeEditSerializer(new_node).data) }))