Beispiel #1
0
def add_new_model(request, app, model_name, form=None):
    normal_model_name = normalize_model_name(model_name)
    model = get_model(app, normal_model_name)
    form = form if form else model.get_form_class()

    if not form:
        form = get_model_form(normal_model_name)

    if request.method == 'POST':
        form = form(request.POST, request.FILES)

        if form.is_valid():
            form.instance.user = request.user
            form.instance.site = request.user.site
            try:
                new_obj = form.save()
            except ValidationError as error:
                new_obj = None

            if new_obj:
                return HttpResponse(
                    '<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' %
                    (escape(new_obj._get_pk_val()), escape(new_obj)))

    else:
        form = form()

    page_context = {'app': app, 'form': form, 'field': normal_model_name, 'is_popup': True}
    return render_to_response('picker/popup.html', page_context,
                              context_instance=RequestContext(request))
Beispiel #2
0
def add_new_model(request, model_name, form=None):
    normal_model_name = normalize_model_name(model_name)
    print normal_model_name

    if not form:
        form = get_model_form(normal_model_name)

    if request.method == 'POST':
        form = form(request.POST)
        if form.is_valid():
            try:
                new_obj = form.save()
            except forms.ValidationError:
                new_obj = None

            if new_obj:
                return HttpResponse(
                    '<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' %
                    (escape(new_obj._get_pk_val()), escape(new_obj))
                )

    else:
        form = form()

    page_context = {'form': form, 'field': normal_model_name}
    return render(request, 'add_another/popup.html', page_context)
Beispiel #3
0
def add_new_model(request, model_name, form=None):
    normal_model_name = normalize_model_name(model_name)


    photos=Medias.objects.filter(str_type="image")
    
    if request.method == 'POST':
        int_height=request.POST.get('int_height')
        int_width=request.POST.get('int_width')
        if model_name == "ExistingMedia" :
            imgsrc = request.POST.get('imgToAdd')
            img="<img src='"+imgsrc+"'"
            
            if int_width :
                img=img+"width='"+int_width+"'"
            if int_height :
                img=img+"height='"+int_height+"'"
                
            img=img + "alt='' />"
                
            #if form.is_valid():
            if imgsrc:
                return HttpResponse('<script type="text/javascript">opener.updateContent(window, "%s", "%s");</script>' % \
                    (escape('12'), escape(img)))
        else :
            if model_name == "NewMedia" :
                
                #print "OK Form isValid"
                form=ImageForm(request.POST, request.FILES)
                #print form
                if form.is_valid() :
                    #print "OK Form isValid"
                    try:
                        new_obj =form.save(commit=False)
                        new_obj.str_type="image"
                        new_obj.author=request.user
                        new_obj.save()
                    except forms.ValidationError, error:
                        new_obj = None
                    
                    if new_obj:
                        img="<img src='"+new_obj.str_file.url+"' alt=''"
                        if int_width :
                            img=img+"width='"+int_width+"'"
                        if int_height :
                            img=img+"height='"+int_height+"'"
                        img=img + "alt='' />"
                        return HttpResponse('<script type="text/javascript">opener.updateContent(window, "%s", "%s");</script>' % \
                    (escape(new_obj._get_pk_val()), escape(img)))
Beispiel #4
0
def add_new_video(request, video_type, form=None):


    normal_model_name = normalize_model_name(video_type)


    videos=Medias.objects.filter(str_type="video")
    
    if request.method == 'POST':
        int_height=request.POST.get('int_height')
        int_width=request.POST.get('int_width')
        if video_type == "ExistingMedia" :
            videosrc = request.POST.get('videoToAdd')
            video="<embed src='"+videosrc+"' type='application/x-shockwave-flash' name='plugin'"
            
            if int_width :
                video=video+"width='"+int_width+"'"
            if int_height :
                
                video=video+"height='"+int_height+"'"
                
            video=video +"/>"
                
            #if form.is_valid():
            if videosrc:
                return HttpResponse('<script type="text/javascript">opener.updateContent(window, "%s", "%s");</script>' % \
                    (escape('12'), escape(video)))
        else :
            if video_type == "NewMedia" :
                
                #print "OK Form isValid"
                form=ImageForm(request.POST, request.FILES)
                #print form
                if form.is_valid() :
                    #print "OK Form isValid"
                    try:
                        new_obj =form.save(commit=False)
                        new_obj.str_type="image"
                        new_obj.author=request.user
                        new_obj.save()
                    except forms.ValidationError, error:
                        new_obj = None
                    
                    if new_obj:
                        video="<embed src='"+new_obj.str_file.url+"' type='application/x-shockwave-flash' name='plugin'"
                        if int_width :
                            video=video+"width='"+int_width+"'"
                        if int_height :
                            video=video+"height='"+int_height+"'"
                        video=video + "alt='' />"
                        return HttpResponse('<script type="text/javascript">opener.updateContent(window, "%s", "%s");</script>' % \
                    (escape(new_obj._get_pk_val()), escape(video)))
            else :
                    if video_type == "WebMedia" :
                        form=WebVideoForm(request.POST)
                        if form.is_valid():
                            try:
                                new_obj =form.save(commit=False)
                                new_obj.str_type="video_url"
                                new_obj.author=request.user
                                new_obj.save()
                            except forms.ValidationError, error:
                               # print "an error occurs"
                                new_obj=None
                        if new_obj :
                            video=new_obj.str_embed_code
                            return HttpResponse('<script type="text/javascript">opener.updateContent(window, "%s", "%s");</script>' % \
                    (escape(new_obj._get_pk_val()), escape(video)))