def test_truncated_key(self): """Make sure truncation works as expected.""" d = {'foo': 'a long string that should be truncated'} trunc = truncated_json_dumps(d, 30, 'foo') obj = json.loads(trunc) eq_(obj['foo'], 'a long string that ') eq_(len(trunc), 30)
def test_truncated_key(self): """Make sure truncation works as expected.""" d = {"foo": "a long string that should be truncated"} trunc = truncated_json_dumps(d, 30, "foo") obj = json.loads(trunc) eq_(obj["foo"], "a long string that ") eq_(len(trunc), 30)
def unhelpful_survey(request): """Ajax only view: Unhelpful vote survey processing.""" vote = get_object_or_404(HelpfulVote, id=smart_int(request.POST.get("vote_id"))) # Only save the survey if it was for a not helpful vote and a survey # doesn't exist for it already. if not vote.helpful and not vote.metadata.filter(key="survey").exists(): # The survey is the posted data, minus the vote_id and button value. survey = request.POST.copy() survey.pop("vote_id") survey.pop("button") # Save the survey in JSON format, taking care not to exceed 1000 chars. vote.add_metadata("survey", truncated_json_dumps(survey, 1000, "comment")) return HttpResponse(json.dumps({"message": _("Thanks for making us better!")}))
def unhelpful_survey(request): """Ajax only view: Unhelpful vote survey processing.""" vote = get_object_or_404(HelpfulVote, id=smart_int(request.POST.get('vote_id'))) # Only save the survey if it was for a not helpful vote and a survey # doesn't exist for it already. if not vote.helpful and not vote.metadata.filter(key='survey').exists(): # The survey is the posted data, minus the vote_id and button value. survey = request.POST.copy() survey.pop('vote_id') survey.pop('button') # Save the survey in JSON format, taking care not to exceed 1000 chars. vote.add_metadata('survey', truncated_json_dumps(survey, 1000, 'comment')) return HttpResponse( json.dumps({'message': _('Thanks for making us better!')}))
def test_truncated_noop(self): """Make sure short enough things are unmodified.""" d = {'foo': 'bar'} trunc = truncated_json_dumps(d, 1000, 'foo') eq_(json.dumps(d), trunc)
def test_unicode(self): """Unicode should not be treated as longer than it is.""" d = {'formula': u'A=πr²'} trunc = truncated_json_dumps(d, 25, 'formula') eq_(json.dumps(d, ensure_ascii=False), trunc)
def test_truncated_noop(self): """Make sure short enough things are unmodified.""" d = {"foo": "bar"} trunc = truncated_json_dumps(d, 1000, "foo") eq_(json.dumps(d), trunc)