Ejemplo n.º 1
0
def get_records(request, record_owner, projects, assignments, assets):
    course = request.course
    logged_in_user = request.user
    
    # Can the record_owner edit the records
    viewing_my_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.has_key('citable') and request.GET.get('citable') == 'true'
    
    # Is the current user faculty OR staff
    is_faculty = course.is_faculty(logged_in_user)
    
    # Does the course allow viewing other user selections?
    owner_selections_are_visible = course_details.all_selections_are_visible(course) or \
        viewing_my_records or viewing_faculty_records or is_faculty
        
    # Filter the assets 
    for fil in filter_by:
        filter_value = request.GET.get(fil)
        if filter_value:
            assets = [asset for asset in assets
                      if filter_by[fil](asset, filter_value, record_owner)]
    
    active_filters = get_active_filters(request, filter_by)
    
    # Spew out json for the assets 
    asset_json = []
    options = {
        'owner_selections_are_visible': request.GET.has_key('annotations') and owner_selections_are_visible,
        'all_selections_are_visible': course_details.all_selections_are_visible(course) or is_faculty,
        'can_edit': viewing_my_records,
        'citable': citable
    }
            
    for asset in assets:
        asset_json.append(homepage_asset_json(request, asset, logged_in_user, record_owner, options))
        
    # Spew out json for the projects
    project_json = []
    for p in projects:
        project_json.append(homepage_project_json(request, p, viewing_my_records))
        
    # Tags
    tags = []
    if record_owner:
        if owner_selections_are_visible:
            # Tags for selected user
            tags = Tag.objects.usage_for_queryset(
                record_owner.sherdnote_set.filter(asset__course=course),
                counts=True)
    else:
        if owner_selections_are_visible:
            # Tags for the whole class
            tags = Tag.objects.usage_for_queryset(
                SherdNote.objects.filter(asset__course=course),
                counts=True)
        else:
            # Tags for myself and faculty members
            tags = Tag.objects.usage_for_queryset(
                       logged_in_user.sherdnote_set.filter(asset__course=course),
                       counts=True)
            
            for f in course.faculty:
                tags.extend(Tag.objects.usage_for_queryset(
                                f.sherdnote_set.filter(asset__course=course),
                                counts=True))
    

    tags.sort(lambda a, b:cmp(a.name.lower(), b.name.lower()))

    # Assemble the context
    data = { 'assets': asset_json,
             'assignments' : [ {'id': a.id, 'url': a.get_absolute_url(), 'title': a.title, 'modified': a.modified.strftime("%m/%d/%y %I:%M %p")} for a in assignments],
             'projects' : project_json,
             'tags': [ { 'name': tag.name } for tag in tags ],
             'active_filters': active_filters,
             'space_viewer'  : { 'username': logged_in_user.username, 'public_name': get_public_name(logged_in_user, request), 'can_manage': (logged_in_user.is_staff and not record_owner) },
             'editable' : viewing_my_records,
             'citable' : citable,
             'owners' : [{ 'username': m.username, 'public_name': get_public_name(m, request) } for m in request.course.members],
             'compositions' : len(projects) > 0 or len(assignments) > 0,
             'is_faculty': is_faculty,
            }
    
    if record_owner:
        data['space_owner'] = { 'username': record_owner.username, 'public_name': get_public_name(record_owner, request) }

    json_stream = simplejson.dumps(data, indent=2)
    return HttpResponse(json_stream, mimetype='application/json')
Ejemplo n.º 2
0
def detail_asset_json(request, asset_id, options):    
    asset = get_object_or_404(Asset, pk=asset_id)
    
    selections_visible = course_details.all_selections_are_visible(request.course) or \
        request.course.is_faculty(request.user)

    asset_json = asset.sherd_json(request)
    asset_key = 'x_%s' % asset.pk
    
    asset_json['user_analysis'] = 0
    
    ga = asset.global_annotation(request.user, False)
    if ga:
        asset_json['global_annotation_id'] = ga.id
        asset_json['notes'] = ga.body
        asset_json['user_tags'] = tag_json(ga.tags_split())
        
        if (asset_json['notes'] and len(asset_json['notes']) > 0) or \
            (asset_json['user_tags'] and len(asset_json['user_tags']) > 0):
            asset_json['user_analysis'] += 1
    
    if not selections_visible:
        owners = [ request.user ]
        owners.extend(request.course.faculty)
        asset_json['tags'] = tag_json(asset.filter_tags_by_users(owners, True))
        
        
    # DiscussionIndex is misleading. Objects returned are projects & discussions
    # title, object_pk, content_type, modified
    asset_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 DiscussionIndex.with_permission(request,
            DiscussionIndex.objects.filter(asset=asset).order_by('-modified')) ]
    
    
    annotations = [{
            'asset_key': asset_key,
            'range1': None,
            'range2': None,
            'annotation': None,
            'id': 'asset-%s' % asset.pk,
            'asset_id': asset.pk,
            }]
    
    
    if request.GET.has_key('annotations') or \
        (options and options.has_key('include_annotations') and options['include_annotations']):
        # @todo: refactor this serialization into a common place.
        def author_name(request, annotation, key):
            if not annotation.author_id:
                return None
            return 'author_name', get_public_name(annotation.author, request)
        def primary_type(request, annotation, key):
            return "primary_type", asset.primary.label
        for ann in asset.sherdnote_set.filter(range1__isnull=False):
            visible = selections_visible or request.user == ann.author or request.course.is_faculty(ann.author)
            if visible:
                if request.user == ann.author:
                    asset_json['user_analysis'] += 1     
                ann_json = ann.sherd_json(request, 'x', ('title', 'author', 'tags', author_name, 'body', 'modified', 'timecode', primary_type))
                annotations.append(ann_json)

    rv = {
        'type': 'asset',
        'assets': { asset_key: asset_json }, #we make assets plural here to be compatible with the project JSON structure
        'annotations': annotations,
        'user_settings': { 'help_item_detail_view': UserSetting.get_setting(request.user, "help_item_detail_view", True) }
    }
    return rv 
