我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用app.db()。
def removeServices(serviceid): logging.info('Remove the service %s', serviceid) try: docker_client = docker.from_env() docker_remove = docker_client.services.get(serviceid) docker_remove.remove() remove_ser = models.Service.query.all() for i in remove_ser: if (i.serviceid == serviceid): db.session.delete(i) db.session.commit() break except docker.errors.APIError as e: if e.status_code == 404: remove_ser = models.Service.query.all() for i in remove_ser: if (i.serviceid == serviceid): db.session.delete(i) db.session.commit() break else: logging.error('Unable to remove the service %s. \nReason: %s', serviceid, str(e))
def deleteImage(image_name): logging.info('Delete the image %s', image_name) try: docker_client = docker.from_env() registry_imagename = registry + '/' + image_name docker_client.images.remove(image=registry_imagename,force=True) image = models.Image.query.filter_by(imagename=image_name).first() db.session.delete(image) db.session.commit() except docker.errors.APIError as e: image = models.Image.query.filter_by(imagename = image_name).first() db.session.delete(image) db.session.commit() error_string = 'Unable to delete the image {}. \nReason: {}. Delete the record'.format(registry_imagename, str(e)) logging.error(error_string) return error_string return None
def signup(): from forms import SignupForm form = SignupForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data.lower()).first() if user is not None: form.email.errors.append("The Email address is already taken.") return render_template('signup.html', form=form) newuser = User(form.firstname.data,form.lastname.data,form.email.data,form.password.data) db.session.add(newuser) db.session.commit() session['email'] = newuser.email return redirect(url_for('login')) return render_template('signup.html', form=form)
def ping(service_id): from app import db, models from models import Service finding = Service.query.filter_by(serviceid=service_id).first() if finding is not None: image_name = finding.imagename uploadn = finding.uploadname usern = finding.username firstcreatetime = finding.firstcreatetime u = Service(serviceid = service_id, createdtime = str(time.time()), imagename = image_name, uploadname = uploadn, username = usern, firstcreatetime = firstcreatetime) db.session.add(u) db.session.commit() db.session.delete(finding) db.session.commit() else: return "The service "+service_id+" has been removed!" return "There are existing service:"+service_id
def put(self): """Change the password""" us = User.query \ .filter(User.disabled == 0) \ .filter(User.id_user == g.current_user) \ .first() abort_if_none(us, 404, 'User not found') if not check_password_hash(us.password, request.json['old_password']): return msg('Old password incorrect'), 403 us.password = request.json['password'] db.session.commit() cache.blacklisted_tokens.append(request.headers['Authorization']) return msg('success!')
def parse_update_optional_args(parser): # just for updating lots parser.add_argument('--limit', "-l", nargs="?", type=int, help="how many jobs to do") parser.add_argument('--chunk', "-ch", nargs="?", default=10, type=int, help="how many to take off db at once") parser.add_argument('--after', nargs="?", type=str, help="minimum id or id start, ie 0000-0001") parser.add_argument('--rq', action="store_true", default=False, help="do jobs in this thread") parser.add_argument('--order', action="store_true", default=True, help="order them") parser.add_argument('--append', action="store_true", default=False, help="append, dont' clear queue") parser.add_argument('--name', nargs="?", type=str, help="name for the thread") # just for updating one parser.add_argument('--id', nargs="?", type=str, help="id of the one thing you want to update") parser.add_argument('--doi', nargs="?", type=str, help="doi of the one thing you want to update") # parse and run parsed_args = parser.parse_args() return parsed_args
def run_update(parsed_args): update = update_registry.get(parsed_args.fn) start = time() #convenience method for handling an doi if parsed_args.doi: from pub import Pub from util import clean_doi my_pub = db.session.query(Pub).filter(Pub.id==clean_doi(parsed_args.doi)).first() parsed_args.id = my_pub.id logger.info(u"Got database hit for this doi: {}".format(my_pub.id)) update.run(**vars(parsed_args)) db.session.remove() logger.info(u"finished update in {} secconds".format(elapsed(start)))
def adduser(): """add user""" from getpass import getpass username = raw_input("\_username: ") email = raw_input("\_email: ") role_id = raw_input("\_[1:moderator 2:admin 3:user]: ") password = getpass("\_password: ") u = User( email = email, username = username, password = password, role_id = role_id ) db.session.add(u) db.session.commit() print "<user %s add in database>" % username
def get_count(): comments = Comment.query.all() for comment in comments: for word in comment.parsed.split('/'): if word in pos_list: comment.pos_count += 1 elif word in neg_list: comment.neg_count += 1 elif '80' <= word and word <= '99': comment.pos_count += 1 elif '0' <= word and word < '80': comment.neg_count += 1 db.session.add(comment) print "Comment %04d counted!" % comment.id db.session.commit() print "ALL DONE!"
def message(): if request.method == 'POST': myid = request.get_json().get("myid") #message list conn = redis.StrictRedis(host='redis', port=6380, db=10) prem = conn.get(myid) if prem is not None: messagelist = pickle.loads(prem) if messagelist == []: return jsonify({ "message":"none" }), 200 else: conn.set(myid,pickle.dumps([])) return jsonify({"messages":messagelist}), 200 else: return jsonify({"message":"notfound"}), 404
def make_shell_context(): return dict(app=app, db=db, User=User, Organization=Organization, Location=Location, Role=Role, Schedule2=Schedule2, Shift2=Shift2, RecurringShift=RecurringShift, RoleToUser=RoleToUser, Preference=Preference, cache=cache, SessionCache=SessionCache, Timeclock=Timeclock, TimeOffRequest=TimeOffRequest, ApiKey=ApiKey)
def reset_password(self, user_id): """ This method reset password of user with specified email and send notification email on it """ user = db.session.query(User).filter_by(id=user_id).first() password = self.generate_password() hashed_password = self.password_to_hash(password) # print password, " --> ", hashed_password user.password = hashed_password # print "Trying to reset password of ", user.full_name db.session.commit() try: send_reset_password_email(user, password) return {'result': 'success'}, 200 except Exception, error: print "EXCEPTION: ", error traceback.print_exc() return {'result': 'error'}, 404
def dashboard_service(): resp = flask.Response(json.dumps({'status': 'failed'})) if request.method == 'GET': result = {} f = db.session.execute(db.session.query(func.count(Formulation.id))).first() result['formulationNumber'] = f[0] if f[0] else 0 t = db.session.execute(db.session.query(func.count(Test.id))).first() result['testNumber'] = t[0] if t[0] else 0 d = db.session.execute(db.session.query(func.count(TestData.id))).first() result['dataNumber'] = d[0] if d[0] else 0 a = db.session.execute(db.session.query(func.count(TestAttachment.id))).first() result['attachmentNumber'] = a[0] if a[0] else 0 result.update({'status': 'success'}) resp = flask.Response(json.dumps(result)) return set_debug_response_header(resp) ############################################################ # dashboard services end # ############################################################ ############################################################ # tests services begin # ############################################################
def adduser(): """add user""" from getpass import getpass username = raw_input("\_username: ") email = raw_input("\_email: ") role_id = raw_input("\_[1:moderator 2:admin 3:user]: ") password = getpass("\_password: ") u = User( email = email, username = username, password = password, role_id = role_id ) db.session.add(u) db.session.commit() print ("<user %s add in database>") % username # ======= # >>>>>>> ec2645b42e8d0b874bf3ca3e57dd7dd3e98d9fb0
def createsuperuser(): """ Method for create super user """ data = _validuser() user = models.User( username=data['username'], email=data['email'], first_name=data['firstname'], last_name=data['lastname'], password=data['password'], is_active=True, is_admin=True ) db.session.add(user) db.session.commit() print("SuperUser created!")
def createuser(): """ Method for create user """ data = _validuser() user = models.User( username=data['username'], email=data['email'], first_name=data['firstname'], last_name=data['lastname'], password=data['password'], is_active=True, is_admin=False ) db.session.add(user) db.session.commit() print("User created!") # Managers commands
def run_update(parsed_args): update = update_registry.get(parsed_args.fn) start = time() #convenience method for handling an doi if parsed_args.doi: from models.doi import Publication my_pub = db.session.query(Publication).filter(Publication.doi==parsed_args.doi).first() parsed_args.id = my_pub.id print u"Got database for this doi: {}".format(my_pub.id) update.run( use_rq=parsed_args.rq, obj_id=parsed_args.id, # is empty unless updating just one row min_id=parsed_args.after, # is empty unless minimum id num_jobs=parsed_args.limit, chunk_size=parsed_args.chunk ) db.session.remove() print "finished update in {}sec".format(elapsed(start))
def index(): # Grab the choice variable passed from POST # for x in request.args['choice']: # print('in args: {}'.format(x)) # hook up database functions Session = sessionmaker() # locate the db location; currently hardcoded to development database engine = create_engine('sqlite:///' + os.path.join(basedir, 'data-dev.sqlite')) Session.configure(bind=engine) session = Session() post_count = session.query(func.count(Post.id)).scalar() # count number of unique posts in the table pic1 = Post.query.get(randint(1, post_count)) # fails if there is 1 or less entries in the database pic2 = None while pic2 == pic1 or pic2 is None: # Don't pick the same file pic2 = Post.query.get(randint(1, post_count)) pic1_filename = url_for('static', filename=pic1.filename) pic2_filename = url_for('static', filename=pic2.filename) return render_template('index.html', pic1=pic1, pic2=pic2, pic1_filename=pic1_filename, pic2_filename=pic2_filename)
def api_get_all(collection): ''' get all documents from a collection ''' if collection not in APIS: abort(400) cursor = db[collection] a = [] for document in cursor.find().sort("created", -1): if collection == 'users': document.update(password='******') document.update(_id=str(document['_id'])) a.append(document) return jsonify({collection:a})
def api_edit_blog(blog_id): ''' edit a blog and post it ''' if not g.__user__.get('admin'): return make_response('Permission denied.', 403) title = request.form.get('title') tag = request.form.get('tag').lstrip(r'/\;,. ').rstrip(r'/\;,. ') content = request.form.get('content') content = content.lstrip('\n').rstrip() db.blogs.update_one( {'_id': ObjectId(blog_id)}, { '$set': { 'title': title.strip(), 'tag': re.split(r'[\s\;\,\.\\\/]+', tag), 'content': content, 'summary': '%s%s' % (content[:140], '...'), 'last_modified': True, 'modified': int(time.time()) } }) return jsonify(blog_id=blog_id)
def api_delete_one(collection, item_id): ''' delete one document from a certain collection ''' if not g.__user__.get('admin'): return make_response('Permission denied.', 403) if collection == 'comments': blog_id = db.comments.find_one({'_id': ObjectId(item_id)}).get('blog_id') db[collection].delete_one({'_id': ObjectId(item_id)}) if collection == 'blogs': db.comments.delete_many({'blog_id': ObjectId(item_id)}) if collection == 'comments': return redirect(url_for('api.api_get_blog_comments', blog_id=blog_id)) return jsonify(item_id=item_id)
def api_delete_subcomment(comment_id, own_id): ''' delete a subcomment from a certain comment ''' if not g.__user__.get('admin'): return make_response('Permission denied.', 403) db.comments.update_one( {'_id': ObjectId(comment_id)}, { '$pull': {'subcontent': {'_id': own_id}} }) if not db.comments.find_one({'_id': ObjectId(comment_id)}).get('subcontent'): db.comments.update_one( {'_id': ObjectId(comment_id)}, { '$set': {'subcomment': False} }) blog_id = db.comments.find_one({'_id': ObjectId(comment_id)}).get('blog_id') return redirect(url_for('api.api_get_blog_comments', blog_id=blog_id))
def make_shell_context(): return dict(app=app, db=db,users=users,backhosts=backhosts,customers=customers,backarchives=backarchives,config=config)
def make_shell_context(): return dict(app = app, db = db, Article = Article, User = User, Site = Site, Packet = Packet)
def getServicePort(image_name): logging.info('Starting a new services with image %s', image_name) try: image = registry+'/'+image_name com_cre_ser = 'docker service create --replicas 1 --publish ' + ':9090 ' + image service_ps = os.popen(com_cre_ser).read().split('\n') service_id = service_ps[0] time.sleep(5) ser_ins = "docker service inspect " + service_id ser_ins_ = json.loads(os.popen(ser_ins).read()) port=ser_ins_[0]["Endpoint"]["Ports"][0]["PublishedPort"] get_node = models.ServerIP.query.first() ip = get_node.serverip except Exception, e: logging.error('Unable to create the service with image %s. \nReason: %s', image_name, str(e)) return logging.info('Store the service infomation to the db') try: imageinfo = models.Image.query.filter_by(imagename = image_name).first() uploadn = imageinfo.uploadname usern = imageinfo.uploaduser service_record = Service(serviceid = service_id, createdtime = str(time.time()), imagename = image_name, uploadname = uploadn, username = usern, firstcreatetime = datetime.now()) db.session.add(service_record) db.session.commit() except Exception, e: logging.error('Failed to store the service info to the db. \nReason: %s', str(e)) return return ip+':'+str(port)+" "+service_id
def setting(): from app.forms import SetForm form = SetForm() if form.validate_on_submit(): servers = models.ServerIP.query.all() for server in servers: db.session.delete(server) db.session.commit() serverip = form.ip.data u = models.ServerIP(serverip = serverip) db.session.add(u) db.session.commit() return render_template('setting.html',form=form, succeed = True) return render_template('setting.html',form=form)
def idetailed(image_name): from app import db, models image = models.Image.query.filter_by(imagename = image_name).first() published_topics = StringToListOfDict(image.published_topics) subscribed_topics = StringToListOfDict(image.subscribed_topics) pub_topic_list = [] sub_topic_list = [] for pub_topic in published_topics: pub_topic_list.append(pub_topic.get("topic_name")) for sub_topic in subscribed_topics: sub_topic_list.append(sub_topic.get("topic_name")) return render_template('idetailed.html',imagename = image.imagename, uploadname = image.uploadname, uploaduser = image.uploaduser, uploadtime = image.uploadtime, subscribed_topics = sub_topic_list, published_topics = pub_topic_list, advertised_services = StringToList(image.advertised_services), advertised_actions = StringToList(image.advertised_actions), comments = image.comments)
def put(self, id): '''Update an event by ID''' ev = Event.query.filter(Event.disabled == 0).filter(Event.id_event == id).first() abort_if_none(ev, 404, 'Not Found') fill_object(ev, request.json) db.session.commit() return msg('success!')
def delete(self, id): '''Delete an event by ID''' ev = Event.query.filter(Event.disabled == 0).filter(Event.id_event == id).first() abort_if_none(ev, 404, 'Not Found') ev.disabled = 1 db.session.commit() return msg('disabled')
def post(self): '''Create a new event''' ev = Event() tags = request.json['tags'][:] # copy the tags dict del request.json['tags'] for tm in tags: t = Tag.query\ .filter(Tag.name == tm['name'])\ .filter(Tag.disabled == 0)\ .first() if t is not None: ev.tags.append(t) continue tag = Tag() fill_object(tag, tm) ev.tags.append(tag) fill_object(ev, request.json) # submit objects to db db.session.add(ev) db.session.commit() return msg(ev.id_event, 'id')
def put(self, id): '''Update an event_type by ID''' et = EventType.query.filter(EventType.disabled == 0).filter(EventType.id_event_type == id) et = et.first() abort_if_none(et, 404, 'Not Found') fill_object(et, request.json) db.session.commit() return msg('altered')
def delete(self, id): '''Delete an event_type by ID''' et = EventType.query.filter(EventType.disabled == 0).filter(EventType.id_event_type == id) et = et.first() abort_if_none(et, 404, 'Not Found') et.disabled = 1 db.session.commit() return msg('disabled')
def put(self, id): """Update an user by ID""" us = User.query\ .filter(User.disabled == 0)\ .filter(User.id_user == id)\ .first() abort_if_none(us, 404, 'not found') fill_object(us, request.json) db.session.commit() return msg('success!')
def delete(self, id): """Delete an user by ID""" us = User.query.filter(User.disabled == 0)\ .filter(User.id_user == id)\ .first() abort_if_none(us, 404, 'not found') us.disabled = 1 db.session.commit() return msg('disabled on db')
def post(self): """Create a new user""" us = User() fill_object(us, request.json) db.session.add(us) db.session.commit() return msg(us.id_user, 'id')
def number_total_on_queue(job_type): num = get_sql_answer(db, "select count(*) from {}".format(table_name(job_type))) return num
def number_waiting_on_queue(job_type): num = get_sql_answer(db, "select count(*) from {} where started is null".format(table_name(job_type))) return num
def number_unfinished(job_type): num = get_sql_answer(db, "select count(*) from {} where finished is null".format(table_name(job_type))) return num
def kick(job_type): q = u"""update {table_name} set started=null, finished=null where finished is null""".format( table_name=table_name(job_type)) run_sql(db, q) print_status(job_type)
def reset_enqueued(job_type): q = u"update {} set started=null, finished=null".format(table_name(job_type)) run_sql(db, q)
def print_idle_dynos(job_type): heroku_conn = heroku3.from_key(os.getenv("HEROKU_API_KEY")) app = heroku_conn.apps()[HEROKU_APP_NAME] running_dynos = [] try: running_dynos = [dyno for dyno in app.dynos() if dyno.name.startswith(process_name(job_type))] except (KeyError, TypeError) as e: pass dynos_still_working = get_sql_answers(db, "select dyno from {} where started is not null and finished is null".format(table_name(job_type))) dynos_still_working_names = [n for n in dynos_still_working] logger.info(u"dynos still running: {}".format([d.name for d in running_dynos if d.name in dynos_still_working_names])) # logger.info(u"dynos stopped:", [d.name for d in running_dynos if d.name not in dynos_still_working_names]) # kill_list = [d.kill() for d in running_dynos if d.name not in dynos_still_working_names]
def add_dois_to_queue_from_file(filename, job_type): start = time() command = """psql `heroku config:get DATABASE_URL`?ssl=true -c "\copy {table_name} (id) FROM '{filename}' WITH CSV DELIMITER E'|';" """.format( table_name=table_name(job_type), filename=filename) call(command, shell=True) q = "update {} set id=lower(id)".format(table_name(job_type)) run_sql(db, q) logger.info(u"add_dois_to_queue_from_file done in {} seconds".format(elapsed(start, 1))) print_status(job_type)
def maint(self, **kwargs): repos = Repository.query.filter(Repository.name==None, Repository.error==None).all() num_to_commit = 0 for my_repo in repos: if not my_repo.name: my_repo.set_repo_info() num_to_commit += 1 db.session.merge(my_repo) logger.info(u"my_repo: {}".format(my_repo)) if num_to_commit >= 1: safe_commit(db) num_to_commit = 0 safe_commit(db)