def class_settings(request): course = request.course user = request.user context = { 'asset_request': request.GET, 'course': course, 'space_viewer': request.user, 'is_staff': request.user.is_staff, 'help_public_compositions': UserSetting.get_setting(user, "help_public_compositions", True), 'help_selection_visibility': UserSetting.get_setting(user, "help_selection_visibility", True), } public_composition_key = course_details.ALLOW_PUBLIC_COMPOSITIONS_KEY context[course_details.ALLOW_PUBLIC_COMPOSITIONS_KEY] = int( course.get_detail(public_composition_key, course_details.ALLOW_PUBLIC_COMPOSITIONS_DEFAULT)) selection_visibility_key = course_details.SELECTION_VISIBILITY_KEY context[course_details.SELECTION_VISIBILITY_KEY] = int( course.get_detail(selection_visibility_key, course_details.SELECTION_VISIBILITY_DEFAULT)) if request.method == "POST": if selection_visibility_key in request.POST: selection_visibility_value = \ int(request.POST.get(selection_visibility_key)) request.course.add_detail(selection_visibility_key, selection_visibility_value) context[selection_visibility_key] = selection_visibility_value if public_composition_key in request.POST: public_composition_value = \ int(request.POST.get(public_composition_key)) request.course.add_detail(public_composition_key, public_composition_value) context[public_composition_key] = public_composition_value if public_composition_value == 0: # Check any existing projects -- if they are # world publishable, turn this feature OFF projects = Project.objects.filter(course=course) for project in projects: try: col = Collaboration.objects.get_for_object(project) if col._policy.policy_name == 'PublicEditorsAreOwners': col.policy = 'CourseProtected' col.save() except: pass context['changes_saved'] = True return context
def set_user_setting(request, user_name): user = get_object_or_404(User, username=user_name) name = request.POST.get("name") value = request.POST.get("value") UserSetting.set_setting(user, name, value) json_stream = simplejson.dumps({'success': True}) return HttpResponse(json_stream, mimetype='application/json')
def class_settings(request): c = request.course user = request.user context = { 'asset_request': request.GET, 'course': c, 'space_viewer': request.user, 'is_staff': request.user.is_staff, 'help_public_compositions': UserSetting.get_setting( user, "help_public_compositions", True), 'help_selection_visibility': UserSetting.get_setting( user, "help_selection_visibility", True), } public_composition_key = course_details.ALLOW_PUBLIC_COMPOSITIONS_KEY context[course_details.ALLOW_PUBLIC_COMPOSITIONS_KEY] = \ int(c.get_detail(public_composition_key, course_details.ALLOW_PUBLIC_COMPOSITIONS_DEFAULT)) selection_visibility_key = course_details.SELECTION_VISIBILITY_KEY context[course_details.SELECTION_VISIBILITY_KEY] = \ int(c.get_detail(selection_visibility_key, course_details.SELECTION_VISIBILITY_DEFAULT)) if request.method == "POST": if selection_visibility_key in request.POST: selection_visibility_value = \ int(request.POST.get(selection_visibility_key)) request.course.add_detail(selection_visibility_key, selection_visibility_value) context[selection_visibility_key] = selection_visibility_value if public_composition_key in request.POST: public_composition_value = \ int(request.POST.get(public_composition_key)) request.course.add_detail(public_composition_key, public_composition_value) context[public_composition_key] = public_composition_value if public_composition_value == 0: # Check any existing projects -- if they are # world publishable, turn this feature OFF projects = Project.objects.filter(course=c) for p in projects: try: col = Collaboration.get_associated_collab(p) if col._policy.policy_name == 'PublicEditorsAreOwners': col.policy = 'CourseProtected' col.save() except: pass context['changes_saved'] = True return context
def class_manage_sources(request): key = course_details.UPLOAD_PERMISSION_KEY course = request.course user = request.user upload_enabled = False for item in course.asset_set.archives().order_by('title'): attribute = item.metadata().get('upload', 0) value = attribute[0] if hasattr(attribute, 'append') else attribute if value and int(value) == 1: upload_enabled = True break context = { 'asset_request': request.GET, 'course': course, 'supported_archives': SupportedSource.objects.all().order_by("title"), 'space_viewer': request.user, 'is_staff': request.user.is_staff, 'newsrc': request.GET.get('newsrc', ''), 'delsrc': request.GET.get('delsrc', ''), 'upload_enabled': upload_enabled, 'permission_levels': course_details.UPLOAD_PERMISSION_LEVELS, 'help_video_upload': UserSetting.get_setting(user, "help_video_upload", True), 'help_supported_collections': UserSetting.get_setting(user, "help_supported_collections", True), 'help_dashboard_nav_actions': UserSetting.get_setting(user, "help_dashboard_nav_actions", False), 'help_dashboard_nav_reports': UserSetting.get_setting(user, "help_dashboard_nav_reports", False) } if request.method == "GET": context[key] = int( course.get_detail(key, course_details.UPLOAD_PERMISSION_DEFAULT)) else: upload_permission = request.POST.get(key) request.course.add_detail(key, upload_permission) context['changes_saved'] = True context[key] = int(upload_permission) return context
def set_user_setting(request, user_name): user = get_object_or_404(User, username=user_name) name = request.POST.get("name", None) value = request.POST.get("value", None) if name is None: return Http404("Key value not specified") UserSetting.set_setting(user, name, value) json_stream = json.dumps({'success': True}) return HttpResponse(json_stream, mimetype='application/json')
def set_user_setting(request, user_name): user = get_object_or_404(User, username=user_name) name = request.POST.get("name", None) value = request.POST.get("value", None) if name is None: return Http404("Key value not specified") UserSetting.set_setting(user, name, value) json_stream = json.dumps({'success': True}) return HttpResponse(json_stream, content_type='application/json')
def get(self, request, asset_id): self.record_owner = request.user the_assets = Asset.objects.filter(pk=asset_id, course=request.course) if the_assets.count() == 0: return asset_switch_course(request, asset_id) (assets, notes) = self.visible_assets_and_notes(request, the_assets) # if asset is not in my collection, it must be in my course if assets.count() == 0 and the_assets[0].course != request.course: return HttpResponseForbidden("forbidden") asset = the_assets[0] help_setting = UserSetting.get_setting(request.user, "help_item_detail_view", True) ctx = AssetResource().render_one(request, asset, notes) ctx = { 'user_settings': { 'help_item_detail_view': help_setting }, 'type': 'asset', 'assets': { asset.pk: AssetResource().render_one(request, asset, notes) } } return self.render_to_json_response(ctx)
def get(self, request, asset_id): the_assets = Asset.objects.filter(pk=asset_id, course=request.course) if the_assets.count() == 0: return asset_switch_course(request, asset_id) (assets, notes) = self.visible_assets_and_notes(request, the_assets) # if asset is not in my collection, it must be in my course if assets.count() == 0 and the_assets[0].course != request.course: return HttpResponseForbidden("forbidden") # only return original author's global annotations notes = notes.exclude(~Q(author=request.user), range1__isnull=True) asset = the_assets[0] help_setting = UserSetting.get_setting(request.user, "help_item_detail_view", True) ctx = { 'user_settings': {'help_item_detail_view': help_setting}, 'type': 'asset', 'assets': { asset.pk: AssetResource().render_one(request, asset, notes) } } return self.render_to_json_response(ctx)
def get(self, request, asset_id): the_assets = Asset.objects.filter(pk=asset_id, course=request.course) if the_assets.count() == 0: return asset_switch_course(request, asset_id) (assets, notes) = self.visible_assets_and_notes(request, the_assets) # if asset is not in my collection, it must be in my course if assets.count() == 0 and the_assets[0].course != request.course: return HttpResponseForbidden("forbidden") # only return original author's global annotations notes = notes.exclude(~Q(author=request.user), range1__isnull=True) asset = the_assets[0] if asset.primary.is_image(): notes = notes.order_by('author', 'title') else: notes = notes.order_by('author', 'range1', 'title') notes = notes.prefetch_related( 'termrelationship_set__term__vocabulary', 'projectnote_set__project') help_setting = UserSetting.get_setting(request.user, "help_item_detail_view", True) ctx = AssetResource().render_one_context(request, asset, notes) ctx['user_settings'] = {'help_item_detail_view': help_setting} ctx['type'] = 'asset' return self.render_to_json_response(ctx)
def get(self, request, asset_id): the_assets = Asset.objects.filter(pk=asset_id, course=request.course) if the_assets.count() == 0: return asset_switch_course(request, asset_id) (assets, notes) = self.visible_assets_and_notes(request, the_assets) # if asset is not in my collection, it must be in my course if assets.count() == 0 and the_assets[0].course != request.course: return HttpResponseForbidden("forbidden") # only return original author's global annotations notes = notes.exclude(~Q(author=request.user), range1__isnull=True) asset = the_assets[0] help_setting = UserSetting.get_setting(request.user, "help_item_detail_view", True) ctx = { 'user_settings': { 'help_item_detail_view': help_setting }, 'type': 'asset', 'assets': { asset.pk: AssetResource().render_one(request, asset, notes) } } return self.render_to_json_response(ctx)
def get(self, request, asset_id): self.record_owner = request.user the_assets = Asset.objects.filter(pk=asset_id, course=request.course) if the_assets.count() == 0: return asset_switch_course(request, asset_id) (assets, notes) = self.visible_assets_and_notes(request, the_assets) # if asset is not in my collection, it must be in my course if assets.count() == 0 and the_assets[0].course != request.course: return HttpResponseForbidden("forbidden") asset = the_assets[0] help_setting = UserSetting.get_setting(request.user, "help_item_detail_view", True) ctx = AssetResource().render_one(request, asset, notes) ctx = { 'user_settings': {'help_item_detail_view': help_setting}, 'type': 'asset', 'assets': { asset.pk: AssetResource().render_one(request, asset, notes) } } return self.render_to_json_response(ctx)
def get(self, request, asset_id): the_assets = Asset.objects.filter(pk=asset_id, course=request.course) if the_assets.count() == 0: return asset_switch_course(request, asset_id) (assets, notes) = self.visible_assets_and_notes(request, the_assets) # if asset is not in my collection, it must be in my course if assets.count() == 0 and the_assets[0].course != request.course: return HttpResponseForbidden("forbidden") # only return original author's global annotations notes = notes.exclude(~Q(author=request.user), range1__isnull=True) asset = the_assets[0] if asset.primary.is_image(): notes = notes.order_by('author', 'title') else: notes = notes.order_by('author', 'range1', 'title') notes = notes.prefetch_related( 'termrelationship_set__term__vocabulary', 'projectnote_set__project') help_setting = UserSetting.get_setting(request.user, "help_item_detail_view", True) ctx = AssetResource().render_one_context(request, asset, notes) ctx['user_settings'] = {'help_item_detail_view': help_setting} ctx['type'] = 'asset' ctx.update(self.get_tags_and_terms(request, assets)) return self.render_to_json_response(ctx)
def get(self, request, asset_id=None, annot_id=None): if asset_id: try: asset = Asset.objects.get(pk=asset_id, course=request.course) asset.primary except Asset.DoesNotExist: return asset_switch_course(request, asset_id) except Source.DoesNotExist: ctx = RequestContext(request) return render_to_response('500.html', {}, context_instance=ctx) data = {'asset_id': asset_id, 'annotation_id': annot_id} if not request.is_ajax(): return render_to_response('assetmgr/asset_workspace.html', data, context_instance=RequestContext(request)) ctx = {'type': 'asset'} if asset_id: # @todo - refactor this context out of the mix # ideally, the client would simply request the json # the mixin is expecting a queryset, so this becomes awkward here assets = Asset.objects.filter(pk=asset_id) (assets, notes) = self.visible_assets_and_notes(request, assets) # only return original author's global annotations notes = notes.exclude(~Q(author=request.user), range1__isnull=True) ares = AssetResource() ctx.update(ares.render_one_context(request, asset, notes)) help_setting = UserSetting.get_setting(request.user, "help_item_detail_view", True) ctx['user_settings'] = {'help_item_detail_view': help_setting} vocabulary = VocabularyResource().render_list( request, Vocabulary.objects.get_for_object(request.course)) user_resource = UserResource() owners = user_resource.render_list(request, request.course.members) update_history = True show_collection = True template = 'asset_workspace' data['panels'] = [{ 'panel_state': 'open', 'panel_state_label': "Annotate Media", 'context': ctx, 'owners': owners, 'vocabulary': vocabulary, 'template': template, 'current_asset': asset_id, 'current_annotation': annot_id, 'update_history': update_history, 'show_collection': show_collection}] return self.render_to_json_response(data)
def get_extra_context(self): parent = self.get_assignment(self.project) key = 'assignment_instructions_{}'.format(parent.id) return { 'show_instructions': UserSetting.get_setting( self.request.user, key, True), 'allow_public_compositions': allow_public_compositions( self.request.course) }
def class_manage_sources(request): key = course_details.UPLOAD_PERMISSION_KEY c = request.course user = request.user upload_enabled = False for a in c.asset_set.archives().order_by('title'): attribute = a.metadata().get('upload', 0) value = attribute[0] if hasattr(attribute, 'append') else attribute if value and int(value) == 1: upload_enabled = True break context = { 'asset_request': request.GET, 'course': c, 'supported_archives': SupportedSource.objects.all().order_by("title"), 'space_viewer': request.user, 'is_staff': request.user.is_staff, 'newsrc': request.GET.get('newsrc', ''), 'delsrc': request.GET.get('delsrc', ''), 'upload_enabled': upload_enabled, 'permission_levels': course_details.UPLOAD_PERMISSION_LEVELS, 'help_video_upload': UserSetting.get_setting( user, "help_video_upload", True), 'help_supported_collections': UserSetting.get_setting( user, "help_supported_collections", True), 'help_dashboard_nav_actions': UserSetting.get_setting( user, "help_dashboard_nav_actions", False), 'help_dashboard_nav_reports': UserSetting.get_setting( user, "help_dashboard_nav_reports", False) } if request.method == "GET": context[key] = int(c.get_detail( key, course_details.UPLOAD_PERMISSION_DEFAULT)) else: upload_permission = request.POST.get(key) request.course.add_detail(key, upload_permission) context['changes_saved'] = True context[key] = int(upload_permission) return context
def detail_asset_json(request, asset): the_json = AssetResource().render_one(request, asset) the_json['user_analysis'] = asset.user_analysis_count(request.user) help_setting = \ UserSetting.get_setting(request.user, "help_item_detail_view", True) return {'type': 'asset', 'assets': {asset.pk: the_json}, 'user_settings': {'help_item_detail_view': help_setting}}
def get(self, request, asset_id=None, annot_id=None): if asset_id: try: asset = Asset.objects.get(pk=asset_id, course=request.course) asset.primary except Asset.DoesNotExist: return asset_switch_course(request, asset_id) except Source.DoesNotExist: ctx = RequestContext(request) return render_to_response('500.html', {}, context_instance=ctx) data = {'asset_id': asset_id, 'annotation_id': annot_id} if not request.is_ajax(): return render_to_response('assetmgr/asset_workspace.html', data, context_instance=RequestContext(request)) context = {'type': 'asset'} if asset_id: # @todo - refactor this context out of the mix # ideally, the client would simply request the json # the mixin is expecting a queryset, so this becomes awkward here self.record_owner = request.user assets = Asset.objects.filter(pk=asset_id) (assets, notes) = self.visible_assets_and_notes(request, assets) context['assets'] = { asset.pk: AssetResource().render_one(request, asset, notes) } help_setting = UserSetting.get_setting(request.user, "help_item_detail_view", True) context['user_settings'] = {'help_item_detail_view': help_setting} vocabulary = VocabularyResource().render_list( request, Vocabulary.objects.get_for_object(request.course)) user_resource = UserResource() owners = user_resource.render_list(request, request.course.members) data['panels'] = [{ 'panel_state': 'open', 'panel_state_label': "Annotate Media", 'context': context, 'owners': owners, 'vocabulary': vocabulary, 'template': 'asset_workspace', 'current_asset': asset_id, 'current_annotation': annot_id, 'update_history': True, 'show_collection': True }] return self.render_to_json_response(data)
def should_show_tour(request, course, user): assets = Asset.objects.annotated_by(course, user, include_archives=False) projects = Project.objects.visible_by_course_and_user(request, user, course) return UserSetting.get_setting(user, "help_show_homepage_tour", len(assets) < 1 and len(projects) < 1)
def detail_asset_json(request, asset): the_json = AssetResource().render_one(request, asset) the_json['user_analysis'] = asset.user_analysis_count(request.user) help_setting = \ UserSetting.get_setting(request.user, "help_item_detail_view", True) return { 'type': 'asset', 'assets': { asset.pk: the_json }, 'user_settings': { 'help_item_detail_view': help_setting } }
def test_get_setting_default_value(self): self.assertEquals(UserSetting.get_setting(self.student_one, '1', 0), 0)
def test_get_setting(self): # default - name='Test', value=1 UserSetting.set_setting(self.student_one, 'Test', value=1) self.assertEquals(UserSetting.get_setting(self.student_one, 'Test', 0), '1') UserSetting.set_setting(self.student_one, 'Test', value='True') self.assertTrue(UserSetting.get_setting(self.student_one, 'Test', 0)) UserSetting.set_setting(self.student_one, 'Test', value='true') self.assertTrue(UserSetting.get_setting(self.student_one, 'Test', 0)) UserSetting.set_setting(self.student_one, 'Test', value='False') self.assertFalse(UserSetting.get_setting(self.student_one, 'Test', 0)) UserSetting.set_setting(self.student_one, 'Test', value='false') self.assertFalse(UserSetting.get_setting(self.student_one, 'Test', 0))
def triple_homepage(request): if not request.course: return HttpResponseRedirect('/accounts/login/') logged_in_user = request.user classwork_owner = request.user # Viewing your own work by default if 'username' in request.GET: user_name = request.GET['username'] in_course_or_404(user_name, request.course) classwork_owner = get_object_or_404(User, username=user_name) course = request.course archives = [] upload_archive = None for item in course.asset_set.archives().order_by('title'): archive = item.sources['archive'] thumb = item.sources.get('thumb', None) description = item.metadata().get('description', '') uploader = item.metadata().get('upload', 0) archive_context = { "id": item.id, "title": item.title, "thumb": (None if not thumb else {"id": thumb.id, "url": thumb.url}), "archive": {"id": archive.id, "url": archive.url}, "metadata": (description[0] if hasattr(description, 'append') else description) } if (uploader[0] if hasattr(uploader, 'append') else uploader): upload_archive = archive_context else: archives.append(archive_context) archives.sort(key=operator.itemgetter('title')) owners = [] if (in_course(logged_in_user.username, request.course) and (logged_in_user.is_staff or logged_in_user.has_perm('assetmgr.can_upload_for'))): owners = UserResource().render_list(request, request.course.members) context = { 'classwork_owner': classwork_owner, 'help_homepage_instructor_column': False, 'help_homepage_classwork_column': False, 'upgrade_bookmarklet': UserSetting.get_setting( logged_in_user, "upgrade_bookmarklet", True), 'faculty_feed': Project.objects.faculty_compositions(request, course), 'is_faculty': course.is_faculty(logged_in_user), 'discussions': get_course_discussions(course), 'msg': request.GET.get('msg', ''), 'view': request.GET.get('view', ''), 'archives': archives, 'upload_archive': upload_archive, 'can_upload': course_details.can_upload(request.user, request.course), 'owners': owners } if getattr(settings, 'DJANGOSHERD_FLICKR_APIKEY', None): # MUST only contain string values for now!! # (see templates/assetmgr/bookmarklet.js to see why or fix) context['bookmarklet_vars'] = { 'flickr_apikey': settings.DJANGOSHERD_FLICKR_APIKEY } return context
def triple_homepage(request): if not request.course: return HttpResponseRedirect('/accounts/login/') logged_in_user = request.user classwork_owner = request.user # Viewing your own work by default if 'username' in request.GET: user_name = request.GET['username'] in_course_or_404(user_name, request.course) classwork_owner = get_object_or_404(User, username=user_name) course = request.course archives = [] upload_archive = None for item in course.asset_set.archives().order_by('title'): archive = item.sources['archive'] thumb = item.sources.get('thumb', None) description = item.metadata().get('description', '') uploader = item.metadata().get('upload', 0) archive_context = { "id": item.id, "title": item.title, "thumb": (None if not thumb else { "id": thumb.id, "url": thumb.url }), "archive": { "id": archive.id, "url": archive.url }, "metadata": (description[0] if hasattr(description, 'append') else description) } if (uploader[0] if hasattr(uploader, 'append') else uploader): upload_archive = archive_context else: archives.append(archive_context) archives.sort(key=operator.itemgetter('title')) owners = [] if (in_course(logged_in_user.username, request.course) and (logged_in_user.is_staff or logged_in_user.has_perm('assetmgr.can_upload_for'))): owners = UserResource().render_list(request, request.course.members) context = { 'classwork_owner': classwork_owner, 'help_homepage_instructor_column': False, 'help_homepage_classwork_column': False, 'upgrade_bookmarklet': UserSetting.get_setting(logged_in_user, "upgrade_bookmarklet", True), 'faculty_feed': Project.objects.faculty_compositions(request, course), 'is_faculty': course.is_faculty(logged_in_user), 'discussions': get_course_discussions(course), 'msg': request.GET.get('msg', ''), 'view': request.GET.get('view', ''), 'archives': archives, 'upload_archive': upload_archive, 'can_upload': course_details.can_upload(request.user, request.course), 'owners': owners } if getattr(settings, 'DJANGOSHERD_FLICKR_APIKEY', None): # MUST only contain string values for now!! # (see templates/assetmgr/bookmarklet.js to see why or fix) context['bookmarklet_vars'] = { 'flickr_apikey': settings.DJANGOSHERD_FLICKR_APIKEY } return context