Пример #1
0
def index(request, collection_id=None):
    """Displays a set of collections to the user depending on whether
    or not the collections are private or public and whether or not the
    user has permission."""

    if collection_id is not None:
        collection_id = int(collection_id)

    role_bucket = services.get_or_update_role_bucket(request)
    lti_req = LTIService(request)
    course_collections = lti_req.getCourseCollections()

    is_teacher = lti_req.isTeacher() or queries.is_superuser_or_staff(
        request.user)
    collection_admin_perms = []
    for r in (Users_Collections.ADMINISTRATOR, Users_Collections.INSTRUCTOR):
        role_name = Users_Collections.role_map[r]
        collection_admin_perms.extend(role_bucket[role_name])

    copy_collections = queries.getCopyCollectionList(request.user)
    collection_filters = dict(
        collection_ids=course_collections,
        can_filter=not queries.is_superuser_or_staff(request.user))
    collection_list = queries.getCollectionList(role_bucket,
                                                **collection_filters)
    display_collections = queries.groupCollectionsByList(collection_list)

    context = {
        "nav_collections": collection_list,
        "display_collections": display_collections,
        "copy_collections": copy_collections,
        "active_collection": None,
        "user_collection_role": role_bucket,
        "collection_admin_perms": collection_admin_perms,
        "is_teacher": is_teacher,
    }

    if collection_id:
        try:
            cur_collection = Collection.objects.get(id=collection_id)
        except Collection.DoesNotExist:
            raise Http404
        filtered_collection_list = [
            c for c in collection_list if c['id'] == cur_collection.id
        ]
        if len(filtered_collection_list) == 0:
            raise Http404
        context['active_collection'] = filtered_collection_list[0]

    analytics.track(actor=request.user,
                    verb=analytics.VERBS.viewed,
                    object=analytics.OBJECTS.collection,
                    context={"collection_id": collection_id})

    return render(request, 'collections/index.html', context)
Пример #2
0
def index(request, collection_id=None):
    """Displays a set of collections to the user depending on whether
    or not the collections are private or public and whether or not the
    user has permission."""

    if collection_id is not None:
        collection_id = int(collection_id)

    role_bucket = services.get_or_update_role_bucket(request)
    lti_req = LTIService(request)
    course_collections = lti_req.getCourseCollections()

    is_teacher = lti_req.isTeacher() or queries.is_superuser_or_staff(request.user)
    collection_admin_perms = []
    for r in (Users_Collections.ADMINISTRATOR, Users_Collections.INSTRUCTOR):
        role_name = Users_Collections.role_map[r]
        collection_admin_perms.extend(role_bucket[role_name])

    copy_collections = queries.getCopyCollectionList(request.user)
    collection_filters = dict(collection_ids=course_collections, can_filter=not queries.is_superuser_or_staff(request.user))
    collection_list = queries.getCollectionList(role_bucket, **collection_filters)
    display_collections = queries.groupCollectionsByList(collection_list)

    context = {
        "nav_collections": collection_list,
        "display_collections": display_collections,
        "copy_collections": copy_collections,
        "active_collection": None,
        "user_collection_role": role_bucket,
        "collection_admin_perms": collection_admin_perms,
        "is_teacher": is_teacher,
    }

    if collection_id:
        try:
            cur_collection = Collection.objects.get(id=collection_id)
        except Collection.DoesNotExist:
            raise Http404
        filtered_collection_list = [c for c in collection_list if c['id'] == cur_collection.id]
        if len(filtered_collection_list) == 0:
            raise Http404
        context['active_collection'] = filtered_collection_list[0]

    analytics.track(
        actor=request.user,
        verb=analytics.VERBS.viewed,
        object=analytics.OBJECTS.collection,
        context={"collection_id": collection_id}
    )

    return render(request, 'collections/index.html', context)
