Пример #1
0
def add_attachment(request, app_label, module_name, pk,
                   template_name='attachments/add.html', extra_context={}):
    ajax = request.is_ajax
    
    next = request.POST.get('next', '/')
    model = get_model(app_label, module_name)
    if model is None:
        return HttpResponseRedirect(next)
    obj = get_object_or_404(model, pk=pk)
    form = AttachmentForm(request.POST, request.FILES)

    if form.is_valid():
        form.save(request, obj)
        message = _('Your attachement was uploaded.')
        if ajax:
            return _json_response(request, _("Success"), message)
        messages.add_message(request, messages.SUCCESS, message)
        return HttpResponseRedirect(next)
    else:
        template_context = {
            'form': form,
            'form_url': add_url_for_obj(obj),
            'next': next,
        }
        template_context.update(extra_context)
        
        if ajax:
            json_context = {
                            'form_html': render_to_string(template_name, template_context)
                            }
            return _json_response(request, _("Error"), _("Please correct the form errors."), json_context)
        
        return render_to_response(template_name, template_context,
                                  RequestContext(request))
Пример #2
0
def add(request, app_label, module_name, pk, template_name="attachments/add_form.html", perm_callback=PERMS["add"]):

    # TODO: check for proper applabel/modelname
    model = get_model(app_label, module_name)
    obj = get_object_or_404(model, pk=pk)

    # TODO: check for valid next attribute
    next = request.REQUEST.get("next") or "/"

    if not perm_callback(request, obj):
        return HttpResponseForbidden("Did't allowed upload attachments for this item")

    if request.method == "POST":
        form = AttachmentForm(request.POST, request.FILES)
    else:
        form = AttachmentForm()

    if form.is_valid():
        form.save(request, obj)
        request.user.message_set.create(message=ugettext("Your attachment was uploaded."))
        return HttpResponseRedirect(next)
    else:
        return render_to_response(
            template_name, {"form": form, "form_url": add_url_for_obj(obj), "next": next}, RequestContext(request)
        )
Пример #3
0
def add_attachment(request, app_label, module_name, pk,
                   template_name='attachments/add.html', extra_context={}):

    next = request.POST.get('next', '/')
    model = get_model(app_label, module_name)
    if model is None:
        return HttpResponseRedirect(next)
    obj = get_object_or_404(model, pk=pk)
    form = AttachmentForm(request.POST, request.FILES)

    if form.is_valid():
        if form.cleaned_data['tag']:
            a = Attachment.objects.filter(tag = form.cleaned_data['tag'], object_id = obj.id, content_type = ContentType.objects.get_for_model(obj))
            for old_item in a:
                old_item.delete()
        form.save(request, obj)
        request.user.message_set.create(message=ugettext('Your attachment was uploaded.'))
        return HttpResponseRedirect(next)
    else:
        template_context = {
            'form': form,
            'form_url': add_url_for_obj(obj),
            'next': next,
        }
        template_context.update(extra_context)
        return render_to_response(template_name, template_context,
                                  RequestContext(request))
Пример #4
0
def new(request):
    if request.method == 'GET':
        form = AttachmentForm()
        content_type = request.GET['content_type']
        object_id = request.GET['object_id']
        next = request.GET['next']

    elif request.method == 'POST':
        data = request.POST
        next = data['next']

        form = AttachmentForm(data = data, files = request.FILES)
        if form.is_valid():
            form.save()
            info_msg(request, u"Pièce jointe ajoutée avec succès.")
            return redirect(next)
        else:
            content_type = data['content_type']
            object_id = data['object_id']

    return render(request, "attachments/form.html", {
        'form': form,
        'content_type': content_type,
        'object_id': object_id,
        'next': next
    })
Пример #5
0
def add_attachment(request, app_label, model_name, pk,
                   template_name='attachments/add.html', extra_context={}):

    model = apps.get_model(app_label, model_name)
    if model is None:
        return HttpResponseRedirect(next)
    obj = get_object_or_404(model, pk=pk)

    next = request.POST.get('next', obj.get_absolute_url())
    if not request.POST:
        form = AttachmentForm()
    else:
        form = AttachmentForm(request.POST, request.FILES)

    if form.is_valid():
        form.save(request, obj)
        messages.success(request, ugettext('Your attachment was uploaded.'))
        return HttpResponseRedirect(next)
    else:
        template_context = {
            'form': form,
            'form_url': add_url_for_obj(obj),
            'next': next,
        }
        template_context.update(extra_context)
        return render(request, template_name, template_context)
