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 update_post(db): stocks_queryset=db.get_stocks_list() stock_list=list(stocks_queryset.order_by('gradient')) local_path_name=dpin.settings.MEDIA_ROOT+"/" superuser_id=User.objects.filter(is_superuser=True)[0].id for x in stock_list: cal_date=db.get_latest_date(x.stock_code) if cal_date == None: print "not data, ignore stock:",x.stock_code add = Post.objects.filter(pk=x.stock_code) if add.exists(): add.delete() continue add = Post.objects.filter(pk=x.stock_code) if add.count(): temp=add[0] if temp.create != cal_date: try: os.remove(local_path_name+'stock_pics/'+str(x.stock_code)+'-'+temp.create.strftime("%Y-%m-%d")+'.svg') except OSError: pass temp.create=cal_date temp.rating=x.gradient temp.text=("%06d %s %f\n" % (x.stock_code, x.stock_name, x.gradient))+'date:'+cal_date.strftime('%Y-%m-%d') temp.save() else: add = Post(image="", url='', stock_code=x.stock_code, rating=x.gradient, create=cal_date, text=("%06d %s %f\n" % (x.stock_code, x.stock_name, x.gradient))+'date:'+cal_date.strftime('%Y-%m-%d'), user_id=superuser_id) add.save()
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")
import sys import os import time sys.path.append("../") from pin.models import Post for dirname, subdirs, files in os.walk('../pin/media/pin/stock_pics'): #print "f =",files #print "dir =",dirname #print "sub =", subdirs for f in files: add = Post(image=('stock_pics/%s' % f),url=('/media/stock_pics/%s' % f) , timestamp=int(time.time()),user_id=12) add.save()