Python PinDirectForm Example

说明

python pindirectform示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Python

命名空间/包名称: pinforms

示例#1
文件: views_device.py项目: boontime/django-pin

def post_send(request):
    user = check_auth(request)
    if not user:
        return HttpResponseForbidden('error in user validation')

    if request.method != 'POST':
        return HttpResponseBadRequest('bad request post')

    form = PinDirectForm(request.POST, request.FILES)
    if form.is_valid():
        upload = request.FILES.values()[0]
        filename = create_filename(upload.name)
        try:
            with BufferedWriter(FileIO("%s/pin/images/o/%s" % (MEDIA_ROOT, filename), "wb")) as dest:
                for c in upload.chunks():
                    dest.write(c)
                model = Post()
                model.image = "pin/images/o/%s" % (filename)
                model.user = user
                model.timestamp = time.time()
                model.text = form.cleaned_data['description']
                model.category_id = form.cleaned_data['category']
                model.device = 2
                model.save()
                return HttpResponse('success')
        except IOError:
            return HttpResponseBadRequest('error')

        return HttpResponseBadRequest('bad request in form')
    else:
        HttpResponseBadRequest('error in form validation')

    return HttpResponseBadRequest('bad request')

示例#2
文件: views.py项目: kharazi/django-pin

def d_send(request):
    token = request.GET.get("token", "")

    if not token:
        return HttpResponse("error in user validate")

    try:
        api = ApiKey.objects.get(key=token)
        user = api.user
    except ApiKey.DoesNotExist:
        return HttpResponse("error in user validate")

    if request.method == "POST" and user.is_active:
        form = PinDirectForm(request.POST, request.FILES)
        if form.is_valid():
            upload = request.FILES.values()[0]
            filename = create_filename(upload.name)

            try:
                from io import FileIO, BufferedWriter

                with BufferedWriter(FileIO("%s/pin/images/o/%s" % (MEDIA_ROOT, filename), "wb")) as dest:
                    for c in upload.chunks():
                        dest.write(c)

                    ## after upload we need to save model
                    model = Post()
                    model.image = "pin/images/o/%s" % (filename)
                    model.user = api.user
                    model.timestamp = time.time()
                    model.text = form.cleaned_data["description"]
                    model.save()

                    return HttpResponse("success")
            except IOError:
                # could not open the file most likely
                return HttpResponse("error")

展开阅读全文