2015-06-11 23:05:30 +03:00
# Copyright (c) 2015 Ansible, Inc.
2013-06-15 07:15:31 +04:00
# All Rights Reserved.
2013-06-14 12:13:34 +04:00
2013-06-15 07:15:31 +04:00
# Django REST Framework
from rest_framework import renderers
Configure Tower in Tower:
* Add separate Django app for configuration: awx.conf.
* Migrate from existing main.TowerSettings model to conf.Setting.
* Add settings wrapper to allow get/set/del via django.conf.settings.
* Update existing references to tower_settings to use django.conf.settings.
* Add a settings registry to allow for each Django app to register configurable settings.
* Support setting validation and conversion using Django REST Framework fields.
* Add /api/v1/settings/ to display a list of setting categories.
* Add /api/v1/settings/<slug>/ to display all settings in a category as a single object.
* Allow PUT/PATCH to update setting singleton, DELETE to reset to defaults.
* Add "all" category to display all settings across categories.
* Add "changed" category to display only settings configured in the database.
* Support per-user settings via "user" category (/api/v1/settings/user/).
* Support defaults for user settings via "user-defaults" category (/api/v1/settings/user-defaults/).
* Update serializer metadata to support category, category_slug and placeholder on OPTIONS responses.
* Update serializer metadata to handle child fields of a list/dict.
* Hide raw data form in browsable API for OPTIONS and DELETE.
* Combine existing licensing code into single "TaskEnhancer" class.
* Move license helper functions from awx.api.license into awx.conf.license.
* Update /api/v1/config/ to read/verify/update license using TaskEnhancer and settings wrapper.
* Add support for caching settings accessed via settings wrapper.
* Invalidate cached settings when Setting model changes or is deleted.
* Preload all database settings into cache on first access via settings wrapper.
* Add support for read-only settings than can update their value depending on other settings.
* Use setting_changed signal whenever a setting changes.
* Register configurable authentication, jobs, system and ui settings.
* Register configurable LDAP, RADIUS and social auth settings.
* Add custom fields and validators for URL, LDAP, RADIUS and social auth settings.
* Rewrite existing validator for Credential ssh_private_key to support validating private keys, certs or combinations of both.
* Get all unit/functional tests working with above changes.
* Add "migrate_to_database_settings" command to determine settings to be migrated into the database and comment them out when set in Python settings files.
* Add support for migrating license key from file to database.
* Remove database-configuable settings from local_settings.py example files.
* Update setup role to no longer install files for database-configurable settings.
f 94ff6ee More settings work.
f af4c4e0 Even more db settings stuff.
f 96ea9c0 More settings, attempt at singleton serializer for settings.
f 937c760 More work on singleton/category views in API, add code to comment out settings in Python files, work on command to migrate settings to database.
f 425b0d3 Minor fixes for sprint demo.
f ea402a4 Add support for read-only settings, cleanup license engine, get license support working with DB settings.
f ec289e4 Rename migration, minor fixmes, update setup role.
f 603640b Rewrite key/cert validator, finish adding social auth fields, hook up signals for setting_changed, use None to imply a setting is not set.
f 67d1b5a Get functional/unit tests passing.
f 2919b62 Flake8 fixes.
f e62f421 Add redbaron to requirements, get file to database migration working (except for license).
f c564508 Add support for migrating license file.
f 982f767 Add support for regex in social map fields.
2016-09-27 05:14:47 +03:00
from rest_framework . request import override_method
2013-06-15 07:15:31 +04:00
2018-02-09 18:28:36 +03:00
import six
2016-02-02 22:50:42 +03:00
2013-06-15 07:15:31 +04:00
class BrowsableAPIRenderer ( renderers . BrowsableAPIRenderer ) :
2013-06-14 12:13:34 +04:00
'''
Customizations to the default browsable API renderer .
'''
2014-04-02 04:51:15 +04:00
def get_default_renderer ( self , view ) :
renderer = super ( BrowsableAPIRenderer , self ) . get_default_renderer ( view )
# Always use JSON renderer for browsable OPTIONS response.
if view . request . method == ' OPTIONS ' and not isinstance ( renderer , renderers . JSONRenderer ) :
return renderers . JSONRenderer ( )
return renderer
2016-02-17 01:49:34 +03:00
def get_context ( self , data , accepted_media_type , renderer_context ) :
# Store the associated response status to know how to populate the raw
# data form.
try :
setattr ( renderer_context [ ' view ' ] , ' _raw_data_response_status ' , renderer_context [ ' response ' ] . status_code )
2017-06-21 22:33:57 +03:00
setattr ( renderer_context [ ' view ' ] , ' _request ' , renderer_context [ ' request ' ] )
2016-02-17 01:49:34 +03:00
return super ( BrowsableAPIRenderer , self ) . get_context ( data , accepted_media_type , renderer_context )
finally :
delattr ( renderer_context [ ' view ' ] , ' _raw_data_response_status ' )
2017-06-21 22:33:57 +03:00
delattr ( renderer_context [ ' view ' ] , ' _request ' )
2016-02-17 01:49:34 +03:00
2016-02-02 22:50:42 +03:00
def get_raw_data_form ( self , data , view , method , request ) :
# Set a flag on the view to indiciate to the view/serializer that we're
2016-02-17 01:49:34 +03:00
# creating a raw data form for the browsable API. Store the original
# request method to determine how to populate the raw data form.
Configure Tower in Tower:
* Add separate Django app for configuration: awx.conf.
* Migrate from existing main.TowerSettings model to conf.Setting.
* Add settings wrapper to allow get/set/del via django.conf.settings.
* Update existing references to tower_settings to use django.conf.settings.
* Add a settings registry to allow for each Django app to register configurable settings.
* Support setting validation and conversion using Django REST Framework fields.
* Add /api/v1/settings/ to display a list of setting categories.
* Add /api/v1/settings/<slug>/ to display all settings in a category as a single object.
* Allow PUT/PATCH to update setting singleton, DELETE to reset to defaults.
* Add "all" category to display all settings across categories.
* Add "changed" category to display only settings configured in the database.
* Support per-user settings via "user" category (/api/v1/settings/user/).
* Support defaults for user settings via "user-defaults" category (/api/v1/settings/user-defaults/).
* Update serializer metadata to support category, category_slug and placeholder on OPTIONS responses.
* Update serializer metadata to handle child fields of a list/dict.
* Hide raw data form in browsable API for OPTIONS and DELETE.
* Combine existing licensing code into single "TaskEnhancer" class.
* Move license helper functions from awx.api.license into awx.conf.license.
* Update /api/v1/config/ to read/verify/update license using TaskEnhancer and settings wrapper.
* Add support for caching settings accessed via settings wrapper.
* Invalidate cached settings when Setting model changes or is deleted.
* Preload all database settings into cache on first access via settings wrapper.
* Add support for read-only settings than can update their value depending on other settings.
* Use setting_changed signal whenever a setting changes.
* Register configurable authentication, jobs, system and ui settings.
* Register configurable LDAP, RADIUS and social auth settings.
* Add custom fields and validators for URL, LDAP, RADIUS and social auth settings.
* Rewrite existing validator for Credential ssh_private_key to support validating private keys, certs or combinations of both.
* Get all unit/functional tests working with above changes.
* Add "migrate_to_database_settings" command to determine settings to be migrated into the database and comment them out when set in Python settings files.
* Add support for migrating license key from file to database.
* Remove database-configuable settings from local_settings.py example files.
* Update setup role to no longer install files for database-configurable settings.
f 94ff6ee More settings work.
f af4c4e0 Even more db settings stuff.
f 96ea9c0 More settings, attempt at singleton serializer for settings.
f 937c760 More work on singleton/category views in API, add code to comment out settings in Python files, work on command to migrate settings to database.
f 425b0d3 Minor fixes for sprint demo.
f ea402a4 Add support for read-only settings, cleanup license engine, get license support working with DB settings.
f ec289e4 Rename migration, minor fixmes, update setup role.
f 603640b Rewrite key/cert validator, finish adding social auth fields, hook up signals for setting_changed, use None to imply a setting is not set.
f 67d1b5a Get functional/unit tests passing.
f 2919b62 Flake8 fixes.
f e62f421 Add redbaron to requirements, get file to database migration working (except for license).
f c564508 Add support for migrating license file.
f 982f767 Add support for regex in social map fields.
2016-09-27 05:14:47 +03:00
if request . method in { ' OPTIONS ' , ' DELETE ' } :
return
2014-04-02 05:10:16 +04:00
try :
setattr ( view , ' _raw_data_form_marker ' , True )
2016-02-17 01:49:34 +03:00
setattr ( view , ' _raw_data_request_method ' , request . method )
2016-02-02 22:50:42 +03:00
return super ( BrowsableAPIRenderer , self ) . get_raw_data_form ( data , view , method , request )
2014-04-02 05:10:16 +04:00
finally :
delattr ( view , ' _raw_data_form_marker ' )
2016-02-17 01:49:34 +03:00
delattr ( view , ' _raw_data_request_method ' )
2014-04-02 05:10:16 +04:00
2016-02-02 22:50:42 +03:00
def get_rendered_html_form ( self , data , view , method , request ) :
2016-02-17 01:49:34 +03:00
# Never show auto-generated form (only raw form).
2013-06-14 12:13:34 +04:00
obj = getattr ( view , ' object ' , None )
2016-11-10 21:07:33 +03:00
if obj is None and hasattr ( view , ' get_object ' ) and hasattr ( view , ' retrieve ' ) :
2016-11-10 19:45:24 +03:00
try :
2017-08-30 23:05:02 +03:00
view . object = view . get_object ( )
obj = view . object
2016-11-10 19:45:24 +03:00
except Exception :
obj = None
Configure Tower in Tower:
* Add separate Django app for configuration: awx.conf.
* Migrate from existing main.TowerSettings model to conf.Setting.
* Add settings wrapper to allow get/set/del via django.conf.settings.
* Update existing references to tower_settings to use django.conf.settings.
* Add a settings registry to allow for each Django app to register configurable settings.
* Support setting validation and conversion using Django REST Framework fields.
* Add /api/v1/settings/ to display a list of setting categories.
* Add /api/v1/settings/<slug>/ to display all settings in a category as a single object.
* Allow PUT/PATCH to update setting singleton, DELETE to reset to defaults.
* Add "all" category to display all settings across categories.
* Add "changed" category to display only settings configured in the database.
* Support per-user settings via "user" category (/api/v1/settings/user/).
* Support defaults for user settings via "user-defaults" category (/api/v1/settings/user-defaults/).
* Update serializer metadata to support category, category_slug and placeholder on OPTIONS responses.
* Update serializer metadata to handle child fields of a list/dict.
* Hide raw data form in browsable API for OPTIONS and DELETE.
* Combine existing licensing code into single "TaskEnhancer" class.
* Move license helper functions from awx.api.license into awx.conf.license.
* Update /api/v1/config/ to read/verify/update license using TaskEnhancer and settings wrapper.
* Add support for caching settings accessed via settings wrapper.
* Invalidate cached settings when Setting model changes or is deleted.
* Preload all database settings into cache on first access via settings wrapper.
* Add support for read-only settings than can update their value depending on other settings.
* Use setting_changed signal whenever a setting changes.
* Register configurable authentication, jobs, system and ui settings.
* Register configurable LDAP, RADIUS and social auth settings.
* Add custom fields and validators for URL, LDAP, RADIUS and social auth settings.
* Rewrite existing validator for Credential ssh_private_key to support validating private keys, certs or combinations of both.
* Get all unit/functional tests working with above changes.
* Add "migrate_to_database_settings" command to determine settings to be migrated into the database and comment them out when set in Python settings files.
* Add support for migrating license key from file to database.
* Remove database-configuable settings from local_settings.py example files.
* Update setup role to no longer install files for database-configurable settings.
f 94ff6ee More settings work.
f af4c4e0 Even more db settings stuff.
f 96ea9c0 More settings, attempt at singleton serializer for settings.
f 937c760 More work on singleton/category views in API, add code to comment out settings in Python files, work on command to migrate settings to database.
f 425b0d3 Minor fixes for sprint demo.
f ea402a4 Add support for read-only settings, cleanup license engine, get license support working with DB settings.
f ec289e4 Rename migration, minor fixmes, update setup role.
f 603640b Rewrite key/cert validator, finish adding social auth fields, hook up signals for setting_changed, use None to imply a setting is not set.
f 67d1b5a Get functional/unit tests passing.
f 2919b62 Flake8 fixes.
f e62f421 Add redbaron to requirements, get file to database migration working (except for license).
f c564508 Add support for migrating license file.
f 982f767 Add support for regex in social map fields.
2016-09-27 05:14:47 +03:00
with override_method ( view , request , method ) as request :
if not self . show_form_for_method ( view , method , request , obj ) :
return
if method in ( ' DELETE ' , ' OPTIONS ' ) :
return True # Don't actually need to return a form
2014-04-02 04:15:36 +04:00
2016-02-02 22:50:42 +03:00
def get_filter_form ( self , data , view , request ) :
# Don't show filter form in browsable API.
return
2014-05-09 23:16:34 +04:00
2014-04-02 04:15:36 +04:00
class PlainTextRenderer ( renderers . BaseRenderer ) :
media_type = ' text/plain '
format = ' txt '
def render ( self , data , media_type = None , renderer_context = None ) :
2018-02-09 18:28:36 +03:00
if not isinstance ( data , six . string_types ) :
2014-04-02 04:15:36 +04:00
data = unicode ( data )
return data . encode ( self . charset )
2016-02-02 22:50:42 +03:00
2015-08-04 16:56:33 +03:00
class DownloadTextRenderer ( PlainTextRenderer ) :
2016-02-02 22:50:42 +03:00
2015-08-04 16:56:33 +03:00
format = " txt_download "
2016-02-02 22:50:42 +03:00
2014-04-02 04:15:36 +04:00
class AnsiTextRenderer ( PlainTextRenderer ) :
media_type = ' text/plain '
format = ' ansi '
2016-12-16 00:03:00 +03:00
class AnsiDownloadRenderer ( PlainTextRenderer ) :
format = " ansi_download "