def test_render_tag_list(self): tags = Tag.objects.usage_for_queryset(self.asset1.sherdnote_set.all(), counts=True) resource = TagResource() lst = resource.render_list(None, tags) self.assertEquals(len(lst), 5) self.assertEquals(lst[0]['count'], 3) self.assertEquals(lst[0]['last'], False) self.assertEquals(lst[0]['name'], 'image') self.assertEquals(lst[1]['count'], 1) self.assertEquals(lst[1]['last'], False) self.assertEquals(lst[1]['name'], 'instructor_one_global') self.assertEquals(lst[2]['count'], 1) self.assertEquals(lst[2]['last'], False) self.assertEquals(lst[2]['name'], 'instructor_one_selection') self.assertEquals(lst[3]['count'], 1) self.assertEquals(lst[3]['last'], False) self.assertEquals(lst[3]['name'], 'student_one_global') self.assertEquals(lst[4]['count'], 1) self.assertEquals(lst[4]['last'], True) self.assertEquals(lst[4]['name'], 'student_one_selection')
def test_render_list(self): asset = Asset.objects.get(id=1) tags = Tag.objects.usage_for_queryset(asset.sherdnote_set.all(), counts=True) resource = TagResource() lst = resource.render_list(None, tags) self.assertEquals(len(lst), 5) self.assertEquals(lst[0]['count'], 1) self.assertEquals(lst[0]['last'], False) self.assertEquals(lst[0]['name'], 'test_instructor_item') self.assertEquals(lst[4]['count'], 1) self.assertEquals(lst[4]['last'], True) self.assertEquals(lst[4]['name'], 'youtube')
def test_render_tag_list_for_course(self): request = RequestFactory().get('') request.course = self.sample_course notes = SherdNote.objects.filter(author=self.student_one) lst = TagResource().render_for_course(request, notes) self.assertEquals(len(lst), 6) self.assertEquals(lst[0]['count'], 1) self.assertEquals(lst[0]['last'], False) self.assertEquals(lst[0]['name'], 'image') self.assertEquals(lst[1]['count'], 0) self.assertEquals(lst[1]['last'], False) self.assertEquals(lst[1]['name'], 'instructor_one_global') self.assertEquals(lst[2]['count'], 0) self.assertEquals(lst[2]['last'], False) self.assertEquals(lst[2]['name'], 'instructor_one_selection') self.assertEquals(lst[3]['count'], 1) self.assertEquals(lst[3]['last'], False) self.assertEquals(lst[3]['name'], 'student_one_global') self.assertEquals(lst[4]['count'], 1) self.assertEquals(lst[4]['last'], False) self.assertEquals(lst[4]['name'], 'student_one_selection') self.assertEquals(lst[5]['count'], 0) self.assertEquals(lst[5]['last'], True) self.assertEquals(lst[5]['name'], 'video')
def dehydrate(self, bundle): try: bundle.data['is_global_annotation'] = \ bundle.obj.is_global_annotation() bundle.data['asset_id'] = str(bundle.obj.asset.id) bundle.data['is_null'] = bundle.obj.is_null() bundle.data['annotation'] = bundle.obj.annotation() bundle.data['url'] = bundle.obj.get_absolute_url() modified = bundle.obj.modified.strftime("%m/%d/%y %I:%M %p") \ if bundle.obj.modified else '' bundle.data['metadata'] = { 'tags': TagResource().render_list(bundle.request, bundle.obj.tags_split()), 'body': bundle.obj.body.strip() if bundle.obj.body else '', 'primary_type': bundle.obj.asset.primary.label, 'modified': modified, 'timecode': bundle.obj.range_as_timecode(), 'title': bundle.obj.title } editable = (bundle.request.user.id == getattr( bundle.obj, 'author_id', -1)) citable = bundle.request.GET.get('citable', '') == 'true' if self.in_selection_assignment_response(bundle.obj): # "first" here kills the prefetch/select_related optimization # use the qs array indexing to keep things fast reference = bundle.obj.projectnote_set.all()[0] editable = editable and not reference.project.is_submitted() # notes in a draft or submitted response may/may not be citable # based on its assignment visibility settings if citable: # this is a heavy operation. don't call it unless needed citable = reference.project.can_cite( bundle.request.course, bundle.request.user) if self.in_sequence_assignment_response(bundle.obj): # notes in a submitted response are not editable editable = False bundle.data['editable'] = editable bundle.data['citable'] = citable bundle.data['vocabulary'] = self.render_related_terms(bundle) except Asset.DoesNotExist: bundle.data['asset_id'] = '' bundle.data['metadata'] = {'title': 'Item Deleted'} return bundle
def get_tags_and_terms(self, request, assets): notes = SherdNote.objects.get_related_notes(assets, self.record_owner or None, self.visible_authors, self.all_items_are_visible) tags = TagResource().render_for_course(request, notes) vocab = VocabularyResource().render_for_course(request, notes) return {'active_tags': tags, 'active_vocabulary': vocab}
def add_metadata(self, request, assets): # metadata for all notes associated with these assets # is displayed in the filtered list. notes = SherdNote.objects.get_related_notes( assets, self.record_owner or None, self.visible_authors, self.all_items_are_visible) tags = TagResource().render_for_course(request, notes) vocab = VocabularyResource().render_for_course(request, notes) return {'active_tags': tags, 'active_vocabulary': vocab}
def test_render_list(self): self.assertTrue( self.api_client.client.login(username="******", password="******")) asset = Asset.objects.get(id=1) tags = Tag.objects.usage_for_queryset(asset.sherdnote_set.all(), counts=True) resource = TagResource() lst = resource.render_list(None, tags) self.assertEquals(len(lst), 5) self.assertEquals(lst[0]['count'], 1) self.assertEquals(lst[0]['last'], False) self.assertEquals(lst[0]['name'], 'test_instructor_item') self.assertEquals(lst[4]['count'], 1) self.assertEquals(lst[4]['last'], True) self.assertEquals(lst[4]['name'], 'youtube')
def add_metadata(self, request, assets): # metadata for all notes associated with these assets # is displayed in the filtered list. # Not sure this is exactly right...will discuss with team notes = SherdNote.objects.get_related_notes(assets, self.record_owner or None, self.visible_authors) tags = TagResource().render_related(request, notes) vocab = VocabularyResource().render_related(request, notes) return {'active_tags': tags, 'active_vocabulary': vocab}
def get(self, request): # Retrieve tags for this course assets = Asset.objects.filter(course=request.course) notes = SherdNote.objects.get_related_notes( assets, self.record_owner or None, self.visible_authors, self.all_items_are_visible) context = {} if len(notes) > 0: context = {'tags': TagResource().render_related(request, notes)} return self.render_to_json_response(context)
def dehydrate(self, bundle): try: bundle.data['is_global_annotation'] = \ bundle.obj.is_global_annotation() bundle.data['asset_id'] = str(bundle.obj.asset.id) bundle.data['is_null'] = bundle.obj.is_null() bundle.data['annotation'] = bundle.obj.annotation() bundle.data['url'] = bundle.obj.get_absolute_url() modified = bundle.obj.modified.strftime("%m/%d/%y %I:%M %p") \ if bundle.obj.modified else '' bundle.data['metadata'] = { 'tags': TagResource().render_list(bundle.request, bundle.obj.tags_split()), 'body': bundle.obj.body.strip() if bundle.obj.body else '', 'primary_type': bundle.obj.asset.primary.label, 'modified': modified, 'timecode': bundle.obj.range_as_timecode(), 'title': bundle.obj.title } bundle.data['editable'] = (bundle.request.user.id == getattr( bundle.obj, 'author_id', -1)) if bundle.request.GET.get('citable', '') == 'true': bundle.data['citable'] = True vocabulary = {} related = list(TermRelationship.objects.get_for_object(bundle.obj)) for rel in related: if rel.term.vocabulary.id not in vocabulary: vocabulary[rel.term.vocabulary.id] = { 'id': rel.term.vocabulary.id, 'display_name': rel.term.vocabulary.display_name, 'terms': [] } vocabulary[rel.term.vocabulary.id]['terms'].append( TermResource().render_one(bundle.request, rel.term)) bundle.data['vocabulary'] = [val for val in vocabulary.values()] except Asset.DoesNotExist: bundle.data['asset_id'] = '' bundle.data['metadata'] = {'title': 'Item Deleted'} return bundle
def asset_references(request, asset_id): if not request.user.is_staff: in_course_or_404(request.user.username, request.course) try: the_json = {} asset = Asset.objects.get(pk=asset_id, course=request.course) filters = {'assets': [asset.id], 'counts': True} the_json['tags'] = TagResource(request.course).filter(request, filters) the_json['vocabulary'] = [] for vocab in Vocabulary.objects.get_for_object(request.course): filters['vocabulary'] = vocab.id concepts = TermRelationshipResource(request.course).filter( request, filters) if len(concepts): the_json['vocabulary'].append({ 'display_name': vocab.display_name, 'term_set': concepts }) # DiscussionIndex is misleading. Objects returned are # projects & discussions title, object_pk, content_type, modified collaboration_items = DiscussionIndex.with_permission( request, DiscussionIndex.objects.filter(asset=asset).order_by('-modified')) the_json['references'] = [{ 'id': obj.collaboration.object_pk, 'title': obj.collaboration.title, 'type': obj.get_type_label(), 'url': obj.get_absolute_url(), 'modified': obj.modified.strftime("%m/%d/%y %I:%M %p") } for obj in collaboration_items] return HttpResponse(simplejson.dumps(the_json, indent=2), mimetype='application/json') except Asset.DoesNotExist: return asset_switch_course(request, asset_id)
def get(self, request, asset_id): ctx = {} qs = Asset.objects.filter(pk=asset_id, course=request.course) if qs.count() > 0: notes = SherdNote.objects.get_related_notes( qs, self.record_owner, self.visible_authors, self.all_items_are_visible) # tags ctx['tags'] = TagResource().render_related(request, notes) # vocabulary ctx['vocabulary'] = VocabularyResource().render_related( request, notes) # DiscussionIndex is misleading. Objects returned are # projects & discussions title, object_pk, content_type, modified indicies = DiscussionIndex.objects.filter( asset=qs[0]).order_by('-modified') ctx.update(DiscussionIndexResource().render_list( request, indicies)) return self.render_to_json_response(ctx)
def get(self, request, asset_id): try: ctx = {} asset = Asset.objects.filter(pk=asset_id, course=request.course) notes = SherdNote.objects.get_related_notes( asset, self.record_owner, self.visible_authors) # tags ctx['tags'] = TagResource().render_related(request, notes) # vocabulary ctx['vocabulary'] = VocabularyResource().render_related( request, notes) # DiscussionIndex is misleading. Objects returned are # projects & discussions title, object_pk, content_type, modified indicies = DiscussionIndex.objects.filter( asset=asset).order_by('-modified') ctx.update(DiscussionIndexResource().render_list( request, indicies)) return self.render_to_json_response(ctx) except Asset.DoesNotExist: return asset_switch_course(request, asset_id)
from mediathread.assetmgr.api import AssetResource from mediathread.main.api import CourseResource, CourseSummaryResource from mediathread.projects.api import ProjectResource from mediathread.taxonomy.api import TermResource, VocabularyResource from tastypie.api import Api import os.path v1_api = Api(api_name='v1') v1_api.register(SherdNoteResource()) v1_api.register(AssetResource()) v1_api.register(ProjectResource()) v1_api.register(CourseResource()) v1_api.register(CourseSummaryResource()) v1_api.register(TermResource()) v1_api.register(VocabularyResource()) v1_api.register(TagResource()) admin.autodiscover() site_media_root = os.path.join(os.path.dirname(__file__), "../media") bookmarklet_root = os.path.join(os.path.dirname(__file__), "../media/", "bookmarklets") redirect_after_logout = getattr(settings, 'LOGOUT_REDIRECT_URL', None) auth_urls = (r'^accounts/', include('django.contrib.auth.urls')) logout_page = (r'^accounts/logout/$', 'django.contrib.auth.views.logout', { 'next_page': redirect_after_logout }) if hasattr(settings, 'WIND_BASE'):
def render_assets(request, record_owner, assets): course = request.course logged_in_user = request.user # Is the current user faculty OR staff is_faculty = cached_course_is_faculty(course, logged_in_user) # Can the record_owner edit the records viewing_own_records = (record_owner == logged_in_user) viewing_faculty_records = record_owner and course.is_faculty(record_owner) # Allow the logged in user to add assets to his composition citable = request.GET.get('citable', '') == 'true' # Does the course allow viewing other user selections? owner_selections_are_visible = ( course_details.all_selections_are_visible(course) or viewing_own_records or viewing_faculty_records or is_faculty) # Spew out json for the assets if request.GET.get('annotations', '') == 'true': resource = AssetResource(owner_selections_are_visible, record_owner, { 'editable': viewing_own_records, 'citable': citable }) else: resource = AssetSummaryResource({ 'editable': viewing_own_records, 'citable': citable }) asset_json = resource.render_list(request, assets) active_filters = {} for key, val in request.GET.items(): if (key == 'tag' or key == 'modified' or key.startswith('vocabulary-')): active_filters[key] = val user_resource = UserResource() # #todo -- figure out a cleaner way to do this. Ugli-ness # Collate tag set & vocabulary set for the result set. # Get all visible notes for the returned asset set # These notes may include global annotations for all users, # whereas the rendered set will not active_asset_ids = [a['id'] for a in asset_json] active_notes = [] if record_owner: if owner_selections_are_visible: active_notes = SherdNote.objects.filter( asset__course=course, asset__id__in=active_asset_ids, author__id=record_owner.id) else: if all_selections_are_visible(course) or is_faculty: # Display all tags for the asset set including globals active_notes = SherdNote.objects.filter( asset__course=course, asset__id__in=active_asset_ids) else: whitelist = [f.id for f in course.faculty] whitelist.append(request.user.id) active_notes = SherdNote.objects.filter( asset__course=course, asset__id__in=active_asset_ids, author__id__in=whitelist) tags = [] if len(active_notes) > 0: tags = Tag.objects.usage_for_queryset(active_notes) tags.sort(lambda a, b: cmp(a.name.lower(), b.name.lower())) active_vocabulary = [] note_ids = [n.id for n in active_notes] content_type = ContentType.objects.get_for_model(SherdNote) term_resource = TermResource() for vocab in Vocabulary.objects.get_for_object(request.course): vocabulary = { 'id': vocab.id, 'display_name': vocab.display_name, 'term_set': [] } related = TermRelationship.objects.filter(term__vocabulary=vocab, content_type=content_type, object_id__in=note_ids) terms = [] for rel in related: if rel.term.display_name not in terms: the_term = term_resource.render_one(request, rel.term) vocabulary['term_set'].append(the_term) terms.append(rel.term.display_name) active_vocabulary.append(vocabulary) # Assemble the context data = { 'assets': asset_json, 'active_tags': TagResource().render_list(request, tags), 'active_filters': active_filters, 'active_vocabulary': active_vocabulary, 'space_viewer': user_resource.render_one(request, logged_in_user), 'editable': viewing_own_records, 'citable': citable, 'is_faculty': is_faculty } if record_owner: data['space_owner'] = user_resource.render_one(request, record_owner) json_stream = simplejson.dumps(data, indent=2) return HttpResponse(json_stream, mimetype='application/json')