def upload_file(request):
    if request.method == 'POST':
    	form = AttachmentForm(request.POST, request.FILES)
    	if form.is_valid():
    		attach = form.save()
    		return JsonResponse({'success': attach.id})
    	return JsonResponse({'errors': form.errors}, status=400)
    return HttpResponseNotAllowed(['POST'])
def create(request):
    form = AttachmentForm(request.POST)
    if form.is_valid():
        attachment = form.save()
        return JsonResponse({
            'success': True,
            'attachment ID': attachment.id
        })
    return JsonResponse({'errors': form.errors}, status=400)
Пример #8
0
def attachments_create(request):
    if request.POST:
        form = AttachmentForm(request.POST, request.FILES)
        if form.is_valid():
            attachment = form.save(commit=False)
            attachment.save()
            return redirect(attachment)
    else:
        form = AttachmentForm()

    return render_to_response('attachments/create.html', RequestContext(request, {
        'form': form,
        'subtitle': 'Upload attachments'
    }))
Пример #9
0
def attachments_edit(request, id):
    attachment = get_object_or_404(Attachment, pk=id)

    if not attachment.can_edit(request.user):
        return HttpResponseForbidden()

    if request.POST:
        form = AttachmentForm(request.POST, request.FILES, instance=attachment)
        if form.is_valid():
            attachment = form.save(commit=False)
            attachment.save()
            return redirect(attachment)
    else:
        form = AttachmentForm(instance=attachment)

    return render_to_response('attachments/edit.html', RequestContext(request, {
        'form': form,
        'subtitle': 'Edit attachments'
    }))
Пример #10
0
def add_attachment(request,
                   app_label,
                   module_name,
                   pk,
                   template_name='attachments/add.html',
                   extra_context={}):
    ajax = request.is_ajax

    next = request.POST.get('next', '/')
    model = get_model(app_label, module_name)
    if model is None:
        return HttpResponseRedirect(next)
    obj = get_object_or_404(model, pk=pk)
    form = AttachmentForm(request.POST, request.FILES)

    if form.is_valid():
        form.save(request, obj)
        message = _('Your attachement was uploaded.')
        if ajax:
            return _json_response(request, _("Success"), message)
        messages.add_message(request, messages.SUCCESS, message)
        return HttpResponseRedirect(next)
    else:
        template_context = {
            'form': form,
            'form_url': add_url_for_obj(obj),
            'next': next,
        }
        template_context.update(extra_context)

        if ajax:
            json_context = {
                'form_html': render_to_string(template_name, template_context)
            }
            return _json_response(request, _("Error"),
                                  _("Please correct the form errors."),
                                  json_context)

        return render_to_response(template_name, template_context,
                                  RequestContext(request))
Пример #11
0
def new_attachment(request, content_type, object_id):
    object_type = get_object_or_404(ContentType, id = int(content_type))
    try:
        object = object_type.get_object_for_this_type(pk=int(object_id))
    except object_type.DoesNotExist:
        raise Http404
    if request.method == "POST":
        attachment_form = AttachmentForm(request.POST, request.FILES)
        if attachment_form.is_valid():
            attachment = attachment_form.save(commit=False)
            attachment.content_type = object_type
            attachment.object_id = object_id
            attachment.attached_by = request.user
            attachment.save()
            return HttpResponseRedirect(object.get_absolute_url())
    else:
        attachment_form = AttachmentForm()
    
    return render_to_response("attachments/new_attachment.html", {
        "attachment_form": attachment_form,
        "object": object
    }, context_instance=RequestContext(request))
