Ejemplo n.º 1
0
    def r(self,id):
        if request.method == 'GET':
            lurlq = Session.query(Redir).filter(Redir.short == id)
            
            if lurlq.count() == 1:
                lurl = lurlq.one()
                if g.anon:
                    rip = g.anon_string
                else:
                    rip = request.environ['REMOTE_ADDR']
                
                Session.query(Counter).filter(Counter.short_id == lurl.id).update({Counter.counter: Counter.counter + 1})

                nv = Visitor(lurl.id, rip, datetime.now(), request.environ['HTTP_USER_AGENT'])
                Session.add(nv)
                Session.commit() 

                redirect(lurl.long,code=301)
            else:
                redirect(url('/'), code=301)
Ejemplo n.º 2
0
    def submit(self):
        out = {}
        if request.method == 'POST':
            shurl = request.POST.get('short').strip()

            # check if url is well formed is prformed client side
            # with a javascript check
            lurl_x = Session.query(Redir).filter(Redir.long == shurl)
            lurl_x_count = lurl_x.count()

            # we already have a shorten for the url
            if lurl_x_count == 1:
                entry = lurl_x.one()
                genshurl = entry.short
                
                reply = self.serv_port+genshurl
                out = {'short': reply} 
                return out
            
            # compute a new shorten
            gs_ex = True
    
            while gs_ex: 
                genshurl = ''.join(random.choice(self.char_set) for x in range(6))
                gs_ex_count = Session.query(Redir).filter(Redir.short == genshurl).count()
            
                if gs_ex_count == 0:
                    gs_ex = False

            # ratelimit ctl
            rip = request.environ['REMOTE_ADDR']
            if not g.rl.check(rip):
            #if not self.ratelimit(datetime.now(),rip):
                rate_limit_ok = False
                out = {'RATE_LIMIT': rate_limit_ok}
                return out

            rate_limit_ok = True

            if not g.anon:
                rip = request.environ['REMOTE_ADDR']
            else:
                rip = g.anon_string
            # add the shorten to the database 
            ne = Redir(genshurl,shurl, datetime.now(), rip, cby=None)                
            Session.add(ne)
            # commit to database
            Session.commit()
            
            nc = Counter(ne.id)
            Session.add(nc)
            Session.commit()
            
            # form the full short url
            short_reply = self.serv_port+genshurl
            session['shorturls'][shurl] = short_reply
            session.save()
            
            if g.qr_enable:
                qr = qrl.QRCode(8, qrl.QRErrorCorrectLevel.H)
                qr.addData(self.serv_port+genshurl)
                qr.make()

                im = qr.makeImage()
                # thumbnail with nearest algorithm
                im.thumbnail(g.qr_pixel_size)
            
                im.save(self.qr_store+genshurl+'.png')
            
                print self.qr_store+genshurl+'.png'

                qr_reply = self.qr_url+genshurl+'.png'
                out = {'short': short_reply, 'qrcode': qr_reply, 'RATE_LIMIT': rate_limit_ok}
                return out
            
            # format and send the reply
            out = {'RATE_LIMIT': rate_limit_ok, 'short': short_reply}
            return out