Esempio n. 1
0
def index(request):
    if request.method == "POST":
        link = request.POST.get("link")
        short_link = ""

        alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']

        url = Url.objects.all()
        
        for i in url:
            if i.link == link:
                short_link = i.short_link
                break

        else:
            for i in range(1, 7):
                letters = random.randint(1, len(alpha) - 1)
                let = alpha[letters]
                short_link += let

            url = Url(link=link, short_link=short_link)
            url.save()

        new_url = "http://127.0.0.1:8000/" + short_link
        return render(request, "index.htm", {"new_url": new_url})

    return render(request, "index.htm")
Esempio n. 2
0
    def test_encoded_url4(self):
        test_data2 = 'https://m.map.naver.com/siteview.nhn?code=11523188&ret_url=https%3A%2F%2Fm.search.naver.com%2Fsearch.naver%3Fwhere%3Dm%26query%3D%25EC%259C%2584%25EB%258B%25B4%25ED%2595%259C%25EB%25B0%25A9%25EB%25B3%2591%25EC%259B%2590%26sm%3Dmsv_nex%23m_local'
        test_data1 = 'https://m.map.naver.com/siteview.nhn?code=11523188&ret_url=https://m.search.naver.com/search.naver?where=m&query=위담한방병원&sm=msv_nex#m_local'

        self.assertEqual(Url.objects.count(), 0)
        url1, is_created = Url.get_or_create_smart(test_data1)
        self.assertEqual(Url.objects.count(), 1)
        url2, is_created = Url.get_or_create_smart(test_data2)
        self.assertEqual(Url.objects.count(), 1)
        self.assertEqual(url1, url2)
        self.assertEqual(url1.content, url2.content)
Esempio n. 3
0
    def test_encoded_url2(self):
        test_data2 = 'https://place.kakao.com/places/10972091/%ED%99%8D%EB%AA%85'
        test_data1 = 'https://place.kakao.com/places/10972091/홍명'

        self.assertEqual(Url.objects.count(), 0)
        url1, is_created = Url.get_or_create_smart(test_data1)
        self.assertEqual(Url.objects.count(), 1)
        url2, is_created = Url.get_or_create_smart(test_data2)
        self.assertEqual(Url.objects.count(), 1)
        self.assertEqual(url1, url2)
        self.assertEqual(url1.content, url2.content)
Esempio n. 4
0
 def test_save_and_retreive(self):
     url, is_created = Url.get_or_create_smart('http://www.naver.com/')
     saved = Url.objects.first()
     self.assertEqual(url.uuid, '%s.url' % b16encode(url.id.bytes))
     self.assertEqual(saved, url)
     self.assertEqual(saved.id, url.id)
     saved2 = Url.get_from_json('{"uuid": "%s", "content": null}' %
                                url.uuid)
     self.assertEqual(saved2, url)
     saved3 = Url.get_from_json('{"uuid": null, "content": "%s"}' %
                                url.content)
     self.assertEqual(saved3, url)
Esempio n. 5
0
    def test_content_property(self):
        test_value = 'http://www.naver.com/'
        url, is_created = Url.get_or_create_smart(test_value)
        saved = Url.objects.first()
        self.assertEqual(saved.content, test_value)

        self.assertEqual(1, Url.objects.count())
        Url.get_or_create_smart(test_value)
        self.assertEqual(1, Url.objects.count())

        with self.assertRaises(IntegrityError):
            Url.objects.create(content=url.content)
Esempio n. 6
0
 def test_add_remove_place(self):
     url1, is_created = Url.get_or_create_smart('http://www.naver.com/')
     url2, is_created = Url.get_or_create_smart('http://www.daum.net/')
     place1 = Place.objects.create()
     place2 = Place.objects.create()
     self.assertEqual(url1.places.count(), 0)
     url1.add_place(place1)
     self.assertEqual(url1.places.count(), 1)
     url1.add_place(place2)
     self.assertEqual(url1.places.count(), 2)
     url1.remove_place(place1)
     self.assertEqual(url1.places.count(), 1)
     self.assertEqual(url2.places.count(), 0)
