def upload(request): if request.method == "POST": if request.is_ajax(): upload = request is_raw = True try: filename = request.GET['qqfile'] except KeyError: return HttpResponseBadRequest("AJAX request not valid") else: is_raw = False if len(request.FILES) == 1: upload = request.FILES.values()[0] else: raise Http404("Bad Upload") filename = upload.name filename = create_filename(filename) success = save_upload(upload, filename, is_raw) if success: image_o = "%s/pin/temp/o/%s" % (MEDIA_ROOT, filename) image_t = "%s/pin/temp/t/%s" % (MEDIA_ROOT, filename) pin_image.resize(image_o, image_t, 99) ret_json = {'success': success, 'file': filename} return HttpResponse(json.dumps(ret_json))
def upload(request): if request.method == "POST": if request.is_ajax(): upload = request is_raw = True try: filename = request.GET["qqfile"] except KeyError: return HttpResponseBadRequest("AJAX request not valid") else: is_raw = False if len(request.FILES) == 1: upload = request.FILES.values()[0] else: raise Http404("Bad Upload") filename = upload.name # str = "%f" % time.time() # str = str.replace('.', '') # filename = "%s%s" % (str, os.path.splitext(filename)[1]) filename = create_filename(filename) # save the file success = save_upload(upload, filename, is_raw) if success: image_o = "%s/pin/temp/o/%s" % (MEDIA_ROOT, filename) image_t = "%s/pin/temp/t/%s" % (MEDIA_ROOT, filename) pin_image.resize(image_o, image_t, 99) ret_json = {"success": success, "file": filename} return HttpResponse(json.dumps(ret_json))
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')
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')
def sendurl(request): if request.method == "POST": post_values = request.POST.copy() tags = post_values['tags'] post_values['tags']=tags[tags.find("[")+1:tags.find("]")] form = PinForm(post_values) print post_values, form if form.is_valid(): model = form.save(commit=False) image_url= model.image print image_url #image_url="http://image.sinajs.cn/newchart/daily/n/%s.gif" % image_url filename = image_url.split('/')[-1] #str = "%f" % time.time() #str = str.replace('.', '') #filename = "%s%s" % (str, os.path.splitext(filename)[1]) filename = create_filename(filename) #filename = "%s%s" % (str, filename) image_on = "%s/pin/images/o/%s" % ( MEDIA_ROOT, filename) urllib.urlretrieve(image_url, image_on) model.image = "pin/images/o/%s" % (filename) model.timestamp = time.time() model.user = request.user model.save() form.save_m2m() return HttpResponseRedirect('/pin/') else: form = PinForm() return render_to_response('pin/sendurl.html',{'form':form}, context_instance=RequestContext(request))
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')
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")
def sendurl(request): if request.method == "POST": post_values = request.POST.copy() tags = post_values['tags'] post_values['tags'] = tags[tags.find("[") + 1:tags.find("]")] form = PinForm(post_values) if form.is_valid(): model = form.save(commit=False) image_url = model.image filename = image_url.split('/')[-1] #str = "%f" % time.time() #str = str.replace('.', '') #filename = "%s%s" % (str, os.path.splitext(filename)[1]) filename = create_filename(filename) #filename = "%s%s" % (str, filename) image_on = "%s/pin/images/o/%s" % (MEDIA_ROOT, filename) urllib.urlretrieve(image_url, image_on) model.image = "pin/images/o/%s" % (filename) model.timestamp = time.time() model.user = request.user model.save() form.save_m2m() return HttpResponseRedirect('/pin/') else: form = PinForm() return render_to_response('pin/sendurl.html', {'form': form}, context_instance=RequestContext(request))
def sendurl(request): if request.method == "POST": post_values = request.POST.copy() tags = post_values['tags'] post_values['tags'] = tags[tags.find("[") + 1:tags.find("]")] form = PinForm(post_values) if form.is_valid(): model = form.save(commit=False) image_url = model.image filename = image_url.split('/')[-1] filename = create_filename(filename) image_on = "%s/pin/images/o/%s" % (MEDIA_ROOT, filename) urllib.urlretrieve(image_url, image_on) model.image = "pin/images/o/%s" % (filename) model.timestamp = time() model.user = request.user model.save() form.save_m2m() if model.status == 1: msg = 'مطلب شما با موفقیت ارسال شد. <a href="%s">مشاهده</a>' %\ reverse('pin-item', args=[model.id]) messages.add_message(request, messages.SUCCESS, msg) elif model.status == 0: msg = 'مطلب شما با موفقیت ارسال شد و بعد از تایید در سایت نمایش داده می شود ' messages.add_message(request, messages.SUCCESS, msg) return HttpResponseRedirect('/pin/') else: form = PinForm() return render(request, 'pin/sendurl.html', {'form': form})
def sendurl(request): if request.method == "POST": post_values = request.POST.copy() tags = post_values["tags"] post_values["tags"] = tags[tags.find("[") + 1 : tags.find("]")] form = PinForm(post_values) if form.is_valid(): model = form.save(commit=False) image_url = model.image filename = image_url.split("/")[-1] # str = "%f" % time.time() # str = str.replace('.', '') # filename = "%s%s" % (str, os.path.splitext(filename)[1]) filename = create_filename(filename) # filename = "%s%s" % (str, filename) image_on = "%s/pin/images/o/%s" % (MEDIA_ROOT, filename) urllib.urlretrieve(image_url, image_on) model.image = "pin/images/o/%s" % (filename) model.timestamp = time.time() model.user = request.user model.save() form.save_m2m() return HttpResponseRedirect("/pin/") else: form = PinForm() return render_to_response("pin/sendurl.html", {"form": form}, context_instance=RequestContext(request))
def sendurl(request): if request.method == "POST": post_values = request.POST.copy() tags = post_values['tags'] post_values['tags'] = tags[tags.find("[")+1:tags.find("]")] form = PinForm(post_values) if form.is_valid(): model = form.save(commit=False) image_url = model.image filename = image_url.split('/')[-1] filename = create_filename(filename) image_on = "%s/pin/images/o/%s" % (MEDIA_ROOT, filename) urllib.urlretrieve(image_url, image_on) model.image = "pin/images/o/%s" % (filename) model.timestamp = time() model.user = request.user model.save() form.save_m2m() if model.status == 1: msg = 'مطلب شما با موفقیت ارسال شد. <a href="%s">مشاهده</a>' %\ reverse('pin-item', args=[model.id]) messages.add_message(request, messages.SUCCESS, msg) elif model.status == 0: msg = 'مطلب شما با موفقیت ارسال شد و بعد از تایید در سایت نمایش داده می شود ' messages.add_message(request, messages.SUCCESS, msg) return HttpResponseRedirect('/pin/') else: form = PinForm() return render(request, 'pin/sendurl.html', {'form': form})