def update_last_term(): query = Legislator.select( Legislator.name, fn.max(Legislator.term).alias("last_term")).group_by(Legislator.name) for legislator in query: Candidate.update(lastTerm=legislator.last_term).where( Candidate.name == legislator.name).execute()
def write_result_to_db(file_path): with open(file_path) as fp: data = json.load(fp) for candidate in data: Candidate.update(dateOfBirth=candidate["date_of_birth"], age=candidate["age"]).where( Candidate.name == candidate["page_name"] ).execute()
def update_candidate(candidate_data): for candidate in candidate_data: try: Candidate.update( wikidataPicUrl=candidate["picUrl"], wikidataDateOfBirth=candidate["dateOfBirth"]).where( Candidate.name == candidate["name"]).execute() except DataError as e: print(f'Error on candidate: {candidate["name"]}') print(e)
def update_photo(): latest_id = Legislator.select(Legislator.id).group_by( Legislator.name).order_by(Legislator.term.desc()) query = Legislator.select(Legislator.name, Legislator.term, Legislator.picUrl).where( Legislator.id.in_(latest_id)) for legislator in query: Candidate.update(picUrl=legislator.picUrl).where( Candidate.name == legislator.name, Candidate.lastTerm == legislator.term).execute()
def tag_history_candidate(): query = Legislator.select(Legislator.name) names = [re.match(r"[\u4e00-\u9fff|.]{2,}", row.name)[0] for row in query] print(f"history legislators: {names}") query = Candidate.update(historyLegislator=True).where( Candidate.name.in_(names)) query.execute()
def tag_current_legislator_in_db(names: List[str]) -> None: """Write current_legislator column. Notice: won't change others to False """ print(f"current legislators: {names}") query = Candidate.update(currentLegislator=True).where(Candidate.name.in_(names)) query.execute()
def tag_current_candidate(): query = Legislator.select(Legislator.name).where( Legislator.term == "09", Legislator.leaveFlag == "否") names = [re.match(r"[\u4e00-\u9fff|.]{2,}", row.name)[0] for row in query] print(f"current legislators: {names}") query = Candidate.update(currentLegislator=True).where( Candidate.name.in_(names)) query.execute()
def write_result_to_db(file_path): """Read candidate file and store to db. input: [{ "constituency": "臺北市第一選舉區", "candidates": [ { "party": "國民黨", "name": "汪志冰" },... ] }...] """ with open(file_path) as fp: data = json.load(fp) data = [ { "name": candidate["name"].replace("․", "."), "party": candidate["party"], "wiki": candidate["wiki"], "constituency": constituency["constituency"], } for constituency in data for candidate in constituency["candidates"] ] Candidate.drop_table() Candidate.create_table() Candidate.insert_many(data).execute()
def get_page_list(): return [row.wiki.split("/")[-1] for row in Candidate.select()]
db.session.add(tempP) tempP = Position(position_name='Senior Staff') db.session.add(tempP) tempP = Position(position_name='Manager') db.session.add(tempP) tempP = Position(position_name='Chairman') db.session.add(tempP) tempP = Position(position_name='EXCOM') db.session.add(tempP) tempP = Position(position_name='Advisor') db.session.add(tempP) db.session.commit() tempC = Candidate( candidate_name='Ivano Ekasetia', candidate_vision='memajukan bssc', candidate_mission='meningkatkan kemampuan setiap anggota bssc', candidate_blueprint='https://www.youtube.com/embed/MJbE3uWN9vE', candidate_video='https://www.youtube.com/embed/MJbE3uWN9vE') db.session.add(tempC) tempC2 = Candidate( candidate_name='Francis Alexander', candidate_vision='memajukan bssc', candidate_mission='meningkatkan kemampuan setiap anggota bssc', candidate_blueprint='https://www.youtube.com/embed/MJbE3uWN9vE', candidate_video='https://www.youtube.com/embed/MJbE3uWN9vE') db.session.add(tempC2) tempC3 = Candidate( candidate_name='Kevin Wijaya', candidate_vision='memajukan bssc', candidate_mission='meningkatkan kemampuan setiap anggota bssc', candidate_blueprint='https://www.youtube.com/embed/MJbE3uWN9vE',
def get_candidates(): return [ candidate.name for candidate in Candidate.select( Candidate.name).group_by(Candidate.name) ]