def create(request): title = request.POST.get('title', None) subject = request.POST.get('subject', None) grade = request.POST.get('grade', None) session = request.POST.get('session', None) raw_start_date = request.POST.get('start_date', None) raw_end_date = request.POST.get('end_date', None) organization_slug = request.POST.get('organization_slug', None) import dateutil.parser from_date = dateutil.parser.parse(raw_start_date) end_date = dateutil.parser.parse(raw_end_date) settings = { 'menu': [ { 'organization': 'textbook-units', 'title': 'UNITS / RESOURCES' } ], 'periods': { 'session': session, 'start': from_date.isoformat(), 'title': 'weekly', 'end': end_date.isoformat(), 'data': [] } } from django.template.defaultfilters import slugify new_curriculum = Curriculum( title=title, user=request.user, description='', visibility='private', grade=grade, subject=subject, settings=settings, slug=slugify(title) ) new_curriculum.save() from django.shortcuts import redirect return redirect('curriculum:curriculum', username=request.user.username, organization_slug=organization_slug, curriculum_slug=new_curriculum.slug)
def copy_curriculum(request): curriculum_id = request.POST.get('curriculum_id', None) title = request.POST.get('title', None) sync = request.POST.get('sync', None) if not request.user.is_authenticated(): return APIUtilities._api_failure() from django.contrib.contenttypes.models import ContentType curriculum_content_type = ContentType.objects.get_for_model(Curriculum) unit_content_type = ContentType.objects.get_for_model(Unit) section_content_type = ContentType.objects.get_for_model(Section) sectionitem_content_type = ContentType.objects.get_for_model(SectionItem) sectionitemresources_content_type = ContentType.objects.get_for_model(SectionItemResources) resource_content_type = ContentType.objects.get_for_model(Resource) standardcategory_content_type = ContentType.objects.get_for_model(StandardCategory) try: curriculum = Curriculum.objects.get(pk=curriculum_id) except: return APIUtilities._api_not_found() 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 make_standard_category_copies(parent, original_standard_category): if original_standard_category.standard_categories.count() == 0: return else: for standard_category in original_standard_category.standard_categories.all(): new_standard_category = StandardCategory( title=standard_category.title, category=standard_category.category, ) new_standard_category.save() parent.standard_categories.add(new_standard_category) # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=standardcategory_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=standard_category.id, target_id=new_standard_category.id ).save() add_sections_to(new_standard_category, standard_category.sections.all()) return make_standard_category_copies(new_standard_category, standard_category) new_curriculum = Curriculum( title=title, user=request.user, description=curriculum.description, visibility=curriculum.visibility, grade=curriculum.grade, subject=curriculum.subject, settings=curriculum.settings, slug=curriculum.slug, synced_to=curriculum ) new_curriculum.save() # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=curriculum_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=curriculum.id, target_id=new_curriculum.id, ).save() # Add references to EXISTING textbooks. for textbook in curriculum.textbooks.all(): new_curriculum.textbooks.add(textbook) # Make a deep copy of the units. for unit in curriculum.units.all(): new_unit = Unit( title=unit.title, period=unit.period ) new_unit.save() new_curriculum.units.add(new_unit) # IF SYNC IS TURNED ON. CurriculumSyncLink( link_type=unit_content_type, from_curriculum=curriculum, to_curriculum=new_curriculum, source_id=unit.id, target_id=new_unit.id ).save() add_sections_to(new_unit, unit.sections.all()) for textbook in new_curriculum.textbooks.all(): if unit in textbook.units.all(): textbook.units.add(new_unit) # Make a deep copy of the standard categories. make_standard_category_copies(new_curriculum, curriculum) return APIUtilities.success({'url': reverse( 'curriculum:curriculum', kwargs={ 'organization_slug': request.organization.slug, 'username': new_curriculum.user.username, 'curriculum_slug': new_curriculum.slug } )})