def __init__(self): self.database = Initialization().get_global_db() self.helper = database_helper(self.database) debug_msg(">>> %s.%s" % (__name__, sys._getframe().f_code.co_name)) self.uid = Users.query.count() self.nick_name = 'fake_user' + str(self.uid) self.password = '******' + str(self.uid) self.primary_id = Primary_Category.query.count() self.primary_name = 'fake_category' + str(self.primary_id) self.primary_color = '#f23456' self.primary_logo = './logo.png' self.secondary_id = Secondary_Category.query.count() self.secondary_name = 'fake_secondary' + str(self.secondary_id) self.secondary_color = "#b33321" self.secondary_logo = './secondary_logo.png' self.date_id = Date.query.count() self.date_date = datetime.date(2017,3,10) self.date_last_changed_time = datetime.datetime.now() self.blocks_id = Blocks.query.count()
def __init__(self): ''' :) ''' debug_msg(">>> %s.%s" % (__name__, sys._getframe().f_code.co_name)) self.database = Initialization().get_global_db() self.db_helper = database_helper(self.database)
def __init__(self, date_string=None, id=None, user_id=None): debug_msg(">>> %s.%s" % (__name__, sys._getframe().f_code.co_name)) self.date = None if date_string != None: self.date = self.__query_date_from_string(date_string, user_id) if id != None: self.date = self.__query_date_from_id(id, user_id) self.database = Initialization().get_global_db() self.db_helper = database_helper(self.database)
def update_last_changed_date(self, date_id): if date_id is None: return None date_obj = Date.query.filter_by(id=date_id).first() if date_obj is None: return None last_changed_time = datetime.datetime.now().replace(microsecond=0) date_obj.last_changed_time = last_changed_time self.db.session.add(date_obj) debug_msg("committed") return 0
def get_all_Category(self): debug_msg(">>> %s.%s" % (__name__, sys._getframe().f_code.co_name)) ret = copy.deepcopy(self.get_all_Primary_Category()) if ret is None: return None for each_obj in ret: each_obj[ 'second_category'] = self.get_all_Second_Category_from_Primary_id( each_obj['id']) debug_msg(ret) return ret
def global_create_app(self, name): basedir = os.path.abspath(os.path.dirname(__file__)) if (name == None): debug_msg("Warning: no app name!") name = '__main__' app = Flask(name) app.config[ 'SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join( basedir, 'database/data.sqlite') app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True self.app = app return app
def update_a_block(self, block_info): if block_info is None: return None id = block_info['id'] block_obj = Blocks.query.filter_by(id=id).first() if block_obj is None: return None block_obj.second_category_id = block_info['second_category_id'] self.db.session.add(block_obj) self.db.session.commit() self.update_last_changed_date(block_info['date_id']) debug_msg("committed") return 0
def get_Date(self, input_date): debug_msg(">>> %s.%s" % (__name__, sys._getframe().f_code.co_name)) date_info = Date.query.filter_by(date=input_date).first() if date_info is None: debug_msg("cannot find data of this day: %s!" % input_date) return None if Date.query.filter_by(date=input_date).count() > 1: debug_msg("Date conflict!") return None debug_msg("date(%s)==>Date item(id=%s)" % (input_date, date_info.serialize['id'])) return date_info.serialize
def add_a_Second_Category(self, name, color, primary_id): if (name or color or primary_id) is None: return None exist = Second_Category.query.filter_by(name=name).count() if not (exist == 0): new_id = Second_Category.query.filter_by( name=name, primary_id=primary_id).first().serialize['id'] debug_msg("conflit category existed (id = %d),modify it" % new_id) else: new_id = Second_Category.query.count() new_Second_Category = Second_Category(id=new_id, name=name, color=color, primary_id=primary_id) self.db.session.add(new_Second_Category) self.db.session.commit() debug_msg("committed") return 0
def get_Second_Category_from_id(self, id): debug_msg(">>> %s.%s" % (__name__, sys._getframe().f_code.co_name)) info = Second_Category.query.filter_by(id=id).all() if (info == []): debug_msg( "cannot find Second_Category item of this date id %d" % id) return None if len(info) > 1: debug_msg("Second_Category conflict!") return None info = info[0] debug_msg(info.serialize) return info.serialize
def get_block_info_from_date(self, date): debug_msg(">>> %s.%s" % (__name__, sys._getframe().f_code.co_name)) ret = {} if date is None: debug_msg("None value") return None date_dict = self.get_Date(date) if date_dict is None: debug_msg("None value") return None #turn datetime to string date_dict['date'] = date_dict['date'].strftime("%Y-%m-%d") date_dict['last_changed_time'] = date_dict[ 'last_changed_time'].strftime("%Y-%m-%d %H:%M:%S") blocks_list = self.get_Blocks_from_date_id(date_dict['id']) for block in blocks_list: second_category_dict = self.get_Second_Category_from_id( block['second_category_id']) block['second_category'] = second_category_dict ret = copy.deepcopy(date_dict) ret['Blocks'] = copy.deepcopy(blocks_list) return ret
def get_Blocks_from_date_id(self, date_id): debug_msg(">>> %s.%s" % (__name__, sys._getframe().f_code.co_name)) blocks_info = Blocks.query.filter_by(date_id=date_id).all() if blocks_info is None: debug_msg("cannot find data of this date id %d" % date_id) return None ret = [] for block in blocks_info: ret.append(block.serialize) debug_msg("date_id(%s)==>Blocks list(%d)" % (date_id, len(blocks_info))) return ret
def get_all_Primary_Category(self): debug_msg(">>> %s.%s" % (__name__, sys._getframe().f_code.co_name)) info = Primary_Category.query.all() if info is None: debug_msg( "cannot find Primary_Category item of this date id %d" % id) return None ret = [] for each_category in info: ret.append(each_category.serialize) debug_msg(ret) return ret
def get_all_Second_Category_from_Primary_id(self, primary_id): debug_msg(">>> %s.%s" % (__name__, sys._getframe().f_code.co_name)) info = Second_Category.query.filter_by(primary_id=primary_id).all() if info is None: debug_msg( "cannot find Second_Category item of this date id %d" % id) return None ret = [] for each_category in info: ret.append(each_category.serialize) debug_msg(ret) return ret
def global_create_app(self, name, db_path): basedir = os.path.abspath(os.path.dirname(__file__)) if (name == None): debug_msg("Warning: no app name!") name = '__main__' app = Flask(name) if db_path is None: database_path = 'sqlite:///' + \ os.path.join( basedir, self.config.getConfig('database_path')) else: database_path = 'sqlite:///' + \ os.path.join( basedir, db_path) debug_msg("----database_path---: " + database_path) app.config['SQLALCHEMY_DATABASE_URI'] = database_path debug_msg(app.config['SQLALCHEMY_DATABASE_URI']) app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True self.app = app return app
from flask_sqlalchemy import SQLAlchemy from Initialization import Initialization from Utilities import debug_msg debug_msg("Database Models") db = Initialization().get_global_db() class Blocks(db.Model): ''' Table Blocks ''' __tablename__ = 'Blocks' id = db.Column(db.Integer, primary_key=True) uid = db.Column(db.Integer) display_time = db.Column(db.String(16)) date_id = db.Column(db.Integer, db.ForeignKey('Date.id')) position = db.Column(db.Integer) secondary_category_id = db.Column(db.Integer, db.ForeignKey('Secondary_Category.id')) primary_category_id = db.Column(db.Integer, db.ForeignKey('Primary_Category.id')) @property def serialize(self): ''' serializing to dict ''' return { 'id': self.id,
def __init__(self, db): debug_msg(">>> %s.%s" % (__name__, sys._getframe().f_code.co_name)) self.db = db db.create_all()
def dump_all_data(self): ''' dump all data in database ''' ''' dump Users data ''' debug_msg('=========Users start========') users_info = Users.query.all() for each in users_info: debug_msg(each.serialize) debug_msg('=========Users end ========') debug_msg('=========Primary_Category start========') Primary_Categoryinfo = Primary_Category.query.all() for each in Primary_Categoryinfo: debug_msg(each.serialize) debug_msg('=========Primary_Category end ========') debug_msg('=========Secondary_Category start========') Secondary_Categoryinfo = Secondary_Category.query.all() for each in Secondary_Categoryinfo: debug_msg(each.serialize) debug_msg('=========Secondary_Category end ========') debug_msg('=========Date start========') Date_info = Date.query.all() for each in Date_info: debug_msg(each.serialize) debug_msg('=========Date end ========') debug_msg('=========Blocks start========') Blocks_info = Blocks.query.all() for each in Blocks_info: debug_msg(each.serialize) debug_msg('=========Blocks end ========')
def initialize_database(self, db): debug_msg(">>> %s.%s" % (__name__, sys._getframe().f_code.co_name)) self.db = db db.create_all() debug_msg("Users: %d" % Users.query.count()) debug_msg("Primary_Category: %d" % Primary_Category.query.count()) debug_msg("Second_Category: %d" % Second_Category.query.count()) debug_msg("Date: %d" % Date.query.count()) debug_msg("Blocks: %d" % Blocks.query.count()) return 0
def create_an_empty_day(self, input_date): debug_msg(">>> %s.%s" % (__name__, sys._getframe().f_code.co_name)) if not db == None: if 0 == Date.query.filter_by(date=input_date).count(): debug_msg("This is a brand new day!!") last_changed_time = datetime.datetime.now().replace( microsecond=0) new_date_id = Date.query.count() new_date = Date(id = new_date_id,\ date = input_date,\ last_changed_time = last_changed_time\ ) self.db.session.add(new_date) debug_msg( "new Date id: %d,date: %s,last_changed_time :%s" % (new_date.id, new_date.date, new_date.last_changed_time)) self.db.session.commit() debug_msg("committed") new_block = {} new_blocks_start_id = Blocks.query.count() for index in range(new_blocks_start_id, new_blocks_start_id + 48): pos = index - new_blocks_start_id print pos display_time = "%02d:%02d" % (pos / 2, (pos % 2) * 30) new_block[pos] = Blocks(id = index,date_id =new_date_id,\ display_time = display_time,\ position = pos,\ second_category_id = 0 \ ) self.db.session.add(new_block[pos]) debug_msg("new Blocks[%d] id: %d,date_id: %s,display_time: %s,pos: %d, second_category_id: %d"% (pos,new_block[pos].id,\ new_block[pos].date_id,\ new_block[pos].display_time,\ new_block[pos].position,\ new_block[pos].second_category_id)) self.db.session.commit() debug_msg("committed") return 0 else: debug_msg("this day already existed") return None
def __init__(self): debug_msg(">>> %s.%s" %( __name__,sys._getframe().f_code.co_name))
Users=Users, Primary_Category=Primary_Category, Second_Category=Second_Category, Date=Date, Blocks=Blocks) manager.add_command('shell', Shell(make_context=my_make_context)) @init_app.route('/', methods=['GET', 'POST', 'OPTIONS']) def index(): return view.generatePage() ''' if __name__ == '__main__': config = MyConfig() debug_msg("**********************************************") debug_msg(" BlockyTime Version: " + str(config.getConfig("version"))) debug_msg("**********************************************") init_app.run(host=config.getConfig("host"), port=config.getConfig("port"), debug=True) manager.run()
db_tester.generate_fake_data() db_tester = database_tester() db_tester.generate_fake_data() db_helper.dump_all_data() migrate = Migrate(init_app, init_db) manager = Manager(init_app) manager.add_command('db', MigrateCommand) if __name__ == '__main__': config = MyConfig() debug_msg("**********************************************") debug_msg(" tester main ver: " + str(config.getConfig("version"))) debug_msg("**********************************************") ''' from testing.day_controller_tester import day_controller_tester day_controller_testing = day_controller_tester() day_controller_testing.testing_get_date_object_from_string() day_controller_testing.testing_get_date_object_from_id() day_controller_testing.testing_get_date_id_from_string() day_controller_testing.testing_get_last_changed_time_from_string() day_controller_testing.testing_get_date_list() day_controller_testing.testing_update_date_data() '''