def check_url_error_message(value): try: inputs.url(value) assert False, u"inputs.url({0}) should raise an exception".format(value) except ValueError as e: assert_equal(six.text_type(e), (u"{0} is not a valid URL. Did you mean: http://{0}".format(value)))
def get(self): # 参数可以是通过URL、form、json等方法传递过来的,比较灵活 parser = RequestParser() # location = 'args' 'json' 'form' parser.add_argument('province', required=True, type=str, help='Tell me you Province!', location="args") parser.add_argument('urls', required=True, type=str, help='Tel me You country URL', location="args") args = parser.parse_args(strict=True) print(args['urls']) print(request.full_path) print(request.url) try: inputs.url(args['urls']) except ValueError: abort(500, 'URL:{} is invalidate'.format(args['urls'])) return { 'country': 'China', 'code': 86010, 'more': { 'province': args['province'], 'url': args['urls'] } }
def post(self): try: inputs.url(request.form['url']) except ValueError as e: return {'response': 'not a valid url'}, 400 response = findDup(request.form['url']) return { 'response': response }
def test_urls(): urls = [ 'http://www.djangoproject.com/', 'http://localhost/', 'http://example.com/', 'http://www.example.com/', 'http://www.example.com:8000/test', 'http://valid-with-hyphens.com/', 'http://subdomain.example.com/', 'http://200.8.9.10/', 'http://200.8.9.10:8000/test', 'http://valid-----hyphens.com/', 'http://example.com?something=value', 'http://example.com/index.php?something=value&another=value2', 'http://*****:*****@example.com', 'http://foo:@example.com', 'http://foo:@2001:db8:85a3::8a2e:370:7334', 'http://*****:*****@example.com', ] for value in urls: yield assert_equal, inputs.url(value), value
def check_bad_url_raises(value): try: inputs.url(value) assert False, "shouldn't get here" except ValueError as e: assert_equal(six.text_type(e), u"{0} is not a valid URL".format(value))
def get(self): self.random = False self.resolution = {'x': 2048, 'y': 2048} self.photos_urls = [] self.photos_number = 0 self.photos_paths = [] parser = reqparse.RequestParser() parser.add_argument('losowo', type=int) parser.add_argument('rozdzielczosc', type=str) parser.add_argument('zdjecia', type=str, required=True) args = parser.parse_args(strict=True) self.random = args.losowo == 1 if args.rozdzielczosc is not None: if re.search("\d+x\d+", args.rozdzielczosc) is not None: self.resolution['x'] = int(args.rozdzielczosc.split('x')[0]) self.resolution['y'] = int(args.rozdzielczosc.split('x')[1]) self.photos_urls = args.zdjecia.split(',') try: self.photos_urls.remove('') del self.photos_urls[self.photos_urls.index('')] except ValueError: pass try: for url in self.photos_urls: url = inputs.url(url) except ValueError as e: resp = make_response(jsonify({'message': {'zdjecia': str(e)}}), 400) return resp self.photos_number = len(self.photos_urls) if self.photos_number > 8 or self.photos_number < 1 or self.photos_urls[0] is '': resp = make_response(jsonify({'message': {'zdjecia': 'Check your URLs'}}), 400) return resp for i in range(self.photos_number): r = requests.get(self.photos_urls[i], stream=True) if r.status_code == 200 and (r.headers['content-type'] == 'image/jpg' or r.headers['content-type'] == 'image/png' or r.headers['content-type'] == 'image/jpeg'): path = str(i)+'.jpg' self.photos_paths.append(path) with open(path, 'wb') as f: r.raw.decode_content = True shutil.copyfileobj(r.raw, f) else: resp = make_response(jsonify({'message': {'zdjecia': {self.photos_urls[i]: 'Can\'t download from this host'}}}), 400) return resp if self.random: random.shuffle(self.photos_paths) if self.photos_number == 1: final_im = Image.open(self.photos_urls[0]) final_im = final_im.resize((self.resolution['x'], self.resolution['y'])) final_im.save('mozaika.jpg', "JPEG") final_im.close() else: result = collage.create_collage(self.photos_paths, self.resolution['x'], self.photos_number / 2) result.save('mozaika.jpg', "JPEG") final_im = Image.open('mozaika.jpg') final_im = final_im.resize((self.resolution['x'], self.resolution['y'])) final_im.save('mozaika.jpg', "JPEG") final_im.close() for path in self.photos_paths: if os.path.exists(path): os.remove(path) return send_file('mozaika.jpg', mimetype='image/jpg')