Пример #12
0
def add_attachment(request, app_label, model_name, pk,
                   template_name='attachments/add.html', extra_context={}):
    
    # when using jquery.forms some browsers must use an iFrame to submit the form data
    # when using this frame HTTP_X_REQUESTED_WITH is not set correctly
    # adding isajaxrequest to your jquery.form options can get around this.
    # var options = {'data': { isajaxrequest: 'true' } }; 
    ajax = request.is_ajax() or 'isajaxrequest' in request.POST

    next = request.POST.get('next', '/')
    model = apps.get_model(app_label, model_name)
    if model is None:
        return HttpResponseRedirect(next)
    obj = get_object_or_404(model, pk=pk)
    form = AttachmentForm(request.POST, request.FILES)

    if form.is_valid():
        form.save(request, obj)
        message = _('Your attachement was uploaded.')
        if ajax:
            return _json_response(request, _("Success"), message)
        messages.add_message(request, messages.SUCCESS, message)
        return HttpResponseRedirect(next)
    else:
        template_context = {
            'form': form,
            'form_url': add_url_for_obj(obj),
            'next': next,
        }
        template_context.update(extra_context)
        
        if ajax:
            json_context = {
                            'form_html': render_to_string(template_name, template_context)
                            }
            return _json_response(request, _("Error"), _("Please correct the form errors."), json_context)
        
        return render_to_response(template_name, template_context,
                                  RequestContext(request))
Пример #13
0
def new_attachment(request, content_type, object_id):
    object_type = get_object_or_404(ContentType, id=int(content_type))
    try:
        object = object_type.get_object_for_this_type(pk=int(object_id))
    except object_type.DoesNotExist:
        raise Http404
    if request.method == "POST":
        attachment_form = AttachmentForm(request.POST, request.FILES)
        if attachment_form.is_valid():
            attachment = attachment_form.save(commit=False)
            attachment.content_type = object_type
            attachment.object_id = object_id
            attachment.attached_by = request.user
            attachment.save()
            return HttpResponseRedirect(object.get_absolute_url())
    else:
        attachment_form = AttachmentForm()

    return render_to_response("attachments/new_attachment.html", {
        "attachment_form": attachment_form,
        "object": object
    },
                              context_instance=RequestContext(request))
Пример #14
0
def add_attachment(request, app_label, module_name, pk,
                   template_name='attachments/add.html', extra_context={}):

    next = request.POST.get('next', '/')
    model = get_model(app_label, module_name)
    if model is None:
        return HttpResponseRedirect(next)
    obj = get_object_or_404(model, pk=pk)
    form = AttachmentForm(request.POST, request.FILES)

    if form.is_valid():
        form.save(request, obj)
        request.user.message_set.create(message=ugettext('Your attachment was uploaded.'))
        return HttpResponseRedirect(next)
    else:
        template_context = {
            'form': form,
            'form_url': add_url_for_obj(obj),
            'next': next,
        }
        template_context.update(extra_context)
        return render_to_response(template_name, template_context,
                                  RequestContext(request))
Пример #15
0
def add_project(request):
    if request.method == 'POST':
        form1 = CreateProjectForm(request.POST)
        input_data = request.POST.copy()
        if form1.is_valid():
            obj1 = sql_util.save_entry_to_db(form1, input_data)
            pk = obj1.id
            app_label = 'pages'
            model_name = 'Project'
            if request.user.has_perm("attachments.add_attachment"):
                model = apps.get_model(app_label, model_name)
                obj = get_object_or_404(model, pk=pk)
                files = request.FILES.getlist('attachment_file')
                images = request.FILES.getlist('attachment_image')
                if len(files) > 0:
                    for f in files:
                        test = MultiValueDict({'attachment_file': [f]})
                        form = AttachmentForm(request.POST, test)
                        if form.is_valid():
                            form.save(request, obj)
                if len(images) > 0:
                    for i in images:
                        test = MultiValueDict({'attachment_image': [i]})
                        form = ImageForm(request.POST, test)
                        if form.is_valid():
                            form.save(request, obj)
            if 'undertaking' in input_data:
                form2 = CreateReferenceProjectForm(request.POST)
                if form2.is_valid():
                    obj2 = sql_util.save_ref_to_db(form2, obj1)
            return HttpResponseRedirect(reverse('success'))
        else:
            input_data = request.POST.copy()
            attachment_model = Attachment(pk=1)  # tää on viel kyssäri
            image_model = Image(pk=1)
            context = {
                'form1':
                CreateProjectForm(request.POST),
                'form2':
                CreateReferenceProjectForm(request.POST),
                'attachment':
                attachment_model,
                'image':
                image_model,
                'filters':
                sql_util.get_filters(),
                'selected_filters':
                parse_util.filters_qs_to_dict(
                    parse_util.parse_input_filters(input_data)),
                'is_reference':
                'undertaking' in input_data
            }
            return render(request, 'add_project.html', context)
    elif request.method == 'GET':
        form1 = CreateProjectForm()
        form2 = CreateReferenceProjectForm()
        attachment_model = Attachment(pk=1)  # tää on viel kyssäri
        image_model = Image(pk=1)
        context = {
            'form1': form1,
            'form2': form2,
            'attachment': attachment_model,
            'image': image_model,
            'filters': sql_util.get_filters(),
        }
        return render(request, 'add_project.html', context)
