Use the request to properly determine the site.

This commit is contained in:
Martin Mahner 2018-06-23 15:04:21 +02:00
parent 3851785b6e
commit cd81a68182
3 changed files with 25 additions and 24 deletions

View file

@ -75,21 +75,6 @@ class dpasteAppConfig(AppConfig):
# Lexers which have wordwrap enabled by default
LEXER_WORDWRAP = ('rst',)
@property
def BASE_URL(self, request=None):
"""
String. The full qualified hostname and path to the dpaste instance.
This is used to generate a link in the API response. If the "Sites"
framework is installed, it uses the current Site domain. Otherwise
it falls back to 'https://dpaste.de'
"""
if apps.is_installed('django.contrib.sites'):
from django.contrib.sites.shortcuts import get_current_site
site = get_current_site(request)
if site:
return 'https://{0}'.format(site.domain)
return 'https://dpaste.de'
# Key names of the default text and code lexer.
PLAIN_TEXT_SYMBOL = '_text'
PLAIN_CODE_SYMBOL = '_code'
@ -208,3 +193,18 @@ class dpasteAppConfig(AppConfig):
('xslt', 'XSLT'),
('yaml', 'YAML'),
]
@staticmethod
def get_base_url(request=None):
"""
String. The full qualified hostname and path to the dpaste instance.
This is used to generate a link in the API response. If the "Sites"
framework is installed, it uses the current Site domain. Otherwise
it falls back to 'https://dpaste.de'
"""
if apps.is_installed('django.contrib.sites'):
from django.contrib.sites.shortcuts import get_current_site
site = get_current_site(request)
if site:
return 'https://{0}'.format(site.domain)
return 'https://dpaste.de'

View file

@ -99,7 +99,7 @@ class SnippetForm(forms.ModelForm):
# Set parent snippet
self.instance.parent = parent
# Add expire datestamp. None indicates 'keep forever', use the default
# Add expire timestamp. None indicates 'keep forever', use the default
# null state of the db column for that.
self.instance.expires = self.cleaned_data['expires']
self.instance.expire_type = self.cleaned_data['expire_type']

View file

@ -188,23 +188,24 @@ class APIView(View):
"""
The default response is the snippet URL wrapped in quotes.
"""
return '"{url}{path}"'.format(url=config.BASE_URL,
path=s.get_absolute_url())
base_url = config.get_base_url(request=self.request)
return '"{url}{path}"'.format(url=base_url, path=s.get_absolute_url())
def _format_url(self, s):
"""
The `url` format returns the snippet URL, no quotes, but a linebreak after.
The `url` format returns the snippet URL,
no quotes, but a linebreak at the end.
"""
return '{url}{path}\n'.format(url=config.BASE_URL,
path=s.get_absolute_url())
base_url = config.get_base_url(request=self.request)
return '{url}{path}\n'.format(url=base_url, path=s.get_absolute_url())
def _format_json(self, s):
"""
The `json` format export.
"""
base_url = config.get_base_url(request=self.request)
return json.dumps({
'url': '{url}{path}'.format(url=config.BASE_URL,
path=s.get_absolute_url()),
'url': '{url}{path}'.format(url=base_url, path=s.get_absolute_url()),
'content': s.content,
'lexer': s.lexer,
})