def initial_database(): """ 初始化数据库, 重建旧表 创建表 Returns: """ # TODO: 重复代码抽象 from yiyun.helpers import find_subclasses from yiyun.models import BaseModel, Sport, User, Team, Activity # drop old database and create new; test_settings = reject_settings() test_db = test_settings['db_name'] raw_sql = ("drop database {test_db};" "create database {test_db};" "use {test_db};") # create new tables BaseModel._meta.database.execute_sql(raw_sql.format(test_db=test_db)) models = find_subclasses(BaseModel) if not Sport.table_exists(): Sport.create_table() if not User.table_exists(): User.create_table() if not Team.table_exists(): Team.create_table() if not Activity.table_exists(): Activity.create_table() for model in models: if model._meta.db_table.startswith("__"): logging.debug(("table skip: " + model._meta.db_table)) elif model.table_exists(): logging.debug(('table exist: ' + model._meta.db_table)) else: model.create_table() logging.debug(('table created: ' + model._meta.db_table)) logging.debug('create all [ok]')
def sport_name(self): if not self.sport_id: return "" sport = Sport.get_or_none(id=self.sport_id) return sport.name if sport else ""
def get(self): query = Sport.select().order_by(Sport.sort.desc()) sports = [] for sport in query: sports.append(sport.info) self.write({"sports": sports})
def info(self): _info = self.to_dict(exclude=[ Activity.creator, Activity.team, Activity.leader, Activity.sport ]) _info['creator_id'] = self._data['creator'] _info['team_id'] = self._data['team'] _info['leader_id'] = self._data['leader'] _info['sport'] = Sport.get( id=self.sport_id).info if self.sport_id else {} return _info
def get(self): team = self.current_team if team.sport: team.sport = [s for s in Sport.select().where(Sport.id << team.sport)] form = TeamBasicFrom(obj=team) province = self.current_team.province if province: form.city.choices = ChinaCity.get_cities(province) self.render("settings/basic.html", form=form, cities=ChinaCity.get_cities() )
def initial_data(self): faker = Faker() sport = Sport.create(name=faker.pystr(), description="测试") self.team_owner = User.create(name='test_match') self.team = Team.create(name='club_test_match', owner_id=self.team_owner.id, sport=sport) self.user = self.creator = User.create(name='match_creator') self.match = Match.create( team_id=self.team.id, user_id=self.user.id, sport_id=sport.id, price=10, title='just a test', description='description', start_time='3000-01-01 00:00:01', end_time='3000-12-31 23:59:59', join_start="2016-01-01 00:00:01", join_end="2999-12-31 23:59:59", state=Match.MatchState.opening.value, )
def get(self, match_id): match = Match.get_or_404(id=match_id) match.sport_id = Sport.get_or_none(id=match.sport_id) match.group_type = str(match.group_type) team = Team.get_or_404(id=match.team_id) form = EditMatchFrom(obj=match, team=team) # 获取赛事分组信息 query = MatchGroup.select().where( MatchGroup.match_id == match.id).order_by( MatchGroup.sort_num.desc()) groups = [] for group in query: group = group.info group['max'] = group['max_members'] groups.append(group) # 获取报名表自定义选项 query = MatchOption.select().where( MatchOption.match_id == match.id).order_by( MatchOption.sort_num.desc()) custom_options = [] for option in query: option = option.info if 'choices' in option: option['choices'] = "|".join(option['choices']) custom_options.append(option) self.render("match/edit.html", form=form, match=match, cities=ChinaCity.get_cities(), group_type=match.group_type, groups=groups, custom_options=custom_options, options=match.fields)
def main(): settings = setting_from_object(local_settings) if settings.get('debug', False): options.logging = "debug" tornado.options.parse_command_line() if options.debug: settings['debug'] = True if options.cmd == 'createall': """Create all database tables""" create_app(settings) if not Sport.table_exists(): Sport.create_table() if not User.table_exists(): User.create_table() if not Team.table_exists(): Team.create_table() if not TeamMemberGroup.table_exists(): TeamMemberGroup.create_table() if not TeamMember.table_exists(): TeamMember.create_table() if not Activity.table_exists(): Activity.create_table() if not Admin.table_exists(): Admin.create_table() if not Match.table_exists(): Match.create_table() if not MatchStatus.table_exists(): MatchStatus.create_table() models = find_subclasses(BaseModel) for model in models: if model._meta.db_table.startswith("__"): print(("table skip: " + model._meta.db_table)) elif model.table_exists(): print(('table exist: ' + model._meta.db_table)) else: model.create_table() print(('table created: ' + model._meta.db_table)) print('create all [ok]') elif options.cmd == 'createadmin': app = create_app(settings) Admin.create(username="******", password=Admin.create_password("admin"), mobile="17088888888", email="*****@*****.**", name="Admin", is_superadmin=True, state=1) elif options.cmd == 'createclient': app = create_app(settings) Client.create(name="ios", key=create_token(32), secret=create_token(32)) elif options.cmd == 'run_as_wsgi': logging.info('server started. port %s' % options.port) import gevent.wsgi app = create_app(settings) # 转换成wsgi实例 wsgi_app = tornado.wsgi.WSGIAdapter(app) http_server = gevent.wsgi.WSGIServer(('', options.port), wsgi_app) http_server.serve_forever() elif options.cmd == 'runserver': tornado.platform.asyncio.AsyncIOMainLoop().install() ioloop = asyncio.get_event_loop() app = create_app(settings) app.listen(options.port, xheaders=True) print("running...") ioloop.run_forever() elif options.cmd == "fix_notify": from fix_script.fix_match_start_nofity import fix_match_start_notify fix_match_start_notify()
def sport(self): if not self.sport_id: return None return Sport.get_or_none(id=self.sport_id)