Пример #3
0
def create(request):
    """Creates a collection."""
    role_bucket = services.get_or_update_role_bucket(request)
    course_collections = LTIService(request).getCourseCollections()
    collection_filters = dict(
        collection_ids=course_collections,
        can_filter=not queries.is_superuser_or_staff(request.user))
    collection_list = queries.getCollectionList(role_bucket,
                                                **collection_filters)

    if request.method == 'POST':
        lti_req = LTIService(request)
        published = not lti_req.isTeacher()
        request.POST = request.POST.copy()
        request.POST['published'] = published
        collection_form = CollectionForm(request.POST)

        card_template_id = collection_form.data['card_template']
        if collection_form.is_valid():
            collection = collection_form.save()
            lti_req.associateCourse(collection.id)
            services.add_user_to_collection(
                user=request.user,
                collection=collection,
                role=Users_Collections.ADMINISTRATOR)
            #update role_bucket to add admin permission to the user for this newly created collection
            services.get_or_update_role_bucket(
                request, collection.id,
                Users_Collections.role_map[Users_Collections.ADMINISTRATOR])
            log.info('Collection %s created.' % collection.id,
                     extra={'user': request.user})
            analytics.track(actor=request.user,
                            verb=analytics.VERBS.created,
                            object=analytics.OBJECTS.collection,
                            context={"custom": False})
            return redirect(collection)
    else:
        rel_templates = CardTemplate.objects.filter(
            Q(owner__isnull=True) | Q(owner=request.user))
        initial = {'card_template': '1'}
        card_template_id = initial['card_template']
        collection_form = CollectionForm(query_set=rel_templates,
                                         initial=initial)

    # Pre-populate the "preview" of the card template
    # This view is also called via AJAX on the page.
    prev_request = HttpRequest()
    prev_request.method = 'GET'
    prev_request.GET['card_template_id'] = card_template_id
    prev_response = card_template.preview(prev_request)
    card_template_preview_html = prev_response.content

    context = {
        "nav_collections": collection_list,
        "active_collection": None,
        "collection_form": collection_form,
        "card_template_preview_html": card_template_preview_html
    }

    return render(request, 'collections/create.html', context)
Пример #4
0
def create(request):
    """Creates a collection."""
    role_bucket = services.get_or_update_role_bucket(request)
    course_collections = LTIService(request).getCourseCollections()
    collection_filters = dict(collection_ids=course_collections, can_filter=not queries.is_superuser_or_staff(request.user))
    collection_list = queries.getCollectionList(role_bucket, **collection_filters)

    if request.method == 'POST':
        lti_req = LTIService(request)
        published = not lti_req.isTeacher()
        request.POST = request.POST.copy()
        request.POST['published'] = published
        collection_form = CollectionForm(request.POST)

        card_template_id = collection_form.data['card_template']
        if collection_form.is_valid():
            collection = collection_form.save()
            lti_req.associateCourse(collection.id)
            services.add_user_to_collection(user=request.user, collection=collection, role=Users_Collections.ADMINISTRATOR)
            #update role_bucket to add admin permission to the user for this newly created collection
            services.get_or_update_role_bucket(request, collection.id, Users_Collections.role_map[Users_Collections.ADMINISTRATOR])
            log.info('Collection %s created.' %collection.id, extra={'user': request.user})
            analytics.track(
                actor=request.user,
                verb=analytics.VERBS.created,
                object=analytics.OBJECTS.collection,
                context={"custom": False}
            )
            return redirect(collection)
    else:
        rel_templates = CardTemplate.objects.filter(Q(owner__isnull=True) | Q(owner=request.user))
        initial = {'card_template': '1'}
        card_template_id = initial['card_template']
        collection_form = CollectionForm(query_set=rel_templates,initial=initial)


    # Pre-populate the "preview" of the card template
    # This view is also called via AJAX on the page.
    prev_request = HttpRequest()
    prev_request.method = 'GET'
    prev_request.GET['card_template_id'] = card_template_id
    prev_response = card_template.preview(prev_request)
    card_template_preview_html = prev_response.content

    context = {
        "nav_collections": collection_list,
        "active_collection": None,
        "collection_form": collection_form,
        "card_template_preview_html": card_template_preview_html
    }

    return render(request, 'collections/create.html', context)
Пример #5
0
def custom_create(request):
    """
    Creates a collection with custom template.
    """
    upload_error = ''
    course_name = ''
    role_bucket = services.get_or_update_role_bucket(request)
    lti_req = LTIService(request)
    course_collections = lti_req.getCourseCollections()
    collection_filters = dict(
        collection_ids=course_collections,
        can_filter=not queries.is_superuser_or_staff(request.user))
    collection_list = queries.getCollectionList(role_bucket,
                                                **collection_filters)
    if request.method == 'POST':
        d = {'user': request.user}
        log.info('The user is uploading a custom deck.', extra=d)

        course_name = request.POST.get('course', '')
        if course_name == '' or 'file' not in request.FILES:
            if course_name == '' and 'file' not in request.FILES:
                upload_error = "Course name needed. No file selected."
            elif course_name != '' and 'file' not in request.FILES:
                upload_error = 'No file selected'
            else:
                upload_error = 'Course name needed.'
        else:
            try:
                is_teacher = lti_req.isTeacher()
                deck = services.handle_custom_file(request.FILES['file'],
                                                   course_name, request.user,
                                                   is_teacher)

                lti_req.associateCourse(deck.collection.id)
                log.info(
                    'Custom deck %(d)s successfully added to the new collection %(c)s.'
                    % {
                        'c': str(deck.collection.id),
                        'd': str(deck.id)
                    },
                    extra=d)
                return redirect(deck)
            except Exception, e:
                upload_error = str(e)
        msg = 'The following error occurred when the user tried uploading a deck: '
        log.error(msg + upload_error, extra=d)
