def post(self): try: # get url param from request long_url = request.args.get('url') # get time param from url time can be null time = request.args.get('time') # check that url has a true value validate(long_url) # make a random short string short_url = random_generator() # make a new object from url model url = Url() # if time doesn't null convert string to a datetime object if time is not None: url.pub_date = datetime.strptime(time, '%Y-%m-%d %H:%M') url.long_url = long_url url.short_url = short_url db.session.add(url) db.session.commit() # if process doesn't have any error we return the short url in format of jason to user return {'short_url': short_url} except ValidationError: return {'error': 'an error has occurred'} except Exception: return {'error': 'wrong format time error'}
def post(self): json_data = request.get_json(force=True) if not json_data: return {'message': 'No input data provided'}, 400 # Validate and deserialize input data, errors = url_schema.load(json_data) if errors: return errors, 422 print data url = data['url'].split('//') url = url[1:] if len(url) == 2 else url[:] url = url[0].split('/') url[0] = url[0].split('.') url[0] = ".".join(url[0][1:] if len(url[0]) == 3 else url[0][:]) url = "/".join(url) id = Url.query.filter_by(url=url).first() if not id: id = Url.query.order_by('-id').first() if not id: id = 0 short_url = host + hashids.encode(id + 1) data = Url(url=url, short_url=short_url) db.session.add(data) db.session.commit() else: return {'message': 'Short url already exists'}, 400 return {'status': 'success'}, 201
def create_new_url(): """Creates a new shorter url and adds to db""" original_url = request.form.get('original_url') #Checks to see if inputted url is valid if not validators.url(original_url): return render_template('not_valid_url.html') custom_url = request.form.get('custom_url') row = Url.query.filter_by(original_url=original_url).first() if row: flash("This URL is already in our system. This is the shortened URL: ") return render_template('result.html', shortened_url=row.shortened_url) if custom_url: if custom_url[:2] == "0x": flash("Please choose another custom url.") return redirect('/') custom_row = Url.query.filter_by(shortened_url=custom_url).first() if custom_row: flash("We're sorry, that custom shortened url is already taken") return redirect('/') else: new_shortened = Url(original_url=original_url, shortened_url=custom_url) db.session.add(new_shortened) db.session.commit() return render_template('result.html', shortened_url=custom_url) new_shortened = Url(original_url=original_url) db.session.add(new_shortened) db.session.commit() new_shortened.shortened_url = hex(int(new_shortened.id)) db.session.commit() return render_template('result.html', shortened_url=new_shortened.shortened_url)
def get_short_url(self, long_url): ## if short url already exists for given long url, return existing found, url = self.url_repository.find_by_long_url(long_url) if found: return url ## if not exists, generate, check if exists, store and return short_url = None found = True while found: ##TODO: unlikely that it will be needed, but probably want some forced exit after n attempts short_url = random_url(self.url_length) found, url = self.url_repository.find_by_short_url(short_url) url = Url(long_url, short_url) self.url_repository.add_url(url) return url
def create(): for i in data['result']['results']: id = i['_id'] name = i['stitle'] transport = i['info'] category = i['CAT2'] longitude = i['longitude'] address = i['address'] latitude = i['latitude'] describe = i['xbody'] mrt = i['MRT'] images = image_site_split(i['file']) spt = TravelSpot(id, name, transport, category, longitude, latitude, address, describe, mrt) db.session.add(spt) for image in images: url = Url(id, image) db.session.add(url) db.session.commit() #TravelSpot(name,transport,category,longitude,latitude,address,describe,mrt) #Url()
def make_obj(self, data, **kwargs): data['short_url'] = generate_short_url() return Url(**data)