def get(self): long_url = self.get_argument("long_url", default='', strip=False) short_url = self.get_argument("short_url", default='', strip=False) user_id = self.get_secure_cookie("token") generate_random_short_url = (len(short_url) == 0) max_left = MAX_SHORT_URL_LEN-BASE_URL_LEN-1 if not generate_random_short_url: if BASE_URL_LEN+len(short_url)+1 > MAX_SHORT_URL_LEN: # Short URL too long data = json.dumps({'error': 'short_url too long'}) self.set_status(405) self.set_header('Content-Type', 'application/json') self.write(data) return success = False while not success: try: if generate_random_short_url: short_url = Shortening.generate_random_shortening(max_left) s = Shortening(user=User.get(id=user_id), short_url=short_url, long_url=long_url) s.save() except IntegrityError: if not generate_random_short_url: self.set_status(400) data = json.dumps({'error': 'Short url already exist'}) self.set_header('Content-Type', 'application/json') self.write(data) return else: success = True full_short_url = BASE_URL+"/"+short_url data = json.dumps({'short_url': full_short_url, 'long_url': long_url}) self.set_header('Content-Type', 'application/json') self.write(data)
def db_init(): db.connect() if not User.table_exists(): User.create_table() if not Logs.table_exists(): Logs.create_table() if not Shortening.table_exists(): Shortening.create_table()
def get(self, short_url): token = self.get_secure_cookie("token") query = Shortening.select().where(Shortening.short_url == short_url) if query.exists(): shortening = query.get().id logs = list(Logs.select().where((Logs.user == token) & (Logs.shortening == shortening)).dicts()) data = json.dumps(logs, default=json_serial) else: self.set_status(400) data = json.dumps({'error': 'Unable to found given url'}) self.set_header('Content-Type', 'application/json') self.write(data)
def get(self, short_url): query = Shortening.select().where(Shortening.short_url == short_url) if query.exists(): shortening = query.get() l = Logs(user=shortening.user, shortening=shortening, ip=self.request.remote_ip) l.save() full_short_url = BASE_URL+"/"+shortening.short_url data = json.dumps({'short_url': full_short_url, 'long_url': shortening.long_url}) else: self.set_status(400) data = json.dumps({'error': 'Unable to found given url'}) self.set_header('Content-Type', 'application/json') self.write(data)
def get(self, short_url): token = self.get_secure_cookie("token") query = Shortening.select().where(Shortening.short_url == short_url) if query.exists(): shortening = query.get() logs = list(Logs.select().where((Logs.user == token) & (Logs.shortening == shortening.id)).dicts()) shortening_dict = model_to_dict(shortening) shortening_dict['short_url'] = BASE_URL+"/"+shortening.short_url shortening_dict['access_count'] = len(logs) data = json.dumps(shortening_dict, default=json_serial) else: self.set_status(400) data = json.dumps({'error': 'Unable to found given url'}) self.set_header('Content-Type', 'application/json') self.write(data)
def get(self): user_id = self.get_secure_cookie("token") shortenings = list(Shortening.select().where(Shortening.user == User.get(id=user_id)).dicts()) data = json.dumps(shortenings, default=json_serial) self.set_header('Content-Type', 'application/json') self.write(data)