Ejemplo n.º 3
0
def get_records(request, record_owner, projects, assignments, assets):
    course = request.course
    logged_in_user = request.user

    # Can the record_owner edit the records
    viewing_my_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.has_key('citable') and request.GET.get(
        'citable') == 'true'

    # Is the current user faculty OR staff
    is_faculty = course.is_faculty(logged_in_user)

    # Does the course allow viewing other user selections?
    owner_selections_are_visible = course_details.all_selections_are_visible(course) or \
        viewing_my_records or viewing_faculty_records or is_faculty

    # Filter the assets
    for fil in filter_by:
        filter_value = request.GET.get(fil)
        if filter_value:
            assets = [
                asset for asset in assets
                if filter_by[fil](asset, filter_value, record_owner)
            ]

    active_filters = get_active_filters(request, filter_by)

    # Spew out json for the assets
    asset_json = []
    options = {
        'owner_selections_are_visible':
        request.GET.has_key('annotations') and owner_selections_are_visible,
        'all_selections_are_visible':
        course_details.all_selections_are_visible(course) or is_faculty,
        'can_edit':
        viewing_my_records,
        'citable':
        citable
    }

    for asset in assets:
        asset_json.append(
            homepage_asset_json(request, asset, logged_in_user, record_owner,
                                options))

    # Spew out json for the projects
    project_json = []
    for p in projects:
        project_json.append(
            homepage_project_json(request, p, viewing_my_records))

    # Tags
    tags = []
    if record_owner:
        if owner_selections_are_visible:
            # Tags for selected user
            tags = Tag.objects.usage_for_queryset(
                record_owner.sherdnote_set.filter(asset__course=course),
                counts=True)
    else:
        if owner_selections_are_visible:
            # Tags for the whole class
            tags = Tag.objects.usage_for_queryset(
                SherdNote.objects.filter(asset__course=course), counts=True)
        else:
            # Tags for myself and faculty members
            tags = Tag.objects.usage_for_queryset(
                logged_in_user.sherdnote_set.filter(asset__course=course),
                counts=True)

            for f in course.faculty:
                tags.extend(
                    Tag.objects.usage_for_queryset(
                        f.sherdnote_set.filter(asset__course=course),
                        counts=True))

    tags.sort(lambda a, b: cmp(a.name.lower(), b.name.lower()))

    # Assemble the context
    data = {
        'assets':
        asset_json,
        'assignments': [{
            'id': a.id,
            'url': a.get_absolute_url(),
            'title': a.title,
            'modified': a.modified.strftime("%m/%d/%y %I:%M %p")
        } for a in assignments],
        'projects':
        project_json,
        'tags': [{
            'name': tag.name
        } for tag in tags],
        'active_filters':
        active_filters,
        'space_viewer': {
            'username': logged_in_user.username,
            'public_name': get_public_name(logged_in_user, request),
            'can_manage': (logged_in_user.is_staff and not record_owner)
        },
        'editable':
        viewing_my_records,
        'citable':
        citable,
        'owners': [{
            'username': m.username,
            'public_name': get_public_name(m, request)
        } for m in request.course.members],
        'compositions':
        len(projects) > 0 or len(assignments) > 0,
        'is_faculty':
        is_faculty,
    }

    if record_owner:
        data['space_owner'] = {
            'username': record_owner.username,
            'public_name': get_public_name(record_owner, request)
        }

    json_stream = simplejson.dumps(data, indent=2)
    return HttpResponse(json_stream, mimetype='application/json')
