import os
from django.conf import settings
from django.contrib.staticfiles import finders
from datetime import datetime
from app.constants import MESES

def link_callback(uri, rel):
    sUrl = settings.STATIC_URL        # Typically /static/
    sRoot = settings.STATIC_ROOT      # Typically /home/userX/project_static/
    mUrl = settings.MEDIA_URL         # Typically /media/
    mRoot = settings.MEDIA_ROOT       # Typically /home/userX/project_static/media/

    if uri.startswith(mUrl):
        path = os.path.join(mRoot, uri.replace(mUrl, ""))
    elif uri.startswith(sUrl):
        path = os.path.join(sRoot, uri.replace(sUrl, ""))
    else:
        return uri

    # make sure that file exists
    if not os.path.isfile(path):
        raise Exception('media URI must start with %s or %s' % (sUrl, mUrl))
    return path

def convertir_fecha(fecha: str) -> datetime:
    formato_fecha = '%B %d, %Y' # January 28, 2021
    for m in MESES:
        if m == fecha.split(' ')[0]:
            return datetime.strptime(fecha.replace(m, MESES[m]), formato_fecha)  