From 1068bc6f03d28113068a63c1cdc0aec5134eb42e Mon Sep 17 00:00:00 2001 From: Lukas P Date: Tue, 6 Dec 2016 16:27:00 -0500 Subject: [PATCH 1/5] Replaces url.patterns and string view arguments Fixes: RemovedInDjango110Warning: django.conf.urls.patterns() is deprecated and will be removed in Django 1.10. Update your urlpatterns to be a list of django.conf.urls.url() instances instead. RemovedInDjango110Warning: Support for string view arguments to url() is deprecated and will be removed in Django 1.10 Details: https://docs.djangoproject.com/en/dev/internals/deprecation /#deprecation-removed-in-1-10 https://docs.djangoproject.com/en/1.10/ref/views /#serving-files-in-development https://docs.djangoproject.com/en/1.10/ref/urls/#url --- html/urls.py | 43 ++++++++++++++++++++++++------------------- urls.py | 19 ++++++++++--------- xmlrpc/urls.py | 10 ++++++---- 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/html/urls.py b/html/urls.py index 0785afe..d684a77 100644 --- a/html/urls.py +++ b/html/urls.py @@ -13,44 +13,49 @@ Custom Installer Builder. """ -from django.conf.urls import patterns, url +from django.conf.urls import url import custominstallerbuilder.common.constants as constants +import custominstallerbuilder.html.views as views -urlpatterns = patterns('custominstallerbuilder.html.views', +urlpatterns = [ # Builder interface. - + # These patterns match the string between ^ and $ against the end portion of # the URL. # Example: http://example.com/custominstallerbuilder/ # or http://example.com/custominstallerbuilder/ajax/build/ - url(r'^$', 'builder_page', name='builder'), - url(r'^fastlane/$', 'fastlane_page', name='fastlane_page'), - url(r'^ajax/build/$', 'build_installers', name='ajax-build'), - url(r'^ajax/save/$', 'save_state', name='ajax-save'), - url(r'^ajax/restore/$', 'restore_state', name='ajax-restore'), - url(r'^ajax/reset/$', 'reset_state', name='ajax-reset'), - url(r'^ajax/add-user/$', 'add_user', name='ajax-add-user'), + url(r'^$', views.builder_page, name='builder'), + url(r'^fastlane/$', views.fastlane_page, name='fastlane_page'), + url(r'^ajax/build/$', views.build_installers, name='ajax-build'), + url(r'^ajax/save/$', views.save_state, name='ajax-save'), + url(r'^ajax/restore/$', views.restore_state, name='ajax-restore'), + url(r'^ajax/reset/$', views.reset_state, name='ajax-reset'), + url(r'^ajax/add-user/$', views.add_user, name='ajax-add-user'), # Download pages. - + # This pattern matches against the end portion of a URL with a valid build # ID (as specified by custominstallerbuilder.common.constants.BUILD_ID_REGEX) # followed by a slash. # Example: http://example.com/custominstallerbuilder/28d0ccc35d16fc9114f47f251968b3354183544c/ - url(r'^(?P' + constants.BUILD_ID_REGEX + ')/$', 'download_installers_page', name='download-installers-page'), + url(r'^(?P' + constants.BUILD_ID_REGEX + ')/$', + views.download_installers_page, name='download-installers-page'), # This pattern extends the one above by appending 'keys/' to the URL. - url(r'^(?P' + constants.BUILD_ID_REGEX + ')/keys/$', 'download_keys_page', name='download-keys-page'), - + url(r'^(?P' + constants.BUILD_ID_REGEX + ')/keys/$', + views.download_keys_page, name='download-keys-page'), + # This pattern extends the one above by further matching against a build ID # and two lowercase alphabetic strings, each followed by a slash. # Example: http://example.com/custominstallerbuilder/28d0ccc35d16fc9114f47f251968b3354183544c/foo/bar/ - url(r'^(?P' + constants.BUILD_ID_REGEX + ')/installers/(?P[a-z]+)/$', - 'download_installer', name='download-installer'), - url(r'^(?P' + constants.BUILD_ID_REGEX + ')/keys/(?P[a-z]+)/$', - 'download_keys', name='download-keys'), -) + url(r'^(?P' + + constants.BUILD_ID_REGEX + ')/installers/(?P[a-z]+)/$', + views.download_installer, name='download-installer'), + url(r'^(?P' + + constants.BUILD_ID_REGEX + ')/keys/(?P[a-z]+)/$', + views.download_keys, name='download-keys'), +] diff --git a/urls.py b/urls.py index 3e526c6..6942a33 100644 --- a/urls.py +++ b/urls.py @@ -14,14 +14,14 @@ initiates a static file server if local settings call for it. """ -from django.conf.urls import include, patterns, url +from django.conf.urls import include, url from django.conf import settings - +from django.views.static import serve # Display errors gracefully. handler500 = 'custominstallerbuilder.html.views.error_page' -urlpatterns = patterns('', +urlpatterns = [ # XML-RPC URL patterns. Matches all URLs which start with 'xmlrpc' followed # by a slash. # Example: http://example.com/custominstallerbuilder/xmlrpc/[...] @@ -32,18 +32,19 @@ # Example: http://example.com/custominstallerbuilder/ # or http://example.com/custominstallerbuilder/ajax/build/ url(r'', include('custominstallerbuilder.html.urls')), -) +] # Use the 'SERVE_STATIC' and 'STATIC_BASE' custom settings to determine if # Django should serve static files itself. This is useful for debugging. if getattr(settings, 'SERVE_STATIC', False): - + # Matches URLs which start with the contents of STATIC_BASE as specified in # settings.py, followed by a slash then any string. That string is # interpreted as a path for a file to lookup. # Example: http://example.com/custominstallerbuilder/static/[...] regex = r'^%s/(?P.*)$' % settings.STATIC_BASE.rstrip('/') - - urlpatterns += patterns('', - url(regex, 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': settings.DEBUG}), - ) + + urlpatterns += [ + url(regex, serve, + {'document_root': settings.MEDIA_ROOT, 'show_indexes': settings.DEBUG}), + ] diff --git a/xmlrpc/urls.py b/xmlrpc/urls.py index 8498d3d..b430920 100644 --- a/xmlrpc/urls.py +++ b/xmlrpc/urls.py @@ -13,15 +13,17 @@ Custom Installer Builder. """ -from django.conf.urls import patterns, url +from django.conf.urls import url +import custominstallerbuilder.xmlrpc.views as views + # Note: All URLs that have been delegated to these patterns have already # matched the '^xmlrpc/' prefix. These patterns test against the rest of the # URL string. -urlpatterns = patterns('', +urlpatterns = [ # Matches the empty string. # Example: http://example.com/custominstallerbuilder/xmlrpc/ - url(r'^$', 'custominstallerbuilder.xmlrpc.views.xmlrpc_handler', name='xmlrpc-handler'), -) + url(r'^$', views.xmlrpc_handler, name='xmlrpc-handler'), +] From cca4ffeabe803cbc7b00012ac9722305c09b0e18 Mon Sep 17 00:00:00 2001 From: Lukas P Date: Tue, 6 Dec 2016 16:39:20 -0500 Subject: [PATCH 2/5] Fixes BaseException.message deprecated in Python2.6 https://www.python.org/dev/peps/pep-0352/#transition-plan --- html/views.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/html/views.py b/html/views.py index 99bfc47..688e8fe 100644 --- a/html/views.py +++ b/html/views.py @@ -137,8 +137,8 @@ def build_installers(request): try: manager = BuildManager(vessel_list=build_data['vessels'], user_data=user_data) build_results = manager.prepare() - except validations.ValidationError, e: - return ErrorResponse(e.message) + except validations.ValidationError as e: + return ErrorResponse(e) except: log_exception(request) return ErrorResponse('Unknown error occured while trying to build the installers.') @@ -271,8 +271,8 @@ def ErrorResponse(message=''): if public_key is not None: validations.validate_public_key(public_key) - except validations.ValidationError, e: - return ErrorResponse(e.message) + except validations.ValidationError as e: + return ErrorResponse(e) except: log_exception(request) return ErrorResponse('Unknown error occured while trying to add user.') From cd42c8bf0ed5e002880ecc76f845a5b946368e9a Mon Sep 17 00:00:00 2001 From: Lukas P Date: Tue, 6 Dec 2016 16:41:05 -0500 Subject: [PATCH 3/5] Replaces mimetype keyword with content_type Fixes: DeprecationWarning: Using mimetype keyword argument is deprecated, use content_type instead Details: https://docs.djangoproject.com/en/dev/internals/deprecation /#deprecation-removed-in-2-0 --- html/views.py | 4 ++-- xmlrpc/views.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/html/views.py b/html/views.py index 688e8fe..c2616e1 100644 --- a/html/views.py +++ b/html/views.py @@ -91,10 +91,10 @@ def wrapper(request, *args, **kwargs): ###################### def TextResponse(message=''): - return HttpResponse(message, mimetype='text/plain') + return HttpResponse(message, content_type='text/plain') def ErrorResponse(message=''): - return HttpResponse(message, status=500, mimetype='text/plain') + return HttpResponse(message, status=500, content_type='text/plain') diff --git a/xmlrpc/views.py b/xmlrpc/views.py index 40feb1a..bece5a3 100644 --- a/xmlrpc/views.py +++ b/xmlrpc/views.py @@ -55,7 +55,7 @@ def xmlrpc_handler(request): xmlrpc_handler = CGIXMLRPCRequestHandler(allow_none=False, encoding=None) xmlrpc_handler.register_instance(PublicFunctions()) - response = HttpResponse(mimetype='application/xml') + response = HttpResponse(content_type='application/xml') response.write(xmlrpc_handler._marshaled_dispatch(request.body)) return response From f4de96147dd6ad1433793a6f0f6b34fab2893979 Mon Sep 17 00:00:00 2001 From: Lukas P Date: Tue, 6 Dec 2016 16:50:42 -0500 Subject: [PATCH 4/5] Replaces HttpResonse with FileResponse Fixes: Creating streaming responses with `HttpResponse` is deprecated. Use `StreamingHttpResponse` instead if you need the streaming behavior. Details: https://docs.djangoproject.com/en/1.10/releases/1.8 /#requests-and-responses https://docs.djangoproject.com/en/1.10/ref/request-response /#fileresponse-objects --- html/views.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/html/views.py b/html/views.py index c2616e1..7e2eaac 100644 --- a/html/views.py +++ b/html/views.py @@ -26,9 +26,9 @@ import json from django.conf import settings -from django.core.servers.basehttp import FileWrapper from django.core.urlresolvers import reverse -from django.http import Http404, HttpResponse, HttpResponseRedirect +from django.http import Http404, HttpResponse, HttpResponseRedirect, \ + FileResponse from django.shortcuts import render_to_response from django.template import RequestContext @@ -352,7 +352,9 @@ def download_keys(request, build_id, key_type): # Generally, it is undesirable to serve files directly through django, but # the key bundles should be very small and still download quickly. bundle_filename = key_filenames[key_type] - response = HttpResponse(FileWrapper(file(bundle_filename)), content_type='application/zip') + # FileResponse is a subclass of StreamingHttpResponse optimized + # for binary files requires Django >1.8 + response = FileResponse(open(bundle_filename), content_type='application/zip') response['Content-Disposition'] = 'attachment; filename=' + os.path.split(bundle_filename)[1] response['Content-Length'] = os.path.getsize(bundle_filename) From 4e4b6abac2cfa32f662f1dfeced6abfca5e1cb8e Mon Sep 17 00:00:00 2001 From: Lukas P Date: Tue, 6 Dec 2016 17:05:55 -0500 Subject: [PATCH 5/5] Upgrades template settings and render contexts - Replaces render_to_response with render - Replaces TEMPLATE_CONTEXT_PROCESSORS and TEMPLATE_LOADERS with TEMPLATES setting - Replaces core.context_processors.* with template.context_processors.* - Comments out unused (default) context_processors - Removes obsolete TEMPLATE_DEBUG Fixes: RemovedInDjango110Warning: django.core.context_processors is deprecated in favor of django.template.context_processors. RemovedInDjango110Warning: The context_instance argument of render_to_string is deprecated. Details: https://docs.djangoproject.com/en/1.10/ref/templates/api /#django-template-context-processors-debug https://docs.djangoproject.com/en/1.10/topics/http/shortcuts /#django.shortcuts.render https://docs.djangoproject.com/en/1.10/ref/templates/upgrading/ --- html/views.py | 63 ++++++++++++++++++++++------------------------- local/settings.py | 5 +++- settings_base.py | 34 ++++++++++++++++--------- xmlrpc/views.py | 11 ++++----- 4 files changed, 61 insertions(+), 52 deletions(-) diff --git a/html/views.py b/html/views.py index 7e2eaac..97cff96 100644 --- a/html/views.py +++ b/html/views.py @@ -29,8 +29,7 @@ from django.core.urlresolvers import reverse from django.http import Http404, HttpResponse, HttpResponseRedirect, \ FileResponse -from django.shortcuts import render_to_response -from django.template import RequestContext +from django.shortcuts import render import custominstallerbuilder.common.constants as constants import custominstallerbuilder.common.packager as packager @@ -177,9 +176,9 @@ def save_state(request): """ if 'build_string' not in request.POST: return ErrorResponse('Unable to save configuration.') - + request.session['build_string'] = request.POST['build_string'] - + return TextResponse() @@ -389,12 +388,10 @@ def builder_page(request): A Django response. """ - return render_to_response('builder.html', - { - 'step': 'build', - }, - context_instance=RequestContext(request), - ) + return render(request, 'builder.html', + { + 'step': 'build', + }) @@ -435,15 +432,13 @@ def download_keys_page(request, build_id): has_private_keys = True break - return render_to_response('download_keys.html', - { - 'build_id': build_id, - 'has_private_keys': has_private_keys, - 'keys_downloaded': keys_downloaded, - 'step': 'keys', - }, - context_instance=RequestContext(request) - ) + return render(request, 'download_keys.html', + { + 'build_id': build_id, + 'has_private_keys': has_private_keys, + 'keys_downloaded': keys_downloaded, + 'step': 'keys', + }) @@ -467,7 +462,7 @@ def download_installers_page(request, build_id): """ manager = BuildManager(build_id=build_id) - + # Invalid build IDs should results in an error. if not os.path.isdir(manager.get_build_directory()): raise Http404 @@ -494,16 +489,14 @@ def download_installers_page(request, build_id): if 'fast_lane_build' in request.session['build_results'][build_id]: step = False - return render_to_response('download_installers.html', - { - 'build_id': build_id, - 'installers': installer_links, - 'share_url': share_url, - 'step': step, - 'user_built': user_built, - }, - context_instance=RequestContext(request) - ) + return render(request, 'download_installers.html', + { + 'build_id': build_id, + 'installers': installer_links, + 'share_url': share_url, + 'step': step, + 'user_built': user_built, + }) @@ -605,13 +598,13 @@ def fastlane_page(request): args=[build_id])) - return render_to_response('download_installers.html', { + return render(request, 'download_installers.html', { 'fast_lane': True, 'build_id': build_id, 'installers': installer_links, 'share_url': share_url, 'keys_downloaded': keys_downloaded, - }, context_instance=RequestContext(request)) + }) @@ -634,5 +627,7 @@ def error_page(request): # Automatically choose the email address of the first administrator given # in the settings file. - return render_to_response('error.html', {'email': settings.ADMINS[0][1]}, - context_instance=RequestContext(request)) + return render(request, 'error.html', + { + 'email': settings.ADMINS[0][1] + }) diff --git a/local/settings.py b/local/settings.py index 86f811c..c865ef4 100644 --- a/local/settings.py +++ b/local/settings.py @@ -28,7 +28,6 @@ # Unless you are actively debugging, these should be set to False. DEBUG = True -TEMPLATE_DEBUG = DEBUG # During testing, you may want to use Django's built-in static file server. SERVE_STATIC = False @@ -45,3 +44,7 @@ # The locations of the customized installers created by this program. CUSTOM_INSTALLER_URL = PROJECT_URL + 'static/installers/' + +# If you set DEBUG to False, you also need to properly set the ALLOWED_HOSTS +# setting, eg.: +# ALLOWED_HOSTS = ["example.com"] diff --git a/settings_base.py b/settings_base.py index a120ee4..3641811 100644 --- a/settings_base.py +++ b/settings_base.py @@ -25,26 +25,39 @@ ROOT_URLCONF = 'custominstallerbuilder.urls' -TEMPLATE_CONTEXT_PROCESSORS = ( - 'django.core.context_processors.debug', - 'django.core.context_processors.media', - 'django.core.context_processors.request', -) +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + # Adds 'debug' and 'sql_queries' to template context + #'django.template.context_processors.debug', + # Adds 'LANGUAGES' and 'LANGUAGE_CODE' to template context + # 'django.template.context_processors.i18n', + 'django.template.context_processors.media', + # Adds 'STATIC_URL' to template context + # 'django.template.context_processors.static', + # Adds 'TIME_ZONE' to template context + # 'django.template.context_processors.tz', + # Adds 'request' to template context + # 'django.template.context_processors.request', + # Adds 'messages' and 'DEFAULT_MESSAGE_LEVELS' to context + #'django.contrib.messages.context_processors.messages', + ], + }, + }, +] -TEMPLATE_LOADERS = ( - 'django.template.loaders.app_directories.Loader', -) MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', - 'custominstallerbuilder.common.logging.AutoLogger', ) INSTALLED_APPS = ( 'django.contrib.sessions', - 'custominstallerbuilder.common', 'custominstallerbuilder.html', 'custominstallerbuilder.xmlrpc', @@ -103,7 +116,6 @@ # Unless you are actively debugging, these should be set to False. DEBUG = False -TEMPLATE_DEBUG = DEBUG SECRET_KEY = '***** This should be changed to a random string *****' diff --git a/xmlrpc/views.py b/xmlrpc/views.py index bece5a3..8c117a3 100644 --- a/xmlrpc/views.py +++ b/xmlrpc/views.py @@ -21,8 +21,7 @@ from SimpleXMLRPCServer import CGIXMLRPCRequestHandler from django.http import HttpResponse -from django.shortcuts import render_to_response -from django.template import RequestContext +from django.shortcuts import render from custominstallerbuilder.xmlrpc.functions import PublicFunctions @@ -46,16 +45,16 @@ def xmlrpc_handler(request): A Django HTTP response. """ - + if request.method == 'GET': - response = render_to_response('xmlrpc-info.html', context_instance=RequestContext(request)) + response = render(request, 'xmlrpc-info.html') elif request.method == 'POST': # Not all languages have the notion of "None". xmlrpc_handler = CGIXMLRPCRequestHandler(allow_none=False, encoding=None) xmlrpc_handler.register_instance(PublicFunctions()) - + response = HttpResponse(content_type='application/xml') response.write(xmlrpc_handler._marshaled_dispatch(request.body)) - + return response