import os
import misc

from storage import StorableObject
from quixote import get_publisher

class Host(StorableObject):
    _names = 'hosts'

    label = None
    name = None
    orig_site = None
    auth_url = None
    auth_form_page_url = None
    auth_form = None
    logout_url = None
    reversed_hostname = None
    reversed_directory = None
    organization_name = None
    private_key = None
    public_key = None
    return_url = '/'

    def get_host_from_url(cls):
        try:
            return list(Host.select(lambda x: x.name == misc.get_proxied_site_name()))[0]
#            return list(Host.select(lambda x: x.reversed_hostname == misc.get_proxied_site_domain() \
#                                          and x.reversed_directory == misc.get_proxied_site_name()))[0]
        except:
#            try:
#                return list(Host.select(lambda x: x.reversed_hostname == misc.get_proxied_site_domain() \
#                                              and x.reversed_directory is None))[0]            
#            except:
            return None
    get_host_from_url = classmethod(get_host_from_url)

    def get_return_url(self):
        if self.return_url.startswith('/'):
            if self.reversed_directory is not None:
                return '/' + self.reversed_directory + self.return_url
            else:
                return self.return_url
        # In this case, must be a full url                
        return self.return_url
