-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
71 lines (59 loc) · 1.9 KB
/
helpers.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
from re import X
from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session
from flask_session import Session
import requests
from functools import wraps
from tempfile import mkdtemp
from datetime import datetime, timezone
import pytz
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/login")
return f(*args, **kwargs)
return decorated_function
def toLitres(value):
return f"{value / 33.814:.2f}"
def calculateSleep(age):
if age in range (1, 2):
return "11 to 14 hours"
elif age in range (3, 5, 1):
return "10 to 13 hours"
elif age in range (6, 12, 1):
return "9 to 12 hours"
elif age in range (13, 18, 1):
return "8 to 10 hours"
elif age in range (18, 64, 1):
return "7 to 9 hours"
elif age >= 65:
return "7 to 8 hours"
def cmToFeet(height):
totalInches = float(height) / 2.54
feet = int(totalInches / 12)
inches = totalInches % 12
return f"{feet} feet {inches:.2f} inches "
def KgToPounds(weight):
return f"{weight * 2.205:.2f} pounds"
def numExtraction(string):
for i in range(len(string) - 1):
if string[i].isnumeric():
if string[i + 1].isnumeric():
return int((string[i] + string[i + 1]))
return int((string[i]))
def getTimeZone(query):
query = "122.161.78.226"
url = f"http://ip-api.com/json/{query}?fields=status,message,timezone,offset,query"
response = requests.get(url)
response = response.json()
try:
return response
except:
return None
def convertToUserTZ(time):
format = "%I:%M %p"
dt = datetime.strptime(time, '%d-%m-%Y %H:%M').replace(tzinfo=timezone.utc)
dt = dt.astimezone(pytz.timezone(session["time_zone_3"]))
return dt.strftime(format)