Ejemplo n.º 4
0
def detail_asset_json(request, asset_id, options):
    asset = get_object_or_404(Asset, pk=asset_id)

    selections_visible = course_details.all_selections_are_visible(request.course) or \
        request.course.is_faculty(request.user)

    asset_json = asset.sherd_json(request)
    asset_key = 'x_%s' % asset.pk

    asset_json['user_analysis'] = 0

    ga = asset.global_annotation(request.user, False)
    if ga:
        asset_json['global_annotation_id'] = ga.id
        asset_json['notes'] = ga.body
        asset_json['user_tags'] = tag_json(ga.tags_split())

        if (asset_json['notes'] and len(asset_json['notes']) > 0) or \
            (asset_json['user_tags'] and len(asset_json['user_tags']) > 0):
            asset_json['user_analysis'] += 1

    if not selections_visible:
        owners = [request.user]
        owners.extend(request.course.faculty)
        asset_json['tags'] = tag_json(asset.filter_tags_by_users(owners, True))

    # DiscussionIndex is misleading. Objects returned are projects & discussions
    # title, object_pk, content_type, modified
    asset_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 DiscussionIndex.with_permission(
        request,
        DiscussionIndex.objects.filter(asset=asset).order_by('-modified'))]

    annotations = [{
        'asset_key': asset_key,
        'range1': None,
        'range2': None,
        'annotation': None,
        'id': 'asset-%s' % asset.pk,
        'asset_id': asset.pk,
    }]


    if request.GET.has_key('annotations') or \
        (options and options.has_key('include_annotations') and options['include_annotations']):
        # @todo: refactor this serialization into a common place.
        def author_name(request, annotation, key):
            if not annotation.author_id:
                return None
            return 'author_name', get_public_name(annotation.author, request)

        def primary_type(request, annotation, key):
            return "primary_type", asset.primary.label

        for ann in asset.sherdnote_set.filter(range1__isnull=False):
            visible = selections_visible or request.user == ann.author or request.course.is_faculty(
                ann.author)
            if visible:
                if request.user == ann.author:
                    asset_json['user_analysis'] += 1
                ann_json = ann.sherd_json(
                    request, 'x',
                    ('title', 'author', 'tags', author_name, 'body',
                     'modified', 'timecode', primary_type))
                annotations.append(ann_json)

    rv = {
        'type': 'asset',
        'assets': {
            asset_key: asset_json
        },  #we make assets plural here to be compatible with the project JSON structure
        'annotations': annotations,
        'user_settings': {
            'help_item_detail_view':
            UserSetting.get_setting(request.user, "help_item_detail_view",
                                    True)
        }
    }
    return rv
def get_records(request, record_owner, assets):
    course = request.course
    logged_in_user = request.user

    # Can the record_owner edit the records
    viewing_own_work = (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 = ('citable' in request.GET and
               request.GET.get('citable') == 'true')

    # Is the current user faculty OR staff
    is_faculty = course.is_faculty(logged_in_user)

    # Does the course allow viewing other user selections?
    owner_selections_are_visible = (
        course_details.all_selections_are_visible(course) or
        viewing_own_work or viewing_faculty_records or is_faculty)

    # Filter the assets
    for fil in filter_by:
        filter_value = request.GET.get(fil)
        if filter_value:
            assets = [asset for asset in assets
                      if filter_by[fil](asset, filter_value, record_owner)]

    active_filters = get_active_filters(request, filter_by)

    # Spew out json for the assets
    asset_json = []
    options = {
        'owner_selections_are_visible': ('annotations' in request.GET and
                                         owner_selections_are_visible),
        'all_selections_are_visible':
        course_details.all_selections_are_visible(course) or is_faculty,
        'can_edit': viewing_own_work,
        'citable': citable
    }

    for asset in assets:
        asset_json.append(gallery_asset_json(request, asset, logged_in_user,
                                             record_owner, options))
    # Tags
    tags = []
    if record_owner:
        if owner_selections_are_visible:
            # Tags for selected user
            tags = Tag.objects.usage_for_queryset(
                record_owner.sherdnote_set.filter(asset__course=course),
                counts=True)
    else:
        if owner_selections_are_visible:
            # Tags for the whole class
            tags = Tag.objects.usage_for_queryset(
                SherdNote.objects.filter(asset__course=course),
                counts=True)
        else:
            # Tags for myself and faculty members
            tags = Tag.objects.usage_for_queryset(
                logged_in_user.sherdnote_set.filter(asset__course=course),
                counts=True)

            for f in course.faculty:
                tags.extend(Tag.objects.usage_for_queryset(
                            f.sherdnote_set.filter(asset__course=course),
                            counts=True))

    tags.sort(lambda a, b: cmp(a.name.lower(), b.name.lower()))

    user_resource = UserResource()
    owners = user_resource.render_list(request, request.course.members)

    # Assemble the context
    data = {'assets': asset_json,
            'tags': [{'name': tag.name} for tag in tags],
            'active_filters': active_filters,
            'space_viewer': user_resource.render_one(request, logged_in_user),
            'editable': viewing_own_work,
            'citable': citable,
            'owners': owners,
            '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')