Esempio n. 7
0
    def test_placed_by_url1(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)

        url.add_place(place1)
        response = self.client.post('/uplaces/', dict(add=json_add, ))
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(UserPlace.objects.count(), 0 + 1)
        place_id = json_loads(response.content)['place_id']
        self.assertEqual(place_id, place1.id)

        url.add_place(place2)
        self.assertEqual(UserPlace.objects.count(), 1 + 2)
        response = self.client.post('/uplaces/', dict(add=json_add, ))
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(UserPlace.objects.count(), 3 + 1)
        place_id = json_loads(response.content)['place_id']
        self.assertEqual(place_id, None)

        url.add_place(place3)
        self.assertEqual(UserPlace.objects.count(), 4 + 1)
        response = self.client.post('/uplaces/', dict(add=json_add, ))
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(UserPlace.objects.count(), 5 + 1)
        place_id = json_loads(response.content)['place_id']
        self.assertEqual(place_id, None)
Esempio n. 8
0
def index():
    if request.method == 'POST':
        def gen():
            chars = string.ascii_letters + string.digits
            length = 5
            code = ''.join(choice(chars) for x in range(length))
            print("Checking", code)
            exists = db.session.query(
                db.exists().where(Url.new == code)).scalar()
            if not exists:
                print("Your new code is:", code)
                return code
        code = gen()
        while code is None:
            code = gen()

    if request.method == 'POST' and code is not None:
        form = UrlForm(request.form)
        if form.validate_on_submit():
            url = form.save_url(Url(new=code))
            db.session.add(url)
            db.session.commit()
            return render_template("success.html", code=code, old=url.old)
        else:
            print("Validation failed")
    else:
        form = UrlForm()
    return render_template("index.html", form=form)
Esempio n. 9
0
def index():
    def gen():
        chars = string.ascii_letters + string.digits
        length = 5
        code = ''.join(choice(chars) for x in range(length))
        exists = db.session.query(db.exists().where(Url.new == code)).scalar()
        if not exists:
            return code

    if request.method == 'POST':
        form = UrlForm(request.form)
        if form.validate():
            url = form.save_url(Url())

            if not url.new:
                code = gen()
                while code is None:
                    code = gen()
                url.new = code

            db.session.add(url)
            db.session.commit()
            return render_template("success.html", code=url.new, old=url.old)
    else:
        form = UrlForm()
    return render_template("index.html", form=form)
Esempio n. 10
0
    def test_access_methods(self):
        test_data = 'http://m.blog.naver.com/mardukas/220555109681'
        url, is_created = Url.get_or_create_smart(test_data)

        url.access()
        self.assertValidLocalFile(url.path_accessed)
        self.assertValidInternetUrl(url.url_accessed)
Esempio n. 11
0
    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)
