def add_url_to_section_item_resources(request): section_item_resources_id = request.POST.get('section_item_resources_id', None) try: section_item_resources = SectionItemResources.objects.get(pk=section_item_resources_id) except: section_item_resources = create_resource_set(request, section_item_resources) if not section_item_resources: return APIUtilities._api_not_found() from oer.views import new_url_from_form new_resource = new_url_from_form( request.user, request.POST.get('title', None), request.POST.get('url', None)) new_objective_resource = Resource(resource=new_resource) new_objective_resource.save() section_item_resources.resources.add(new_objective_resource) context = { 'resource': { 'id': new_objective_resource.id, 'url': new_resource.revision.content.url, 'title': new_resource.title, 'thumbnail': settings.MEDIA_URL + new_resource.image.name if new_resource.image else '', } } return APIUtilities.success(context)
def add_sections_to(parent, sections): for section in sections: new_section = Section( position=section.position, title=section.title, settings=section.settings ) new_section.save() parent.sections.add(new_section) # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=section_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=section.id, target_id=new_section.id ).save() for item in section.items.all(): new_item = SectionItem( content_type=item.content_type, content_id=item.content_id, description=item.description, meta=item.meta, position=item.position ) new_item.save() new_section.items.add(new_item) # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=sectionitem_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=item.id, target_id=new_item.id ).save() for resource_set in item.resource_sets.all(): new_item_resources = SectionItemResources( title=resource_set.title, position=resource_set.position ) new_item_resources.save() new_item.resource_sets.add(new_item_resources) # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=sectionitemresources_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=resource_set.id, target_id=new_item_resources.id ).save() for resource in resource_set.resources.all(): new_curriculum_resource = Resource( resource=resource.resource, notes=resource.notes ) new_curriculum_resource.save() new_item_resources.resources.add(new_curriculum_resource) # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=resource_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=resource.id, target_id=new_curriculum_resource.id ).save()
def create_textbook_reference(textbook, scope, title, username, description, license, collection): DEFAULT_COST = 0 try: from django.contrib.auth.models import User title = title if title else 'Untitled reference' user = User.objects.get(username=username) from django.template.defaultfilters import slugify # Create a new resource from oer.models import Resource as OEResource new_resource = OEResource( title=title, cost=DEFAULT_COST, user=user, slug=slugify(title), visibility='public', description=description, license=license ) new_reference = Reference(source_type='textbook', textbook=textbook, scope=scope) new_reference.save() from oer.models import ResourceRevision new_resource_revision = ResourceRevision() new_resource_revision.content = new_reference new_resource_revision.user = user new_resource_revision.save() new_resource.revision = new_resource_revision new_resource.save() # Assign this resource to the revision created. new_resource.revision.resource = new_resource new_resource.revision.save() # Now add this resource to the collection it belongs to collection.resources.add(new_resource) collection.save() new_curriculum_resource = Resource(resource=new_resource) new_curriculum_resource.save() return (new_resource.id, new_curriculum_resource.id) except Exception, exception: print exception
def add_existing_to_section_item_resources(request): section_item_resources_id = request.POST.get('section_item_resources_id', None) #is_resource = request.POST.get('is_resource', None) == 'true' resource_collection_id = request.POST.get('resource_collection_ID', None) try: section_item_resources = SectionItemResources.objects.get(pk=section_item_resources_id) except: section_item_resources = create_resource_set(request, section_item_resources) if not section_item_resources: return APIUtilities._api_not_found() from oer.models import Resource as OEResource resource = OEResource.objects.get(pk=resource_collection_id) new_objective_resource = Resource(resource=resource) new_objective_resource.save() section_item_resources.resources.add(new_objective_resource) import oer.CollectionUtilities as cu cu.set_resources_type([resource]) cu.preprocess_collection_listings([resource]) context = { 'resource': { 'id': new_objective_resource.id, 'url': reverse( 'read', kwargs={ 'resource_id': resource.id, 'resource_slug': resource.slug } ), 'thumbnail': settings.MEDIA_URL + resource.image.name if resource.image else '', 'title': resource.title, 'type': resource.type } } return APIUtilities.success(context)
def add_upload_to_section_item_resources(request): section_item_resources_id = request.POST.get('section_item_resources_id', None) resource_id = request.POST.get('key', None) from oer.models import Resource as OEResource try: section_item_resources = SectionItemResources.objects.get(pk=section_item_resources_id) resource = OEResource.objects.get(pk=resource_id) except: section_item_resources = create_resource_set(request, section_item_resources) if not section_item_resources: return APIUtilities._api_not_found() new_objective_resource = Resource(resource=resource) new_objective_resource.save() section_item_resources.resources.add(new_objective_resource) context = { 'resource': { 'id': new_objective_resource.id, 'url': reverse( 'read', kwargs={ 'resource_id': resource.id, 'resource_slug': resource.slug } ), 'thumbnail': settings.MEDIA_URL + resource.image.name if resource.image else '', 'title': resource.title } } return APIUtilities.success(context)