Ejemplo n.º 1
0
   def post(self, request, *args, **kwargs):

        try:
            form_type = request.POST['form_type']
            the_id = request.POST['id']
        except KeyError:
            return JsonResponse({})

        instance = None
        thread_content=None
        thread_section=None

        # If form type is "edit", then the_id must be 
        # a valid thread_content id.
        # Populate form with instance of that thread_content
        if form_type=="edit":
            try:
                thread_content = ThreadContent.objects.get(id=the_id)
            except (KeyError,ObjectDoesNotExist):
                return JsonResponse({})

            instance = thread_content
            the_content_type = thread_content.content_type

        # If form type is "insert", then the_id must be
        # a valid thread_section id.
        # Create blank form 
        elif form_type=="insert":
            try:
                thread_section = ThreadSection.objects.get(id=the_id)
            except ObjectDoesNotExist:
                return JsonResponse({})
            the_content_type = None


        # else invalid form_type
        else:
            return JsonResponse({})


        # must be designer of course
        if thread_content:
            course=thread_content.course
        else:
            course=thread_section.get_course()
        is_designer = False
        if request.user.is_authenticated():
            courseuser = request.user.courseuser
            role = courseuser.get_current_role(course) 
            if role==DESIGNER_ROLE:
                is_designer=True

        if not is_designer:
            return JsonResponse({})


        form_identifier = "%s_%s" % (form_type, the_id)

        update_options_command="update_content_options('%s', this.value, '%s')" % \
            (form_identifier, course.code)

        form = thread_content_form_factory(
            course=course,
            the_content_type=the_content_type,
            update_options_command=update_options_command
        )
        form = form(instance=instance, 
                    auto_id="content_form_%s_%%s" % form_identifier,
        )

        form_html = form.as_p()

        return JsonResponse({'form_type': form_type,
                             'id': the_id,
                             'form_html': form_html})
Ejemplo n.º 2
0
    def post(self, request, *args, **kwargs):

        try:
            action = request.POST['action']
            the_id = request.POST['id']
        except KeyError:
            return JsonResponse({})

        form_html=""

        # if action is insert, then the_id must be a valid section_id
        if action=='insert':
            try:
                thread_section = ThreadSection.objects.get(id=the_id)
            except (KeyError, ValueError, ObjectDoesNotExist):
                return JsonResponse({})

            course = thread_section.get_course()
            thread_content=None

        # else, the_id must be a valid content_id
        else:
            try:
                thread_content = ThreadContent.objects.get(id=the_id)
            except (KeyError, ValueError, ObjectDoesNotExist):
                return JsonResponse({})

            course = thread_content.course
            thread_section=thread_content.section

        
        # must be designer of course
        is_designer = False
        if request.user.is_authenticated():
            courseuser = request.user.courseuser
            role = courseuser.get_current_role(course) 
            if role==DESIGNER_ROLE:
                is_designer=True

        if not is_designer:
            return JsonResponse({})


        rerender_sections = []

        # move content up
        if action=="move_up":
            previous_in_section = thread_content.find_previous(in_section=True)
            if previous_in_section:
                if previous_in_section.sort_order == thread_content.sort_order:
                    thread_section.reset_thread_content_sort_order()
                    previous_in_section.refresh_from_db()
                    thread_content.refresh_from_db()
                sort_order = thread_content.sort_order
                thread_content.sort_order = previous_in_section.sort_order
                previous_in_section.sort_order = sort_order
                with transaction.atomic(), reversion.create_revision():
                    thread_content.save()
                    previous_in_section.save()

                rerender_sections = [thread_section,]

            else:
                # if thread_content is first in section, then move up to
                # end of previous section

                previous_section = thread_section.find_previous()
                try:
                    thread_content.sort_order = previous_section\
                                  .thread_contents.last().sort_order+1
                except AttributeError:
                    thread_content.sort_order = 0
                thread_content.section = previous_section
                with transaction.atomic(), reversion.create_revision():
                    thread_content.save()

                rerender_sections = [thread_section, previous_section]

        # move content down
        if action=="move_down":
            next_in_section = thread_content.find_next(in_section=True)
            if next_in_section:
                if next_in_section.sort_order == thread_content.sort_order:
                    thread_section.reset_thread_content_sort_order()
                    next_in_section.refresh_from_db()
                    thread_content.refresh_from_db()
                sort_order = thread_content.sort_order
                thread_content.sort_order = next_in_section.sort_order
                next_in_section.sort_order = sort_order
                with transaction.atomic(), reversion.create_revision():
                    thread_content.save()
                    next_in_section.save()

                rerender_sections = [thread_section,]

            else:
                # if thread_content is last in section, then move down to
                # beginning of next section

                next_section = thread_section.find_next()
                try:
                    thread_content.sort_order = next_section\
                                  .thread_contents.first().sort_order-1
                except AttributeError:
                    thread_content.sort_order = 0
                thread_content.section = next_section
                with transaction.atomic(), reversion.create_revision():
                    thread_content.save()

                rerender_sections = [thread_section, next_section]
        
        # delete content
        elif action=="delete":
            thread_content.mark_deleted()
            rerender_sections = [thread_section,]


        # edit content
        elif action=="edit":
            try:
                content_type = ContentType.objects.get(id=request.POST['content_type'])
                
            except (KeyError, ObjectDoesNotExist):
                return JsonResponse({});

            form_identifier = "edit_%s" % the_id

            update_options_command="update_content_options('%s', this.value)"% \
                form_identifier

            form = thread_content_form_factory(
                course=thread_content.course,
                the_content_type=content_type,
                update_options_command=update_options_command
            )
            form = form(request.POST, instance=thread_content,
                        auto_id="content_form_%s_%%s" % form_identifier,
                    )
            if form.is_valid():
                with transaction.atomic(), reversion.create_revision():
                    form.save()
                rerender_sections = [thread_section,]
            else:
                form_html = form.as_p()


        # insert content
        elif action=="insert":
            try:
                content_type = ContentType.objects.get(id=request.POST['content_type'])
                
            except (KeyError, ObjectDoesNotExist):
                return JsonResponse({});

            form_identifier = "insert_%s" % the_id

            update_options_command="update_content_options('%s', this.value)"% \
                form_identifier

            try:
                new_sort_order = thread_section.thread_contents.last()\
                                                              .sort_order+1
            except AttributeError:
                new_sort_order = 0

            form = thread_content_form_factory(
                course = thread_section.get_course(),
                the_content_type=content_type,
                update_options_command=update_options_command
            )
            form = form(request.POST,
                        auto_id="content_form_%s_%%s" % form_identifier,
                    )
            if form.is_valid():
                with transaction.atomic(), reversion.create_revision():
                    new_thread_content=form.save(commit=False)
                    new_thread_content.section=thread_section
                    new_thread_content.sort_order = new_sort_order
                    new_thread_content.save()

                rerender_sections = [thread_section,]
            else:
                form_html = form.as_p()


        all_thread_contents = course.thread_contents.all()

        section_contents={}
        for section in rerender_sections:
            # generate html for thread_content of section
            from django.template.loader import render_to_string
            
            thread_contents = section.thread_contents.all()
            thread_contents = course.thread_content_select_related_content_objects(
                thread_contents)


            content_html = render_to_string(
                template_name='micourses/threads/thread_content_edit_container.html',
                context = {'thread_contents': thread_contents,
                           'all_thread_contents': all_thread_contents,
                           'courseuser': courseuser, },
            )
            
            section_contents[section.id] = content_html

        return JsonResponse({'section_contents': section_contents,
                             'action': action,
                             'id': the_id,
                             'form_html': form_html,
                             })