Esempio n. 12
0
    def test_get_from_url(self):
        # naver disabled
        '''
        url, is_created = Url.get_or_create_smart('http://map.naver.com/local/siteview.nhn?code=21149144')
        self.assertEqual(LegacyPlace.get_from_url(url).content, '21149144.naver')

        url, is_created = Url.get_or_create_smart('http://map.naver.com/?app=Y&version=10&appMenu=location&pinId=31130096&pinType=site&lat=37.3916387&lng=127.0584149&title=능이향기&dlevel=11')
        self.assertEqual(LegacyPlace.get_from_url(url).content, '31130096.naver')

        url, is_created = Url.get_or_create_smart('http://m.store.naver.com/restaurants/detail?id=37333252')
        self.assertEqual(LegacyPlace.get_from_url(url).content, '37333252.naver')

        url, is_created = Url.get_or_create_smart('http://m.map.naver.com/siteview.nhn?code=31176899')
        self.assertEqual(LegacyPlace.get_from_url(url).content, '31176899.naver')

        url, is_created = Url.get_or_create_smart('https://m.map.naver.com/viewer/map.nhn?pinType=site&pinId=21652462')
        self.assertEqual(LegacyPlace.get_from_url(url).content, '21652462.naver')

        url, is_created = Url.get_or_create_smart('https://m.map.naver.com/siteview.nhn?code=11523188&ret_url=https%3A%2F%2Fm.search.naver.com%2Fsearch.naver%3Fwhere%3Dm%26query%3D%25EC%259C%2584%25EB%258B%25B4%25ED%2595%259C%25EB%25B0%25A9%25EB%25B3%2591%25EC%259B%2590%26sm%3Dmsv_nex%23m_local')
        self.assertEqual(LegacyPlace.get_from_url(url).content, '11523188.naver')
        '''


        url, is_created = Url.get_or_create_smart('http://place.kakao.com/places/14720610')
        self.assertEqual(LegacyPlace.get_from_url(url).content, '14720610.kakao')

        url, is_created = Url.get_or_create_smart('http://m.map.daum.net/actions/detailInfoView?id=15493954')
        self.assertEqual(LegacyPlace.get_from_url(url).content, '15493954.kakao')

        url, is_created = Url.get_or_create_smart('http://m.map.daum.net/actions/detailInfoView?id=15493954')
        self.assertEqual(LegacyPlace.get_from_url(url).content, '15493954.kakao')

        url, is_created = Url.get_or_create_smart('http://place.map.daum.net/26799590')
        self.assertEqual(LegacyPlace.get_from_url(url).content, '26799590.kakao')

        url, is_created = Url.get_or_create_smart('http://foursquare.com/v/4ccffc63f6378cfaace1b1d6')
        self.assertEqual(LegacyPlace.get_from_url(url).content, '4ccffc63f6378cfaace1b1d6.4square')

        url, is_created = Url.get_or_create_smart('https://ko.foursquare.com/v/방아깐/4ccffc63f6378cfaace1b1d6')
        self.assertEqual(LegacyPlace.get_from_url(url).content, '4ccffc63f6378cfaace1b1d6.4square')

        url, is_created = Url.get_or_create_smart('http://ko.foursquare.com/v/4ccffc63f6378cfaace1b1d6')
        self.assertEqual(LegacyPlace.get_from_url(url).content, '4ccffc63f6378cfaace1b1d6.4square')

        url, is_created = Url.get_or_create_smart('https://www.mangoplate.com/restaurants/f-YvkBx8IemC')
        self.assertEqual(LegacyPlace.get_from_url(url).content, 'f-YvkBx8IemC.mango')

        if not WORK_ENVIRONMENT:
            url, is_created = Url.get_or_create_smart('https://www.google.com/maps/place/Han+Ha+Rum+Korean+Restaurant/@22.3636765,113.4067433,9z/data=!4m8!1m2!2m1!1z7Iud64u5!3m4!1s0x34040056de2d51b3:0xae100fb893526bdf!8m2!3d22.2801408!4d114.182783')
            self.assertEqual(LegacyPlace.get_from_url(url).content, 'ChIJs1Et3lYABDQR32tSk7gPEK4.google')
Esempio n. 13
0
 def test_naver_shortener_url3(self):
     test_value = 'http://me2.do/xgcFeqMZ'
     normalized_value = 'http://m.map.naver.com/siteview.nhn?code=31176899'
     self.assertEqual(Url.objects.count(), 0)
     url, is_created = Url.get_or_create_smart(test_value)
     self.assertEqual(Url.objects.count(), 1)
     saved = Url.objects.first()
     self.assertEqual(url.content, normalized_value)
     self.assertEqual(saved, url)
     self.assertEqual(saved.content, normalized_value)
Esempio n. 14
0
 def test_naver_shortener_url4(self):
     test_value = 'http://me2.do/GNAl9bvK'
     normalized_value = 'http://blog.naver.com/a878062/220392611381'
     self.assertEqual(Url.objects.count(), 0)
     url, is_created = Url.get_or_create_smart(test_value)
     self.assertEqual(Url.objects.count(), 1)
     saved = Url.objects.first()
     self.assertEqual(url.content, normalized_value)
     self.assertEqual(saved, url)
     self.assertEqual(saved.content, normalized_value)
Esempio n. 15
0
 def test_naver_shortener_url2(self):
     test_value = 'http://me2.do/xLOGJZ19'
     normalized_value = 'http://m.store.naver.com/restaurants/detail?id=37333252'
     self.assertEqual(Url.objects.count(), 0)
     url, is_created = Url.get_or_create_smart(test_value)
     self.assertEqual(Url.objects.count(), 1)
     saved = Url.objects.first()
     self.assertEqual(url.content, normalized_value)
     self.assertEqual(saved, url)
     self.assertEqual(saved.content, normalized_value)
