Beispiel #1
0
def insert_content_below_section(request, section_code, thread_code, form):
    
    dajax = Dajax()

    try:

        thread_section = ThreadSection.objects.get(code=section_code, thread__code=thread_code)
        
        form_dict={}
        for  obj in form:
            form_dict[obj['name']]=obj['value']
        form =  ThreadContentForm(form_dict, prefix=section_code)
        if form.is_valid():
            content_type = form.cleaned_data.get('content_type')
            object_id = form.cleaned_data.get('object_id')
            substitute_title = form.cleaned_data.get('substitute_title')

            other_content = thread_section.threadcontent_set.reverse()
            if other_content:
                last_sort_order = other_content[0].sort_order
                sort_order = last_sort_order+1
            else:
                sort_order = 0

            thread_content = ThreadContent(section=thread_section,
                                           content_type=content_type,
                                           object_id=object_id,
                                           sort_order=sort_order,
                                           substitute_title=substitute_title)

            # since added section after form, need to validate again
            # to see if have duplicate content for thread
            try:
                thread_content.full_clean()
            except ValidationError as ve:
                dajax.assign('#%s_object_id_errors' % section_code,
                             'innerHTML',
                             ve.messages)
            else:
                thread_content.save()

                dajax.assign('#thread_contents', 'innerHTML', \
                                 thread_section.thread.render_html_edit_string())
        # if form is not valid
        else:
            dajax.assign('#%s_content_type_errors' % section_code,
                         'innerHTML',
                         form['content_type'].errors)
            dajax.assign('#%s_object_id_errors' % section_code,
                         'innerHTML',
                         form['object_id'].errors)
            dajax.assign('#%s_substitute_title_errors' % section_code,
                         'innerHTML',
                         form['substitute_title'].errors)

    except Exception as e:
        dajax.alert("something wrong: %s" % e)
        
    return dajax.json()
Beispiel #2
0
def confirm_edit_content(request, threadcontent_id, form):
    dajax = Dajax()

    try:
        thread_content=ThreadContent.objects.get(id=threadcontent_id)

        form_dict={}
        for  obj in form:
            form_dict[obj['name']]=obj['value']
        form = ThreadContentForm(form_dict, prefix="threadcontent_%s" % threadcontent_id)

        if form.is_valid():
            content_type = form.cleaned_data.get('content_type')
            object_id = form.cleaned_data.get('object_id')
            substitute_title = form.cleaned_data.get('substitute_title')
            
            thread_content.content_type=content_type
            thread_content.object_id=object_id
            thread_content.substitute_title =substitute_title

            # since form didn't include section, need to validate again
            # to see if have duplicate content for thread
            try:
                thread_content.full_clean()
            except ValidationError as ve:
                dajax.assign('#threadcontent_%s_object_id_errors' % threadcontent_id,
                             'innerHTML',
                             ve.messages)
            else:
                thread_content.save()
                dajax.assign('#thread_contents', 'innerHTML', \
                                 thread_content.section.thread.render_html_edit_string())

        # if form is not valid
        else:
            dajax.assign('#threadcontent_%s_content_type_errors' % threadcontent_id,
                         'innerHTML',
                         form['content_type'].errors)
            dajax.assign('#threadcontent_%s_object_id_errors' % threadcontent_id,
                         'innerHTML',
                         form['object_id'].errors)
            dajax.assign('#threadcontent_%s_substitute_title_errors' % threadcontent_id,
                         'innerHTML',
                         form['substitute_title'].errors)
 
    except Exception as e:
        dajax.alert("something wrong: %s" % e)

    return dajax.json()