Ticket #32397: add-user-agent-to-django-admin-templates.diff

File add-user-agent-to-django-admin-templates.diff, 1.8 KB (added by rsp2k, 4 years ago)
  • django/core/management/templates.py

    diff --git a/django/core/management/templates.py b/django/core/management/templates.py
    a b  
    66import stat
    77import tempfile
    88from importlib import import_module
    9 from urllib.request import urlretrieve
     9from urllib.request import Request, urlopen
    1010
    1111import django
    1212from django.conf import settings
     
    262262        if self.verbosity >= 2:
    263263            self.stdout.write('Downloading %s' % display_url)
    264264        try:
    265             the_path, info = urlretrieve(url, os.path.join(tempdir, filename))
     265            response = urlopen(Request(url, headers={'User-Agent': f'Django/{django.getversion()}'}))
     266            the_path = os.path.join(tempdir, filename)
     267            with open(the_path, 'wb') as f:
     268                f.write(response.read())
     269
    266270        except OSError as e:
    267271            raise CommandError("couldn't download URL %s to %s: %s" %
    268272                               (url, filename, e))
     
    270274        used_name = the_path.split('/')[-1]
    271275
    272276        # Trying to get better name from response headers
    273         content_disposition = info.get('content-disposition')
     277        content_disposition = response.info.getheader('content-disposition')
    274278        if content_disposition:
    275279            _, params = cgi.parse_header(content_disposition)
    276280            guessed_filename = params.get('filename') or used_name
     
    279283
    280284        # Falling back to content type guessing
    281285        ext = self.splitext(guessed_filename)[1]
    282         content_type = info.get('content-type')
     286        content_type = response.info.getheader('content-type')
    283287        if not ext and content_type:
    284288            ext = mimetypes.guess_extension(content_type)
    285289            if ext:
Back to Top