2018-09-10 09:55:26 -07:00
|
|
|
import datetime
|
2018-11-02 09:07:06 -07:00
|
|
|
import random
|
2018-11-16 08:16:52 -08:00
|
|
|
import re
|
2019-01-23 07:00:30 -08:00
|
|
|
import string
|
|
|
|
import time
|
|
|
|
|
2018-11-02 09:07:06 -07:00
|
|
|
from grant.settings import SITE_URL
|
2018-09-10 09:55:26 -07:00
|
|
|
|
|
|
|
epoch = datetime.datetime.utcfromtimestamp(0)
|
|
|
|
|
|
|
|
|
|
|
|
def dt_from_ms(ms):
|
|
|
|
return datetime.datetime.utcfromtimestamp(ms / 1000.0)
|
|
|
|
|
|
|
|
|
|
|
|
def dt_to_ms(dt):
|
|
|
|
delta = dt - epoch
|
|
|
|
return int(delta.total_seconds() * 1000)
|
|
|
|
|
|
|
|
|
|
|
|
def dt_to_unix(dt):
|
|
|
|
return int(time.mktime(dt.timetuple()))
|
2018-11-02 09:07:06 -07:00
|
|
|
|
2019-01-23 07:00:30 -08:00
|
|
|
|
2018-11-02 09:07:06 -07:00
|
|
|
def gen_random_code(length=32):
|
2019-01-23 07:00:30 -08:00
|
|
|
return ''.join(
|
|
|
|
[random.choice(string.ascii_letters + string.digits) for n in range(length)]
|
|
|
|
)
|
|
|
|
|
2018-11-02 09:07:06 -07:00
|
|
|
|
|
|
|
def make_url(path: str):
|
2019-01-23 07:00:30 -08:00
|
|
|
return f'{SITE_URL}{path}'
|
|
|
|
|
2018-11-16 08:16:52 -08:00
|
|
|
|
|
|
|
def is_email(email: str):
|
2019-01-23 07:00:30 -08:00
|
|
|
return bool(re.match(r"[^@]+@[^@]+\.[^@]+", email))
|
|
|
|
|
2019-01-09 11:35:37 -08:00
|
|
|
|
|
|
|
def from_zat(zat: int):
|
2019-01-23 07:00:30 -08:00
|
|
|
return zat / 100000000
|
|
|
|
|
2019-01-09 11:35:37 -08:00
|
|
|
|
2019-01-09 11:45:16 -08:00
|
|
|
def to_zat(zec: float):
|
2019-01-23 07:00:30 -08:00
|
|
|
return zec * 100000000
|
|
|
|
|
2019-01-16 14:26:45 -08:00
|
|
|
|
|
|
|
def make_preview(content: str, max_length: int):
|
2019-01-23 07:00:30 -08:00
|
|
|
truncated = False
|
2019-01-16 14:26:45 -08:00
|
|
|
|
2019-01-23 07:00:30 -08:00
|
|
|
# Show only the first line. Add ellipsis if there are more than two lines,
|
|
|
|
# even if first line isn't truncated.
|
|
|
|
preview = content.split('\n', 1)[0]
|
|
|
|
if len(preview) != len(content):
|
|
|
|
truncated = True
|
2019-01-16 14:26:45 -08:00
|
|
|
|
2019-01-23 07:00:30 -08:00
|
|
|
# Truncate to max length
|
|
|
|
if len(preview) > max_length:
|
|
|
|
preview = preview[:max_length - 3]
|
|
|
|
truncated = True
|
2019-01-16 14:26:45 -08:00
|
|
|
|
2019-01-23 07:00:30 -08:00
|
|
|
return content + '...' if truncated else content
|