def send_to_db(self): try: aArtist = dbArtists(name=self.artistname) db_session.merge(aArtist) db_session.commit() except Exception as e: traceback.print_exc()
def on_success(self, data): if 'text' in data: try: (_, tag, name, address) = data['text'].split('\\', 4) result = Geocoder.geocode(address) (lat, lng) = result[0].coordinates formatted_address = result[0].formatted_address feed = self.get_feed(name.upper(), lat, lng) if feed: # Filter dupes logging.debug('Feed exists: %s' % (feed)) if tag.upper() in self.reply_tags.upper(): feed.state = 'closed' feed.last_modified = datetime.now() db_session.merge(feed) db_session.commit() logging.debug('Feed updated: %s' % (feed)) else: f = Feed(name=name.upper(),lat=lat, lng=lng, address=formatted_address) db_session.add(f) db_session.commit() logging.debug('Feed created: %s' % (f)) except (SQLAlchemyError, DatabaseError) as e: logging.error(e) except ValueError as e: pass except (DBAPIError, Exception) as e: logging.error(e) finally: db_session.remove()
def upload_ratings(self): USERFOUND = False # check if user exists, if not upload user... exists = db_session.query(dbUsers.name).first() if exists is not None: rows = dbUsers.query.all() for row in rows: d = object_as_dict(row) logger.debug('Read userid:{}, name:{}'.format(d['userid'], d['name'])) if d['name'] == self.username: # we found the user! Do nothing... USERFOUND = True if USERFOUND == False: # if user not found, must add user to DB aUser = dbUsers(name=self.username) db_session.merge(aUser) db_session.commit() # upload ratings for artistid, rating in self.ratings.items(): logger.debug("Attempting to upload userid:{} artistid:{} rating:{}".format(self.userid, artistid, rating)) try: aRating = dbRatings(userid=self.userid, artistid=artistid, rating=rating) db_session.merge(aRating, load=True) db_session.commit() tot_ratings.add_rating(self.userid, artistid, aRating.ratingid, rating) except Exception as e: traceback.print_exc()
def __init__(self,username): self.ratings = {} # self.userid = userid self.username = username # no longer needs to specify userid ... (will generate as needed) # check if username exists. # If so, use user id... get user ratings... # If not, insert into db... exists = db_session.query(dbUsers.name).first() USERFOUND = False if exists is not None: rows = dbUsers.query.all() for row in rows: d = object_as_dict(row) logger.debug('Read userid:{}, name:{}'.format(d['userid'], d['name'])) if d['name'] == self.username: logger.debug('We found the user! userid:{}, name:{}'.format(d['userid'], d['name'])) self.userid = d['userid'] # need to get all ratings ... self.ratings = self.get_db_ratings(self.userid) USERFOUND = True if USERFOUND == False: aUser = dbUsers(name=self.username) db_session.merge(aUser) db_session.commit() self.userid = aUser.userid logger.debug('initalized userid {}, username {} ratings: {}'.format(self.userid, self.username, self.ratings))
def on_success(self, data): if 'text' in data: try: (_, tag, name, address) = data['text'].split('\\', 4) result = Geocoder.geocode(address) (lat, lng) = result[0].coordinates formatted_address = result[0].formatted_address feed = self.get_feed(name.upper(), lat, lng) if feed: # Filter dupes logging.debug('Feed exists: %s' % (feed)) if tag.upper() in self.reply_tags.upper(): feed.state = 'closed' feed.last_modified = datetime.now() db_session.merge(feed) db_session.commit() logging.debug('Feed updated: %s' % (feed)) else: f = Feed(name=name.upper(), lat=lat, lng=lng, address=formatted_address) db_session.add(f) db_session.commit() logging.debug('Feed created: %s' % (f)) except (SQLAlchemyError, DatabaseError) as e: logging.error(e) except ValueError as e: pass except (DBAPIError, Exception) as e: logging.error(e) finally: db_session.remove()
def removeSecret(key): user = UserDroit.query.filter(UserDroit.username == key).first() if user: user.secret = None db_session.merge(user) db_session.commit() return redirect("/admin/secret/list")
def userinfo(): resp = {'msg':'request params is error', 'flag':'-1'} if not request.json : print resp return jsonify(resp) try: print 'raw requet :', request.json userinfo = UserInfo(request.json) print 'put in db\'s data:', userinfo db_session.merge(userinfo) db_session.commit() resp = {'msg':'post userinfo succeed', 'flag':'0'} except KeyError, e: resp = {'msg':'request params is error, had not "%s"' % e.message, 'flag':'-1'}
def save_secret(): """ Save the new secret in the database """ post_value = request.form current_user = UserDroit.query.filter( UserDroit.username == post_value["username"]).first() if current_user: # If the user is present in the database current_user.secret = post_value["qrcode_secret"] db_session.merge(current_user) db_session.commit() return redirect(url_for("csoGestion.list_secret"))
def get(self, user_id): ''' "get" is the default method for entering the keying page. We do not submit form data across get, only "post." Here, we resume a previously started keying task by checking cache. This is in case the user closed their browser or navigated away during a task. If no task is found in cache, then we select a new task. First, we select something that is on second pass not keyed by the user. If nothing, we select something that is on first pass. ''' #this would be for /key/<user_id> #which is not defined right now. #testing username='******' #this will load cache with user_id; if #there is no active user_id, app #will fail to 401 unauthorized. myid = cache_methods.cacheGetUserID(username=username) #Check permissions here #check to see if we already have a task we should be keying #otherwise, we need to fetch a task from the db. my_current_task = cache_methods.cacheGetAKeyingTask(userid=myid) #if we still don't have a task, that means there's nothing to key #at the moment. if my_current_task is None: flash('There are no valid keying tasks at this time, please check back later') return render_template('index.html') #Turn task into a session object so we can update it, etc. #For testing only at this point. my_current_task = db_session.merge(my_current_task) #testing area #my_current_task.png = '/static/images/demo3.png' #db_session.commit() #cache.set(str(myid)+':current_task',my_current_task,) #determine field type goes here #This information should go in fielddefs table. resource='Recipient' #This information should go in res_ourcedefs table. attributes=['id', 'lname', 'fname', 'mname'] #retrieve field resources my_dict = cache_methods.cacheGetDict(fieldtype=my_current_task.fieldtype_id, resource=resource, attributes=attributes) #flash(my_dict[1]) #return render_template('index.html') return render_template('key.html', kt=my_current_task, auto_dict=my_dict)
def enable_user_2factor(secret): """ Enable for the current user the 2Factor auth :param secret: :return: """ if secret: current_user = UserDroit.query.filter( UserDroit.username == session["username"]).first() if current_user: # If the user is present in the database current_user.secret = secret db_session.merge(current_user) db_session.commit() return True else: return False else: return False
def create_or_update_flashcard(): fc = Flashcard.query.filter(Flashcard.source_word == request.json['sourceWord']).first() if fc is None: new_fc = Flashcard(source_word=request.json['sourceWord'], dest_word=request.json['destWord'], association_image_url=request.json['associationImageUrl'], association_text=request.json['associationText']) db_session.merge(new_fc) else: fc.dest_word = request.json['destWord'] fc.association_image_url = request.json['associationImageUrl'] fc.association_text = request.json['associationText'] db_session.commit() return return_200_json_from_dict(request.json)
def order_handler(msg): """ Update our global Order data response dict """ if msg.typeName in ['orderStatus', 'openOrder']: d = msg_to_dict(msg) g.order_resp[msg.typeName].append(d.copy()) order_msg = g.order_resp_by_order.get( d['orderId'], dict(openOrder=dict(), orderStatus=dict())) order_msg[msg.typeName] = d.copy() g.order_resp_by_order[d['orderId']] = order_msg # Save all filled orders to SQLite DB if msg.typeName == 'orderStatus' and msg.status == 'Filled': filled_order = FilledOrders(msg.id, json.dumps(d)) db_session.merge(filled_order) db_session.commit() log.debug('ORDER: {}'.format(d)) elif msg.typeName == 'openOrderEnd': g.order_resp['openOrderEnd'] = True log.debug('ORDER: {})'.format(msg))
def executions_handler(msg): """ Update our global to keep the latest execDetails available for API returns. https://www.interactivebrokers.com/en/software/api/apiguide/java/execdetails.htm """ if msg.typeName in ['execDetails', 'commissionReport']: d = msg_to_dict(msg) log.debug('Dictified msg: {}'.format(d)) if msg.typeName == 'execDetails': g.executions_resp[msg.typeName].append( dict(execution=d['execution'].copy(), contract=d['contract'].copy())) # Save all CommissionReports to SQLite DB elif msg.typeName == 'commissionReport': commission_report = Commissions(msg.m_execId, json.dumps(d)) db_session.merge(commission_report) db_session.commit() log.debug('EXECUTIONS: {}'.format(d)) elif msg.typeName == 'execDetailsEnd': g.executions_resp['execDetailsEnd'] = True log.debug('EXECUTIONS: {})'.format(msg))
def save(tbl_name): """ Save or update the Post data in the tbl_name """ post_value = request.form tbl_Object = get_tbl_object(tbl_name)() # Fill the object with post data for curHeader in tbl_Object.header: if post_value[curHeader] == "": val = None else: val = post_value[curHeader] setattr(tbl_Object, curHeader, val) # Save the object try: db_session.merge(tbl_Object) db_session.commit() except: db_session.rollback() return redirect(get_listing_redirection(tbl_name))
def cmd_init_hue(msg): # pragma: no cover reply_to_user(msg, "Association avec votre bridge HUE.") reply_to_user( msg, "Pour m’autoriser à dialoguer avec votre bridge HUE, merci d’appuyer sur le bouton présent sur celui-ci." ) # Récupération du « username » depuis le BRIDGE HUE hue_username = create_new_username(HUE_BRIDGE) # Récupération du pseudo de la personne qui parle pseudo = get_username(msg) # Sauvegarde de l’association entre l’username hue et la personne qui parle current_user = save_new_user(pseudo, msg["telegram"]["update"].message.chat.id) current_user.settings.hue_username = hue_username db_session.merge(current_user) db_session.commit() # Retour pour le client reply_to_user( msg, "L’association avec votre brigde est maintenant effectif, vous pouvez utiliser toutes les commandes." )
def get_or_update_user(user): merged_user = db_session.merge(user) db_session.commit() return merged_user
def sessionAdd(obj): try: db_session.merge(obj) db_session.flush() except IntegrityError: db_session.rollback()
def post(self, user_id): ''' This method is used for submission of keying data. First, the submitted task id is check against the cache. If the cache contains a keying task with the user's id, then the form is submitted. Otherwise, the user will be redirected to the index page to log in again. This is to prevent stale data from being submitted. ''' #testing username='******' #this will load cache with user_id; if #there is no active user_id, app #will fail to 401 unauthorized. myid = cache_methods.cacheGetUserID(username=username) # Make sure our answer fields are all present from the form. if (request.form.get('kt_id') is None or request.form.get('pass_number') is None or request.form.get('answer') is None): abort(500, "Invalid form submitted. If you continue to see this error, contact your administrator") # check cache for 'keyingtask:<kt_id>:<pass_number>' = <myid> # This ensure's we're not keying a stale task, as well as preventing spoofing. if (cache.get('keyingtask:'+str(request.form['kt_id'])+':'+str(request.form['pass_number'])) != myid): # if no task / user_id match found in cache, exit to index. cache.delete(str(myid)+':current_task') flash("Invalid Keying Task Submitteda") return render_template('index.html') # if task was found, submit task to rabbitmq, unset user cache and task cache. #in testing, all that happens here. #Ensure we have a current task in cache. my_current_task = cache.get(str(myid)+':current_task') if (my_current_task is None): #No current task means we cannot procedure further. Fall back to index. cache.delete(str(myid)+':current_task') flash("Invalid Keying Task Submitted") return render_template('index.html') #Task is valid, submit message to process results. #-------------RabbitMQ part----------------- #Testing: Make sure warm cache is implemented before offloading db update to rabbit. my_current_task = db_session.merge(my_current_task) if (request.form['pass_number'] == str(1)): my_current_task.firstpass = 2 my_current_answer = KeyingAnswer(kt_id=request.form['kt_id'], firstanswer=request.form['answer']) db_session.add(my_current_answer) else: my_current_task.secondpass = 2 my_current_answer = db_session.query(KeyingAnswer).with_lockmode('update').filter(KeyingAnswer.kt_id == int(request.form['kt_id'])).first() my_current_answer.secondanswer = request.form['answer'] db_session.commit() #-------------End RabbitMQ part----------------- cache.delete(str(myid)+':current_task') cache.delete('keyingtask:'+str(request.form['kt_id'])+':'+str(request.form['pass_number'])) #get new task my_current_task = cache_methods.cacheGetAKeyingTask(userid=myid) if my_current_task is None: flash('There are no valid keying tasks at this time, please check back later') return render_template('index.html') #testing area #my_current_task.png = '/static/images/demo.png' #db_session.commit() #cache.set(str(myid)+':current_task',my_current_task,) #determine field type goes here #This information should go in fielddefs table. resource='Recipient' #This information should go in res_ourcedefs table. attributes=['id', 'lname', 'fname', 'mname'] #retrieve field resources my_dict = cache_methods.cacheGetDict(fieldtype=my_current_task.fieldtype_id, resource=resource, attributes=attributes) #flash(my_dict[1]) #return render_template('index.html') return render_template('key.html', kt=my_current_task, auto_dict=my_dict)
def merge_fake_data(): db_session.merge(Meter(id=0, label='Meter A')) db_session.merge(Meter(id=1, label='Meter B')) db_session.merge(Meter(id=2, label='Meter C')) db_session.merge( MeterData(id=0, meter_id=0, timestamp=datetime.fromordinal(10000), value=10)) db_session.merge( MeterData(id=1, meter_id=0, timestamp=datetime.fromordinal(40000), value=20)) db_session.merge( MeterData(id=2, meter_id=0, timestamp=datetime.fromordinal(20000), value=30)) db_session.merge( MeterData(id=3, meter_id=0, timestamp=datetime.fromordinal(30000), value=10)) db_session.merge( MeterData(id=4, meter_id=1, timestamp=datetime.fromordinal(60000), value=100)) db_session.merge( MeterData(id=5, meter_id=1, timestamp=datetime.fromordinal(50000), value=10)) db_session.commit()
def add_or_update_tweet(tweet): merged_tweet = db_session.merge(tweet) db_session.commit() return merged_tweet
class Brand: def get_data(self): url = 'https://rem.ru/catalog/shini-496/' headers = {} conn = Connector(url, headers) return conn.get_data() def process_data(self, data): result = [] soup = BeautifulSoup(self.get_data().content, features="html.parser") for option in soup.find('select', class_='brand').find_all('option'): if option['value']: result.append({ 'id': option['value'], 'name': option.get_text() }) return result if __name__ == '__main__': brand = Brand() current_datetime = datetime.datetime.now() for item in brand.process_data(): model = CarBrand(id=item['id'], name=item['name'], parse_date=current_datetime) db_session.merge(model) db_session.commit()