-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlast_competed.py
More file actions
60 lines (47 loc) · 2.16 KB
/
last_competed.py
File metadata and controls
60 lines (47 loc) · 2.16 KB
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
52
53
54
55
56
57
58
59
60
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.scan_csv(comp_path,separator='\t',quote_char='',low_memory=True)).select(["countryId", "id","endDay","endMonth","year"]).filter(pl.col('countryId')=='Denmark')
res = pl.scan_csv(res_path,separator='\t',quote_char='',low_memory=True).select(["competitionId", "personId"])
dk_res = dk.join(res,left_on='id',right_on='competitionId')
dk_res = dk_res.group_by(['id','personId']).first()
dk_res = dk_res.with_columns(
pl.concat_str(
[pl.col("year").cast(str), pl.col("endMonth").cast(str), pl.col("endDay").cast(str)],
separator="-"
).str.strptime(pl.Date, "%Y-%m-%d").alias("Sidste comp")
).select(["Sidste comp","personId"])
dk_res = (
dk_res.sort("Sidste comp", descending=True)
.group_by("personId")
.first()
).collect()
return dk_res
def get_last_competed(df,wcaid):
if not pd.isnull(wcaid):
try:
competed = df.filter(pl.col("personId") == wcaid).select("Sidste comp").to_series(0).to_list()[0]
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