本文整理汇总了Python中tastypie.utils.mime.determine_format函数的典型用法代码示例。如果您正苦于以下问题:Python determine_format函数的具体用法?Python determine_format怎么用?Python determine_format使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了determine_format函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_determine_format
def test_determine_format(self):
serializer = Serializer()
full_serializer = Serializer(formats=['json', 'jsonp', 'xml', 'yaml', 'html', 'plist'])
request = HttpRequest()
# Default.
self.assertEqual(determine_format(request, serializer), 'application/json')
# Test forcing the ``format`` parameter.
request.GET = {'format': 'json'}
self.assertEqual(determine_format(request, serializer), 'application/json')
# Disabled by default.
request.GET = {'format': 'jsonp'}
self.assertEqual(determine_format(request, serializer), 'application/json')
# Explicitly enabled.
request.GET = {'format': 'jsonp'}
self.assertEqual(determine_format(request, full_serializer), 'text/javascript')
request.GET = {'format': 'xml'}
self.assertEqual(determine_format(request, serializer), 'application/xml')
request.GET = {'format': 'yaml'}
self.assertEqual(determine_format(request, serializer), 'text/yaml')
request.GET = {'format': 'plist'}
self.assertEqual(determine_format(request, serializer), 'application/x-plist')
request.GET = {'format': 'foo'}
self.assertEqual(determine_format(request, serializer), 'application/json')
# Test the ``Accept`` header.
request.META = {'HTTP_ACCEPT': 'application/json'}
self.assertEqual(determine_format(request, serializer), 'application/json')
示例2: top_level
def top_level(self, request, api_name=None):
serializer = Serializer()
available_resources = {}
if api_name is None:
api_name = self.api_name
for name in sorted(self._registry.keys()):
available_resources[name] = {
'list_endpoint': self._build_reverse_url('api_dispatch_list', kwargs={
'api_name': api_name,
'resource_name': name,
}),
'schema': self._build_reverse_url('api_get_schema', kwargs={
'api_name': api_name,
'resource_name': name,
}),
}
if request.GET.get('include_schemas', False) is not False:
available_resources[name]['schema_detailed'] = self._registry[name].build_schema()
desired_format = determine_format(request, serializer)
options = {}
if 'text/javascript' in desired_format:
callback = request.GET.get('callback', 'callback')
if not is_valid_jsonp_callback_value(callback):
raise BadRequest('JSONP callback name is invalid.')
options['callback'] = callback
serialized = serializer.serialize(available_resources, desired_format, options)
return HttpResponse(content=serialized, content_type=build_content_type(desired_format))
示例3: top_level
def top_level(self, request, api_name=None):
"""
A view that returns a serialized list of all resources registers
to the ``Api``. Useful for discovery.
"""
available_resources = {}
if api_name is None:
api_name = self.api_name
for name in sorted(self._registry.keys()):
available_resources[name] = {
"list_endpoint": self._build_reverse_url(
"api_dispatch_list", kwargs={"api_name": api_name, "resource_name": name}
),
"schema": self._build_reverse_url(
"api_get_schema", kwargs={"api_name": api_name, "resource_name": name}
),
}
desired_format = determine_format(request, self.serializer)
options = {}
if "text/javascript" in desired_format:
callback = request.GET.get("callback", "callback")
if not is_valid_jsonp_callback_value(callback):
raise BadRequest("JSONP callback name is invalid.")
options["callback"] = callback
serialized = self.serializer.serialize(available_resources, desired_format, options)
return HttpResponse(content=serialized, content_type=build_content_type(desired_format))
示例4: top_level
def top_level(self, request, api_name=None):
"""
A view that returns a serialized list of all resources registers
to the ``Api``. Useful for discovery.
"""
serializer = Serializer()
if api_name is None:
api_name = self.api_name
available_resources = dict((name, {
'list_endpoint': self._build_reverse_url("api_dispatch_list", kwargs={
'api_name': api_name,
'resource_name': name,
}),
'schema': self._build_reverse_url("api_get_schema", kwargs={
'api_name': api_name,
'resource_name': name,
}),
}) for name in self._registry.iterkeys())
desired_format = determine_format(request, serializer)
options = {}
if 'text/javascript' in desired_format:
callback = request.GET.get('callback', 'callback')
if not is_valid_jsonp_callback_value(callback):
raise BadRequest('JSONP callback name is invalid.')
options['callback'] = callback
serialized = serializer.serialize(available_resources, desired_format, options)
return HttpResponse(content=serialized, content_type=build_content_type(desired_format))
示例5: top_level
def top_level(self, request, api_name=None):
"""
A view that returns a serialized list of all resources registers
to the ``Api``. Useful for discovery.
"""
serializer = Serializer()
available_resources = {}
if api_name is None:
api_name = self.api_name
for name in sorted(self._registry.keys()):
available_resources[name] = {
'list_endpoint': self._build_reverse_url("api_dispatch_list", kwargs={
'api_name': api_name,
'resource_name': name,
}),
'schema': self._build_reverse_url("api_get_schema", kwargs={
'api_name': api_name,
'resource_name': name,
}),
}
desired_format = determine_format(request, serializer)
serialized = serializer.serialize(available_resources, desired_format)
return HttpResponse(content=serialized, content_type=build_content_type(desired_format))
示例6: top_level
def top_level(request):
available_resources = {}
apitop = reverse('ietf.api.top_level')
for name in sorted([ name for name, api in _api_list if len(api._registry) > 0 ]):
available_resources[name] = {
'list_endpoint': '%s/%s/' % (apitop, name),
}
serializer = Serializer()
desired_format = determine_format(request, serializer)
options = {}
if 'text/javascript' in desired_format:
callback = request.GET.get('callback', 'callback')
if not is_valid_jsonp_callback_value(callback):
raise BadRequest('JSONP callback name is invalid.')
options['callback'] = callback
serialized = serializer.serialize(available_resources, desired_format, options)
return HttpResponse(content=serialized, content_type=build_content_type(desired_format))
示例7: login
def login(request, **kwargs):
# self.method_check(request, allowed=['post'])
# self.is_authenticated(request)
# self.throttle_check(request)
developer_key = request.GET.get('developer_key')
username = request.GET.get('username')
password = request.GET.get('password')
if not developer_key or not username or not password:
return HttpUnauthorized()
try:
dev_key = DeveloperApiKey.objects.get(key=developer_key, approved=True)
except DeveloperApiKey.DoesNotExist:
return HttpUnauthorized()
user = authenticate(username=username, password=password)
if user:
try:
key = UserApiKey.objects.get(user=user, developer_key=dev_key)
except UserApiKey.DoesNotExist:
key = UserApiKey(user=user, developer_key=dev_key)
key.save()
# self.log_throttled_access(request)
serializer = Serializer()
desired_format = determine_format(request, serializer)
serialized = serializer.serialize({'key' : key.key}, desired_format)
return HttpResponse(content=serialized, content_type=build_content_type(desired_format))
else:
return HttpUnauthorized()
示例8: top_level
def top_level(self, request, api_name=None):
"""This is the same method in the Api model with the execption
of the serializer used. This uses the SafeSerializer.
"""
serializer = SafeSerializer()
available_resources = {}
if api_name is None:
api_name = self.api_name
for name in sorted(self._registry.keys()):
available_resources[name] = {
'list_endpoint': self._build_reverse_url("api_dispatch_list", kwargs={
'api_name': api_name,
'resource_name': name,
}),
'schema': self._build_reverse_url("api_get_schema", kwargs={
'api_name': api_name,
'resource_name': name,
}),
}
desired_format = determine_format(request, serializer)
options = {}
if 'text/javascript' in desired_format:
callback = request.GET.get('callback', 'callback')
if not is_valid_jsonp_callback_value(callback):
raise BadRequest('JSONP callback name is invalid.')
options['callback'] = callback
serialized = serializer.serialize(available_resources, desired_format, options)
return HttpResponse(content=serialized, content_type=build_content_type(desired_format))
示例9: serialized
def serialized(self, request, api_name=None, desired_format=None):
"""
Nabbed from top_level of the parent class
Would be nice if this was abstracted as it's good for auto discovery
to be rendered as json on initial page load
"""
serializer = Serializer()
available_resources = {}
if api_name is None:
api_name = self.api_name
if desired_format is None:
desired_format = determine_format(request, serializer)
for name in sorted(self._registry.keys()):
available_resources[name] = {
'list_endpoint': self._build_reverse_url("api_dispatch_list",
kwargs={
'api_name': api_name,
'resource_name': name,
}),
'schema': self._build_reverse_url("api_get_schema", kwargs={
'api_name': api_name,
'resource_name': name,
}),
}
return serializer.serialize(available_resources, desired_format)
示例10: error_response
def error_response(request, error_code=None, message=None, status_code=403, serializer_class=HTMLSerializer, **kwargs):
context = {}
if error_code:
context['error_code'] = error_code
if not message:
message = ERRORS[error_code].format(**kwargs)
if message:
context['error_message'] = message
context.update(kwargs)
serializer = serializer_class()
desired_format = determine_format(request, serializer)
options = {}
if 'text/javascript' in desired_format:
callback = request.GET.get('callback', 'callback')
if not is_valid_jsonp_callback_value(callback):
raise BadRequest('JSONP callback name is invalid.')
options['callback'] = callback
serialized = serializer.serialize(context, desired_format, options)
return HttpResponse(content=serialized, content_type=build_content_type(desired_format), status=status_code)
示例11: determine_format
def determine_format(self, request):
"""return application/json as the default format """
fmt = determine_format(request, self._meta.serializer,\
default_format=self._meta.default_format)
if fmt == 'text/html' and 'format' not in request:
fmt = 'application/json'
return fmt
示例12: determine_format
def determine_format(self, request):
"""
return application/json as the default format
"""
fmt = determine_format(request, self._meta.serializer, default_format=self._meta.default_format)
if fmt == "text/html" and "format" not in request:
fmt = "application/json"
return fmt
示例13: determine_format
def determine_format(self, request):
"""
Used to determine the desired format.
Largely relies on ``tastypie.utils.mime.determine_format`` but here
as a point of extension.
"""
return determine_format(request, self._meta.serializer, default_format=self._meta.default_format)
示例14: determine_format
def determine_format(self, request):
'''
Overriding this to by pass text/html format when accessing ``API
directly through browser.
'''
req_format = determine_format(request, self._meta.serializer,
default_format=self._meta.default_format)
if req_format == "text/html":
req_format = self._meta.default_format
return req_format
示例15: is_key_valid
def is_key_valid(request, **kwargs):
user_key = request.GET.get('user_key')
try:
user_key = UserApiKey.objects.get(key=user_key)
key_valid = True
except UserApiKey.DoesNotExist:
key_valid = False
# self.log_throttled_access(request)
serializer = Serializer()
desired_format = determine_format(request, serializer)
serialized = serializer.serialize({'valid' : key_valid}, desired_format)
return HttpResponse(content=serialized, content_type=build_content_type(desired_format))