def sell_stock_request(): if request.headers.get('Authorization') == API_KEY: req_json = json.loads(request.get_data(as_text=True)) stock_id = req_json['stock_id'] customer_id = req_json['customer_id'] no_stocks = req_json['no_stocks'] stock = db.session.query(models.Stock).filter_by(id=stock_id).first() customer = db.session.query( models.Customer).filter_by(id=customer_id).first() customer_stock = db.session.query(models.CustomerStocks).filter_by( stock_id=stock_id, customer_id=customer_id).first() if customer_stock: if customer_stock.quantity >= no_stocks: value = db.session.query( models.StockValues).filter_by(stock_id=stock_id).order_by( desc(models.StockValues.date_add)).first() stock_request = models.Request(type=bin(0), stock=stock, customer=customer, no_stocks=no_stocks, value=value) db.session.add(stock_request) db.session.flush() db.session.commit() return {"response": stock_request.id} else: return {"response": -2} else: return {"response": -1} return {"response": -400}
def save_request(uuid, req): files = [] for name, fs in request.files.items(): dst = tempfile.NamedTemporaryFile() fs.save(dst) dst.flush() filesize = os.stat(dst.name).st_size dst.close() files.append({ 'name': name, 'filename': fs.filename, 'filesize': filesize, 'mimetype': fs.mimetype, 'mimetype_params': fs.mimetype_params }) r = models.Request(uuid=uuid, endpoint=req.endpoint, method=req.method, url=req.url, data=req.data.decode("utf-8"), headers=dict(req.headers), args=req.args, form=req.form, remote_addr=req.remote_addr, files={i: item for i, item in enumerate(files)}) db.session.add(r) db.session.commit()
def buy_stock_request(): if request.headers.get('Authorization') == API_KEY: req_json = json.loads(request.get_data(as_text=True)) stock_id = req_json['stock_id'] customer_id = req_json['customer_id'] no_stocks = req_json['no_stocks'] stock = db.session.query(models.Stock).filter_by(id=stock_id).first() print(str(stock.curr_no)) if stock.curr_no: curr_no = stock.curr_no else: curr_no = stock.init_no print("requested stocks: " + str(curr_no) + ":" + str(no_stocks) + str(int(curr_no) >= int(no_stocks))) if int(curr_no) >= int(no_stocks): customer = db.session.query( models.Customer).filter_by(id=customer_id).first() value = db.session.query( models.StockValues).filter_by(stock_id=stock_id).first() stock_request = models.Request(type=bin(1), stock=stock, customer=customer, no_stocks=no_stocks, value=value) db.session.add(stock_request) db.session.flush() db.session.commit() return {"response": stock_request.id} else: return {"response": -2} return {"response": -400}
def request_register_submit(request): new_req = models.Request() new_req.poster = request.user new_req.budget = request.POST.get('price', 0) new_req.move_day = request.POST.get('move_date', 0) new_req.prefer_city = request.POST.get('city', 0) new_req.save() return HttpResponseRedirect(reverse('user_requests'))
def store_request(book, uuid): """Store uuid(hash value) and other parameters to Db""" md5_uuid = hashlib.md5(uuid).hexdigest() md5_request = hashlib.md5(book.Id + book.library_id + book.email).hexdigest() md5_book = hashlib.md5(book.Id + book.library_id).hexdigest() request = models.Request(book_id=book.Id, library=book.library_id, email=book.email, md5_request=md5_request, md5_book=md5_book, md5_uuid=md5_uuid) db.session.add(request) db.session.commit()
def requestform(WHICH): form = Request() # import pdb;pdb.set_trace() if form.validate_on_submit(): print 'submit' # import pdb;pdb.set_trace() p = models.Request(email=g.user.email, username=g.user.name, jobTitle=form.jobTitle.data, deadlinedate=form.deadlinedate.data, emanio=form.emanio.data, MHorSUD=form.MHorSUD.data, keyQuestions=form.keyQuestions.data, problem=form.problem.data, specialFacts=form.specialFacts.data, requestedBy=form.requestedBy.data, priority=form.priority.data, timeframe=form.timeframe.data, timeBreakdown=form.timeBreakdown.data, specialPop=form.specialPop.data, agency=form.agency.data, ru=form.ru.data, specialInstructions=form.specialInstructions.data, typeOfService=form.typeOfService.data, timeframestart=form.timeframestart.data, timeframeend=form.timeframeend.data, longDescription=form.longDescription.data, requestDate=datetime.datetime.utcnow(), audience=form.audience.data, columnsRequired=form.columnsRequired.data, deadlinetime=form.deadlinetime.data, deadlineWhy=form.deadlineWhy.data) db.session.add(p) db.session.commit() return redirect(url_for('followup')) else: flash('validation fail') if WHICH == '1': print 'short!!' return render_template("short.html", email=g.user.email, name=g.user.name, form=form) else: return render_template("long.html", email=g.user.email, name=g.user.name, form=form)
def index(): date_seven_days_ago = (datetime.datetime.now() - datetime.timedelta(days=7)).date() current_form = CurrentDataForm(prefix="current_form") crop_form = CropSelectorForm(prefix="crop_form") if current_form.submit_current.data and current_form.validate_on_submit(): if (valid_email(current_form.email.data) and is_json(current_form.fieldpoly.data)): if (valid_field(current_form.fieldpoly.data)): if (models.User.query.filter(models.User.email == current_form. email.data).first() == None): u = models.User(timestamp=datetime.datetime.utcnow(), email=current_form.email.data) db.session.add(u) else: u = models.User.query.filter( models.User.email == current_form.email.data).first() rq = models.Request( body=current_form.fieldpoly.data, timestamp=datetime.datetime.utcnow(), crop_type='None', season='Now!', processed=False, author=u, request_queued=False) #crop_type="Winterwheat" db.session.add(rq) db.session.commit() flash('Data is going to be send to: %s' % current_form.email.data) #flash('GOT="%s", email=%s' % (form.fieldpoly.data, crop_form.email.data)) else: flash( "Not a valid JSON polygon!!! Either the area is to large/small or you have multiple areas" ) else: if (is_json(current_form.fieldpoly.data)): flash('Not a valid email:%s' % current_form.email.data) else: flash('BROKEN JSON SYNTAX! Did you copy it correctly') return redirect('/') elif crop_form.crops_submit.data and crop_form.validate_on_submit(): if (valid_email(crop_form.email.data) and is_json(crop_form.fieldpoly.data)): if (valid_field(crop_form.fieldpoly.data)): if (models.User.query.filter(models.User.email == crop_form. email.data).first() == None): u = models.User(timestamp=datetime.datetime.utcnow(), email=crop_form.email.data) db.session.add(u) else: u = models.User.query.filter( models.User.email == crop_form.email.data).first() rq = models.Request( body=crop_form.fieldpoly.data, timestamp=datetime.datetime.utcnow(), crop_type=crop_form.crop_type.data, season=crop_form.season.data, processed=False, author=u, request_queued=False) #crop_type="Winterwheat" db.session.add(rq) db.session.commit() flash('Data is going to be send to: %s' % crop_form.email.data) #flash('GOT="%s", email=%s' % (form.fieldpoly.data, crop_form.email.data)) else: flash( "Not a valid JSON polygon!!! Either the area is to large/small or you have multiple areas" ) else: if (is_json(crop_form.fieldpoly.data)): flash('Not a valid email:%s' % crop_form.email.data) else: flash('BROKEN JSON SYNTAX! Did you copy it correctly') return redirect('/') return render_template( 'login.html', title='Fieldbabel service for sentinel data', current_form=current_form, crop_form=crop_form, requests=models.Request.query.filter( models.Request.processed == False).order_by( models.Request.id).all(), peformed=models.Request.query.filter( models.Request.processed == True, models.Request.timestamp >= date_seven_days_ago).order_by( models.Request.id).all(), html=HTMLParser())