mirror of
https://github.com/boostorg/website-v2.git
synced 2026-02-27 17:42:08 +00:00
57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
from datetime import datetime
|
|
from dateutil.relativedelta import relativedelta
|
|
import structlog
|
|
import tempfile
|
|
|
|
from dateutil.parser import ParserError, parse
|
|
from django.utils.text import slugify
|
|
|
|
|
|
logger = structlog.get_logger()
|
|
|
|
|
|
def decode_content(content):
|
|
"""Decode bytes to string."""
|
|
if isinstance(content, bytes):
|
|
return content.decode("utf-8")
|
|
return content
|
|
|
|
|
|
def generate_fake_email(val: str) -> str:
|
|
"""Slugify a string to make a fake email.
|
|
|
|
Would not necessarily be unique -- this is a lazy way for us to avoid creating
|
|
multiple new user records for one contributor who contributes to multiple libraries.
|
|
"""
|
|
slug = slugify(val)
|
|
local_email = slug.replace("-", "_")[:50]
|
|
return f"{local_email}@example.com"
|
|
|
|
|
|
def get_first_last_day_last_month():
|
|
now = datetime.now()
|
|
first_day_this_month = now.replace(day=1)
|
|
last_day_last_month = first_day_this_month - relativedelta(days=1)
|
|
first_day_last_month = last_day_last_month.replace(day=1)
|
|
return first_day_last_month, last_day_last_month
|
|
|
|
|
|
def parse_date(date_str):
|
|
"""Parses a date string to a datetime. Does not return an error."""
|
|
try:
|
|
return parse(date_str)
|
|
except ParserError:
|
|
logger.info("parse_date_invalid_date", date_str=date_str)
|
|
return None
|
|
|
|
|
|
def write_content_to_tempfile(content):
|
|
"""Accepts string or bytes content, writes it to a temporary file, and returns the
|
|
file object."""
|
|
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
|
if isinstance(content, bytes):
|
|
temp_file = open(temp_file.name, "wb")
|
|
temp_file.write(content)
|
|
temp_file.close()
|
|
return temp_file
|