def check_register(req): username = req.GET['username'] name = req.GET['name'] password = req.GET['password'] if len(User.objects.filter(username__exact=username)) == 0: user = User() user.username = username user.password = password user.name = name user.save() result = {'rst': 0, 'msg': '注册成功'} else: result = {'rst': -1, 'msg': '用户已经存在'} return HttpResponse(json.dumps(result), content_type='application/json')
def test_user_badge_serialize(self): "Tests if user_badge object serialize with RouteSerializer" user = User() user.id = 21 user.name = "Anselmos" user.surname = "Somlesna" user.weight = 80 user.height = 175 badge = Badge() badge.id = 1 badge.name = 'FasterThenUniverse' badge.description = 'you\'re average lap was faster then previous one' route = Route() route.id = 2 route.route_name = "NameOfThisFancyRoute" route.avg_route = 19.9 input_user_badge = UserBadge() input_user_badge.id_user = user input_user_badge.id_badge = badge input_user_badge.id_route = route input_user_badge.active = True input_user_badge.badge_acquiring_date = "2017-03-03T12:00:00+00:00" input_user_badge.activation_modification_date = "2017-03-03T12:00:00+00:00" input_serialized = UserBadgeSerializer(input_user_badge) self.assertEqual( (input_serialized.data), { 'id': None, 'id_badge': 1, 'id_user': 21, 'id_route': 2, 'active': True, 'badge_acquiring_date': '2017-03-03T12:00:00+00:00', 'activation_modification_date': '2017-03-03T12:00:00+00:00', } )
def test_user_serialize_to_json(self): "test if serializing to JSON works" mocked = User() mocked.name = "Anselmos" mocked.surname = "Somlesna" mocked.weight = 80 mocked.height = 175 user_serialized = UserSerializer(mocked) self.assertEqual( (user_serialized.data), { 'height': 175, 'surname': u'Somlesna', 'id': None, 'weight': 80, 'name': u'Anselmos', 'bmi': 26.12, 'bmi_health_name': u'Overweight' } )