def test_place_property(self): place = Place() place.save() self.assertEqual(LegacyPlace.objects.count(), 0) lp, is_created = LegacyPlace.get_or_create_smart('14720610.kakao') self.assertEqual(LegacyPlace.objects.count(), 1) self.assertEqual(lp.place, None) self.assertEqual(lp.lp_type, LP_TYPE['kakao']) lp2, is_created = LegacyPlace.get_or_create_smart('14720610.kakao') self.assertEqual(LegacyPlace.objects.count(), 1) self.assertEqual(lp2.place, None) self.assertEqual(lp2.lp_type, LP_TYPE['kakao']) self.assertEqual(lp2, lp) lp3, is_created = LegacyPlace.get_or_create_smart('15738374.kakao') self.assertEqual(LegacyPlace.objects.count(), 2) self.assertEqual(lp3.place, None) self.assertEqual(lp3.lp_type, LP_TYPE['kakao']) self.assertNotEqual(lp3, lp) lp.place = place lp.save() self.assertEqual(place.lps.first(), lp) lp3.place = place with self.assertRaises(IntegrityError): lp3.save()
def setUp(self): super(UserPlaceTest, self).setUp() self.place = Place() point = GEOSGeometry('POINT(127.1037430 37.3997320)', srid=4326) self.place.lonLat = point self.place.save() self.vd = VD() self.vd.save()
def test_string_representation(self): place = Place() place.save() self.assertEqual(unicode(place), 'No named place object') placeName = PlaceName(content='능이향기') placeName.save() place.placeName = placeName place.save() self.assertEqual(unicode(place), '능이향기')
def create_place(place_object): if not 'name' in place_object: create_moderaion(place_object, None) return False if not place_object['name']: create_moderaion(place_object, None) return False kwargs = { 'is_published': True, 'manual_changed': False, 'identity': place_object['response_url'], } place = Place(**kwargs) for f in ['name', 'url', 'email']: if f in place_object: setattr(place, f, place_object[f]) place.save(site=Site.objects.get(id=place_object['site_id'])) for cat in place_object['category']: place.category.add(PlaceCategory.objects.get(id=cat['id'])) for tag in place_object['tagging']: place.tagging.add(Tag.objects.get(id=tag['id'])) payments = place_object.get('payments') if payments: payments_list = [] for p in payments: p_slug = pytils.translit.slugify(p) payment, is_created = PaymentSystem.objects.get_or_create( name=p_slug, display=p) payments_list.append(payment) place.payments.add(*payments_list) if not place_object['adr']: create_moderaion(place_object, place) return False for adr_object in place_object['adr']: adr = PlaceAddress() adr.address = adr_object['address'] if 'phone' in adr_object: adr.phone = adr_object['phone'] adr.place = place adr.save() if 'wt_list' in adr_object: for wt_dump in adr_object['wt_list']: wt = PlaceAddressWorkTime(**wt_dump) wt.address = adr wt.save() place.save() return True
def test_lonLat_column(self): place = Place() point1 = GEOSGeometry('POINT(127.1037430 37.3997320)', srid=4326) place.lonLat = point1 place.save() saved = Place.objects.first() self.assertEqual(place.lonLat, point1) self.assertEqual(saved.lonLat, point1) point2 = GEOSGeometry('POINT(127.107316 37.400998)', srid=4326) qs1 = Place.objects.filter(lonLat__distance_lte=(point2, D(m=100))) self.assertEqual(len(qs1), 0) qs2 = Place.objects.filter(lonLat__distance_lte=(point2, D(m=1000))) self.assertEqual(len(qs2), 1)
def test_placeName_column(self): place = Place() test_data = '능이향기' placeName = PlaceName(content=test_data) placeName.save() place.placeName = placeName place.save() saved = Place.objects.first() self.assertEqual(place.placeName, placeName) self.assertEqual(place.placeName.content, test_data) self.assertEqual(saved, place) self.assertEqual(saved.placeName, placeName) self.assertEqual(saved.placeName.content, test_data) self.assertEqual(placeName.places.first(), saved) self.assertEqual( Place.objects.filter(placeName=placeName).first(), saved)
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_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)
def test_save_and_retreive(self): place = Place() place.save() saved = Place.objects.first() self.assertEqual(saved, place)
# y1= y.replace('(', '') # y1 = y1.replace(')', '') # y1 = y1.split(',') # if len(y1) > 1: get_place = Place.objects.filter(id=a).first() if get_place: get_place.description = e get_place.save() else: place = Place( source_id=a, county=b, title=c, description=e, action_type=action_type, place_type=g, rule_text=y, owner_text=v, administrator_text=w, contact_text=n, nursery_text=ad, garden_text=ae, link=h, cover_url=i, lat=j, lon=k, ) place.save() counter += 1
def import_places(): OLD_LOGOTYPE_PATH = os.path.join(PATH_FOR_OLD_SITE, 'images/afisha/zaveds') OLD_PROMOPICTURE_PATH = os.path.join(PATH_FOR_OLD_SITE, 'images/places') content_type = ContentType.objects.get_for_model(Place) fields = [ 'id', #pk 'title', #title 'description', #description 'url', #url 'urlhits', #urlhits 'email', #email 'logotype', #logotype 'logotype_alt', #logotype_alt 'picture', #photo 'picture_alt', #photo_alt 'published', #is_published 'promo_published', #promo_is_up 'promo_from_date', #date_promo_up 'promo_till_date', #date_promo_down 'hits', #hits ] sql = "SELECT %s FROM jos_places_company" % (', '.join(fields)) cursor = db.cursor() cursor.execute(sql) all_rows = cursor.fetchall() count = 0 bar = progressbar.ProgressBar(maxval=len(all_rows), widgets=[ 'import places: ', progressbar.SimpleProgress(), ]).start() for row in all_rows: place = Place(pk=int(row[0]), name=str(row[1]), description=str(row[2]), url=str(row[3]), urlhits=int(row[4]), email=str(row[5]), logotype_alt=str(row[7]), photo_alt=str(row[9]), is_published=bool(row[10]), promo_is_up=bool(row[11]), hits=int(row[14])) try: place.date_promo_up = datetime.strptime(str(row[12]), \ '%Y-%m-%d') except ValueError: pass try: place.date_promo_down = datetime.strptime(str(row[13]), \ '%Y-%m-%d') except ValueError: pass path = os.path.join(OLD_LOGOTYPE_PATH, str(row[6])) if os.path.isfile(path): place.logotype = path place.logotype.save(place_gen_file_name(place, path), \ ContentFile(open(path, 'rb').read())) path = os.path.join(OLD_PROMOPICTURE_PATH, str(place.pk), 'promo_picture/big', str(row[8])) if os.path.isfile(path): place.photo = path place.photo.save(place_gen_file_name(place, path), \ ContentFile(open(path, 'rb').read())) sql = "SELECT COUNT(*) FROM jos_places_company_extensions WHERE company_id=%s and extension_id=1" \ % place.pk cursor = db.cursor() cursor.execute(sql) result = cursor.fetchall() if bool(result[0][0]): place.is_new = True sql = "SELECT COUNT(*) FROM jos_places_company_extensions WHERE company_id=%s and extension_id=2" \ % place.pk cursor = db.cursor() cursor.execute(sql) result = cursor.fetchall() if bool(result[0][0]): place.expert_choice = True place.save() sql = "SELECT tag_id FROM jos_places_category WHERE company_id=%s" \ % place.pk cursor = db.cursor() cursor.execute(sql) cat_list = cursor.fetchall() for row in cat_list: cat_pk = int(row[0]) cat = PlaceCategory.objects.get(main_tag__pk=cat_pk) place.category.add(cat) sql = "SELECT teg_id FROM jos_tegs_zaveds WHERE zaved_id=%s" \ % place.pk cursor = db.cursor() cursor.execute(sql) tag_list = cursor.fetchall() for row in tag_list: tag_pk = int(row[0]) try: tag = Tag.objects.get(pk=tag_pk) except: pass else: place.tagging.add(tag) import_place_comment(content_type, place) place.save() import_address(place) count += 1 bar.update(count) bar.finish()