def test_placed_by_name2(self): vd = VD() vd.save() pb_add = PostBase('''{ "urls": [{"content": "http://www.naver.com/"}] }''') pb_name = PostBase('{"name": {"content": "바이키 문정점"}}') pb_place2 = PostBase( '{"urls": [{"content": "http://place.kakao.com/places/26455534"}]}' ) self.assertEqual(Place.objects.count(), 0) self.assertEqual(PostPiece.objects.count(), 0) uplace, is_created = UserPlace.get_or_create_smart(pb_add, vd) self.assertEqual(uplace.place, None) self.assertEqual(Place.objects.count(), 0) self.assertEqual(PostPiece.objects.count(), 0) pb_place2.uplace_uuid = uplace.uuid uplace, is_created = UserPlace.get_or_create_smart( pb_place2.pb_MAMMA, vd) self.assertNotEqual(uplace.place, None) place2 = uplace.place self.assertEqual(Place.objects.count(), 1) self.assertNotEqual(place2.placePost.addr1, None) self.assertEqual(PostPiece.objects.count(), 1) pb_name.uplace_uuid = uplace.uuid uplace, is_created = UserPlace.get_or_create_smart(pb_name, vd) self.assertNotEqual(uplace.place, None) self.assertEqual(Place.objects.count(), 1) self.assertEqual(PostPiece.objects.count(), 1) place1 = uplace.place self.assertEqual(place1, place2) self.assertNotEqual(place1.placePost.addr1, None)
def test_placed(self): vd = VD() vd.save() pb_add = PostBase('{"urls": [{"content": "http://www.naver.com/"}]}') pb_place1 = PostBase( '{"urls": [{"content": "http://place.kakao.com/places/15738374"}]}' ) pb_place2 = PostBase( '{"urls": [{"content": "http://place.kakao.com/places/26455534"}]}' ) uplace, is_created = UserPlace.get_or_create_smart(pb_add, vd) self.assertEqual(uplace.place, None) pb_place1.uplace_uuid = uplace.uuid uplace, is_created = UserPlace.get_or_create_smart( pb_place1.pb_MAMMA, vd) self.assertNotEqual(uplace.place, None) self.assertEqual(uplace.lonLat, uplace.place.lonLat) place1 = uplace.place pb_place2.uplace_uuid = uplace.uuid pb_place2.place_id = place1.id uplace, is_created = UserPlace.get_or_create_smart( pb_place2.pb_MAMMA, vd) self.assertNotEqual(uplace.place, None) self.assertEqual(uplace.lonLat, uplace.place.lonLat) place2 = uplace.place self.assertNotEqual(place1, place2) self.assertNotEqual(place1.lonLat, place2.lonLat)
def test_id_property_with_timestamp(self): uplace = UserPlace(vd=self.vd) timestamp = get_timestamp() uplace.save(timestamp=timestamp) self.assertEqual((int(uplace.id) >> 8 * 8) & BIT_ON_8_BYTE, timestamp) self.assertEqual((int(uplace.id) >> 2 * 8) & BIT_ON_6_BYTE, self.vd.id) saved = UserPlace.objects.first() self.assertEqual(saved, uplace) self.assertEqual(saved.id, uplace.id)
def setUp(self): super(RegionTest, self).setUp() self.uplaces = list() with open('place/samples/lonLat.txt') as f: for line in f.readlines(): uplace = UserPlace() uplace.lonLat = GEOSGeometry(line) uplace.value = uplace.lonLat uplace.timestamp = 1 self.uplaces.append(uplace)
def test_non_unique_vd_place(self): uplace = UserPlace(vd=self.vd, place=self.place) uplace.save() uplace2 = UserPlace(vd=self.vd, place=self.place) uplace2.save() self.assertNotEqual(uplace, uplace2) self.assertEqual(uplace.place, uplace2.place)
class AfterLoginTaskTest(FunctionalTestAfterLoginBase): def setUp(self): super(AfterLoginTaskTest, self).setUp() self.uplace = UserPlace(vd=self.vd) self.uplace.lonLat = GEOSGeometry('POINT(127.1037430 37.3997320)', srid=4326) self.uplace.save() def test_basic(self): from account.task_wrappers import AfterLoginTaskWrapper task = AfterLoginTaskWrapper() r = task.delay(self.vd_id) result = cache_get(self.vd, 'regions') self.assertNotEqual(result, None)
def test_placed(self): test_data = 'http://www.mangoplate.com/top_lists/1073_2016_secondhalf_busan' pb = PostBase('{"urls": [{"content": "%s"}]}' % test_data) vd = VD.objects.create() parent, is_created = UserPlace.get_or_create_smart(pb, vd) self.assertEqual(parent.place, None) url, is_created = Url.get_or_create_smart(test_data) pp = PostPiece.create_smart(parent, pb) self.assertIn(url, parent.userPost.urls) self.assertEqual(UserPlace.objects.count(), 1) place1 = Place.objects.create() url.add_place(place1) self.assertEqual(UserPlace.objects.count(), 1) parent = UserPlace.objects.get(id=parent.id) self.assertEqual(parent.place, place1) place2 = Place.objects.create() url.add_place(place2) self.assertEqual(UserPlace.objects.count(), 3) parent = UserPlace.objects.get(id=parent.id) self.assertEqual(parent.place, None) self.assertEqual(UserPlace.objects.exclude(place=None).count(), 2) place3 = Place.objects.create() url.add_place(place3) self.assertEqual(UserPlace.objects.count(), 4) parent = UserPlace.objects.get(id=parent.id) self.assertEqual(parent.place, None) self.assertEqual(UserPlace.objects.exclude(place=None).count(), 3)
def take(self, iplace, vd): pb = PostBase() pb.iplace_uuid = iplace.uuid pb.place_id = iplace.place_id pb.uplace_uuid = None uplace, is_created = UserPlace.get_or_create_smart(pb, vd) pp = PostPiece.create_smart(uplace, pb) return uplace
def test_lonLat_column(self): uplace = UserPlace(vd=self.vd, place=self.place) uplace.save() point2 = GEOSGeometry('POINT(127.107316 37.400998)', srid=4326) qs1 = UserPlace.objects.filter(place__lonLat__distance_lte=(point2, D(m=100))) self.assertEqual(len(qs1), 0) qs2 = UserPlace.objects.filter(place__lonLat__distance_lte=(point2, D(m=1000))) self.assertEqual(len(qs2), 1) qs3 = UserPlace.objects.filter(lonLat__distance_lte=(point2, D(m=100))) self.assertEqual(len(qs3), 0) qs4 = UserPlace.objects.filter(vd_id=self.vd.id).filter( lonLat__distance_lte=(point2, D(m=1000))) self.assertEqual(len(qs4), 1) qs5 = UserPlace.objects.filter(vd_id=0).filter( lonLat__distance_lte=(point2, D(m=1000))) self.assertEqual(len(qs5), 0)
def accessUplaces(self): from place.models import UserPlace result = [] if self.accessHistory and 'uplaces' in self.accessHistory: for d in self.accessHistory['uplaces']: uplace = UserPlace.get_from_uuid(d['uuid']) uplace.accessed = d['timestamp'] result.append(uplace) return result
def test_id_property(self): uplace = UserPlace(vd=self.vd) self.assertEqual(uplace.id, None) timestamp = get_timestamp() uplace.save() self.assertNotEqual(uplace.id, None) self.assertAlmostEqual((int(uplace.id) >> 8 * 8) & BIT_ON_8_BYTE, timestamp, delta=1000) self.assertEqual((int(uplace.id) >> 2 * 8) & BIT_ON_6_BYTE, self.vd.id) saved = UserPlace.objects.first() self.assertEqual(saved, uplace) self.assertEqual(saved.id, uplace.id) # for timestamp property self.assertEqual(uplace.created, uplace.modified) self.assertEqual(saved.created, uplace.created) self.assertEqual(saved.modified, uplace.modified) self.assertAlmostEqual(uplace.created, timestamp, delta=1000)
def setUp(self): super(PostPieceTest, self).setUp() self.place = Place() self.place.save() self.uplace = UserPlace() self.uplace.save() self.vd = VD() self.vd.save() self.image, is_created = Image.get_or_create_smart( 'http://blogthumb2.naver.net/20160302_285/mardukas_1456922688406bYGAH_JPEG/DSC07301.jpg' ) self.url, is_created = Url.get_or_create_smart('http://www.naver.com/') self.lp, is_created = LegacyPlace.get_or_create_smart( '4ccffc63f6378cfaace1b1d6.4square') self.addr, is_created = Address.get_or_create_smart( '경기도 하남시 풍산로 270, 206동 402호 (선동, 미사강변도시2단지)') self.phone, is_created = PhoneNumber.get_or_create_smart( '010-5597-9245')
def helper(vd, uplaces=None): if not uplaces: if not vd: raise NotImplementedError uplaces = list(get_proper_uplaces_qs(vd).exclude(lonLat=None)) for uplace in uplaces: uplace.value = uplace.lonLat uplace.timestamp = uplace.modified if len(uplaces) == 0: return None group0 = Group() group0.members = uplaces m_value = group0.lonLat from place.models import RADIUS_LOCAL_RANGE cluster = RegionClustering(group0, RADIUS_LOCAL_RANGE, distance_geography, m_value, True) cluster.run() elements = cluster.result for e in elements: e.type = 'lonLat' for i in [6, 5, 4]: group0 = Group() group0.members = elements m_value = group0.lonLat cluster = RegionClustering(group0, i*1000, distance_geography, m_value, True) cluster.run() cluster.result.sort(key=lambda g: g.count, reverse=True) for r in cluster.result: uplace = UserPlace(lonLat=r.lonLat) uplace.value = uplace.lonLat uplace.timestamp = 0 g = Group([uplace]) g.type = 'lonLat' elements.append(g) result = [member for member in cluster.result if member.count > 0] result.sort(key=lambda g: g.count, reverse=True) for g in result: g.distance = distance_geography return result
def test_created_modified(self): uplace = UserPlace(vd=self.vd, place=self.place) self.assertEqual(uplace.created, None) self.assertEqual(uplace.modified, None) uplace.save() t1 = uplace.modified self.assertNotEqual(t1, None) self.assertEqual(uplace.created, t1) self.assertAlmostEqual(t1, get_timestamp(), delta=1000) uplace.save() self.assertGreater(uplace.modified, t1) self.assertAlmostEqual(uplace.modified, t1, delta=1000) self.assertEqual(uplace.created, t1) t2 = get_timestamp() uplace.save(timestamp=t2) self.assertEqual(uplace.modified, t2) self.assertEqual(uplace.created, t1)
def test_get_from_post(self): vd_other = VD.objects.create() place_other = Place.objects.create() uplace1 = UserPlace.objects.create(vd=vd_other, place=self.place) uplace2 = UserPlace.objects.create(vd=self.vd, place=place_other) uplace3 = UserPlace.objects.create(vd=self.vd, place=self.place) pb = PostBase() pb.place_id = self.place.id ru = RealUser.objects.create(email='*****@*****.**') self.vd.realOwner = ru self.vd.save() vd_mine = VD.objects.create(realOwner=ru) uplace_check, is_created = UserPlace.get_or_create_smart(pb, vd_mine) self.assertNotEqual(uplace1, uplace_check) self.assertNotEqual(uplace2, uplace_check) self.assertEqual(uplace3, uplace_check)
def test_placed_by_url0_2(self): url, is_created = Url.get_or_create_smart('http://www.naver.com/') json_add = '''{"urls": [{"content": "%s"}]}''' % (url.content, ) place1 = Place.objects.create() place2 = Place.objects.create() place3 = Place.objects.create() self.assertEqual(UserPlace.objects.count(), 0) pb = PostBase(json_add) uplace = UserPlace.objects.create(vd=self.vd) PostPiece.create_smart(uplace, pb) url.add_place(place1) self.assertEqual(UserPlace.objects.count(), 1) ''' pb = PostBase(json_add) uplace, is_created = UserPlace.get_or_create_smart(pb, self.vd) PostPiece.create_smart(uplace, pb) print(is_created) #''' #UserPlace.dump_all() #''' response = self.client.post('/uplaces/', dict(add=json_add, )) self.assertEqual(response.status_code, status.HTTP_201_CREATED) uplace = UserPlace.get_from_uuid( json_loads(response.content)['uplace_uuid']) uplace.place = None uplace.save() #''' self.assertEqual(UserPlace.objects.count(), 1 + 1) #UserPlace.dump_all() url.add_place(place2) self.assertEqual(UserPlace.objects.count(), 2 + 2) pb = PostBase(json_add) uplace = UserPlace.objects.create(vd=self.vd) PostPiece.create_smart(uplace, pb) self.assertEqual(UserPlace.objects.count(), 4 + 1) url.add_place(place3) self.assertEqual(UserPlace.objects.count(), 5 + 1) pb = PostBase(json_add) uplace = UserPlace.objects.create(vd=self.vd) PostPiece.create_smart(uplace, pb) self.assertEqual(UserPlace.objects.count(), 6 + 1)
def take(self, request, pk=None): # vd 조회 vd = self.vd if not vd: return Response(status=status.HTTP_401_UNAUTHORIZED) iplace = self.get_object() pb = PostBase() pb.iplace_uuid = iplace.uuid pb.place_id = iplace.place_id pb.uplace_uuid = None uplace, is_created = UserPlace.get_or_create_smart(pb, vd) pp = PostPiece.create_smart(uplace, pb) serializer = UserPlaceSerializer(uplace) # for make taken place hidden from base.cache import cache_expire, cache_expire_ru cache_expire(vd, 'realOwner_place_ids') cache_expire_ru(vd, 'uplaces') return Response(serializer.data, status=status.HTTP_201_CREATED)
def detail(request, enc_uplace_id): vd = vd_login_for_browser(request) uplace_id = UserPlace.aid2id(enc_uplace_id) uplace = UserPlace.objects.get(id=uplace_id) etc = {} etc['title'] = (uplace.placePost and uplace.placePost.name ) or uplace.userPost.note or '사진을 공유하는 새로운 방법!' etc['desc'] = uplace.userPost.note or ( uplace.placePost and uplace.placePost.addr) or '사진을 공유하는 새로운 방법!' from pks.settings import SERVER_HOST url = uplace.wrapping_shorten_url if not url: url = '%s%s' % (SERVER_HOST, request.get_full_path()) etc['url'] = url etc['datetime_desc'] = convert_to_datetime_desc(uplace.modified) ru = uplace.userPost.cjson['ru'] vd.add_access_history(uplace) return render(request, 'ui/album.html', context=dict(uplace=uplace, ru=ru, etc=etc))
def drop(self, request, pk=None): # vd 조회 vd = self.vd if not vd: return Response(status=status.HTTP_401_UNAUTHORIZED) iplace = self.get_object() pb = PostBase() pb.iplace_uuid = iplace.uuid pb.place_id = iplace.place_id pb.uplace_uuid = None uplace, is_created = UserPlace.get_or_create_smart(pb, vd) if is_created: # 매칭되는 UserPlace 가 없었다면 drop 처리 pp = PostPiece.create_smart(uplace, pb, is_drop=True) uplace.is_drop = True uplace.save() serializer = UserPlaceSerializer(uplace) return Response(serializer.data, status=status.HTTP_204_NO_CONTENT) else: # 매칭되는 UserPlace 가 있었다면 무시 # 이를 drop 처리하려면 delete /uplaces/detail/ serializer = UserPlaceSerializer(uplace) return Response(serializer.data, status=status.HTTP_200_OK)
def test_id_property_with_no_vd(self): uplace = UserPlace() uplace.save() self.assertEqual((int(uplace.id) >> 2 * 8) & BIT_ON_6_BYTE, 0)
def create(self, request, *args, **kwargs): # vd 조회 vd = self.vd if not vd: return Response(status=status.HTTP_401_UNAUTHORIZED) # 결과 처리를 위한 변수 선언 uplace = None ####################################### # 삭제 포스트 처리 ####################################### if 'remove' in request.data and request.data['remove']: # PostBase instance 생성 pb = PostBase(request.data['remove']) if 'place_id' in request.data and request.data['place_id']: pb.place_id = request.data['place_id'] if 'uplace_uuid' in request.data and request.data['uplace_uuid']: pb.uplace_uuid = request.data['uplace_uuid'] # 삭제 포스트는 반드시 uplace_uuid 가 지정되어야 한다. uplace = UserPlace.get_from_uuid(pb.uplace_uuid) if not uplace: raise ValueError('삭제 포스트 처리 시에는 반드시 uplace_uuid 가 지정되어야 함') # 삭제 PostPiece 생성 pp = PostPiece.create_smart(uplace, pb, is_remove=True) ####################################### # 추가 포스트 처리 ####################################### if 'add' in request.data and request.data['add']: # PostBase instance 생성 pb = PostBase(request.data['add']) if 'place_id' in request.data and request.data['place_id']: pb.place_id = request.data['place_id'] if 'uplace_uuid' in request.data and request.data['uplace_uuid']: pb.uplace_uuid = request.data['uplace_uuid'] # 추가 정보 가져오기 : 유저가 직접 입력했다고 봐도 무방한 사항만 pb.load_additional_info() # UserPlace/Place 찾기 uplace, is_created = UserPlace.get_or_create_smart(pb, vd) # valid check if not pb.is_valid(uplace): raise ValueError('PostPiece 생성을 위한 최소한의 정보도 없음') # PostPiece 생성 pp = PostPiece.create_smart(uplace, pb) # 임시적인 어드민 구현을 위해, MAMMA 가 추가로 뽑아준 post 가 있으면 추가로 포스팅 pb_MAMMA = pb.pb_MAMMA if pb_MAMMA: # 아래 호출에서 Place 가 생성되고, 필요시 Place PostPiece 도 생성됨 # TODO : 좀 더 Readability 가 높은 형태로 리팩토링 uplace, is_created = UserPlace.get_or_create_smart( pb_MAMMA, vd) # Place.lonLat 관련 예외 처리 lonLat = (pb_MAMMA and pb_MAMMA.lonLat) or pb.lonLat if lonLat and uplace.place and (not uplace.place.lonLat or (pb_MAMMA and pb_MAMMA.lonLat)): uplace.place.lonLat = lonLat uplace.place.save() # 현재 위치 저장인 경우 이미지에 추가 정보 붙이기 if is_created and lonLat and pb.images and len( pb.images) == 1 and pb.images[0]: img = pb.images[0] img.lonLat = lonLat img.timestamp = uplace.created - 1000 img.save() # 빠른 장소화를 위한 flag 세팅 if is_created and not uplace.place: uplace.is_hurry2placed = True # 최종 저장 uplace.lonLat = (uplace.place and uplace.place.lonLat) or lonLat or uplace.lonLat uplace.modified = get_timestamp() # TODO : 아래 코드가 테스트되는 테스트 추가 uplace.is_drop = False uplace.save() # Placed by Contents if pb.urls: placesets = [ set(url.places.all()) for url in pb.urls if url.places ] if placesets: places = list(reduce(lambda a, b: a & b, placesets)) for place in places: uplace.process_child(place) ####################################### # 결과 처리 ####################################### serializer = self.get_serializer(uplace) cache_expire_ru(vd, 'uplaces') cache_expire_ru(vd, uplace.post_cache_name) return Response(serializer.data, status=status.HTTP_201_CREATED)
def test_mask(self): uplace = UserPlace(vd=self.vd, place=self.place) uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved.is_drop, False) self.assertEqual(saved.mask, 0 | 0) uplace.is_drop = True uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved.is_drop, True) self.assertEqual(saved.mask, 0 | 1) uplace.is_drop = False uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved.is_drop, False) self.assertEqual(saved.mask, 0 | 0) uplace.is_hard2placed = True uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved.is_hard2placed, True) self.assertEqual(saved.mask, 0 | 2) uplace.is_hard2placed = False uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved.is_hard2placed, False) self.assertEqual(saved.mask, 0 | 0) uplace.is_hurry2placed = True uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved.is_hurry2placed, True) self.assertEqual(saved.mask, 0 | 4) uplace.is_hurry2placed = False uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved.is_hurry2placed, False) self.assertEqual(saved.mask, 0 | 0) uplace.is_parent = True uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved.is_parent, True) self.assertEqual(saved.mask, 0 | 8) uplace.is_parent = False uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved.is_parent, False) self.assertEqual(saved.mask, 0 | 0) uplace.is_empty = True uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved.is_empty, True) self.assertEqual(saved.mask, 0 | 16) uplace.is_empty = False uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved.is_empty, False) self.assertEqual(saved.mask, 0 | 0) uplace.is_bounded = True uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved.is_bounded, True) self.assertEqual(saved.mask, 0 | 32) uplace.is_bounded = False uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved.is_bounded, False) self.assertEqual(saved.mask, 0 | 0)
def placed_detail(request, uplace_id): if request.method == 'POST': vd = VD.objects.get(id=request.session[VD_SESSION_KEY]) # LegacyPlace URL 로 장소화 if 'url' in request.POST and request.POST['url']: url = request.POST['url'] # 삭제 if url in ('삭제', '제거', 'delete', 'remove'): uplace = UserPlace.objects.get(id=uplace_id) uplace.delete() return redirect('/admin2/placed/%s.uplace/' % uplace_id) # 나중에 if url in ('나중에', '나중', 'later', 'delay'): uplace = UserPlace.objects.get(id=uplace_id) uplace.is_hard2placed = True uplace.save() return redirect('/admin2/placed/%s.uplace/' % uplace_id) # PostBase instance 생성 json_add = '{"urls": [{"content": "%s"}]}' % url pb = PostBase(json_add) pb.uplace_uuid = '%s.uplace' % uplace_id # UserPlace/Place 찾기 uplace, is_created = UserPlace.get_or_create_smart(pb, vd) pb.uplace_uuid = uplace.uuid # valid check if not pb.is_valid(uplace): raise ValueError('PostPiece 생성을 위한 최소한의 정보도 없음') pb_MAMMA = pb.pb_MAMMA if pb_MAMMA: uplace, is_created = UserPlace.get_or_create_smart( pb_MAMMA, vd) # redirect return redirect('/admin2/placed/%s.uplace/' % uplace_id) # placeName/lonLat 로 장소화 elif 'placeName' in request.POST and request.POST['placeName']: placeName = request.POST['placeName'] lon = None lat = None if 'lonLat' in request.POST and request.POST['lonLat']: raw_lonLat = request.POST['lonLat'] regexs = [ re_compile( r'^.*[&\?]?lat=(?P<lat>-?[0-9\.]+)&lng=(?P<lon>-?[0-9\.]+)&?.*$' ), re_compile( r'^.*[&\?]?lng=(?P<lon>-?[0-9\.]+)&lat=(?P<lat>-?[0-9\.]+)&?.*$' ), re_compile( r'^.*[&\?]?lat=(?P<lat>-?[0-9\.]+)&lon=(?P<lon>-?[0-9\.]+)&?.*$' ), re_compile( r'^.*[&\?]?lon=(?P<lon>-?[0-9\.]+)&lat=(?P<lat>-?[0-9\.]+)&?.*$' ), re_compile( r'^.*[&\?]?x=(?P<lon>-?[0-9\.]+)&y=(?P<lat>-?[0-9\.]+)&?.*$' ), re_compile( r'^.*/data=.*!3d(?P<lat>-?[0-9\.]+)!4d(?P<lon>-?[0-9\.]+).*$' ), re_compile( r'^.*/@(?P<lat>-?[0-9\.]+),(?P<lon>-?[0-9\.]+).*$'), ] for regex in regexs: searcher = regex.search(raw_lonLat) if searcher: lon = float(searcher.group('lon')) lat = float(searcher.group('lat')) print('lon', searcher.group('lon')) print('lat', searcher.group('lat')) break if not lon or not lat: _uplace = UserPlace.objects.get(id=uplace_id) if _uplace and _uplace.lonLat: lon = _uplace.lonLat.x lat = _uplace.lonLat.y if not lon or not lat: # 현재는 이름과 함께 좌표(위도경도)를 넣어줘야 장소화가 됨 (향후 좌표 대신 주소도 가능) raise NotImplementedError # PostBase instance 생성 json_add = '{"lonLat": {"lon": %f, "lat": %f}, "name": {"content": "%s"}}' % ( lon, lat, placeName, ) pb = PostBase(json_add) pb.uplace_uuid = '%s.uplace' % uplace_id # 장소화 uplace, is_created = UserPlace.get_or_create_smart(pb, vd) # redirect return redirect('/admin2/placed/%s.uplace/' % uplace_id) try: uplace = UserPlace.objects.get(id=uplace_id) except UserPlace.DoesNotExist: return HttpResponse('삭제되었습니다') default_lonLat = 'LonLat Required' if uplace.lonLat: default_lonLat = 'lon=%f&lat=%f' % (uplace.lonLat.x, uplace.lonLat.y) #uplace.userPost.map_url = get_map_url(uplace.lonLat) uplace.userPost.map_url_daum = get_map_url_daum(uplace.lonLat) uplace.userPost.map_url_google = get_map_url_google(uplace.lonLat) if uplace.placePost: #uplace.placePost.map_url = get_map_url(uplace.placePost.lonLat) uplace.placePost.map_url_daum = get_map_url_daum( uplace.placePost.lonLat) uplace.placePost.map_url_google = get_map_url_google( uplace.placePost.lonLat) context = dict(userPost=uplace.userPost, placePost=uplace.placePost, default_lonLat=default_lonLat) return render(request, 'admin2/placed_detail.html', context)
def test_aid(self): uplace = UserPlace.objects.create() aid = uplace.aid id = UserPlace.aid2id(aid) self.assertEqual(id, uplace.id)
def test_post(self): place = Place() place.save() vd1 = VD() vd1.save() uplace1 = UserPlace(vd=vd1, place=place) uplace1.save() point1 = GEOSGeometry('POINT(127 37)', srid=4326) name1, is_created = PlaceName.get_or_create_smart('능라') addr1, is_created = Address.get_or_create_smart( '경기도 성남시 분당구 운중동 883-3') note11, is_created = PlaceNote.get_or_create_smart('분당 냉면 최고') note12, is_created = PlaceNote.get_or_create_smart('만두도 괜찮음') imgNote1, is_created = ImageNote.get_or_create_smart('냉면 사진') img1_content = 'http://blogthumb2.naver.net/20160302_285/mardukas_1456922688406bYGAH_JPEG/DSC07301.jpg' img1, is_created = Image.get_or_create_smart(img1_content) phone1, is_created = PhoneNumber.get_or_create_smart('010-5686-1613') vd2 = VD() vd2.save() uplace2 = UserPlace(vd=vd2, place=place) uplace2.save() point2 = GEOSGeometry('POINT(127.1037430 37.3997320)', srid=4326) name2, is_created = PlaceName.get_or_create_smart('능라도') addr2, is_created = Address.get_or_create_smart( '경기도 성남시 분당구 산운로32번길 12') note21, is_created = PlaceNote.get_or_create_smart('여기 가게 바로 옆으로 이전') note22, is_created = PlaceNote.get_or_create_smart('평양냉면 맛집') img21_content = 'http://blogpfthumb.phinf.naver.net/20100110_16/mardukas_1263055491560_VI01Ic_JPG/DSCN1968.JPG' img22_content = 'http://mblogthumb3.phinf.naver.net/20160807_298/mardukas_14705287064440EYVC_JPEG/DSC03530.JPG?type=w800' img21, is_created = Image.get_or_create_smart(img21_content) img22, is_created = Image.get_or_create_smart(img22_content) imgNote2, is_created = ImageNote.get_or_create_smart('만두 사진') url2, is_created = Url.get_or_create_smart('http://www.naver.com/') lp, is_created = LegacyPlace.get_or_create_smart( '4ccffc63f6378cfaace1b1d6.4square') phone2, is_created = PhoneNumber.get_or_create_smart('010-5597-9245') json_userPost = ''' { "lonLat": {"lon": %f, "lat": %f}, "name": {"uuid": "%s", "content": "%s"}, "addr2": {"uuid": "%s", "content": "%s"}, "notes": [{"uuid": "%s", "content": "%s"}, {"uuid": "%s", "content": "%s"}], "images": [{"uuid": "%s", "content": "%s", "note": {"uuid": "%s", "content": "%s"}}], "urls": [], "lps": [], "phone": {"uuid": "%s", "content": "%s"} } ''' % ( point1.x, point1.y, name1.uuid, name1.content, addr1.uuid, addr1.content, note12.uuid, note12.content, note11.uuid, note11.content, img1.uuid, img1.content, imgNote1.uuid, imgNote1.content, phone1.uuid, phone1.content, ) json_placePost = ''' { "lonLat": {"lon": %f, "lat": %f}, "name": {"uuid": "%s", "content": "%s"}, "addr1": {"uuid": "%s", "content": "%s"}, "addr2": {"uuid": "%s", "content": "%s"}, "notes": [ {"uuid": "%s", "content": "%s"}, {"uuid": "%s", "content": "%s"}, {"uuid": "%s", "content": "%s"}, {"uuid": "%s", "content": "%s"} ], "images": [ {"uuid": "%s", "content": "%s", "note": null}, {"uuid": "%s", "content": "%s", "note": {"uuid": "%s", "content": "%s"}}, {"uuid": "%s", "content": "%s", "note": {"uuid": "%s", "content": "%s"}} ], "urls": [{"uuid": "%s", "content": "%s"}], "lps": [{"uuid": "%s", "content": "%s"}], "phone": {"uuid": "%s", "content": "%s"} } ''' % ( point2.x, point2.y, name2.uuid, name2.content, addr2.uuid, addr2.content, addr1.uuid, addr1.content, note22.uuid, note22.content, note21.uuid, note21.content, note12.uuid, note12.content, note11.uuid, note11.content, img22.uuid, img22.content, img21.uuid, img21.content, imgNote2.uuid, imgNote2.content, img1.uuid, img1.content, imgNote1.uuid, imgNote1.content, url2.uuid, url2.content, lp.uuid, lp.content, phone2.uuid, phone2.content, ) pb1 = PostBase(json_userPost) pb2 = PostBase(json_placePost) self.assertEqual(PostPiece.objects.count(), 0) pp1 = PostPiece.create_smart(uplace1, pb1) self.assertEqual(PostPiece.objects.count(), 1) pp2 = PostPiece.create_smart(uplace2, pb2) pp3 = PostPiece.create_smart_4place(place, vd1, pb2, by_MAMMA=True) self.assertEqual(PostPiece.objects.count(), 3) want_userPost = json_loads(json_userPost) want_placePost = json_loads(json_placePost) self.assertNotIn('timestamp', uplace1.userPost.json['lonLat']) self.assertNotIn('timestamp', uplace1.userPost.json['name']) self.assertIn('timestamp', uplace1.userPost.json['notes'][0]) self.assertNotIn('timestamp', uplace1.userPost.json['images'][0]) self.assertIn('timestamp', uplace1.userPost.json['images'][0]['note']) self.assertNotIn('timestamp', uplace2.userPost.json['urls'][0]) self.assertNotIn('timestamp', uplace2.userPost.json['lps'][0]) timestamp = uplace1.userPost.json['notes'][0]['timestamp'] self.assertAlmostEqual(get_timestamp(), timestamp, delta=1000) self.assertIn('summary', uplace1.userPost.json['images'][0]) self.assertIn('phone', uplace1.userPost.json) self.assertNotEqual(uplace1.userPost.json['images'][0]['content'], None) self.assertIsSubsetOf(want_userPost, uplace1.userPost) self.assertIsNotSubsetOf(uplace1.userPost, want_userPost) self.assertIsSubsetOf(want_placePost, uplace1.place.placePost) self.assertIsNotSubsetOf(uplace1.place.placePost, want_placePost) uplace1._clearCache() p1 = uplace1.place.placePost uplace2._clearCache() p2 = uplace2.place.placePost place._clearCache() p3 = place.placePost self.assertDictEqual(p1.json, p3.json) self.assertDictEqual(p2.json, p3.json) pb12 = PostBase(json_userPost) pb12.update(pb1) self.assertNotEqual(pb12.json, pb1.json) pb12.normalize() self.assertEqual(pb12.json, pb1.json) pb13 = PostBase(json_userPost) pb13.update(pb1) pb13.update(pb1, add=False) pb_null = PostBase() self.assertEqual(pb13.json, pb_null.json) totalPost = place._totalPost self.assertIsSubsetOf(uplace1.place.placePost, totalPost) #self.assertIsSubsetOf(uplace1.userPost, totalPost) # Note 에서 timestamp 를 제거해야... #self.assertIsSubsetOf(uplace2.userPost, totalPost) # 상동 #self.assertIsNotSubsetOf(totalPost, uplace1.place.placePost) # userPost 를 하나 더 생성해야... # child/parent test uplace3 = UserPlace.objects.create(parent=uplace1) self.assertEqual(uplace3.parent, uplace1) self.assertNotEqual(uplace3.userPost, uplace1.userPost) self.printJson(uplace3.userPost.json) self.printJson(uplace1.userPost.json) self.assertEqual(uplace3.userPost.json, uplace1.userPost.json) uplace1._clearCache() uplace3._clearCache() pb3 = PostBase('{"notes": [{"content": "child"}]}') pp3 = PostPiece.create_smart(uplace3, pb3) self.assertNotEqual(uplace3.userPost, uplace1.userPost) self.assertNotEqual(uplace3.userPost.json, uplace1.userPost.json) place4 = Place.objects.create() uplace4 = UserPlace.objects.create(parent=uplace1, vd=vd1, place=place4) self.assertEqual(uplace4.parent, uplace1) self.assertNotEqual(uplace4.userPost, uplace1.userPost) self.assertEqual(uplace4.userPost.json, uplace1.userPost.json) uplace1._clearCache() uplace4._clearCache() pb3 = PostBase('{"notes": [{"content": "child"}]}') pp3 = PostPiece.create_smart(uplace4, pb3) self.assertNotEqual(uplace4.userPost, uplace1.userPost) self.assertNotEqual(uplace4.userPost.json, uplace1.userPost.json)
class PostPieceTest(APITestBase): def setUp(self): super(PostPieceTest, self).setUp() self.place = Place() self.place.save() self.uplace = UserPlace() self.uplace.save() self.vd = VD() self.vd.save() self.image, is_created = Image.get_or_create_smart( 'http://blogthumb2.naver.net/20160302_285/mardukas_1456922688406bYGAH_JPEG/DSC07301.jpg' ) self.url, is_created = Url.get_or_create_smart('http://www.naver.com/') self.lp, is_created = LegacyPlace.get_or_create_smart( '4ccffc63f6378cfaace1b1d6.4square') self.addr, is_created = Address.get_or_create_smart( '경기도 하남시 풍산로 270, 206동 402호 (선동, 미사강변도시2단지)') self.phone, is_created = PhoneNumber.get_or_create_smart( '010-5597-9245') def test_save_and_retreive(self): pp = PostPiece() pp.save() saved = PostPiece.objects.first() self.assertEqual(saved, pp) def test_id_property(self): pp = PostPiece(vd=self.vd) self.assertEqual(pp.id, None) timestamp = get_timestamp() pp.save() self.assertNotEqual(pp.id, None) self.assertAlmostEqual((int(pp.id) >> 8 * 8) & BIT_ON_8_BYTE, timestamp, delta=1000) self.assertEqual((int(pp.id) >> 2 * 8) & BIT_ON_6_BYTE, self.vd.id) saved = PostPiece.objects.first() self.assertEqual(saved, pp) self.assertEqual(saved.id, pp.id) # for timestamp property self.assertEqual(saved.timestamp, pp.timestamp) self.assertAlmostEqual(pp.timestamp, timestamp, delta=1000) def test_id_property_with_timestamp(self): pp = PostPiece(vd=self.vd) timestamp = get_timestamp() pp.save(timestamp=timestamp) self.assertEqual((int(pp.id) >> 8 * 8) & BIT_ON_8_BYTE, timestamp) self.assertEqual((int(pp.id) >> 2 * 8) & BIT_ON_6_BYTE, self.vd.id) saved = PostPiece.objects.first() self.assertEqual(saved, pp) self.assertEqual(saved.id, pp.id) def test_id_property_with_no_vd(self): pp = PostPiece() pp.save() self.assertEqual((int(pp.id) >> 2 * 8) & BIT_ON_6_BYTE, 0) def test_uplace_property(self): pp = PostPiece() pp.uplace = self.uplace pp.save() saved = self.uplace.pps.first() self.assertEqual(saved, pp) self.assertEqual(saved.uplace, pp.uplace) self.assertEqual(saved.place, None) def test_place_property(self): pp = PostPiece() pp.place = self.place pp.save() saved = self.place.pps.first() self.assertEqual(saved, pp) self.assertEqual(saved.place, pp.place) self.uplace.place = self.place self.uplace.save() pp2 = PostPiece() pp2.uplace = self.uplace self.assertEqual(pp2.place, None) pp2.save() self.assertEqual(pp2.place, self.place) def test_vd_property(self): pp = PostPiece() pp.vd = self.vd pp.save() saved = self.vd.pps.first() self.assertEqual(saved, pp) self.assertEqual(saved.vd, pp.vd) def test_data_property(self): # TODO : json 에 대한 query 테스트 추가 uplace = UserPlace.objects.create() pp = PostPiece(uplace=uplace) json_add = json_loads(''' { "lonLat": {"lon": 127.1037430, "lat": 37.3997320}, "images": [{"content": "http://blogthumb2.naver.net/20160302_285/mardukas_1456922688406bYGAH_JPEG/DSC07301.jpg"}], "addr1": {"content": "경기도 성남시 분당구 판교로 256번길 25"}, "addr2": {"content": "경기도 성남시 분당구 삼평동 631"}, "addr3": {"content": "경기도 성남시 분당구 삼평동"}, "urls": [{"content": "http://place.kakao.com/places/15738374"}] } ''') pp.data = json_add pp.vd = self.vd pp.save() saved = PostPiece.objects.first() self.assertEqual(json_add, pp.data) self.assertEqual(json_add, saved.data) self.assertEqual(pp.pb.image.vd, pp.vd) self.assertEqual(pp.pb.url.vd, pp.vd) pp2 = PostPiece() pb = PostBase(json_add) pp2.pb = pb pp2.save() saved = PostPiece.objects.order_by('-id').first() self.assertEqual(pp2, saved) self.assertDictEqual(pp2.pb.json, saved.pb.json) self.assertDictEqual(pp2.pb.cjson, saved.pb.cjson) self.assertDictEqual(pb.json, saved.pb.json) self.assertDictEqual(pb.cjson, saved.pb.cjson) self.assertEqual(pp2.pb.image.vd, pp2.vd) self.assertEqual(pp2.pb.url.vd, pp2.vd) def test_mask(self): pp = PostPiece() pp.save() saved = PostPiece.objects.first() self.assertEqual(saved.is_remove, False) self.assertEqual(saved.is_add, True) self.assertEqual(saved.by_MAMMA, False) self.assertEqual(saved.is_drop, False) self.assertEqual(saved.mask, 0 | 0) pp.is_remove = True pp.by_MAMMA = False pp.is_drop = True pp.save() saved = PostPiece.objects.first() self.assertEqual(saved.is_remove, True) self.assertEqual(saved.is_add, False) self.assertEqual(saved.by_MAMMA, False) self.assertEqual(saved.is_drop, True) self.assertEqual(saved.mask, 4 | 0 | 1) pp.is_remove = False pp.by_MAMMA = True pp.is_drop = False pp.save() saved = PostPiece.objects.first() self.assertEqual(saved.is_remove, False) self.assertEqual(saved.is_add, True) self.assertEqual(saved.by_MAMMA, True) self.assertEqual(saved.is_drop, False) self.assertEqual(saved.mask, 0 | 2 | 0) pp.by_MAMMA = False pp.is_drop = True pp.save() saved = PostPiece.objects.first() self.assertEqual(saved.is_remove, False) self.assertEqual(saved.is_add, True) self.assertEqual(saved.by_MAMMA, False) self.assertEqual(saved.is_drop, True) self.assertEqual(saved.mask, 4 | 0 | 0) pp.mask = 0 pp.is_bounded = True pp.save() saved = PostPiece.objects.first() self.assertEqual(saved.is_bounded, True) self.assertEqual(saved.mask, 0 | 8) pp.is_bounded = False pp.save() saved = PostPiece.objects.first() self.assertEqual(saved.is_bounded, False) self.assertEqual(saved.mask, 0 | 0)
def setUp(self): super(AfterLoginTaskTest, self).setUp() self.uplace = UserPlace(vd=self.vd) self.uplace.lonLat = GEOSGeometry('POINT(127.1037430 37.3997320)', srid=4326) self.uplace.save()
def test_save_and_retreive(self): uplace = UserPlace(vd=self.vd) uplace.save() saved = UserPlace.objects.first() self.assertEqual(saved, uplace)