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)
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)
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)
def test_collection_form(self): form = CollectionForm({ "title": "foo", "card_template": self.card_template.id }) self.assertTrue(form.is_valid()) form1 = CollectionForm({}) self.assertFalse(form1.is_valid())
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)
def test_collection_form(self): form = CollectionForm({"title":"foo","card_template":self.card_template.id}) self.assertTrue(form.is_valid()) form1 = CollectionForm({}) self.assertFalse(form1.is_valid())