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 # Lexers which have wordwrap enabled by default
LEXER_WORDWRAP = ('rst',) 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. # Key names of the default text and code lexer.
PLAIN_TEXT_SYMBOL = '_text' PLAIN_TEXT_SYMBOL = '_text'
PLAIN_CODE_SYMBOL = '_code' PLAIN_CODE_SYMBOL = '_code'
@ -184,7 +169,7 @@ class dpasteAppConfig(AppConfig):
('prolog', 'Prolog'), ('prolog', 'Prolog'),
('properties', 'Properties'), ('properties', 'Properties'),
('puppet', 'Puppet'), ('puppet', 'Puppet'),
('python', 'Python'), # Default lexer ('python', 'Python'), # Default lexer
('r', 'R'), ('r', 'R'),
('rb', 'Ruby'), ('rb', 'Ruby'),
('rst', 'reStructuredText'), ('rst', 'reStructuredText'),
@ -208,3 +193,18 @@ class dpasteAppConfig(AppConfig):
('xslt', 'XSLT'), ('xslt', 'XSLT'),
('yaml', 'YAML'), ('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 # Set parent snippet
self.instance.parent = parent 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. # null state of the db column for that.
self.instance.expires = self.cleaned_data['expires'] self.instance.expires = self.cleaned_data['expires']
self.instance.expire_type = self.cleaned_data['expire_type'] 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. The default response is the snippet URL wrapped in quotes.
""" """
return '"{url}{path}"'.format(url=config.BASE_URL, base_url = config.get_base_url(request=self.request)
path=s.get_absolute_url()) return '"{url}{path}"'.format(url=base_url, path=s.get_absolute_url())
def _format_url(self, s): 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, base_url = config.get_base_url(request=self.request)
path=s.get_absolute_url()) return '{url}{path}\n'.format(url=base_url, path=s.get_absolute_url())
def _format_json(self, s): def _format_json(self, s):
""" """
The `json` format export. The `json` format export.
""" """
base_url = config.get_base_url(request=self.request)
return json.dumps({ return json.dumps({
'url': '{url}{path}'.format(url=config.BASE_URL, 'url': '{url}{path}'.format(url=base_url, path=s.get_absolute_url()),
path=s.get_absolute_url()),
'content': s.content, 'content': s.content,
'lexer': s.lexer, 'lexer': s.lexer,
}) })