def new_meal_save(): status, msg = True, '' b_name = request.args.get('name') s = Session() b = Meal(b_name) for key, val in request.args.items(): if key == 'name': continue a = s.query(Product).filter_by(id=key).first() b.products.append(a) s.add(b) try: s.commit() except IntegrityError: status = False msg = 'The meal with name %s already exists' % b_name s.close() return json.dumps({'ok': status, 'msg': msg})
def do_command (task_id) : try : task_res = get_task_by_id(task_id) if task_res["status"] != 0 : return {"status":-1 , "val":None} task = task_res["val"]["task"] group_id = script_group = task_res["val"]["script_group"].id server_group = task_res["val"]["server_group"] scripts = get_scripts_by_group_id(group_id)["val"] script_list = [] for script in scripts : script_list.append(str(script.location)) server_list = [] for server in server_group.servers : server_list.append(server.dump()) if script_list == [] or server_list == [] : return {"status":-1 , "val":None} task_to_do = {"scripts":script_list , "servers":server_list} success_array , key_code_list = upload_remotefile.do_task(task_to_do) session = Session() taskStatus = session.query(TaskStatus).filter(TaskStatus.task_id == task_id).first() if taskStatus == None : taskStatus = TaskStatus(task_id , json.dumps(success_array) , json.dumps(key_code_list)) taskStatus_id = taskStatus.save_return_id(session) else : taskStatus.success_array = json.dumps(success_array) taskStatus.key_code_list = json.dumps(key_code_list) taskStatus_id = taskStatus.id taskStatus.save(session) session.close() return {"status":0 , "val":taskStatus_id} except Exception , msginfo : return {"status":-1 , "val":msginfo}
def insert_db_weather(weather_values): """Function for inserting scraped data from Weather API into database""" session = Session() new_data = Weather( coord_lon=weather_values["coord"]["lon"], coord_lat=weather_values["coord"]["lat"], weather_id=weather_values["weather"][0]["id"], weather_main=weather_values["weather"][0]["main"], weather_description=weather_values["weather"][0]["description"], weather_icon=weather_values["weather"][0]["icon"], base=weather_values["base"], main_temp=weather_values["main"]["temp"], main_pressure=weather_values["main"]["pressure"], main_humidity=weather_values["main"]["humidity"], main_temp_min=weather_values["main"]["temp_min"], main_temp_max=weather_values["main"]["temp_max"], visibility=weather_values["visibility"], wind_speed=weather_values["wind"]["speed"], wind_deg=weather_values["wind"]["deg"], clouds_all=weather_values["clouds"]["all"], dt=datetime.fromtimestamp(weather_values["dt"]), sys_type=weather_values["sys"]["type"], sys_id=weather_values["sys"]["id"], sys_message=weather_values["sys"]["message"], sys_country=weather_values["sys"]["country"], sys_sunrise=datetime.fromtimestamp(weather_values["sys"]["sunrise"]), sys_sunset=datetime.fromtimestamp(weather_values["sys"]["sunset"]), city_id=weather_values["id"], city_name=weather_values["name"], cod=weather_values["cod"]) session.add(new_data) session.commit() session.close()
def draw_table(self): s = Session() self.orders = s.query(Order).all() s.close() self.ui.orders_table.clear() self.ui.orders_table.setRowCount(1) self.ui.orders_table.setColumnCount(5) self.ui.orders_table.setHorizontalHeaderLabels([QString.fromUtf8('Номер'), QString.fromUtf8('Поломка'), QString.fromUtf8('Дата приемки'), QString.fromUtf8('Клиент'), QString.fromUtf8('Статус')]) #self.ui.orders_table.resizeColumnsToContents() for order in self.orders: data = [] data.append(str(order.id)) data.append(QString.fromUtf8(order.device)) data.append(str(order.get_ordered_date())) data.append(QString.fromUtf8(order.get_client().get_fio())) data.append(QString.fromUtf8(order.get_status(1).to_string())) for i in range(0,5): tableitem = QTableWidgetItem() tableitem.setText(data[i]) tableitem.font = QFont("Arial", 10) tableitem.font.setBold(True) tableitem.textcolor = QColor("black") self.ui.orders_table.setItem(self.ui.orders_table.rowCount() - 1,i,tableitem) self.ui.orders_table.setRowCount(self.ui.orders_table.rowCount()+1) self.ui.orders_table.resizeColumnsToContents()
def create_default_user(): session = Session() default_user = User('admin', 'password1234', '*****@*****.**') session.add(default_user) session.commit() session.close() return
def initialize_device(): """ Set up the Device info into the database. By this time, the HardwareDefinition and SoftwareDefinition entries must be entered. """ session = Session() hd = session.query(HardwareDefinition).one() sd = session.query(SoftwareDefinition).one() device = Device(id=hd.serial_number, interior_sensor=hd.interior_sensor, exterior_sensor=hd.exterior_sensor) device.hardware_version = hd.hardware_version device.software_version = sd.software_version device.database_service = True # database always set to True device.device_service = sd.device_service device.grainbin_service = sd.grainbin_service session.add(device) # set grainbin info if sd.grainbin_service: grainbins = initialize_grainbin(device.id, hd.grainbin_reader_count) device.grainbin_count = len(grainbins) session.add_all(grainbins) session.commit() session.close() return
def get(self): try: session = Session() uuid = request.headers["GreenList-User-Id"] user = UserController().get_user_by_id(uuid, session) if not user: abort(401) products = ( SuggestionController().get_suboptimal_products(session)) result = [] for product in products: product_json = self.product_to_json(product) suggestions = SuggestionController().get_suggestions( user, product, session) alternatives = [ self.suggestion_to_json(suggestion) for suggestion in suggestions ] product_json.update({'alternatives': alternatives}) result.append(product_json) return result finally: session.close()
def main_page(): s = Session() books = s.query(Book).all() authors = s.query(Author).all() s.close() return render_template('beta.html', books=books, authors=authors)
def book_save(_id): status, msg = True, '' b_name = request.args.get('name') s = Session() b = s.query(Book).filter_by(id=_id).first() del b.authors[:] for key, val in request.args.items(): if key == 'name': continue a = s.query(Author).filter_by(id=key).first() b.authors.append(a) b.name = b_name try: s.commit() except IntegrityError: status = False msg = 'The book with name %s already exists' % b_name s.close() return json.dumps({'ok': status, 'msg': msg})
def main_page(): s = Session() meals = s.query(Meal).all() products = s.query(Product).all() s.close() return render_template('beta.html', meals=meals, products=products)
def get_station(): session = Session() stations = [] rows = session.execute("select * from station;") for row in rows: stations.append(dict(row)) session.close() return jsonify(stations=stations)
def login(self): s = Session() res = s.query(Staff).filter_by(login=unicode(self.ui.login.text()), passwd=unicode(self.ui.password.text())).all() if len(res): self.mv = MainWindow(res[0]) self.mv.show() self.close() s.close()
def draw_client_combo(self): combo = self.ui.client s = Session() clients = s.query(Client).all() s.close() combo.clear() for cl in clients: combo.addItem('%i %s %s'%(cl.id, cl.surname, cl.name)) #QObject.connect(self.ui.manufacter_combo, SIGNAL("currentIndexChanged(int)"), self.setManufacter) #self.setManufacter(0)
def save_item(self, item): '''写数据库''' session = Session() try: session.add(Item(**item)) session.commit() except Exception as e: logger.warning('页面 {} 的数据写入数据库出错,错误原因{}'.format(item['link'], e)) session.rollback() session.close()
def mul(): session = Session() # select the rows that doesn't have result for datarow in session.query(ClientData).all(): if not datarow.result: datarow.result = mul_data( datarow.rawData) # add result to the data row session.add(datarow) # update the datarow in the DB session.commit() # commit all changes to DB session.close()
def add(proxy): session = Session() count = session.query(Proxy).filter(Proxy.ip == proxy.ip, Proxy.port == proxy.port).count() if 0 == count: proxy.createtime = int(time.time()) session.add(proxy) session.commit() logger.info("add proxy: %s" % proxy) else: logger.info("exists proxy: %s" % proxy) session.close()
def remove_book(): b_id = request.args.get('id') s = Session() b = s.query(Book).filter_by(id=b_id).first() s.delete(b) s.commit() s.close() return json.dumps({'ok': True, 'msg': ''})
def get_user_by_id(self, user_id, session=None): own_session = False if not session: session = Session() own_session = True user = session.query(User).filter( User.greenlist_uuid == user_id).one_or_none() if own_session: session.close() return user
def get_station_available_info(station_id): session = Session() available_info = [] rows = session.execute("select available_bike_stands, available_bikes from bike \ where station_id = {} and last_update = \ (select max(last_update) from bike where station_id = {});".format(station_id, station_id)) for row in rows: print(row) available_info.append(dict(row)) session.close() return jsonify(available_info=available_info)
def add_order(self): s = Session() ord = Order() ord.manager_id = self.parent.user.id ord.device = unicode(self.ui.device.text()) ord.description = unicode(self.ui.description) ord.client_id = s.query(Client).filter_by(id=unicode(self.ui.client.itemText(self.ui.client.currentIndex())).split()[0]).one().id s.add(ord) s.commit() s.close() self.close()
def get_suboptimal_products(self, session=None): own_session = False if not session: session = Session() own_session = True products = (session.query(Product).filter( or_(Product.id == 1, Product.id == 3)).all()) if own_session: session.close() return products
def remove_author(): a_id = request.args.get('id') s = Session() a = s.query(Author).filter_by(id=a_id).first() s.delete(a) s.commit() s.close() return json.dumps({'ok': True, 'msg': ''})
def add_purchase(self, user, product, quantity, session=None): own_session = False if not session: session = Session() own_session = True purchase = Purchase(user, product, quantity) session.add(purchase) if own_session: session.commit() session.close()
def get_product(self, product_id, session=None): own_session = False if not session: session = Session() own_session = True product = (session.query(Product).filter( Product.id == product_id).first()) if own_session: session.close() return product
def get_list(uuid): session = Session() resp = [] user = session.query(User).filter(User.greenlist_uuid == uuid).one_or_none() for elem in session.query(UserList).filter(UserList.user_id == user.id).all(): resp.append({ "productId": elem.product.id, "name": elem.product.name, "footprint": elem.product.footprint, "image": elem.product.image }) session.close() return resp
def get_challenge(uuid): session = Session() user = session.query(User).filter( User.greenlist_uuid == uuid).one_or_none() if user is None: raise Exception("There is no user [id={}]".format(uuid)) resp = { "startTime": str(user.challenge_start_ts), "endTime": str(user.challenge_end_ts), "score": ChallengeController.score_calc(user) } session.close() return resp
def edit_meal(_id): s = Session() b = s.query(Meal).filter_by(id=_id).first() current_products = b.products products = s.query(Product).all() other_products = [] for a in products: if a not in b.products: other_products.append(a) s.close() return render_template('edit_meal.html', meal=b, other_products=other_products, current_products=current_products)
def edit_book(_id): s = Session() b = s.query(Book).filter_by(id=_id).first() current_authors = b.authors authors = s.query(Author).all() other_authors = [] for a in authors: if a not in b.authors: other_authors.append(a) s.close() return render_template('edit_book.html', book=b, other_authors=other_authors, current_authors=current_authors)
def score_calc(user): try: session = Session() score = 0 score_product_list = session.query(UserList, Purchase).join( Purchase, Purchase.product_id == UserList.product_id).filter( UserList.user_id == user.id).filter( Purchase.user_id == user.id).all() for product in score_product_list: score += product[0].trees_difference * product[1].quantity session.close() return round(score) except Exception as e: return e
def add_provider(db_enabled, lastname, firstname, credentials, addr1, addr2, city, zipcode, state,latitude, longitude): # if db is enabled, then open a session with the database if db_enabled: session = Session() # create an instance of the Provider type provider = Provider(lastname=lastname, firstname=firstname, credentials=credentials, addr1=addr1, addr2=addr2, city=city, zipcode=zipcode,state=state, latitude=latitude, longitude=longitude) #To check if the record already exists in database p = select([Provider.firstname]).where(Provider.firstname+Provider.lastname+Provider.credentials +Provider.addr1+Provider.addr2 == firstname+lastname+credentials+addr1+addr2) res = session.execute(p) prov_data = res.fetchone() session.close() #To check if record exists, then this step will be skipped if not(prov_data): session = Session() # To fetch the Geographical coordinates from Zip_geotable z = select([Zip_geo.latitude, Zip_geo.longitude]).where(Zip_geo.zipcode == zipcode) result = session.execute(z) geo_data = result.fetchone() if geo_data: latitude = geo_data.latitude longitude= geo_data.longitude #print db_enabled, lastname, firstname, credentials, addr1, addr2, city, zipcode, state,latitude, longitude # create an instance of the Provider type provider = Provider(lastname=lastname, firstname=firstname, credentials=credentials, addr1=addr1, addr2=addr2, city=city, zipcode=zipcode,state=state, latitude=latitude, longitude=longitude) # if db is enabled, then add to the recordset and commit the txn session.add(provider) session.commit() session.close() return provider
def add_zipdata(db_enabled, zipcode, latitude, longitude): # if db is enabled, then open a session with the database if db_enabled: session = Session() # create an instance of the Zip_Geo type zip_geo= Zip_geo(zipcode=zipcode, latitude=latitude, longitude=longitude) # if db is enabled, then add to the recordset and commit the txn if db_enabled: session.add(zip_geo) session.commit() session.close() return zip_geo
def rep_generacional(): session = Session() representantes = [ as_dict(representante) for representante in session.query(Representante).all() ] for indice, representante in enumerate(representantes): asistencias = limpiar_asistencias( session.query(Asistencias).filter( Asistencias.nombre_representante == representante["nombre"]).all()) representantes[indice]["asistencias"] = asistencias session.close() return representantes
def add_to_list(uuid, product_id, trees_difference): msg = "Success" try: session = Session() user = session.query(User).filter(User.greenlist_uuid == uuid).one_or_none() if session.query(UserList).filter( UserList.user_id == user.id).filter(UserList.product_id == product_id).first() is None: product = session.query(Product).filter(Product.id == product_id).one_or_none() product_to_user = UserList(user, product, trees_difference) session.add(product_to_user) session.commit() session.close() except Exception as e: msg = e finally: return msg
def create_challenge(uuid, duration): msg = "Success" try: session = Session() user = User(greenlist_uuid=uuid, challenge_start_ts=datetime.utcnow(), challenge_end_ts=datetime.utcnow() + timedelta(days=duration * 30), challenge_score=0) session.add(user) session.commit() session.close() except Exception as e: msg = e finally: return msg
def weather_to_db(data): session = Session() weather = json.loads(data) print(weather) print(type(weather), len(weather)) timestamp_dt = weather.get('dt') print(timestamp_dt) time_standard_dt = timestamp_convert(timestamp_dt) timestamp_sunrise = weather.get('sys').get('sunrise') time_standard_surise = timestamp_convert(timestamp_sunrise) timestamp_sunset = weather.get('sys').get('sunset') time_standard_sunset = timestamp_convert(timestamp_sunset) kwargs ={ 'coord_lon':weather.get('coord').get('lon'), 'coord_lat':weather.get('coord').get('lat'), 'weather_id':weather.get('weather')[0]['id'], 'weather_main':weather.get('weather')[0]['main'], 'weather_description': weather.get('weather')[0]['description'], 'weather_icon':weather.get('weather')[0]['icon'], 'base':weather.get('base'), 'main_temp': weather.get('main').get('temp'), 'main_feels_like':weather.get('main').get('feels_like'), 'main_temp_max':weather.get('main').get('temp_max'), 'main_temp_min': weather.get('main').get('temp_min'), 'main_pressure':weather.get('main').get('pressure'), 'main_humidity': weather.get('main').get('humidity'), 'visibility': weather.get('visibility'), 'wind_speed': weather.get('wind').get('speed'), 'wind_deg': weather.get('wind').get('speed'), 'clouds_all': weather.get('clouds').get('all'), 'dt':time_standard_dt, 'sys_type':weather.get('sys').get('type'), 'sys_id': weather.get('sys').get('id'), 'sys_country': weather.get('sys').get('country'), 'sys_sunrise':time_standard_surise, 'sys_sunset':time_standard_sunset, 'timezone':weather.get('timezone'), 'city_id':weather.get('id'), 'name': weather.get('name'), 'cod':weather.get('cod'), } row_weather = Weather(**kwargs) session.add(row_weather) session.commit() session.close() return
def count_distinct(db_enabled): # if db is enabled, then open a session with the database if db_enabled: session = Session() # if db is enabled, then query the database and w results = session.query(label ('Zipcode', Provider.zipcode), label('Count', func.count(Provider.firstname+Provider.lastname+Provider.credentials))).group_by(Provider.zipcode).order_by('"Count" desc').all() session.close() # Write the output to CSV file for result in results: write_csv(result) print 'Data saved to output.csv'
def new_author_save(): status, msg = True, '' a_name = request.args.get('name') s = Session() s.add(Author(a_name)) try: s.commit() except IntegrityError: status = False msg = 'The author with name %s already exists' % a_name s.close() return json.dumps({'ok': status, 'msg': msg})
def post(self): try: req = request.get_json() user_id = req['userId'] product_id = req['productId'] quantity = req['quantity'] session = Session() user = UserController().get_user_by_id(user_id, session) product = ProductController().get_product(product_id, session) PurchasesController().add_purchase(user, product, quantity, session) finally: session.commit() session.close()
def co2_difference_to_trees(self, user, original, alternative, session=None): own_session = False if not session: session = Session() own_session = True challenge_months = ( UserController().get_challenge_duration_in_months(user)) tree_co2_equivalent = TREE_CO2_GRAMS_PER_MONTH * challenge_months saved_trees = (original - alternative) / tree_co2_equivalent if own_session: session.close() return saved_trees
def insert_db_bikes(values): """Function for inserting scraped data from Bikes API into database""" fields = [ 'status', 'bike_stands', 'available_bike_stands', 'available_bikes' ] session = Session() for data in values: station = session.query(Station).get(data['number']) # checking if the timestamp is greater than the last update to ensure no duplicates are added to the DB if datetime.fromtimestamp( data['last_update'] / 1000) > station.last_updated: new_data = UsageData(**{field: data[field] for field in fields}) new_data.dt_last_update = data['last_update'] station.station_usage.append(new_data) session.commit() session.close()
def get_coordinates(zipcode , miles): session = Session() # To fetch the Geographical coordinates from Zip_geotable z = select([Zip_geo.latitude, Zip_geo.longitude]).where(Zip_geo.zipcode == zipcode) result = session.execute(z) #print result geo_data = result.fetchone() #If zipcode exists in database if geo_data: latitude = geo_data.latitude longitude= geo_data.longitude patient_loc = geopy.Point(latitude,longitude ) #print latitude,longitude find_distance(patient_loc, miles) else: print 'Zipcode not found. Please enter a valid zipcode' session.close()
def suvan(): s = Session() try: pictures = s.query(Picture).order_by(desc(Picture.date_uploaded)).all() d = OrderedDict() for p in pictures: dstr = p.date_uploaded.strftime('%B %d, %Y') if not d.has_key(dstr): d[dstr] = {} d[dstr]['date'] = dstr d[dstr]['age'] = getAge(p.date_uploaded) d[dstr]['pictures'] = [] d[dstr]['pictures'].append(p) return render_template('suvan.html', pictures=d.values()) finally: s.close()
def get_coordinates(zipcode, miles): session = Session() # To fetch the Geographical coordinates from Zip_geotable z = select([Zip_geo.latitude, Zip_geo.longitude]).where(Zip_geo.zipcode == zipcode) result = session.execute(z) #print result geo_data = result.fetchone() #If zipcode exists in database if geo_data: latitude = geo_data.latitude longitude = geo_data.longitude patient_loc = geopy.Point(latitude, longitude) #print latitude,longitude find_distance(patient_loc, miles) else: print 'Zipcode not found. Please enter a valid zipcode' session.close()
def stations_to_db(data): session = Session() stations = json.loads(data) print(type(stations), len(stations)) for station in stations: timestamp = station.get('last_update') time_standard = timestamp_convert(timestamp) kwargs = {'station_id': int(station.get('number')), 'bike_stands': int(station.get('bike_stands')), 'available_bike_stands': int(station.get('available_bike_stands')), 'available_bikes': int(station.get('available_bikes')), 'status': station.get('status'), 'last_update': time_standard } row_bike = Bike(**kwargs) session.add(row_bike) session.commit() session.close() return
def search_meal_go(): b_pattern = request.args.get('meal_pattern') a_pattern = request.args.get('product_pattern') s = Session() if not a_pattern: meals = s.query(Meal).filter(Meal.name.like('%' + b_pattern + '%')) else: meals = s.query(Meal).filter(and_(Meal.name.like('%' + b_pattern + '%'), \ Meal.products.any(Product.name.like('%' + a_pattern + '%')))) out = [] for b in meals: out.append({'id': b.id, 'name': b.name}) s.close() return json.dumps(out)
def author_save(_id): status, msg = True, '' a_name = request.args.get('name') s = Session() a = s.query(Author).filter_by(id=_id).first() a.name = a_name try: s.commit() except IntegrityError: status = False msg = 'The author with name %s already exists' % a_name s.close() return json.dumps({'ok': status, 'msg': msg})
def search_book_go(): b_pattern = request.args.get('book_pattern') a_pattern = request.args.get('author_pattern') s = Session() if not a_pattern: books = s.query(Book).filter(Book.name.like('%' + b_pattern + '%')) else: books = s.query(Book).filter(and_(Book.name.like('%' + b_pattern + '%'), \ Book.authors.any(Author.name.like('%' + a_pattern + '%')))) out = [] for b in books: out.append({'id': b.id, 'name': b.name}) s.close() return json.dumps(out)
def __save_exercicio(mdex, id_parlamentar, id_mandato): exercicio = Exercicio() exercicio.codigo = mdex['CodigoExercicio'] exercicio.parlamentar_id = id_parlamentar exercicio.mandato_id = id_mandato exercicio.inicio = datetime.strptime(mdex['DataInicio'], self.__fmt) if 'DataFim' in mdex.keys(): exercicio.fim = datetime.strptime(mdex['DataFim'], self.__fmt) exercicio.sigla_afastamento = mdex['SiglaCausaAfastamento'] exercicio.causa_afastamento = mdex['DescricaoCausaAfastamento'] session = Session() session.add(exercicio) session.commit() session.close()
def main(): logger.setLevel(logging.INFO) handler = logging.FileHandler('../log/crawler.log') formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) contest_list = ['abc002'] with Crawler() as crawler: scraper = Scraper(crawler) for contest in contest_list: db_session = Session() try: # scraper.crawl_results(contest, db_session) scraper.crawl_contest_by_id(contest, db_session) except Exception as e: db_session.rollback() raise e finally: db_session.commit() db_session.close()
def system_setup_init(status): """ Create a SystemSetup entry in the database. Specify if the device is standalone configuration (status=True) or alongside a FarmMonitor (status=False) """ session = Session() system_setup = SystemSetup() if status == 'True': system_setup.standalone_configuration = True else: system_setup.standalone_configuration = False session.add(system_setup) session.commit() initialize_routes(session) session.close() return
def get_suggestions(self, user, product, session=None): own_session = False if not session: session = Session() own_session = True already_in_list = (session.query(UserList).filter( UserList.user_id == user.id).all()) already_in_list = set(p.product_id for p in already_in_list) results = [] bigger_sizes = (session.query( Product, ProductSize).filter(ProductSize.smallest_id == product.id).filter( Product.id == ProductSize.product_id).filter( Product.id != product.id).all()) for alternative, product_size in bigger_sizes: multiplier = product_size.multiplier trees_difference = (TreesController().co2_difference_to_trees( user, product.footprint * multiplier, alternative.footprint)) if alternative.id not in already_in_list: suggestion = Suggestion(alternative, trees_difference, multiplier) results.append(suggestion) same_class = (session.query(Product).filter( Product.product_cl_id == product.product_cl_id).filter( Product.id != product.id).all()) for alternative in same_class: trees_difference = (TreesController().co2_difference_to_trees( user, product.footprint, alternative.footprint)) if alternative.id not in already_in_list: suggestion = Suggestion(alternative, trees_difference) results.append(suggestion) if own_session: session.close() return results
def new_meal(): s = Session() products = s.query(Product).all() s.close() return render_template('edit_meal.html', other_products=products)
def edit_author(_id): s = Session() a = s.query(Author).filter_by(id=_id).first() s.close() return render_template('edit_author.html', author=a)
def edit_product(_id): s = Session() a = s.query(Product).filter_by(id=_id).first() s.close() return render_template('edit_product.html', product=a)
def new_book(): s = Session() authors = s.query(Author).all() s.close() return render_template('edit_book.html', other_authors=authors)