def init_data_of_tables(): """ 从JSON文件初始化表数据 :return: None """ from utils import add_url_rows with open('films.json', encoding="utf-8") as f: film_list = json.load(f) for film_info in film_list: film = Film(id=film_info["id"], title=film_info["title"], description=film_info["description"], director=film_info["director"], producer=film_info["producer"], release_date=film_info["release_date"], rt_score=film_info["rt_score"], url=film_info["url"]) session.add(film) location_list = film_info["locations"] add_url_rows('Location', film.id, location_list) people_list = film_info["people"] add_url_rows('People', film.id, people_list) specie_list = film_info["species"] add_url_rows('Specie', film.id, specie_list) vehicle_list = film_info["vehicles"] add_url_rows('Vehicle', film.id, vehicle_list) session.commit()
def setInfo(frameid, **kwdargs): locks.frame.acquire() rb = False try: try: #session.begin(subtransactions=True) rb = True fr_rec = getFrame(frameid) # NOTE: No type check on values! for name, val in kwdargs.items(): setattr(fr_rec, name, val) # Commit! session.commit() rb = False # TODO: "expunge" temp records from session? except dbError, e: raise g2dbError(e) finally: try: if rb: try: session.rollback() except Exception, e: # TODO: log exception print str(e) finally: locks.frame.release()
def newOption(category_name): log = str(login_session.has_key('username')) category = get_cat_by_name(category_name) if request.method == 'POST': # check if required fields are filled if request.form['name'] == '' or request.form['description'] == '': warning = "Fill both name and description to add option" return render_template('new-option.html', category=category, categories=get_cats(), log=log, warning=warning) # create new option else: user_id = login_session['user_id'] anotherOption = Option(name=request.form['name'], description=request.form['description'], cat_id=category.id, user_id=user_id) session.add(anotherOption) session.commit() return redirect(url_for('category', category_name=category.name)) else: return render_template('new-option.html', category=category, categories=get_cats(), log=log)
def deleteOption(category_name, option_id): option = get_option_by_id(option_id) delete_name = option.name session.delete(option) session.commit() flash(delete_name + ' has been deleted.') return redirect(url_for('category', category_name=category_name))
def createUser(login_session): ''' register the user and return the user id''' newUser = User(name=login_session['username'], email=login_session[ 'email']) session.add(newUser) session.commit() user = session.query(User).filter_by(email=login_session['email']).one() return user.id
def add_resources(cls, owner_uri, references): all = [] for ref in references: all.append(session.merge(cls(owner_uri, ref))) session.commit() print('[ OK ] Inserted {} items to [{}].'.format( len(all), cls.__tablename__)) return all
def delete_film(): _id = request.form.get('id') if _id: film = session.query(Film).filter(Film.id == _id).delete() session.commit() return {"status": "0", "message": "电影 " + _id + " 删除成功"} return {"status": "1", "message": "电影编号无效"}
def deleteLink(category_name, option_id, link_id): link = get_link_by_id(link_id) delete_name = link.title session.delete(link) session.commit() flash(delete_name + ' has been deleted.') return redirect( url_for('editOption', category_name=category_name, option_id=option_id))
def drop_table(name: str): """ 从数据库删除表 :param name: 表名称 :return: None """ if name: session.execute('DROP TABLE IF EXISTS ' + name) session.commit()
def resetCount(insname, frtype, prefix, count): """Reset a frame counter. Returns the frame id corresponding to the reset. Parameters: insname -- instrument name, short form (e.g. 'MOIRCS', 'SPCAM', etc.) frtype -- frame type (e.g. 'A' or 'Q') prefix -- prefix for frame following the frametype should be '0'==normal, '7'==engineering, '8'==simulator, '9'==summit (defaults to '0') count -- desired count USE WITH CAUTION!!! """ locks.frame.acquire() rb = False try: #session.begin(subtransactions=True) rb = True # Get FrameMap entry (ins_rec, frmap_rec) = _get_framemap_name(insname, frtype, prefix=prefix) # Reset count in FrameMap table frmap_rec.count = count d = {'code': ins_rec.code, 'type': frtype, 'pfx': prefix, 'num': count} frameid = str(frame_template % d) # Commit! session.commit() rb = False # TODO: "expunge" temp records from session? return frameid finally: try: if rb: try: session.rollback() except Exception, e: # TODO: log exception print str(e) finally: locks.frame.release()
def addLink(category_name, option_id): log = str(login_session.has_key('username')) user_id = login_session['user_id'] option = get_option_by_id(option_id) f = request.form if f['link'] == '' or f['url'] == '': flash('You need both Link name and Url to add a new link') else: link = Link(title=f['link'], url=f['url'], option_id=option.id) session.add(link) session.commit() return redirect( url_for('editOption', category_name=category_name, option_id=option_id))
def loads(cls, jsondata): # Create accounts accounts = cls.add_resources(jsondata['users']) # Initial add reference Reference.add_resources(accounts) # Bind user account to provider accounts # spotify_acc = SpotifyAccount.query.filter().first() for acc in SpotifyAccount.query.filter().all(): user_acc = acc # # -> It's critical here we use app account's URI as real_uri # because we want the User Account to be the real existence. session.merge(Reference(acc, user_acc.uri, 1)) session.commit()
def addTransfer(frameid, **paramDict): locks.frame.acquire() rb = False try: try: #session.begin(subtransactions=True) rb = True # Get Frame entry fr_rec = getFrame(frameid) # Add entry to FrameTransfer table xfer_rec = FrameTransfer(frame=fr_rec, **paramDict) # save to session #xfer_rec.save() # Commit! session.commit() rb = False # TODO: "expunge" temp records from session? return xfer_rec except dbError, e: raise g2dbError(e) finally: try: if rb: try: session.rollback() except Exception, e: # TODO: log exception print str(e) finally: locks.frame.release()
def add_film(): _id = request.form.get('id') _title = request.form.get('title') _description = request.form.get('description') _director = request.form.get('director') _producer = request.form.get('producer') _release_date = request.form.get('release_date') _rt_score = request.form.get('rt_score') _url = request.form.get('url') hlog.var('_id', _id) hlog.var('_title', _title) hlog.var('_description', _description) hlog.var('_director', _director) hlog.var('_producer', _producer) hlog.var('_release_date', _release_date) hlog.var('_rt_score', _rt_score) hlog.var('_url', _url) old_film = session.query(Film).filter(Film.id == _id).all() if old_film: return {"status": "1", "message": "电影编号已经存在"} film = Film(id=_id, title=_title, description=_description, director=_director, producer=_producer, release_date=_release_date, rt_score=_rt_score, url=_url) session.add(film) session.commit() return {"status": "0", "message": "新增成功"}
def alloc(insname, frtype, count=1, prefix='0', propid=None, handling=0, memo=None): """Get one or more frames. Returns a list of the frame ids. Parameters: insname -- instrument name, short form (e.g. 'MOIRCS', 'SPCAM', etc.) frtype -- frame type (e.g. 'A' or 'Q') count -- number of desired frames (defaults to 1) prefix -- prefix for frame following the frametype should be '0'==normal, '7'==engineering, '8'==simulator, '9'==summit (defaults to '0') propid -- proposal id this frame was allocated under """ locks.frame.acquire() rb = False try: try: #session.begin(subtransactions=True) rb = True # Get FrameMap entry (ins_rec, frmap_rec) = _get_framemap_name(insname, frtype, prefix=prefix) fr_num = frmap_rec.count d = {'code': ins_rec.code, 'type': frtype, 'pfx': prefix, 'num': 0} # This is the time the frames were allocated alloc_time = datetime.datetime.now() # Construct frame list frames = [] for i in xrange(fr_num, fr_num + count): d['num'] = i frameid = str(frame_template % d) frames.append(frameid) # Add frame to Frames table frame = Frame(framemap=frmap_rec, number=i, time_alloc=alloc_time, propid=propid, handling=handling, memo=memo) # save to session #frame.save() # Update count in FrameMap table frmap_rec.count += count # Commit! session.commit() rb = False # TODO: "expunge" temp records from session? return frames except dbError, e: raise g2dbError(e) finally: try: if rb: try: session.rollback() except Exception, e: # TODO: log exception print str(e) finally: locks.frame.release()