Пример #6
0
def edit(request, collection_id=None):
    """Edits a collection."""

    collection = Collection.objects.get(id=collection_id)

    role_bucket = services.get_or_update_role_bucket(request)
    course_collections = LTIService(request).getCourseCollections()
    collection_filters = dict(
        collection_ids=course_collections,
        can_filter=not queries.is_superuser_or_staff(request.user))
    collection_list = queries.getCollectionList(role_bucket,
                                                **collection_filters)

    if request.method == 'POST':
        collection_form = CollectionForm(request.POST, instance=collection)
        if collection_form.is_valid():
            collection = collection_form.save()
            response = redirect(collection)
            response['Location'] += '?instructor=edit'
            analytics.track(actor=request.user,
                            verb=analytics.VERBS.modified,
                            object=analytics.OBJECTS.collection,
                            context={"collection_id": collection_id})
            return response
    else:
        collection_form = CollectionForm(instance=collection)

    collection_decks = []
    for c in collection_list:
        if c['id'] == collection.id:
            collection_decks = c['decks']
            break

    print collection_decks

    context = {
        "collection_form": collection_form,
        "nav_collections": collection_list,
        "collection": collection,
        "collection_decks": collection_decks,
    }

    return render(request, 'collections/edit.html', context)
Пример #7
0
def edit(request, collection_id=None):
    """Edits a collection."""

    collection = Collection.objects.get(id=collection_id)

    role_bucket = services.get_or_update_role_bucket(request)
    course_collections = LTIService(request).getCourseCollections()
    collection_filters = dict(collection_ids=course_collections, can_filter=not queries.is_superuser_or_staff(request.user))
    collection_list = queries.getCollectionList(role_bucket, **collection_filters)

    if request.method == 'POST':
        collection_form = CollectionForm(request.POST, instance=collection)
        if collection_form.is_valid():
            collection = collection_form.save()
            response = redirect(collection)
            response['Location'] += '?instructor=edit'
            analytics.track(
                actor=request.user,
                verb=analytics.VERBS.modified,
                object=analytics.OBJECTS.collection,
                context={"collection_id": collection_id}
            )
            return response
    else:
        collection_form = CollectionForm(instance=collection)

    collection_decks = []
    for c in collection_list:
        if c['id'] == collection.id:
            collection_decks = c['decks']
            break

    print collection_decks

    context = {
        "collection_form": collection_form,
        "nav_collections": collection_list,
        "collection": collection,
        "collection_decks": collection_decks,
    }

    return render(request, 'collections/edit.html', context)
Пример #8
0
def custom_create(request):
    """
    Creates a collection with custom template.
    """
    upload_error = ''
    course_name = ''
    role_bucket = services.get_or_update_role_bucket(request)
    lti_req = LTIService(request)
    course_collections = lti_req.getCourseCollections()
    collection_filters = dict(collection_ids=course_collections, can_filter=not queries.is_superuser_or_staff(request.user))
    collection_list = queries.getCollectionList(role_bucket, **collection_filters)
    if request.method == 'POST':
        d = {'user': request.user}
        log.info('The user is uploading a custom deck.', extra=d)

        course_name = request.POST.get('course', '')
        if course_name == '' or 'file' not in request.FILES:
            if course_name == '' and 'file' not in request.FILES:
                upload_error = "Course name needed. No file selected."
            elif course_name != '' and 'file' not in request.FILES:
                upload_error = 'No file selected'
            else:
                upload_error = 'Course name needed.'
        else:
            try:
                is_teacher = lti_req.isTeacher()
                deck = services.handle_custom_file(request.FILES['file'], course_name, request.user, is_teacher)

                lti_req.associateCourse(deck.collection.id)
                log.info('Custom deck %(d)s successfully added to the new collection %(c)s.'
                         %{'c': str(deck.collection.id), 'd':str(deck.id)}, extra=d)
                return redirect(deck)
            except Exception, e:
                    upload_error = str(e)
        msg = 'The following error occurred when the user tried uploading a deck: '
        log.error(msg + upload_error , extra=d)