def get_tile(request,x,y): x = int(x) y = int(y) planets = Planet.all() planets.filter('x = ',x) planets.filter('y = ',y) t = planets.get() if t: return HttpResponse(print_tile(t)) t = Planet(x=x,y=y,color_r=random.randrange(0,255),color_g=random.randrange(0,255),color_b=random.randrange(0,255)) t.put() return HttpResponse(print_tile(t))
def test_planet_to_dict_no_id(self): p = Planet(id=1, planet_id=2, name='Test Planet', diameter=123456, rotation_period=1, orbital_period=365, gravity='1', population=6000000, climate='tundra,desert,rainforest', terrain='flat', surface_water=8) p_dict = p.to_dict() self.assertEqual(p.id, 1) self.assertNotIn('id', p_dict)
def test_planet_to_imperial(self): p = Planet(id=1, planet_id=2, name='Test Planet', diameter=123456, rotation_period=1, orbital_period=365, gravity='1', population=6000000, climate='tundra,desert,rainforest', terrain='flat', surface_water=8) imperial_dict = p.to_imperial() # 123456*0.621371 = 76711 self.assertEqual(imperial_dict['diameter'], 76711)
def get_by_id_imperial(request, id): try: planet = Planet.objects.get(pk=id) except Planet.DoesNotExist as dne: success, response = get_planet_by_id(id) planet = Planet(planet_id=id, name=response['name'], diameter=int(response['diameter']), rotation_period=int(response['rotation_period']), orbital_period=int(response['orbital_period']), gravity=response['gravity'], population=int(response['population']), climate=response['climate'], terrain=response['terrain'], surface_water=int(response['surface_water'])) planet.save() return JsonResponse(planet.to_imperial(), status=200)
def test_planet_to_dict(self): expected_dict = { 'planet_id': 2, 'name': 'Test Planet', 'diameter': 123456, 'rotation_period': 1, 'orbital_period': 365, 'gravity': 1, 'population': 6000000, 'climate': 'tundra,desert,rainforect', 'terrain': 'flat', 'surface_water': 8 } p = Planet(id=1, **expected_dict) p_dict = p.to_dict() self.assertEqual(p.id, 1) self.assertEqual(p_dict, expected_dict)
def create(self, request, *args, **kwargs): ''' 이번주 행성(Planet)에 참여 --- (토큰 필요) 성공적으로 실행되면 200 응답을 리턴하며 이번주에 이미 참여했다면 403 응답을 리턴합니다. ''' user = User.objects.get(id=self.request.user.id) if user.planet: return Response(False, status=status.HTTP_403_FORBIDDEN) # 속한 유저 수가 10보다 작으면서 id 가장 작은 행성(first) cur_planet = Planet.objects.filter(user_cnt__lt=10).first() if not cur_planet: pre_planet = Planet.objects.last() cur_planet = Planet(start_date=pre_planet.start_date, end_date=pre_planet.end_date) cur_planet.user_cnt += 1 cur_planet.save() user.planet = cur_planet user.save() # serializer = self.get_serializer(cur_planet) return Response(True, status=status.HTTP_200_OK)
def get_all_tiles(request): planets = Planet.all() out = ','.join([str(p) for p in planets]) return HttpResponse(out)