本文整理汇总了Python中django.utils.simplejson.dumps方法的典型用法代码示例。如果您正苦于以下问题:Python simplejson.dumps方法的具体用法?Python simplejson.dumps怎么用?Python simplejson.dumps使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.simplejson
的用法示例。
在下文中一共展示了simplejson.dumps方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_emails_to_addressbook
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def add_emails_to_addressbook(self, id, emails):
""" Add new emails to addressbook
@param id: unsigned int addressbook ID
@param emails: list of dictionaries [
{'email': 'test@test.com', 'variables': {'varname_1': 'value_1', ..., 'varname_n': 'value_n' }},
{...},
{'email': 'testn@testn.com'}}
]
@return: dictionary with response message
"""
logger.info("Function call: add_emails_to_addressbook into: {}".format(id, ))
if not id or not emails:
self.__handle_error("Empty addressbook id or emails")
try:
emails = json.dumps(emails)
except:
logger.debug("Emails: {}".format(emails))
return self.__handle_error("Emails list can't be converted by JSON library")
return self.__handle_result(self.__send_request('addressbooks/{}/emails'.format(id), 'POST', {'emails': emails}))
示例2: delete_emails_from_addressbook
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def delete_emails_from_addressbook(self, id, emails):
""" Delete email addresses from addressbook
@param id: unsigned int addressbook ID
@param emails: list of emails ['test_1@test_1.com', ..., 'test_n@test_n.com']
@return: dictionary with response message
"""
logger.info("Function call: delete_emails_from_addressbook from: {}".format(id, ))
if not id or not emails:
self.__handle_error("Empty addressbook id or emails")
try:
emails = json.dumps(emails)
except:
logger.debug("Emails: {}".format(emails))
return self.__handle_error("Emails list can't be converted by JSON library")
return self.__handle_result(self.__send_request('addressbooks/{}/emails'.format(id), 'DELETE', {'emails': emails}))
示例3: sms_add_phones
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def sms_add_phones(self, addressbook_id, phones):
""" SMS: add phones from the address book
@return: dictionary with response message
"""
if not addressbook_id or not phones:
return self.__handle_error("Empty addressbook id or phones")
try:
phones = json.dumps(phones)
except:
logger.debug("Phones: {}".format(phones))
return self.__handle_error("Phones list can't be converted by JSON library")
data_to_send = {
'addressBookId': addressbook_id,
'phones': phones
}
logger.info("Function call: sms_add_phones")
return self.__handle_result(self.__send_request('sms/numbers', 'POST', data_to_send))
示例4: sms_add_phones_with_variables
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def sms_add_phones_with_variables(self, addressbook_id, phones):
""" SMS: add phones with variables from the address book
@return: dictionary with response message
"""
if not addressbook_id or not phones:
return self.__handle_error("Empty addressbook id or phones")
try:
phones = json.dumps(phones)
except:
logger.debug("Phones: {}".format(phones))
return self.__handle_error("Phones list can't be converted by JSON library")
data_to_send = {
'addressBookId': addressbook_id,
'phones': phones
}
logger.info("Function call: sms_add_phones_with_variables")
return self.__handle_result(self.__send_request('sms/numbers/variables', 'POST', data_to_send))
示例5: sms_delete_phones
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def sms_delete_phones(self, addressbook_id, phones):
""" SMS: remove phones from the address book
@return: dictionary with response message
"""
if not addressbook_id or not phones:
return self.__handle_error("Empty addressbook id or phones")
try:
phones = json.dumps(phones)
except:
logger.debug("Phones: {}".format(phones))
return self.__handle_error("Phones list can't be converted by JSON library")
data_to_send = {
'addressBookId': addressbook_id,
'phones': phones
}
logger.info("Function call: sms_delete_phones")
return self.__handle_result(self.__send_request('sms/numbers', 'DELETE', data_to_send))
示例6: sms_get_phones_info_from_blacklist
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def sms_get_phones_info_from_blacklist(self, phones):
""" SMS: get info by phones from the blacklist
@param phones: array phones
@return: dictionary with response message
"""
if not phones:
return self.__handle_error("Empty phones")
try:
phones = json.dumps(phones)
except:
logger.debug("Phones: {}".format(phones))
return self.__handle_error("Phones list can't be converted by JSON library")
data_to_send = {
'phones': phones
}
logger.info("Function call: sms_add_phones_to_blacklist")
return self.__handle_result(self.__send_request('sms/black_list/by_numbers', 'GET', data_to_send))
示例7: sms_add_phones_to_blacklist
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def sms_add_phones_to_blacklist(self, phones, comment):
""" SMS: add phones to blacklist
@param phones: array phones
@param comment: string describing why phones added to blacklist
@return: dictionary with response message
"""
if not phones:
return self.__handle_error("Empty phones")
try:
phones = json.dumps(phones)
except:
logger.debug("Phones: {}".format(phones))
return self.__handle_error("Phones list can't be converted by JSON library")
data_to_send = {
'phones': phones,
'description': comment
}
logger.info("Function call: sms_add_phones_to_blacklist")
return self.__handle_result(self.__send_request('sms/black_list', 'POST', data_to_send))
示例8: sms_delete_phones_from_blacklist
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def sms_delete_phones_from_blacklist(self, phones):
""" SMS: remove phones from blacklist
@param phones: array phones
@return: dictionary with response message
"""
if not phones:
return self.__handle_error("Empty phones")
try:
phones = json.dumps(phones)
except:
logger.debug("Phones: {}".format(phones))
return self.__handle_error("Phones list can't be converted by JSON library")
data_to_send = {
'phones': phones
}
logger.info("Function call: sms_add_phones_to_blacklist")
return self.__handle_result(self.__send_request('sms/black_list', 'DELETE', data_to_send))
示例9: apply
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def apply(self, callback, _):
dumps = self.json_dumps
if not dumps: return callback
def wrapper(*a, **ka):
try:
rv = callback(*a, **ka)
except HTTPError:
rv = _e()
if isinstance(rv, dict):
#Attempt to serialize, raises exception on failure
json_response = dumps(rv)
#Set content type only if serialization successful
response.content_type = 'application/json'
return json_response
elif isinstance(rv, HTTPResponse) and isinstance(rv.body, dict):
rv.body = dumps(rv.body)
rv.content_type = 'application/json'
return rv
return wrapper
示例10: put
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def put(self, keyname, value, session):
"""
Insert a keyname/value pair into the datastore for the session.
Args:
keyname: The keyname of the mapping.
value: The value of the mapping.
"""
keyname = session._validate_key(keyname)
if value is None:
raise ValueError('You must pass a value to put.')
# Use simplejson for cookies instead of pickle.
session.cookie_vals[keyname] = value
# update the requests session cache as well.
session.cache[keyname] = value
session.output_cookie[session.cookie_name + '_data'] = \
simplejson.dumps(session.cookie_vals)
print session.output_cookie.output()
示例11: __delitem__
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def __delitem__(self, keyname):
"""
Delete item from session data.
Args:
keyname: The keyname of the object to delete.
"""
bad_key = False
sessdata = self._get(keyname = keyname)
if sessdata is None:
bad_key = True
else:
sessdata.delete()
if keyname in self.cookie_vals:
del self.cookie_vals[keyname]
bad_key = False
self.output_cookie[self.cookie_name + '_data'] = \
simplejson.dumps(self.cookie_vals)
print self.output_cookie.output()
if bad_key:
raise KeyError(str(keyname))
if keyname in self.cache:
del self.cache[keyname]
示例12: ajax_add_toggleproperty
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def ajax_add_toggleproperty(request):
if request.method == "POST":
object_id = request.POST.get("object_id")
content_type = get_object_or_404(ContentType, pk=request.POST.get("content_type_id"))
property_type = request.POST.get("property_type")
obj = content_type.get_object_for_this_type(pk=object_id)
response_dict = {}
# check if it was created already
if ToggleProperty.objects.filter(content_type=content_type, object_id=object_id, \
property_type=property_type, user=request.user):
# return conflict response code if already satisfied
return HttpResponse(status=409)
# if not create it
tp = ToggleProperty.objects.create_toggleproperty(property_type, obj, request.user)
if settings.TOGGLEPROPERTIES.get('show_count'):
count = ToggleProperty.objects.toggleproperties_for_object(property_type, obj).count()
response_dict['count'] = count
return HttpResponse(simplejson.dumps(response_dict),
'application/javascript',
status=200)
else:
return HttpResponse(status=405)
示例13: ajax_remove_toggleproperty
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def ajax_remove_toggleproperty(request):
if request.method == "POST":
object_id = request.POST.get("object_id")
content_type = get_object_or_404(ContentType,
pk=request.POST.get("content_type_id"))
property_type = request.POST.get("property_type")
response_dict = {}
tp = get_object_or_404(ToggleProperty, object_id=object_id,
content_type=content_type,
property_type=property_type,
user=request.user)
tp.delete()
obj = content_type.get_object_for_this_type(pk=object_id)
if settings.TOGGLEPROPERTIES.get('show_count'):
count = ToggleProperty.objects.toggleproperties_for_object(property_type, obj).count()
response_dict['count'] = count
return HttpResponse(simplejson.dumps(response_dict),
'application/javascript',
status=200)
else:
return HttpResponse(status=405)
示例14: DataDumpKeys
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def DataDumpKeys(request):
"""This is used by bin/data_dump.py to get ResultParent keys."""
bookmark = request.REQUEST.get('bookmark')
model_name = request.REQUEST.get('model')
count = int(request.REQUEST.get('count', 0))
fetch_limit = int(request.REQUEST.get('fetch_limit', 999))
created_str = request.REQUEST.get('created', 0)
created = None
if created_str:
created = datetime.datetime.strptime(created_str, '%Y-%m-%d %H:%M:%S')
models = {
'UserAgent': UserAgent,
'ResultParent': ResultParent,
'ResultTime': ResultTime,
}
model = models.get(model_name, UserAgent)
query = pager.PagerQuery(model, keys_only=True)
if created:
query.filter('created >=', created)
query.order('created')
try:
prev_bookmark, results, next_bookmark = query.fetch(fetch_limit, bookmark)
except db.Timeout:
logging.warn('db.Timeout during initial fetch.')
return http.HttpResponseServerError('db.Timeout during initial fetch.')
response_params = {
'bookmark': next_bookmark,
'model': model_name,
'count': count + len(results),
'keys': [str(key) for key in results]
}
if created_str:
response_params['created'] = created_str
return http.HttpResponse(content=simplejson.dumps(response_params),
content_type='application/json')
示例15: testBasic
# 需要导入模块: from django.utils import simplejson [as 别名]
# 或者: from django.utils.simplejson import dumps [as 别名]
def testBasic(self):
test_key_browsers = (
('apple', 'Firefox 3.0'),
('coconut', 'Firefox 3.0'),
)
ranker_values = (
(1, 5, '2|3'),
(101, 7, '101|99|101|2|988|3|101'),
)
params = {
'category': self.test_set.category,
'test_key_browsers_json': simplejson.dumps(test_key_browsers),
'ranker_values_json': simplejson.dumps(ranker_values),
'time_limit': 10,
}
time.clock().AndReturn(0)
test_browsers = [
(self.apple_test, 'Firefox 3.0'),
(self.coconut_test, 'Firefox 3.0'),
]
result_ranker.GetOrCreateRankers(test_browsers, None).AndReturn(
[self.apple_ranker, self.coconut_ranker])
time.clock().AndReturn(0.5)
self.apple_ranker.GetMedianAndNumScores().AndReturn((1, 2))
self.apple_ranker.SetValues([2, 3], 5)
time.clock().AndReturn(1) # under timelimit
self.coconut_ranker.GetMedianAndNumScores().AndReturn((50, 5))
self.coconut_ranker.SetValues([101, 99, 101, 2, 988, 3, 101], 7)
self.mox.ReplayAll()
response = self.client.get('/admin/rankers/upload', params)
self.mox.VerifyAll()
self.assertEqual(simplejson.dumps({}), response.content)
self.assertEqual(200, response.status_code)