Esempio n. 16
0
def check_url(request):
    response = False
    redirect = None
    if 'url' in request.GET:
        url = request.GET['url']
        if url:
            try:
                urlobj = Url.objects.get(src = url)
                redirect = urlobj.redirect
            except Url.DoesNotExist or not urlobj:
                try:
                    res = urllib2.urlopen(url)
                    redirect = res.geturl()
                    urlobj = Url(src=url, redirect=redirect)
                    urlobj.save()
                except Exception:
                    response = {'result': 'false'}
            if redirect and redirect != url:
                alerts = 'no'
                if urlobj.alerts:
                    alerts = 'yes'
                else:
                    urlparts = urlparse(redirect)
                    hostname = urlparts.hostname
                    try:
                        malw = Malware.objects.get(pk=hostname)
                        if malw:
                            alerts = 'yes'
                    except Malware.DoesNotExist:
                        pass
                    res = look.lookup_by_url(redirect)
                    if res and res == 'M':
                        alerts = 'yes'
                response = {'result': 'true', 'redirect': redirect, 'alert': alerts }
            else:
                response = {'result': 'false'}
        else:
            response = {'result': 'false'}
    else:
        response = {'result': 'false'}
    return response
Esempio n. 17
0
 def test_naver_shortener_url5(self):
     test_value = 'http://naver.me/GjM8YqPC'
     normalized_value = 'http://map.naver.com/?app=Y&version=10&appMenu=location&pinId=36229742&pinType=site&lat=37.4893023&lng=127.1350392&title=%EB%B0%94%EC%9D%B4%ED%82%A4%20%EB%AC%B8%EC%A0%95%EC%A0%90&dlevel=11'
     self.assertEqual(Url.objects.count(), 0)
     url, is_created = Url.get_or_create_smart(test_value)
     self.assertEqual(Url.objects.count(), 1)
     saved = Url.objects.first()
     self.assertEqual(
         url.content,
         unquote_plus(normalized_value.encode('utf-8')).decode('utf-8'))
     self.assertEqual(saved, url)
     self.assertEqual(
         saved.content,
         unquote_plus(normalized_value.encode('utf-8')).decode('utf-8'))
Esempio n. 18
0
 def test_naver_shortener_url1(self):
     test_value = 'http://me2.do/GZkw1y27'
     normalized_value = 'http://map.naver.com/?app=Y&version=10&appMenu=location&pinId=31130096&pinType=site&lat=37.3916387&lng=127.0584149&title=%EB%8A%A5%EC%9D%B4%ED%96%A5%EA%B8%B0&dlevel=11'
     self.assertEqual(Url.objects.count(), 0)
     url, is_created = Url.get_or_create_smart(test_value)
     self.assertEqual(Url.objects.count(), 1)
     saved = Url.objects.first()
     self.assertEqual(
         url.content,
         unquote_plus(normalized_value.encode('utf-8')).decode('utf-8'))
     self.assertEqual(saved, url)
     self.assertEqual(
         saved.content,
         unquote_plus(normalized_value.encode('utf-8')).decode('utf-8'))
Esempio n. 19
0
 def test_4square_shortener_url_with_garbage(self):
     if WORK_ENVIRONMENT: return
     test_value = 'DOREDORE (도레도레) - 하남대로 929 - http://4sq.com/MVWRaG'
     normalized_values = [
         'https://foursquare.com/v/doré-doré/500d3737e4b03e92379f2714',
         'https://foursquare.com/v/doré-doré-도레도레/500d3737e4b03e92379f2714'
     ]
     self.assertEqual(Url.objects.count(), 0)
     url, is_created = Url.get_or_create_smart(test_value)
     self.assertEqual(Url.objects.count(), 1)
     saved = Url.objects.first()
     self.assertIn(url.content, normalized_values)
     self.assertEqual(saved, url)
     self.assertIn(saved.content, normalized_values)
Esempio n. 20
0
    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)
Esempio n. 21
0
def makeANewUrl(old):
    def gen():
        chars = string.ascii_letters + string.digits
        length = 5
        code = ''.join(choice(chars) for x in range(length))
        print("Checking", code)
        exists = db.session.query(db.exists().where(Url.new == code)).scalar()
        if not exists:
            print("Your new code is:", code)
            return code

    code = gen()
    while code is None:
        code = gen()

    if code is not None:
        addedurl = Url(new=code, old=old)
        db.session.add(addedurl)
        db.session.commit()
        return jsonify(Url=addedurl.serialize)
Esempio n. 22
0
    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')
