-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlast_competed.py
51 lines (40 loc) · 1.9 KB
/
last_competed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import polars as pl
import pandas as pd
from datetime import datetime,timedelta
# from models import *
import os
from models import Users,External_payments
def get_date(x):
return f"{x['year']}-{x['endMonth']}-{x['endDay']}"
def get_df():
script_dir = os.path.dirname(os.path.abspath(__file__))
comp_path = os.path.join(script_dir, '../wca_export/WCA_export_Competitions.tsv')
res_path = os.path.join(script_dir, '../wca_export/WCA_export_Results.tsv')
dk = (pl.read_csv(comp_path,sep='\t')).lazy().filter(pl.col('countryId')=='Denmark')
res = pl.read_csv(res_path,sep='\t').lazy()
dk_res = dk.join(res,left_on='id',right_on='competitionId').select(["id","personId","personName","endDay","endMonth","year"])
dk_res = dk_res.groupby(['id','personId']).first().collect().to_pandas()
dk_res['Sidste comp'] = pd.to_datetime(dk_res.apply(get_date,axis=1))
max_dates = dk_res.groupby('personId')['Sidste comp'].idxmax()
dk_res = dk_res.loc[max_dates]
dk_res = dk_res.rename(columns={'personId':'WCA ID',"personName":'Navn'})
return dk_res
def get_last_competed(df,wcaid):
if not pd.isnull(wcaid):
try:
competed = df[df['WCA ID']==wcaid]['Sidste comp'].iloc[0].to_pydatetime()
return 200,competed
except IndexError: # If they have a WCA ID, but haven't competed in Denmark
return 404,'blank'
return 404,"blank"
def active_member(user:Users):
fourteen_months_ago = datetime.now() - timedelta(days=14*30)
if user.sidste_comp:
eligible = user.sidste_comp >= fourteen_months_ago
else:
eligible = False
wants_to = user.medlem
payment = External_payments.query.filter_by(user_id=user.user_id).order_by(External_payments.payment_date.desc()).first()
has_payment = payment is not None and payment.payment_date >= fourteen_months_ago
eligible = eligible or has_payment
return eligible,wants_to