def post(self, request): user = self.parse_user(request) if not request.course or not request.course.is_member(user): return HttpResponseForbidden( "You must be a member of the course to add assets.") req_dict = getattr(request, request.method) metadata = self.parse_metadata(req_dict) title = req_dict.get('title', '') success, asset = Asset.objects.get_by_args( req_dict, asset__course=request.course) if success is False: raise AssertionError("no arguments were supplied to make an asset") if asset is None: asset = Asset(title=title[:1020], # max title length course=request.course, author=user) asset.save() self.add_sources(request, asset) self.add_tags(asset, user, metadata) asset.metadata_blob = json.dumps(metadata) asset.save() try: asset.primary # make sure a primary source was specified except Source.DoesNotExist: # we'll make it here if someone doesn't submit # any primary_labels as arguments # @todo verify the above comment. raise AssertionError("no primary source provided") # create a global annotation asset.global_annotation(user, True) asset_url = reverse('asset-view', args=[asset.id]) source = request.POST.get('asset-source', "") if source == 'bookmarklet': # bookmarklet create asset_url += "?level=item" template = loader.get_template('assetmgr/analyze.html') context = RequestContext(request, { 'request': request, 'user': user, 'action': request.POST.get('button', None), 'asset_url': asset_url }) return HttpResponse(template.render(context)) else: # server2server create (wardenclyffe) return HttpResponseRedirect(asset_url)
def import_course_data(self, json_file, course): raw_data = open(json_file) json_data = json.load(raw_data) for asset_data in json_data['asset_set']: # Create asset author = User.objects.get( username=asset_data["author"]["username"]) asset = Asset(author=author, title=asset_data["title"], course=course) asset.metadata_blob = asset_data["metadata_blob"] asset.save() # Add sources for key, value in asset_data["sources"].items(): source_data = asset_data["sources"][key] source = Source(asset=asset, label=source_data["label"], url=source_data["url"], primary=source_data["primary"], media_type=source_data["media_type"], size=source_data["size"], height=source_data["height"], width=source_data["width"]) source.save() # Recreate annotations for ann_data in asset_data["annotations"]: ann_author = User.objects.get( username=ann_data["author"]["username"]) if ann_data["is_global_annotation"]: ann = asset.global_annotation(ann_author, True) else: ann = SherdNote(asset=asset, author=ann_author) ann.range1 = ann_data["range1"] ann.range2 = ann_data["range2"] ann.annotation_data = ann_data["annotation_data"] ann.title = ann_data["title"] tags = "" for tag in ann_data["metadata"]["tags"]: tags = tags + "," + tag["name"] ann.tags = tags ann.body = ann_data["metadata"]["body"] ann.save()
def asset_create(request): """ We'd like to support basically the Delicious URL API as much as possible /save?jump={yes|close}&url={url}&title={title}&{noui}&v={5}&share={yes} But also thumb={url}&stream={url}&... Other groups to pay attention to are MediaMatrix (seems subset of delicious: url=) """ req_dict = getattr(request, request.method) user = _parse_user(request) metadata = _parse_metadata(req_dict) title = req_dict.get('title', '') asset = Asset.objects.get_by_args(req_dict, asset__course=request.course) if asset is False: raise AssertionError("no arguments were supplied to make an asset") if asset is None: try: asset = Asset(title=title[:1020], # max title length course=request.course, author=user) asset.save() for source in sources_from_args(request, asset).values(): if len(source.url) <= 4096: source.save() if "tag" in metadata: for t in metadata["tag"]: asset.save_tag(user, t) asset.metadata_blob = simplejson.dumps(metadata) asset.save() except: # we'll make it here if someone doesn't submit # any primary_labels as arguments # @todo verify the above comment. raise AssertionError("no primary source provided") # create a global annotation asset.global_annotation(user, True) asset_url = reverse('asset-view', args=[asset.id]) source = request.POST.get('asset-source', "") if source == 'bookmarklet': asset_url += "?level=item" # for bookmarklet mass-adding if request.REQUEST.get('noui', '').startswith('postMessage'): return render_to_response('assetmgr/interface_iframe.html', {'message': ('%s|%s' % (request.build_absolute_uri(asset_url)), request.REQUEST['noui']), }) elif request.is_ajax(): return HttpResponse(serializers.serialize('json', asset), mimetype="application/json") elif "archive" == asset.primary.label: redirect_url = request.POST.get('redirect-url', reverse('class-manage-sources')) url = "%s?newsrc=%s" % (redirect_url, asset.title) return HttpResponseRedirect(url) else: return HttpResponseRedirect(asset_url)
def post(self, request): user = self.parse_user(request) if not request.course: raise Http404("No course provided") if not request.course.is_member(user): return HttpResponseForbidden( "You must be a member of the course to add assets.") req_dict = getattr(request, request.method) d = self.process_request_data(req_dict) metadata = self.parse_metadata(d) title = d.get('title', '') success, asset = Asset.objects.get_by_args( d, asset__course=request.course) if success is False: capture_exception('Asset creation failed with request data: ' + str(d)) return HttpResponseBadRequest( 'The selected asset didn\'t have the correct data to be ' + 'imported into Mediathread.') if asset is None: asset = Asset( title=title[:1020], # max title length course=request.course, author=user) asset.save() req = HttpRequest() req.method = 'POST' req.POST.update(d) self.add_sources(req, asset) self.add_tags(asset, user, metadata) asset.metadata_blob = json.dumps(metadata) asset.save() try: asset.primary # make sure a primary source was specified except Source.DoesNotExist: # we'll make it here if someone doesn't submit # any primary_labels as arguments # @todo verify the above comment. raise AssertionError("no primary source provided") # create a global annotation asset.global_annotation(user, True) asset_url = reverse('asset-view', args=[request.course.id, asset.id]) source = request.POST.get('asset-source', "") if source == 'bookmarklet': # bookmarklet create asset_url += "?level=item" template = loader.get_template('assetmgr/analyze.html') context = { 'request': request, 'user': user, 'action': request.POST.get('button', None), 'asset_url': asset_url } return HttpResponse(template.render(context)) else: # server2server create (wardenclyffe) return HttpResponseRedirect(asset_url)
def asset_create(request): """ We'd like to support basically the Delicious URL API as much as possible /save?jump={yes|close}&url={url}&title={title}&{noui}&v={5}&share={yes} But also thumb={url}&stream={url}&... Other groups to pay attention to are MediaMatrix (seems subset of delicious: url=) """ req_dict = getattr(request, request.method) user = _parse_user(request) metadata = _parse_metadata(req_dict) title = req_dict.get('title', '') asset = Asset.objects.get_by_args(req_dict, asset__course=request.course) if asset is False: raise AssertionError("no arguments were supplied to make an asset") if asset is None: try: asset = Asset( title=title[:1020], # max title length course=request.course, author=user) asset.save() for source in sources_from_args(request, asset).values(): if len(source.url) <= 4096: source.save() if "tag" in metadata: for each_tag in metadata["tag"]: asset.save_tag(user, each_tag) asset.metadata_blob = simplejson.dumps(metadata) asset.save() except: # we'll make it here if someone doesn't submit # any primary_labels as arguments # @todo verify the above comment. raise AssertionError("no primary source provided") # create a global annotation asset.global_annotation(user, True) asset_url = reverse('asset-view', args=[asset.id]) source = request.POST.get('asset-source', "") if source == 'bookmarklet': # bookmarklet create asset_url += "?level=item" template = loader.get_template('assetmgr/analyze.html') context = RequestContext( request, { 'request': request, 'user': user, 'action': request.POST.get('button', None), 'asset_url': asset_url }) return HttpResponse(template.render(context)) elif request.REQUEST.get('noui', '').startswith('postMessage'): # for bookmarklet mass-adding return render_to_response( 'assetmgr/interface_iframe.html', { 'message': ('%s|%s' % (request.build_absolute_uri(asset_url)), request.REQUEST['noui']), }) elif request.is_ajax(): # unsure when asset_create is called via ajax return HttpResponse(serializers.serialize('json', asset), mimetype="application/json") elif "archive" == asset.primary.label: redirect_url = request.POST.get('redirect-url', reverse('class-manage-sources')) url = "%s?newsrc=%s" % (redirect_url, asset.title) return HttpResponseRedirect(url) else: # server2server create return HttpResponseRedirect(asset_url)