Пример #16
0
def edit_project(request, id):
    project = models.Project.project_db.get(id=id)
    if request.method == 'POST':
        form1 = CreateProjectForm(request.POST)
        if form1.is_valid():
            input_data = request.POST.copy()
            sql_util.edit_entry_in_db(project, form1, input_data)
            app_label = 'pages'
            model_name = 'Project'
            if request.user.has_perm("attachments.add_attachment"):
                model = apps.get_model(app_label, model_name)
                obj = get_object_or_404(model, pk=id)
                files = request.FILES.getlist('attachment_file')
                images = request.FILES.getlist('attachment_image')
                if len(files) > 0:
                    for f in files:
                        test = MultiValueDict({'attachment_file': [f]})
                        form = AttachmentForm(request.POST, test)
                        if form.is_valid():
                            form.save(request, obj)
                        #else invalid
                if len(images) > 0:
                    existing_images = Image.objects.attachments_for_object(id)
                    for image in existing_images:
                        delete_image(request, image.pk)
                    for i in images:
                        test = MultiValueDict({'attachment_image': [i]})
                        form = ImageForm(request.POST, test)
                        if form.is_valid():
                            form.save(request, obj)
                        #else invalid
            print(input_data)
            if 'undertaking' in input_data:
                form2 = CreateReferenceProjectForm(request.POST)
                print(form2.is_valid())
                if form2.is_valid():
                    sql_util.edit_ref_in_db(project.referenceproject, form2)
                #else invalid - look at what happens when form1 is not valid
            return HttpResponseRedirect(reverse(success))
        else:
            input_data = request.POST.copy()
            attachment_model = Attachment(pk=1)  # tää on viel kyssäri
            image_model = Image(pk=1)
            context = {
                'form1':
                CreateProjectForm(request.POST),
                'form2':
                CreateReferenceProjectForm(request.POST),
                'attachment':
                attachment_model,
                'image':
                image_model,
                'id':
                id,
                'filters':
                sql_util.get_filters(),
                'selected_filters':
                parse_util.filters_qs_to_dict(
                    parse_util.parse_input_filters(input_data))
            }
            return render(request, 'edit_project.html', context)

    elif request.method == 'GET':
        f1, f2, f3, f4, f5 = sql_util.get_filters()
        filters_qset = project.filters.all()
        filters_dict = parse_util.filters_qs_to_dict(filters_qset)
        form1 = CreateProjectForm(
            initial={
                'project_name':
                project.project_name,
                'destination_name':
                '' if project.destination_name ==
                '—' else project.destination_name,
                'start_date':
                parse_util.format_time(project.start_date),
                'end_date':
                parse_util.format_time(project.end_date),
                'keywords':
                '' if project.keywords == '—' else project.keywords,
                'project_description':
                project.project_description,
                'documentation_path':
                '' if project.documentation_path ==
                '—' else project.documentation_path,
                'project_manager':
                '' if project.project_manager ==
                '—' else project.project_manager,
            })
        try:
            form2 = CreateReferenceProjectForm(
                initial={
                    'undertaking':
                    project.referenceproject.undertaking,
                    'client':
                    project.referenceproject.client,
                    'area':
                    project.referenceproject.area,
                    'construction_cost':
                    project.referenceproject.construction_cost,
                    'project_accepted':
                    parse_util.format_time(
                        project.referenceproject.project_accepted),
                    'construction_permit_granted':
                    parse_util.format_time(
                        project.referenceproject.construction_permit_granted),
                })
        except ObjectDoesNotExist:
            form2 = False
        attachment_model = Attachment(pk=1)
        image_model = Image(pk=1)
        context = {
            'form1':
            form1,
            'form2':
            form2,
            'attachment':
            attachment_model,
            'image':
            image_model,
            'id':
            id,
            'filters':
            sql_util.get_filters(),
            'selected_filters':
            parse_util.filters_qs_to_dict(project.filters.all())
        }
        return render(request, 'edit_project.html', context)