132 lines
3.2 KiB
Python
132 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
|
|
try:
|
|
|
|
from settings import config
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
class config:
|
|
base_url='/'
|
|
domain_url='http://localhost:8080'
|
|
media_url='/'
|
|
yes_static=True
|
|
|
|
|
|
from bottle import request, response, HTTPResponse
|
|
import urllib.parse
|
|
|
|
# A modified version of bottle url for don't need set x-proxy for redirect. Probably is not a good idea.
|
|
|
|
def redirect(url, code=None):
|
|
""" Aborts execution and causes a 303 or 302 redirect, depending on
|
|
the HTTP protocol version. """
|
|
if not code:
|
|
code = 303 if request.get('SERVER_PROTOCOL') == "HTTP/1.1" else 302
|
|
res = response.copy(cls=HTTPResponse)
|
|
res.status = code
|
|
res.body = ""
|
|
res.set_header('Location', url)
|
|
raise res
|
|
|
|
#Simple method for make urls
|
|
|
|
def make_url(path, query_args={}):
|
|
|
|
"""
|
|
This is a method for create urls for the system
|
|
|
|
Keyword arguments:
|
|
path -- The path to the module
|
|
query_args -- a ser of get variables for add to url
|
|
|
|
"""
|
|
|
|
get_query=''
|
|
|
|
if len(query_args)>0:
|
|
|
|
#get_query='?'+"&".join( [x+'='+y for x,y in query_args.items()] )
|
|
get_query='?'+urllib.parse.urlencode(query_args)
|
|
|
|
return config.base_url+path+get_query
|
|
|
|
def make_url_domain(path, query_args={}):
|
|
|
|
"""
|
|
This is a method for create an url with the real domain of site how prefix
|
|
|
|
Keyword arguments:
|
|
path -- The path of the module
|
|
query_args -- a ser of get variables for add to url
|
|
|
|
"""
|
|
|
|
return config.domain_url+make_url(path, query_args)
|
|
|
|
def add_get_parameters(url, **args):
|
|
|
|
added_url='&'
|
|
|
|
if url.find('?')==-1:
|
|
added_url='?'
|
|
|
|
get_query=urllib.parse.urlencode(args)
|
|
|
|
return url+added_url+get_query
|
|
|
|
def make_external_url(path, query_args={}):
|
|
|
|
"""
|
|
This is a method for create urls for external systems
|
|
|
|
Keyword arguments:
|
|
path -- The base url of the url
|
|
query_args -- a ser of get variables for add to url
|
|
|
|
"""
|
|
|
|
get_query=''
|
|
|
|
if len(query_args)>0:
|
|
|
|
#get_query='?'+"&".join( [x+'='+y for x,y in query_args.items()] )
|
|
get_query='?'+urllib.parse.urlencode(query_args)
|
|
|
|
return path+get_query
|
|
|
|
def get_actual_url():
|
|
|
|
return config.base_url[:len(config.base_url)-1]+request.path
|
|
|
|
if config.yes_static==True:
|
|
|
|
def make_media_url(file_path, module=''):
|
|
|
|
if module=='':
|
|
|
|
return config.media_url+'media/'+file_path
|
|
|
|
else:
|
|
|
|
return make_media_url_module(file_path, module)
|
|
|
|
def make_media_url_module(file_path, module):
|
|
|
|
return config.media_url+'mediafrom/'+module+'/'+file_path
|
|
else:
|
|
|
|
def make_media_url(file_path, module=''):
|
|
|
|
if module=='':
|
|
|
|
return config.media_url+'media/'+file_path
|
|
|
|
else:
|
|
|
|
return make_media_url_module(file_path, module)
|
|
|
|
|
|
def make_media_url_module(file_path, module):
|
|
|
|
return config.media_url+'media/'+module+'/'+file_path
|