/
server.py
57 lines (40 loc) · 1.69 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import socket
from urllib.parse import urljoin
from django.contrib.sites.models import Site
from djblets.siteconfig.models import SiteConfiguration
from reviewboard.site.urlresolvers import local_site_reverse
def get_server_url(local_site_name=None, local_site=None, request=None):
"""Return the URL for the root of the server.
This will construct a URL that points to the root of the server, factoring
in whether to use HTTP or HTTPS.
If ``local_site_name`` or ``local_site`` is provided, then the URL will be
the root to the LocalSite's root, rather than the server's root.
If ``request`` is provided, then the Local Site, if any, will be
inferred from the request.
"""
site = Site.objects.get_current()
siteconfig = SiteConfiguration.objects.get_current()
root = local_site_reverse('root', local_site_name=local_site_name,
local_site=local_site, request=request)
return '%s://%s%s' % (siteconfig.get('site_domain_method'),
site.domain, root)
def build_server_url(path=None, **kwargs):
"""Build an absolute URL containing the full URL to the server.
A path can be supplied that will be joined to the server URL.
Args:
path (unicode):
The path to append to the server URL.
**kwargs (dict):
Additional arguments to pass to :py:func:`get_server_url`.
Returns:
unicode:
The resulting URL.
"""
return urljoin(get_server_url(**kwargs), path)
def get_hostname():
"""Return the hostname for this Review Board server.
Returns:
unicode:
The hostname for the server.
"""
return str(socket.gethostname())