def update_competitor_db_record(table_row, competitor: Competitor): competitor.level = to_int(table_row[3], None) competitor.matches = to_int(table_row[4]) competitor.wins = to_int(table_row[5]) competitor.losses = to_int(table_row[6]) competitor.performance = to_int(table_row[7]) competitor.save()
def get_posts(page=1, per_page=12, query='', sticky=None, slugs=[], group_ids=[], category_ids=[], tag_ids=[], author_ids=[], before=None, after=None, exclude=None): """ Get posts by querying the Wordpress API, including retrieving pagination information. Gracefully handle errors for pages that don't exist, returning empty data instead of an error. Allow filtering on various criteria, using sensible defaults. """ try: response = get( 'posts', { '_embed': True, 'per_page': per_page, 'page': page, 'search': query, 'sticky': sticky, 'slug': ','.join(slugs), 'group': helpers.join_ids(group_ids), 'categories': helpers.join_ids(category_ids), 'tags': helpers.join_ids(tag_ids), 'author': helpers.join_ids(author_ids), 'before': before.isoformat() if before else None, 'after': after.isoformat() if after else None, 'exclude': exclude }) except requests.exceptions.HTTPError as request_error: response = request_error.response.json() if (type(response) is dict and response.get('code') == 'rest_post_invalid_page_number'): # The page doesn't exist, so set everything to empty posts = [] total_posts = None total_pages = None else: # We don't recognise this error, re-raise it raise request_error else: posts = response.json() total_pages = helpers.to_int(response.headers.get('X-WP-TotalPages'), None) total_posts = helpers.to_int(response.headers.get('X-WP-Total'), None) posts = _normalise_resources(posts) return posts, total_posts, total_pages
def update_competitor_performance_from_table(competitor: Competitor, row=None): try: if row is None: data = UsersSheet.get_all_users() for row_number, row_data in enumerate(data, 2): if row_data[0] == competitor.legacy_number and row_data[1] == competitor.name: row = row_number competitor.performance = to_int(row_data[7]) competitor.save() break if row is None: logger.error(f'Cannot get new performance for competitor: {competitor} - cannot found record in gsheet') return cfg = get_config() new_perf = retrieve_data( cfg.spreadsheet_id, f'{cfg.spreadsheet_users_sheet}!H{row}' ) new_perf = to_int(new_perf, None) if new_perf is None: logger.error(f'Cannot get new performance for competitor: {competitor}') return competitor.performance = new_perf competitor.save() except: logger.exception(f'Error occurred while updating performance for competitor: {competitor}')
def __init__(self, name, sailing_time, time_row): super(BCFerriesScheduledCrossing, self).__init__(self) scheduled_dep, actual_dep, arrival, status, _ = time_row self.boat_name = name self.name = '{} at {}'.format(name, scheduled_dep) self.status = status.strip() self.sailing_time = 0 match = time_regex.match(sailing_time) if match: hours, minutes = match.group(1, 2) self.sailing_time = datetime.timedelta(minutes=to_int(minutes), hours=to_int(hours)) self.scheduled_departure = dateutil.parser.parse( scheduled_dep, fuzzy=True) if scheduled_dep else None self.actual_departure = dateutil.parser.parse( actual_dep, fuzzy=True) if actual_dep else None self.arrival = dateutil.parser.parse(arrival, fuzzy=True) if arrival else None self.__fake_actual_departure = self.actual_departure or self.scheduled_departure self._register_properties([ 'boat_name', 'sailing_time', 'scheduled_departure', 'actual_departure', 'arrival' ])
def archives(): page = helpers.to_int(flask.request.args.get('page'), default=1) year = helpers.to_int(flask.request.args.get('year')) month = helpers.to_int(flask.request.args.get('month')) group_slug = flask.request.args.get('group') category_slug = flask.request.args.get('category') if month and month > 12: month = None friendly_date = None group = None after = None before = None if year: if month: after = datetime(year=year, month=month, day=1) before = after + relativedelta(months=1) friendly_date = after.strftime('%B %Y') if not month: after = datetime(year=year, month=1, day=1) before = after + relativedelta(years=1) friendly_date = after.strftime('%Y') if group_slug: groups = api.get_groups(slugs=[group_slug]) if groups: group = groups[0] if category_slug: categories = api.get_categories(slugs=[category_slug]) category_ids = [category['id'] for category in categories] else: categories = [] category_ids = [] posts, total_posts, total_pages = helpers.get_formatted_posts( page=page, after=after, before=before, group_ids=[group['id']] if group else [], category_ids=category_ids if category_ids else [], ) return flask.render_template( 'archives.html', categories=categories, category_ids=category_ids, category_slug=category_slug if category_slug else None, current_page=page, friendly_date=friendly_date, group=group, now=datetime.now(), posts=posts, total_pages=total_pages, total_posts=total_posts, )
def __init__(self, a): super(BCFerriesCapacity, self).__init__(self) percents = parse_qs(urlparse(a.get('href')).query)['est'][0].split(',') self.filled = to_int(percents[0]) self.mixed_filled = to_int(percents[1]) self.passenger_filled = to_int(percents[2]) self.name = "{}% Full".format(percents[0]) self._register_properties(['filled', 'mixed_filled', 'passenger_filled'])
def __init__(self, a): super(BCFerriesCapacity, self).__init__(self) percents = parse_qs(urlparse(a.get('href')).query)['est'][0].split(',') self.filled = to_int(percents[0]) self.mixed_filled = to_int(percents[1]) self.passenger_filled = to_int(percents[2]) self.name = "{}% Full".format(percents[0]) self._register_properties( ['filled', 'mixed_filled', 'passenger_filled'])
def __init__(self, api, page, index): super(BCFerriesRoute, self).__init__(self) self._api = api self.name = page.find_by_selector('div.ferry_name > div.td')[index].text self.__time_block = page.find_by_selector('div.time_block > div.td > div')[index] self.car_waits = to_int(page.find_by_selector('div.car_waits > div.td > div')[index].text) self.oversize_waits = to_int(page.find_by_selector('div.car_waits > div.td > div')[index + 1].text) self.__schedule_url = page.find_by_selector('div.buttons')[index].find_one('a').get('href') from_, to = self.name.split(' to ') self.from_ = api.bc.terminals().get(from_) self.to = api.bc.terminals().get(to) self._register_properties(['car_waits', 'oversize_waits', 'crossings', 'schedule', 'distance', 'from_', 'to'])
def upcoming(): page = helpers.to_int(flask.request.args.get('page'), default=1) posts_per_page = 12 upcoming_categories = api.get_categories(slugs=['events', 'webinars']) upcoming_category_ids = [] for upcoming_category_id in upcoming_categories: upcoming_category_ids.append(upcoming_category_id['id']) upcoming_events, _, _ = helpers.get_formatted_expanded_posts( per_page=3, category_ids=upcoming_category_ids ) posts, total_posts, total_pages = helpers.get_formatted_expanded_posts( per_page=posts_per_page, category_ids=upcoming_category_ids, page=page ) return flask.render_template( 'upcoming.html', posts=posts, current_page=page, total_posts=total_posts, total_pages=total_pages )
def _tag_view(tag_slug, page_slug, template): """ View function which gets all posts for a given tag, and returns a response loading those posts with the template provided """ page = helpers.to_int(flask.request.args.get('page'), default=1) tags = api.get_tags(slugs=[tag_slug]) if not tags: flask.abort(404) tag = tags[0] posts, total_posts, total_pages = helpers.get_formatted_expanded_posts( tag_ids=[tag['id']], page=page ) return flask.render_template( template, posts=posts, tag=tag, current_page=page, page_slug=page_slug, total_posts=total_posts, total_pages=total_pages, )
def alternate_homepage(): category_slug = flask.request.args.get('category') category = None sticky_posts, _, _ = helpers.get_formatted_expanded_posts(sticky=True) featured_posts = sticky_posts[:3] if sticky_posts else None page = helpers.to_int(flask.request.args.get('page'), default=1) posts_per_page = 12 if category_slug: categories = api.get_categories(slugs=[category_slug]) if categories: category = categories[0] posts, total_posts, total_pages = helpers.get_formatted_expanded_posts( per_page=posts_per_page, category_ids=[category['id']] if category else [], page=page, sticky=False ) return flask.render_template( 'alternate_index.html', posts=posts, category=category, current_page=page, total_posts=total_posts, total_pages=total_pages, featured_posts=featured_posts )
def process_message(self, message: Message, user: User, bot: TeleBot, competitor: Competitor): score = to_int(message.text, None) if score is None: return super().process_message(message, user, bot) res: Result = user.check_result() if res.result is not None: return RET.OK, None, None, None # TODO: checks if len(res.scores) > 10: bot.send_message( user.user_id, get_translation_for('results_maximum_scores_entered_msg'), reply_markup=self.__base_keyboard()) return RET.OK, None, None, None if score < 0 or score > 15: bot.send_message( user.user_id, get_translation_for('results_incorrect_score_msg'), reply_markup=self.__base_keyboard()) return RET.OK, None, None, None res.scores.append(score) res.save() return self.process_result(message, user, bot, competitor)
def recieve_response(self, data): drone_id = helpers.to_int(data['source_addr']) if drone_id not in self.active_drone_ids: self.active_drone_ids.append(drone_id) print 'Discovered drone with ID: %s' % drone_id else: print 'Rediscovered drone with ID: %s' % drone_id string = data['rf_data']
def __init__(self, name, sailing_time, time_row): super(BCFerriesScheduledCrossing, self).__init__(self) scheduled_dep, actual_dep, arrival, status, _ = time_row self.boat_name = name self.name = '{} at {}'.format(name, scheduled_dep) self.status = status.strip() self.sailing_time = 0 match = time_regex.match(sailing_time) if match: hours, minutes = match.group(1, 2) self.sailing_time = datetime.timedelta(minutes=to_int(minutes), hours=to_int(hours)) self.scheduled_departure = dateutil.parser.parse(scheduled_dep, fuzzy=True) if scheduled_dep else None self.actual_departure = dateutil.parser.parse(actual_dep, fuzzy=True) if actual_dep else None self.arrival = dateutil.parser.parse(arrival, fuzzy=True) if arrival else None self.__fake_actual_departure = self.actual_departure or self.scheduled_departure self._register_properties(['boat_name', 'sailing_time', 'scheduled_departure', 'actual_departure', 'arrival'])
def run_check(dfDV,dfSent,groupId,year): if dfSent is not None: dfSent=clean_df_sent(dfSent) if dfDV is not None: dfDV=clean_df_dv(dfDV) dfGroup = dfSent.groupby([groupId]) df_to_csv(dfGroup.sum(),"dados\emprego\sent\RaisGroup"+str(year)+".csv") total=0 print "Enterin in for "+groupId for id in dfDV['id']: idint =to_int(id) if not id: continue valDV = dfDV[(dfDV['id']==id)]['val'].values[0] valCSV=get_valueGroup(dfGroup,id,'wage') if valCSV==False: valCSV=get_valueGroup(dfGroup,idint,'wage') if valCSV==False: total=total+1 print "Not found in CSV a value for "+str(id)+" - "+str(idint)+" : Exports of value "+ str(valDV)+ " in the year "+str(year) continue valCSV=to_int(valCSV) valDV=to_int(valDV) if valDV!=valCSV: txt= "ERROR in groupId ("+str(year)+"): "+str(id)+" / "+str(idint)+" - Value in CSV "+ str(valCSV)+ " <> Value in DV "+str(valDV) + " - Difference: "+str(valCSV - valDV) print txt total=total+1 else: txt="OK "+str(id) #print txt return total
def update_model(cleanse=True): data = UsersSheet.get_all_users() if not data: pass else: stored_in_sheet_records = set() row_num = 1 for row in data: existing_data = Competitor.objects( name=row[1], legacy_number=row[0] ).first() if existing_data is None: cfg = get_config() new_data = Competitor( legacy_number=row[0], name=row[1], status=COMPETITOR_STATUS.UNAUTHORIZED, level=to_int(row[3], None), matches=to_int(row[4]), wins=to_int(row[5]), losses=to_int(row[6]), performance=to_int(row[7]), used_vacation_time=0 ) new_data.save() new_data.reload() stored_in_sheet_records.add(new_data.id) else: stored_in_sheet_records.add(existing_data.id) UsersSheet.update_competitor_db_record(row, existing_data) row_num += 1 for new_record in Competitor.objects(id__not__in=stored_in_sheet_records): if not cleanse: row_num += 1 UsersSheet.insert_competitor_in_table(new_record, at_row=row_num) else: new_record.delete() hr_logger.info('Оновлено список гравців з гугл-таблиці')
def run_check(dfDV,dfSent,groupId,month,valId): dfGroup = dfSent.groupby([groupId]) #df_to_csv(dfGroup.sum(),"dados\emprego\sent\RaisGroup"+str(year)+".csv") print "Running check " for id in dfDV['id']: idint =to_int(id) if not id: continue valDV = dfDV[(dfDV['id']==id)][valId].values[0] total=0 valCSV=get_valueGroup(dfGroup,id,valId) if valCSV==False: valCSV=get_valueGroup(dfGroup,idint,valId) if valCSV==False: if valDV>1: print "Not found in CSV a value for "+str(id)+" - "+str(idint)+" : Exports of value "+ str(valDV)+ " in the month "+str(month) total+=1 continue valCSV=to_int(valCSV) valDV=to_int(valDV) diffDVCSV=valCSV - valDV if valDV!=valCSV and (diffDVCSV>1 or diffDVCSV<-1): txt= "ERROR in groupId ("+str(month)+"): "+str(id)+" / "+str(idint)+" - Value in CSV "+ str(valCSV)+ " <> Value in DV "+str(valDV) + " - Difference: "+str(diffDVCSV) print txt total+=1 else: txt="OK "+str(id) print txt return total
def try_to_parse_score(score): import re from helpers import to_int try: ret = [] for ps in filter(None, re.split('[-,\s]', score)): i = to_int(ps, None) if i is None: return None ret.append(i) return ret except: return None
def __init__(self, api, page, index): super(BCFerriesRoute, self).__init__(self) self._api = api self.name = page.find_by_selector( 'div.ferry_name > div.td')[index].text self.__time_block = page.find_by_selector( 'div.time_block > div.td > div')[index] self.car_waits = to_int( page.find_by_selector('div.car_waits > div.td > div')[index].text) self.oversize_waits = to_int( page.find_by_selector('div.car_waits > div.td > div')[index + 1].text) self.__schedule_url = page.find_by_selector( 'div.buttons')[index].find_one('a').get('href') from_, to = self.name.split(' to ') self.from_ = api.bc.terminals().get(from_) self.to = api.bc.terminals().get(to) self._register_properties([ 'car_waits', 'oversize_waits', 'crossings', 'schedule', 'distance', 'from_', 'to' ])
def homepage(): category_slug = flask.request.args.get("category") category = None sticky_posts, _, _ = helpers.get_formatted_expanded_posts(sticky=True) featured_posts = sticky_posts[:3] if sticky_posts else None page = helpers.to_int(flask.request.args.get("page"), default=1) posts_per_page = 12 upcoming_categories = api.get_categories(slugs=["events", "webinars"]) upcoming_category_ids = [] for upcoming_category_id in upcoming_categories: upcoming_category_ids.append(upcoming_category_id["id"]) upcoming_events, _, _ = helpers.get_formatted_expanded_posts( per_page=3, category_ids=upcoming_category_ids ) if category_slug: categories = api.get_categories(slugs=[category_slug]) if categories: category = categories[0] posts, total_posts, total_pages = helpers.get_formatted_expanded_posts( per_page=posts_per_page, category_ids=[category["id"]] if category else [], page=page, sticky=False, ) # Manipulate the posts to add a newsletter placeholder if page == 1: print("page: " + str(page)) posts.insert(2, "newsletter") posts.pop(11) return flask.render_template( "index.html", posts=posts, category=category, current_page=page, total_posts=total_posts, total_pages=total_pages, featured_posts=featured_posts, upcoming_events=upcoming_events, )
def recieve_response(self, data): drone_id = helpers.to_int(data['source_addr']) if drone_id not in self.active_drone_ids: print 'Unexpected response from inactive Drone %s' % drone_id return if drone_id in self.drones_responded: print 'Drone %s responded more than once' % drone_id return print 'Recieved response from Drone %s' % drone_id self.drones_responded.append(drone_id) string = data['rf_data'] msg = communication.Message(string) print 'Message type: %s' % msg.type_id params = parser.parse(msg) # Cases if msg.type_id == 3: print 'Duration in state: %s' % (params['duration'],) if params['duration'] > 10: order = communication.Message(to_id=drone_id, from_id=1, type_id=12, payload='1') self.link.send_message(order) print "ERMAGAUD!ERMAGAUD!ERMAGAUD!" elif msg.type_id == 4: print 'In state "Deploy"\nDuration: %s\nComplete: %s' % (params['duration'], params['complete']) if params['complete']: order = communication.Message(to_id=drone_id, from_id=1, type_id=12, payload='2') self.link.send_message(order) print "ERMAGAUD!ERMAGAUD!ERMAGAUD!" elif msg.type_id == 5: print 'In state "Searching"\nDuration: %s\nTracking: %s' % (params['duration'], params['tracking']) if params['tracking']: order = communication.Message(to_id=drone_id, from_id=1, type_id=12, payload='4') self.link.send_message(order) print "ERMAGAUD!ERMAGAUD!ERMAGAUD!" elif msg.type_id == 7: print 'In state "Attacking"\nDuration: %s\nDistance: %s\nCaptured: %s' % (params['duration'], params['distance'], params['captured']) if params['captured']: order = communication.Message(to_id=drone_id, from_id=1, type_id=12, payload='6') self.link.send_message(order) print "ERMAGAUD!ERMAGAUD!ERMAGAUD!" elif msg.type_id == 9: print 'In state "Returning"\nDuration: %s\nTracking: %s\nComplete: %s' % (params['duration'], params['tracking'], params['complete']) check = [i in self.drones_responded for i in self.active_drone_ids] if False not in check: print 'Routine complete' self.complete = True
def homepage(): category_slug = flask.request.args.get('category') category = None sticky_posts, _, _ = helpers.get_formatted_expanded_posts(sticky=True) featured_posts = sticky_posts[:3] if sticky_posts else None page = helpers.to_int(flask.request.args.get('page'), default=1) posts_per_page = 12 upcoming_categories = api.get_categories(slugs=['events', 'webinars']) upcoming_category_ids = [] for upcoming_category_id in upcoming_categories: upcoming_category_ids.append(upcoming_category_id['id']) upcoming_events, _, _ = helpers.get_formatted_expanded_posts( per_page=3, category_ids=upcoming_category_ids ) if category_slug: categories = api.get_categories(slugs=[category_slug]) if categories: category = categories[0] posts, total_posts, total_pages = helpers.get_formatted_expanded_posts( per_page=posts_per_page, category_ids=[category['id']] if category else [], page=page, sticky=False ) return flask.render_template( 'index.html', posts=posts, category=category, current_page=page, total_posts=total_posts, total_pages=total_pages, featured_posts=featured_posts, upcoming_events=upcoming_events )
def search(): query = flask.request.args.get('q') or '' page = helpers.to_int(flask.request.args.get('page'), default=1) posts = [] total_pages = None total_posts = None if query: posts, total_posts, total_pages = helpers.get_formatted_posts( query=query, page=page) return flask.render_template( 'search.html', posts=posts, query=query, current_page=page, total_posts=total_posts, total_pages=total_pages, )
def alternate_homepage(): category_slug = flask.request.args.get('category') category = None sticky_posts, _, _ = helpers.get_formatted_expanded_posts(sticky=True) featured_posts = sticky_posts[:3] if sticky_posts else None page = helpers.to_int(flask.request.args.get('page'), default=1) posts_per_page = 13 if page == 1 else 12 if category_slug: categories = api.get_categories(slugs=[category_slug]) if categories: category = categories[0] posts, total_posts, total_pages = helpers.get_formatted_expanded_posts( per_page=posts_per_page, category_ids=[category['id']] if category else [], page=page ) if featured_posts: for post in featured_posts: if post in posts: posts.remove(post) return flask.render_template( 'alternate_index.html', posts=posts, category=category, current_page=page, total_posts=total_posts, total_pages=total_pages, featured_posts=featured_posts, webinars=feeds.get_rss_feed_content( 'https://www.brighttalk.com/channel/6793/feed' ) )
def get_posts( page=1, per_page=12, query="", sticky=None, slugs=[], group_ids=[], category_ids=[], tag_ids=[], # Exclude "lang:jp" tagged posts tags_exclude_ids=[3184], author_ids=[], before=None, after=None, exclude=None, ): """ Get posts by querying the Wordpress API, including retrieving pagination information. Gracefully handle errors for pages that don't exist, returning empty data instead of an error. Allow filtering on various criteria, using sensible defaults. """ try: response = get( "posts", { "_embed": True, "per_page": per_page, "page": page, "search": query, "sticky": sticky, "slug": ",".join(slugs), "group": helpers.join_ids(group_ids), "categories": helpers.join_ids(category_ids), "tags": helpers.join_ids(tag_ids), "tags_exclude": helpers.join_ids(tags_exclude_ids), "author": helpers.join_ids(author_ids), "before": before.isoformat() if before else None, "after": after.isoformat() if after else None, "exclude": exclude, }, ) except requests.exceptions.HTTPError as request_error: response = request_error.response.json() if (type(response) is dict and response.get("code") == "rest_post_invalid_page_number"): # The page doesn't exist, so set everything to empty posts = [] total_posts = None total_pages = None else: # We don't recognise this error, re-raise it raise request_error else: posts = response.json() total_pages = helpers.to_int(response.headers.get("X-WP-TotalPages"), None) total_posts = helpers.to_int(response.headers.get("X-WP-Total"), None) posts = _normalise_resources(posts) return posts, total_posts, total_pages
def book(book_id): # User reached route via POST (as by submitting a form via POST) if request.method == "POST": #check text filled if not request.form.get("review_text"): return render_template("message.html", header="Error", message="must provide text") #check rating filled if not request.form.get("stars"): return render_template("message.html", header="Error", message="must provide score") user_id = int(session["user_id"]) book_id = book_id starstext = request.form.get("stars") stars = to_int(starstext) review_text = request.form.get("review_text") #check if user already reviewed this book if db.execute( "SELECT * FROM reviews WHERE user_id = :user_id AND book_id = :book_id", { "user_id": user_id, "book_id": book_id }).rowcount > 0: return render_template("message.html", header="Sorry", message="you already reviewed this book") else: db.execute( "INSERT INTO reviews (user_id, book_id, stars, review_text) VALUES (:user_id, :book_id, :stars, :review_text)", { "user_id": user_id, "book_id": book_id, "stars": stars, "review_text": review_text }) db.commit() return render_template("message.html", header="Succes", message="your review is added") # User reached route via GET else: # get book info book = db.execute("SELECT * FROM books WHERE id = :id", { "id": book_id }).fetchone() if book is None: return render_template("message.html", header="Error", message="book not found") # get goodread info isbn = book.isbn print(isbn) dictgr = lookup(isbn) ratings_count = dictgr["ratings_count"] rating = dictgr["rating"] #get reviews rows = db.execute( "SELECT review_text FROM reviews WHERE book_id = :book_id", { "book_id": book_id }).fetchall() return render_template("book.html", book=book, rows=rows, ratings_count=ratings_count, rating=rating)
mongo = MongoClient(db_url) db = mongo.ks5 print("Updating school information from 2014 data...") for school in parse_csv("../data/2014/ks5_attainment.csv"): # All schools are RECTYPE=1. Other RECTYPEs are used for local averages. # Closed schools are ICLOSE=1. We skip them too. if (school["RECTYPE"] != "1") or (school["ICLOSE"] == "1"): continue db.schools.update( { "_id": school["URN"] }, { "$set": { "lea": to_int(school["LEA"]), "name": school["SCHNAME"], "address": [school["ADDRESS1"], school["ADDRESS2"], school["ADDRESS3"]], "town": school["TOWN"], "postcode": school["PCODE"], "phone": school["TELNUM"], "type": school["NFTYPE"], "religious": school["RELDENOM"], "admissions": school["ADMPOL"], "gender": school["GENDER1618"].capitalize(), "ages": school["AGERANGE"], "performance.2014.students": { "16-18": to_int(school["TPUP1618"], True), "ks5": to_int(school["TALLPUPA"]), "academic": to_int(school["TALLPUP_ACADA"]), "vocational": to_int(school["TALLPUP_VQA"]),
from pymongo import MongoClient from helpers import db_url, parse_xls, group_xls, to_int mongo = MongoClient(db_url) db = mongo.ks5 print("Updating IB student numbers (2014)... ") for school in parse_xls("../data/2014/ks5_subjects.xlsx", "IB Diploma", header_row=2): db.schools.update( { "_id": str(school["URN"]) }, { "$set": { "performance.2014.students.ib": to_int(school["Number entered"]) } } ) print("Updating AQA Baccalaureate student numbers (2014)... ") for school in parse_xls("../data/2014/ks5_subjects.xlsx", "AQA Baccalaureate", header_row=2): db.schools.update( { "_id": str(school["URN"]) }, { "$set": { "performance.2014.students.aqa-bacc": to_int(school["Number entered"]) } } ) mongo.disconnect() print("\nDone.")
def process_callback(self, callback: CallbackQuery, user, bot: TeleBot): data = callback.data if data.find('auth_') != 0: return RET.ANSWER_CALLBACK, None, callback, user data = data.replace('auth_', '') if data.find('user='******'=') if len(ds) == 2: competitor_id = ds[1] try: competitor = Competitor.objects(id=competitor_id).first() if competitor is not None: competitor.change_status(COMPETITOR_STATUS.ACTIVE) competitor.associated_user_vanished = False competitor.save() user.associated_with = competitor user.save() competitor.reload() hr_logger.info( f'Користувач {user.str_repr()} аутентифікувався як {competitor.name}' ) return RET.ANSWER_AND_GO_TO_STATE, 'MenuState', callback, user else: hr_logger.error( f'Сталася помилка при аутентифікації користувача {user.str_repr()} - не вдається знайти в базі обраного ним гравця' ) return RET.ANSWER_CALLBACK, get_translation_for( 'authentication_specified_competitor_not_found_clb' ), callback, user except ValidationError: logger.exception( f'Incorrect competitor_id: {competitor_id} from user with user_id: {user.user_id}' ) elif data.find('paginate=') == 0: ds = data.split('=') if len(ds) == 2: page = ds[1] page = to_int(page, 1) try: available_competitors = Competitor.objects( status=COMPETITOR_STATUS.UNAUTHORIZED).paginate( page=page, per_page=10) except NotFound: logger.exception( f'User {user.user_id} tried to switch to nonexistent page {page}' ) available_competitors = Competitor.objects( status=COMPETITOR_STATUS.UNAUTHORIZED).paginate( page=1, per_page=10) if not render_pagination( available_competitors, callback.message, bot, get_translation_for('authentication_msg'), self.__base_keyboard, update=True): bot.send_message( callback.message.chat.id, get_translation_for( 'authentication_cannot_find_competitors_msg')) hr_logger.error( f'При авторизації користувача {user.str_repr()} не вдалося знайти жодного гравця' ) elif data.find('none') == 0: return RET.ANSWER_CALLBACK, None, callback, user elif data.find('refresh') == 0: if guard.get_allowed()[0] and guard.update_allowed()[0]: UsersSheet.update_model() available_competitors = Competitor.objects( status=COMPETITOR_STATUS.UNAUTHORIZED).paginate(page=1, per_page=10) if not render_pagination( available_competitors, callback.message, bot, get_translation_for('authentication_msg'), self.__base_keyboard, ): bot.send_message( callback.message.chat.id, get_translation_for( 'authentication_cannot_find_competitors_msg')) hr_logger.error( f'При авторизації користувача {user.str_repr()} не вдалося знайти жодного гравця' ) return RET.ANSWER_CALLBACK, None, callback, user
def process_callback(self, callback: CallbackQuery, user: User, bot: TeleBot): data = callback.data if data.find('challenge_to_') != 0: return RET.ANSWER_CALLBACK, None, callback, user data = data.replace('challenge_to_', '') competitor: Competitor = user.check_association() if not competitor: bot.send_message( callback.message.chat.id, get_translation_for('competitor_record_vanished_msg')) return RET.ANSWER_AND_GO_TO_STATE, 'AuthenticationState', callback, user if data.find('user='******'=') if len(ds) == 2: opponent_id = ds[1] try: opponent = Competitor.objects(id=opponent_id).first() if opponent is not None: text = get_translation_for( 'challenge_send_confirm_msg').format(opponent.name) text = f'<b>{text}</b>\n' \ f'{get_translation_for("info_status_str")}: {Competitor.status_code_to_str_dict[opponent.status]}\n' \ f'{get_translation_for("info_level_str")}: {opponent.level or get_translation_for("not_found_str")}\n' \ f'{get_translation_for("info_matches_str")}: {opponent.matches}\n' \ f'{get_translation_for("info_wins_str")}: {opponent.wins}\n' \ f'{get_translation_for("info_losses_str")}: {opponent.losses}\n' \ f'{get_translation_for("info_performance_str")}: {opponent.performance}' update_failed = False try: bot.edit_message_text( text, callback.message.chat.id, callback.message.message_id, reply_markup=self.__confirmation_keyboard( id=opponent_id), parse_mode='html') except: update_failed = True logger.exception( f'Exception occurred while updating keyboard for challenge sending. Chat: {callback.message.chat.id}' ) if update_failed: bot.send_message( callback.message.chat.id, text, reply_markup=self.__confirmation_keyboard( id=opponent_id), parse_mode='html') return RET.ANSWER_CALLBACK, None, callback, user else: return RET.ANSWER_CALLBACK, get_translation_for( 'authentication_specified_competitor_not_found_clb' ), callback, user except ValidationError: logger.exception( f'Incorrect opponent_id: {opponent_id} from user with user_id: {user.user_id}' ) elif data.find('paginate=') == 0: ds = data.split('=') if len(ds) == 2: page = ds[1] page = to_int(page, 1) try: available_competitors = Competitor.objects( status__in=(COMPETITOR_STATUS.ACTIVE, COMPETITOR_STATUS.PASSIVE), level__in=self.get_possible_levels(competitor.level), id__ne=competitor.latest_challenge_sent_to.id if competitor.latest_challenge_sent_to is not None else ObjectId(), associated_user_vanished__ne=True).paginate( page=page, per_page=10) except NotFound: logger.exception( f'User {user.user_id} tried to switch to nonexistent page {page}' ) available_competitors = Competitor.objects( status__in=(COMPETITOR_STATUS.ACTIVE, COMPETITOR_STATUS.PASSIVE), level__in=self.get_possible_levels(competitor.level), id__ne=competitor.latest_challenge_sent_to.id if competitor.latest_challenge_sent_to is not None else ObjectId(), associated_user_vanished__ne=True).paginate( page=1, per_page=10) if not render_pagination( available_competitors, callback.message, bot, get_translation_for('challenge_send_msg'), self.__base_keyboard, update=True): bot.send_message( callback.message.chat.id, get_translation_for( 'challenge_no_applicable_competitors_msg')) elif data.find('none') == 0: return RET.ANSWER_CALLBACK, None, callback, user elif data.find('refresh') == 0: if guard.get_allowed()[0] and guard.update_allowed()[0]: UsersSheet.update_model() available_competitors = Competitor.objects( status__in=(COMPETITOR_STATUS.ACTIVE, COMPETITOR_STATUS.PASSIVE), level__in=self.get_possible_levels(competitor.level), id__ne=competitor.latest_challenge_sent_to.id if competitor.latest_challenge_sent_to is not None else ObjectId(), associated_user_vanished__ne=True).paginate(page=1, per_page=10) if not render_pagination( available_competitors, callback.message, bot, get_translation_for('challenge_send_msg'), self.__base_keyboard, update=True, ): bot.send_message( callback.message.chat.id, get_translation_for( 'challenge_no_applicable_competitors_msg')) elif data.find('confirm_send=') == 0: ds = data.split('=') if len(ds) == 2: opponent_id = ds[1] if competitor.latest_challenge_sent_to and opponent_id == str( competitor.latest_challenge_sent_to): bot.send_message( callback.message.chat.id, get_translation_for( 'challenge_opponent_no_longer_available_msg')) return RET.ANSWER_AND_GO_TO_STATE, 'MenuState', callback, user try: opponent: Competitor = Competitor.objects( status__in=(COMPETITOR_STATUS.ACTIVE, COMPETITOR_STATUS.PASSIVE), level__in=self.get_possible_levels(competitor.level), id=opponent_id, associated_user_vanished__ne=True).first() if opponent is None: bot.send_message( callback.message.chat.id, get_translation_for( 'challenge_opponent_no_longer_available_msg')) return RET.ANSWER_AND_GO_TO_STATE, 'MenuState', callback, user opponent_user: User = User.objects( associated_with=opponent).first() if opponent_user is None: bot.send_message( callback.message.chat.id, get_translation_for( 'challenge_cannot_send_message_to_opponent_msg' )) send_msg_to_admin( get_translation_for( 'admin_notification_tg_user_vanished').format( opponent.name)) opponent.associated_user_vanished = True opponent.save() return RET.ANSWER_AND_GO_TO_STATE, 'MenuState', callback, user config = get_config() if opponent.status == COMPETITOR_STATUS.ACTIVE: if not smwae_check( opponent_user.user_id, get_translation_for( 'challenge_you_are_challenged_msg').format( user.user_id, competitor.name, competitor.level, config.time_to_accept_challenge), opponent_user, reply_markup= get_challenge_confirmation_keyboard(), parse_mode='html'): bot.send_message( callback.message.chat.id, get_translation_for( 'error_bot_blocked_by_opponent_challenge_canceled_msg' ) + '\n' + get_translation_for( 'error_bot_blocked_by_opponent_challenge_canceled_contact_admin_str' ), parse_mode='html') return RET.ANSWER_AND_GO_TO_STATE, 'MenuState', callback, user opponent_user.reload() opponent_user.states.append('ChallengeReceivedState') if len(opponent_user.states) > STATES_HISTORY_LEN: del opponent_user.states[0] opponent_user.save() elif opponent.status == COMPETITOR_STATUS.PASSIVE: if not smwae_check( opponent_user.user_id, get_translation_for( 'challenge_you_are_challenged_passive_msg' ).format(user.user_id, competitor.name, competitor.level, config.time_to_accept_challenge), opponent_user, reply_markup= get_challenge_confirmation_keyboard(), parse_mode='html'): bot.send_message( callback.message.chat.id, get_translation_for( 'error_bot_blocked_by_opponent_challenge_canceled_msg' ) + '\n' + get_translation_for( 'error_bot_blocked_by_opponent_challenge_canceled_contact_admin_str' ), parse_mode='html') return RET.ANSWER_AND_GO_TO_STATE, 'MenuState', callback, user opponent_user.reload() opponent_user.states.append('ChallengeReceivedState') if len(opponent_user.states) > STATES_HISTORY_LEN: del opponent_user.states[0] opponent_user.save() else: logger.error( f'Trying to send message to opponent with incorrect state: {opponent.name} {opponent.legacy_number}' ) bot.send_message( callback.message.chat.id, get_translation_for( 'error_occurred_contact_administrator_msg'), parse_mode='html') return RET.ANSWER_AND_GO_TO_STATE, 'MenuState', callback, user opponent.previous_status = opponent.status opponent.change_status( COMPETITOR_STATUS.CHALLENGE_NEED_RESPONSE) opponent.in_challenge_with = competitor opponent.latest_challenge_received_at = datetime.now( tz=timezone('Europe/Kiev')) opponent.challenge_remainder_sent = False opponent.save() competitor.previous_status = competitor.status competitor.change_status( COMPETITOR_STATUS.CHALLENGE_INITIATED) competitor.in_challenge_with = opponent competitor.latest_challenge_sent_to = opponent competitor.challenge_remainder_sent = False competitor.save() user.dismiss_confirmed = False user.save() bot.send_message( callback.message.chat.id, get_translation_for('challenge_sent_msg').format( opponent_user.user_id, opponent.name), parse_mode='html') LogsSheet.glog( get_translation_for( 'gsheet_log_player_created_challenge_for_player'). format(competitor.name, opponent.name)) return RET.ANSWER_AND_GO_TO_STATE, 'MenuState', callback, user except ValidationError: logger.exception( f'Incorrect opponent_id: {opponent_id} from user with user_id: {user.user_id}' ) elif data.find('cancel_send') == 0: if not self.update_and_paginate(callback.message, user, bot, competitor, update=True, updateText=True): bot.send_message( callback.message.chat.id, get_translation_for( 'challenge_no_applicable_competitors_msg')) elif data.find('back') == 0: return RET.ANSWER_AND_GO_TO_STATE, 'MenuState', callback, user return RET.ANSWER_CALLBACK, None, callback, user
A2 = group_xls("../data/2015/ks5_subjects.xlsx", "1.A level", group_by="URN", extract=["Subject", "Number entered", "Grade A*", "Grade A", "Grade B", "Grade C", "Grade D", "Grade E", "No results"], header_row=2) for school in A2: urn = str(school["URN"]) subjects = {} for row in school["rows"]: total = int(row["Number entered"]) subject = subject_name(row["Subject"]) if total > 5: subjects[subject] = { "A*": to_int(row["Grade A*"]), "A": to_int(row["Grade A"]), "B": to_int(row["Grade B"]), "C": to_int(row["Grade C"]), "D": to_int(row["Grade D"]), "E": to_int(row["Grade E"]), "NR": to_int(row["No results"]), "total": total } else: subjects[subject] = { "total": total } db.schools.update( { "_id": urn },