Esempio n. 23
0
def url_placed(request):
    url = None
    places = None
    vd_id = request.session[VD_SESSION_KEY]
    vd = VD.objects.get(id=vd_id)
    if not vd:
        raise ValueError('Admin Login Required')

    if request.method == 'POST':
        raw_url = request.POST['url']
        url, is_created = Url.get_or_create_smart(raw_url)
        if not url:
            raise ValueError('Invalid URL')
        raw_places = request.POST['places']

        for raw_place in raw_places.split('\n'):
            raw_place = raw_place.strip()
            is_remove = False
            if raw_place.startswith('-'):
                is_remove = True
                raw_place = raw_place[1:]
            if not raw_place:
                continue
            lp_content = LegacyPlace.normalize_content(raw_place)
            if lp_content:
                lp, is_created = LegacyPlace.get_or_create_smart(lp_content)
                pb = PostBase()
                pb.lps.append(lp)
                place, is_created = Place.get_or_create_smart(pb.pb_MAMMA, vd)
                if is_remove:
                    url.remove_place(place)
                else:
                    url.add_place(place)
            else:
                raise NotImplementedError
        places = [place for place in url.places.all()]

    return render(request, 'admin2/url_placed.html',
                  dict(url=url, places=places))
Esempio n. 24
0
    def setUp(self, json, timestamp=None, vd=None):

        # name 조회
        if is_valid_json_item('name', json):
            name = PlaceName.get_from_json(json['name'])
            if name:
                name.vd = vd
                self.names.append(name)

        # visit 조회
        if is_valid_json_item('visit', json):
            visit = Visit.get_from_json(json['visit'])
            if visit:
                visit.vd = vd
                self.visits.append(visit)

        # rating 조회
        if is_valid_json_item('rating', json):
            rating = Rating.get_from_json(json['rating'])
            if rating:
                rating.vd = vd
                self.ratings.append(rating)

        # lonLat 조회
        if is_valid_json_item('lonLat', json):
            point = Point.get_from_json(json['lonLat'])
            if point:
                point.vd = vd
                self.points.append(point)

        # phone 조회
        if is_valid_json_item('phone', json):
            phone = PhoneNumber.get_from_json(json['phone'])
            if phone:
                phone.vd = vd
                self.phone = phone

        # addr1 조회
        if is_valid_json_item('addr1', json):
            addr1 = Address.get_from_json(json['addr1'])
            if addr1:
                addr1.vd = vd
                self.addrs1.append(addr1)

        # addr2 조회
        if is_valid_json_item('addr2', json):
            addr2 = Address.get_from_json(json['addr2'])
            if addr2:
                addr2.vd = vd
                self.addrs2.append(addr2)

        # addr3 조회
        if is_valid_json_item('addr3', json):
            addr3 = Address.get_from_json(json['addr3'])
            if addr3:
                addr3.vd = vd
                self.addrs3.append(addr3)

        # lps 조회
        if is_valid_json_item('lps', json):
            for lp_json in json['lps']:
                if is_valid_json_item(None, json):
                    lp = LegacyPlace.get_from_json(lp_json)
                    if lp:
                        lp.vd = vd
                        self.lps.append(lp)

        # urls 조회
        if is_valid_json_item('urls', json):
            from url.models import Url
            for url_json in json['urls']:
                if is_valid_json_item(None, url_json):
                    url = Url.get_from_json(url_json)
                    if url:
                        url.vd = vd
                        self.urls.append(url)

        # notes 조회
        if is_valid_json_item('notes', json):
            for note_json in json['notes']:
                if is_valid_json_item(None, note_json):
                    note = PlaceNote.get_from_json(note_json)
                    if note:
                        note.timestamp = timestamp
                        note.vd = vd
                        self.notes.append(note)

        # images 조회
        if is_valid_json_item('images', json):
            for img_json in json['images']:
                if is_valid_json_item(None, img_json):
                    img = Image.get_from_json(img_json)
                    if img:
                        if 'note' in img_json and img_json['note']:
                            note = ImageNote.get_from_json(img_json['note'])
                            if note:
                                note.timestamp = timestamp
                                note.vd = vd
                                img.note = note
                        img.vd = vd
                        self.images.append(img)

        # place_id, uplace_uuid, iplace_uuid 조회
        if is_valid_json_item('place_id', json):
            self.place_id = json['place_id']
        if is_valid_json_item('uplace_uuid', json):
            self.uplace_uuid = json['uplace_uuid']
        if is_valid_json_item('iplace_uuid', json):
            self.iplace_uuid = json['iplace_uuid']
Esempio n. 25
0
    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)
Esempio n. 26
0
 def test_string_representation(self):
     url, is_created = Url.get_or_create_smart('http://www.naver.com/')
     self.assertEqual(unicode(url), url.content)