def build_url(netloc, port=None, scheme=None, path="", query=None, fragment=""): """ Builds a url given all its parts :netloc: string :port: int :scheme: string :path: string :query: A dictionary with any GET parameters :fragment: string :return: URL string """ if query: query = quote_plus(urlencode(query)) if scheme is None: scheme = GlobalRequestMiddleware.get_current_request().scheme if port is not None: netloc = replace_netloc_port(netloc, port) return SplitResult( scheme=scheme, netloc=netloc, path=path, query=query or "", fragment=fragment, ).geturl()
def reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None): """ This monkey patch will add the journal_code to reverse kwargs if the URL_CONFIG setting is set to 'patch' """ if not viewname.startswith('djdt'): local_request = GlobalRequestMiddleware.get_current_request() if settings.URL_CONFIG == 'path': code = local_request.journal.code if local_request.journal else 'press' if kwargs and not args: kwargs['journal_code'] = code else: kwargs = {'journal_code': code} # Drop kwargs if user is accessing admin site. if local_request.path.startswith('/admin/'): kwargs.pop('journal_code') # Drop kwargs if we have args (most likely from the template if args: kwargs = None args = [code] + args url = django_reverse(viewname, urlconf, args, kwargs, current_app) # Ensure any unicode characters in the URL are escaped. return iri_to_uri(url)
def requestless_url(self): from core.middleware import GlobalRequestMiddleware local_request = GlobalRequestMiddleware.get_current_request() if local_request.journal: return local_request.journal_base_url else: return local_request.press_base_url
def url(self): from core.middleware import GlobalRequestMiddleware request = GlobalRequestMiddleware.get_current_request() url_kwargs = {'file_id': self.pk} if request.journal and self.article_id: raise NotImplementedError elif request.journal: raise NotImplementedError else: return reverse( 'serve_press_file', kwargs=url_kwargs, )
def save(self, commit=True): press = super(PressForm, self).save(commit=False) request = GlobalRequestMiddleware.get_current_request() file = self.cleaned_data.get('press_logo', None) if file: file = files.save_file_to_press(request, file, 'Press Logo', '') # Delete the old file from the disk if press.thumbnail_image: press.thumbnail_image.delete() press.thumbnail_image = file if commit: press.save() return press
def get_current_request(): try: return GlobalRequestMiddleware.get_current_request() except (KeyError, AttributeError): return None