def test_get_item_pictures__success(self): item = Item.create(**TEST_ITEM) Picture.create(item=item, **TEST_PICTURE) Picture.create(item=item, **TEST_PICTURE2) resp = self.app.get( '/items/{item_uuid}/pictures/'.format(item_uuid=item.uuid)) assert resp.status_code == client.OK test_utils.assert_valid_response( resp.data, EXPECTED_RESULTS['get_item_pictures__success'])
def search(parameters): # Préparation des paramètres de la requête payload = { 'client': "v8.a.3", 'price_range': "%s,%s" % (parameters['price'][0], parameters['price'][1]), # Loyer 'area_range': "%s,%s" % (parameters['surface'][0], parameters['surface'][1]), # Surface 'rooms_range': "%s,%s" % (parameters['rooms'][0], parameters['rooms'][1]), # Pièces 'bedrooms_range': "%s,%s" % (parameters['bedrooms'][0], parameters['bedrooms'][1]), # Chambres 'localities': ','.join(key for key in search_city_code(parameters['cities'])) } # Insertion des paramètres propres à LeBonCoin payload.update(parameters['logic-immo']) request = requests.post( "http://lisemobile.logic-immo.com/li.search_ads.php", params=payload, headers=header) data = request.json() for ad in data['items']: annonce, created = Annonce.get_or_create( id='logic-immo-' + ad['identifiers']['main'], site="Logic Immo", created=datetime.fromtimestamp(ad['info']['firstOnlineDate']), title="%s %s pièces" % (ad['info']['propertyType']['name'], ad['properties']['rooms']), description=ad['info']['text'], telephone=ad['contact'].get('phone'), price=ad['pricing']['amount'], surface=ad['properties']['area'], rooms=ad['properties']['rooms'], bedrooms=ad['properties'].get('bedrooms'), city=ad['location']['city']['name'], link=ad['info']['link']) if created: pictures = [ picture.replace("[WIDTH]", "1440").replace("[HEIGHT]", "956").replace( "[SCALE]", "3.5") for picture in ad.get('pictures') ] for picture in pictures: Picture.create(url=picture, annonce=annonce) annonce.save()
def picture_creator(num_picture, index, item): ALLOWED_EXTENSION = ['jpg', 'jpeg', 'png', 'gif'] pictures_path = get_random_pictures(num_picture) picture_id = fake.uuid4() extension = random.choice(ALLOWED_EXTENSION) Picture.create(uuid=picture_id, extension=extension, item=item) image_folder = utils.get_image_folder() if not os.path.exists(image_folder): os.makedirs(image_folder) shutil.copy2(pictures_path[index], '/{}/{}.{}'.format(image_folder, picture_id, extension))
def create_photos(self, photo_count = default_photo_count): count = 1; for photo in self.flickr.walk(text='butteryfly', content_type='1', media='photos', per_page=photo_count): farm_id = photo.get('farm') server_id = photo.get('server') _id = photo.get('id') secret = photo.get('secret') url = 'http://farm' + farm_id + '.staticflickr.com/' + server_id + '/' + _id + '_' + secret + '.jpg' print url Picture.create(url) count +=1 if count > photo_count: break return self
def create_item_picture(self, item=None, title="Picture title"): if not item: item = self.create_item() # TODO: Add option for different images and extensions? extension = 'jpg' picture = Picture.create(uuid=uuid.uuid4(), title=title, extension=extension, item=item) config = app.config with open(os.path.join('.', 'tests', 'images', 'test_image.jpg'), 'rb') as image: image = FileStorage(image) save_path = os.path.join('.', config['UPLOADS_FOLDER'], 'items', str(item.uuid)) new_filename = secure_filename('.'.join( [str(picture.uuid), extension])) os.makedirs(save_path, exist_ok=True) image.save(os.path.join(save_path, new_filename)) return picture
def setup_method(self): super(TestItemSchema, self).setup_method() self.data = { 'name': 'Test item', 'price': 10.25, 'description': 'Test item description', 'availability': 73, 'category': 'scarpe', } self.item1 = Item.create( uuid='25da606b-dbd3-45e1-bb23-ff1f84a5622a', name='Item 1', description='Item 1 description', price=5.24, availability=5, category='scarpe', ) self.item2 = Item.create( uuid='08bd8de0-a4ac-459d-956f-cf6d8b8a7507', name='Item 2', description='Item 2 description', price=8, availability=10, category='accessori', ) self.picture = Picture.create( item=self.item1, uuid='08bd8de0-a4ac-459d-956f-cf6d8b8a7507', extension='png', )
def post(self, item_id): try: item = Item.get(Item.uuid == item_id) except Item.DoesNotExist: return None, NOT_FOUND parser = reqparse.RequestParser() parser.add_argument('title', type=non_empty_string, required=True) parser.add_argument('file', type=FileStorage, location='files', required=True) args = parser.parse_args(strict=True) image = args['file'] title = args['title'] extension = image.filename.rsplit('.', 1)[1].lower() config = current_app.config if '.' in image.filename and extension not in config['ALLOWED_EXTENSIONS']: abort(400, message="Extension not supported.") picture = Picture.create( uuid=uuid.uuid4(), title=title, extension=extension, item=item ) save_path = os.path.join('.', config['UPLOADS_FOLDER'], 'items', str(item_id)) new_filename = secure_filename('.'.join([str(picture.uuid), extension])) os.makedirs(save_path, exist_ok=True) image.save(os.path.join(save_path, new_filename)) return picture.json(), CREATED
def test_delete_item__pictures_cascade(self): """ delete a selected item and all its binded pictures """ test_utils.setup_images() item = Item.create(**TEST_ITEM) item2 = Item.create(**TEST_ITEM2) picture = Picture.create(item=item, **TEST_PICTURE) picture2 = Picture.create(item=item, **TEST_PICTURE2) picture3 = Picture.create(item=item2, **TEST_PICTURE3) imgfolder = utils.get_image_folder() path_pic = os.path.join(imgfolder, "{uuid}.{extension}".format( uuid=picture.uuid, extension=picture.extension)) path_pic2 = os.path.join(imgfolder, "{uuid}.{extension}".format( uuid=picture2.uuid, extension=picture2.extension)) path_pic3 = os.path.join(imgfolder, "{uuid}.{extension}".format( uuid=picture3.uuid, extension=picture3.extension)) open(path_pic, "wb") open(path_pic2, "wb") open(path_pic3, "wb") resp = self.app.delete('/items/{item_uuid}'.format( item_uuid=item.uuid)) assert resp.status_code == client.NO_CONTENT assert Picture.select().count() == 1 assert Item.select().count() == 1 item2 = Item.get() pic = Picture.get() assert pic == picture3 assert os.path.isfile("{path}/{picture_uuid}.{extension}".format( path=utils.get_image_folder(), picture_uuid=picture3.uuid, extension=picture3.extension)) assert not os.path.isfile("{path}/{picture_uuid}.{extension}".format( path=utils.get_image_folder(), picture_uuid=picture.uuid, extension=picture.extension)) assert not os.path.isfile("{path}/{picture_uuid}.{extension}".format( path=utils.get_image_folder(), picture_uuid=picture2.uuid, extension=picture2.extension)) test_utils.clean_images()
def test_delete_picture__success(self): test_utils.setup_images() item = Item.create(**TEST_ITEM) picture = Picture.create(item=item, **TEST_PICTURE) picture2 = Picture.create(item=item, **TEST_PICTURE2) open( "{path}/{picture_uuid}.{extension}".format( path=utils.get_image_folder(), picture_uuid=picture.uuid, extension=picture.extension), "wb") open( "{path}/{picture_uuid}.{extension}".format( path=utils.get_image_folder(), picture_uuid=WRONG_UUID, extension='jpg'), "wb") open( "{path}/{picture_uuid}.{extension}".format( path=utils.get_image_folder(), picture_uuid=picture2.uuid, extension=picture2.extension), "wb") resp = self.app.delete( '/pictures/{picture_uuid}'.format(picture_uuid=picture.uuid)) assert resp.status_code == client.NO_CONTENT assert Picture.select().count() == 1 assert Item.select().count() == 1 item2 = Item.get() assert str(item2.uuid) == TEST_ITEM['uuid'] assert item2.name == TEST_ITEM['name'] assert float(item2.price) == TEST_ITEM['price'] assert item2.description == TEST_ITEM['description'] assert os.path.isfile("{path}/{picture_uuid}.{extension}".format( path=utils.get_image_folder(), picture_uuid=WRONG_UUID, extension='jpg')) assert not os.path.isfile("{path}/{picture_uuid}.{extension}".format( path=utils.get_image_folder(), picture_uuid=picture.uuid, extension=picture.extension)) assert os.path.isfile("{path}/{picture_uuid}.{extension}".format( path=utils.get_image_folder(), picture_uuid=picture2.uuid, extension=picture2.extension)) test_utils.clean_images()
def test_delete_pictures__missing_file(self): item = Item.create(**TEST_ITEM) picture = Picture.create(item=item, **TEST_PICTURE) resp = self.app.delete( '/pictures/{picture_uuid}'.format(picture_uuid=picture.uuid)) assert resp.status_code == client.NO_CONTENT assert not Picture.select().exists() assert Item.select().exists()
def setup_method(self): super(TestPictureSchema, self).setup_method() self.item = Item.create( uuid='25da606b-dbd3-45e1-bb23-ff1f84a5622a', name='Item 1', description='Item 1 description', price=5.24, availability=5, category='scarpe', ) self.picture = Picture.create( item=self.item, uuid='08bd8de0-a4ac-459d-956f-cf6d8b8a7507', extension='png', )
def __iter__(self): session = requests.Session() for thread in self.search(): for post in chan.Thread(self.board, num=thread).posts: for pic in post.pictures: if PictureModel.check_existance(pic.url): continue picture = PictureModel.create( url=pic.url, thread=thread, caption=purify_message(post.message)) yield picture
def test_get_picture__success(self): test_utils.setup_images() item = Item.create(**TEST_ITEM) picture = Picture.create(item=item, **TEST_PICTURE) open( "{path}/{picture_uuid}.jpg".format(path=utils.get_image_folder(), picture_uuid=picture.uuid), "wb") resp = self.app.get( '/pictures/{picture_uuid}'.format(picture_uuid=picture.uuid)) assert resp.status_code == client.OK test_picture = TEST_PICTURE.copy() test_picture['item_uuid'] = item.uuid assert resp.data == b'' assert resp.headers['Content-Type'] == 'image/jpeg' test_utils.clean_images()
def post(self, item_uuid): """Insert a new picture for the specified item""" if 'image' not in request.files: return {"message": "No image received"},\ client.BAD_REQUEST try: item = Item.get(Item.uuid == item_uuid) except Item.DoesNotExist: return None, client.NOT_FOUND file = request.files['image'] picture_uuid = uuid.uuid4() extension = os.path.splitext(file.filename)[1][1:] if extension not in ALLOWED_EXTENSION: return {"message": "File extension not allowed"},\ client.BAD_REQUEST utils.save_image(file, picture_uuid, extension) return Picture.create(uuid=picture_uuid, extension=extension, item=item).json(), client.CREATED
def search(parameters): # Préparation des paramètres de la requête payload = { 'recherche[prix][min]': parameters['price'][0], # Loyer min 'recherche[prix][max]': parameters['price'][1], # Loyer max 'recherche[surface][min]': parameters['surface'][0], # Surface min 'recherche[surface][max]': parameters['surface'][1], # Surface max 'recherche[nb_pieces][min]': parameters['rooms'][0], # Pièces min 'recherche[nb_chambres][min]': parameters['bedrooms'][0], # Chambres min 'size': 200, 'page': 1 } # Insertion des paramètres propres à PAP payload.update(parameters['pap']) params = urlencode(payload) # Ajout des villes for city in parameters['cities']: params += "&recherche[geo][ids][]=%s" % place_search(city[1]) request = requests.get("https://ws.pap.fr/immobilier/annonces", params=unquote(params), headers=header) data = request.json() if 'annonce' in data['_embedded']: for ad in data['_embedded']['annonce']: _request = requests.get( "https://ws.pap.fr/immobilier/annonces/%s" % ad['id'], headers=header) _data = _request.json() photos = list() if _data.get("nb_photos") > 0: for photo in _data["_embedded"]['photo']: photos.append(photo['_links']['self']['href']) annonce, created = Annonce.get_or_create( id='pap-%s' % _data.get('id'), site="PAP", title="%s %s pièces" % (_data.get("typebien"), _data.get("nb_pieces")), description=str(_data.get("texte")), telephone=_data.get("telephones")[0].replace('.', '') if len(_data.get("telephones")) > 0 else None, created=datetime.fromtimestamp(_data.get("date_classement")), price=_data.get('prix'), surface=_data.get('surface'), rooms=_data.get('nb_pieces'), bedrooms=_data.get('nb_chambres_max'), city=_data["_embedded"]['place'][0]['title'], link=_data["_links"]['desktop']['href'] # pictures=photos, ) if created: for photo in photos: Picture.create(url=photo, annonce=annonce) annonce.save()
def picture_add(site, host, netloc, csrf, logged_in, user_id_logged_in): """ Add picture post url """ # POST parameters site_id = int(request.POST.get('site')) user_id = int(request.POST.get('user')) portfolio_id = int(request.POST.get('portfolio')) title = request.POST.get('title') description = request.POST.get('description') # Portfolio try: portfolio = Portfolio.get(Portfolio.id == portfolio_id, Portfolio.site == site) except Portfolio.DoesNotExist: return dict(status=False, info='Portfólio não encontrado') # Upload picture image start_index = 1 try: pictures = site.pictures.order_by(Picture.id.desc()) last_picture = pictures.get() start_index = last_picture.id + 1 except Picture.DoesNotExist: pass img_path = site.get_image_path(IMAGE_PATH) img_url = '%s/%s/' % (host, IMAGE_DIR) upload_response = upload_new_image(request, 'picture', start_index, img_path, img_url, ORIG_SIZE, NORM_SIZE, THUMB_SIZE) if not upload_response['status']: return upload_response # Upload data original_image = upload_response['original_filename'] normalized_image = upload_response['normalized_filename'] thumbnail_image = upload_response['thumbnail_filename'] original_url = upload_response['original_url'] normalized_url = upload_response['normalized_url'] thumbnail_url = upload_response['thumbnail_url'] # Create Picture picture_created = None with db.transaction(): try: picture_created = Picture.create(site=site, portfolio=portfolio, title=title, description=description, original_image=original_image, normalized_image=normalized_image, thumbnail_image=thumbnail_image) except IntegrityError as exp: if picture_created: picture_created.delete_instance(IMAGE_PATH) else: site_img_path = site.get_image_path(IMAGE_PATH) if original_image: original_file = os.path.join(site_img_path, original_image) if os.path.exists(original_file): os.remove(original_file) if normalized_image: normalized_file = os.path.join(site_img_path, normalized_image) if os.path.exists(normalized_file): os.remove(normalized_file) if thumbnail_image: thumbnail_file = os.path.join(site_img_path, thumbnail_image) if os.path.exists(thumbnail_file): os.remove(thumbnail_file) # Return error return dict(status=False, info='%s' % exp) # Lista de pictures atualizada try: picture_list = template('pictures_admin_list.html', site=site, host=host, csrf=csrf, portfolio=portfolio, img_url=img_url) except Exception as exp: return dict(status=False, info='%s' % exp) # Return OK return dict(status=True, info='Adicionada com sucesso', picture_id=picture_created.get_id(), original=original_url, normalized=normalized_url, thumbnail=thumbnail_url, picture_list=picture_list)
def search(parameters): # Préparation des paramètres de la requête payload = { 'px_loyermin': parameters['price'][0] if parameters['seloger']['idtt'] == 1 else None, 'px_loyermax': parameters['price'][1] if parameters['seloger']['idtt'] == 1 else None, 'pxmin': parameters['price'][0] if parameters['seloger']['idtt'] == 2 else None, 'pxmax': parameters['price'][1] if parameters['seloger']['idtt'] == 2 else None, 'surfacemin': parameters['surface'][0], 'surfacemax': parameters['surface'][1], # Si parameters['rooms'] = (2, 4) => "2,3,4" 'nbpieces': list(range(parameters['rooms'][0], parameters['rooms'][1] + 1)), # Si parameters['bedrooms'] = (2, 4) => "2,3,4" 'nb_chambres': list(range(parameters['bedrooms'][0], parameters['bedrooms'][1] + 1)), 'ci': [int(cp[2]) for cp in parameters['cities']] } # Insertion des paramètres propres à LeBonCoin payload.update(parameters['seloger']) headers = { 'user-agent': 'Dalvik/2.1.0 (Linux; U; Android 6.0.1; D5803 Build/MOB30M.Z1)' } request = requests.get("http://ws.seloger.com/search_4.0.xml", params=payload, headers=headers) xml_root = ET.fromstring(request.text) for annonceNode in xml_root.findall('annonces/annonce'): # Seconde requête pour obtenir la description de l'annonce _payload = { 'noAudiotel': 1, 'idAnnonce': annonceNode.findtext('idAnnonce') } _request = requests.get("http://ws.seloger.com/annonceDetail_4.0.xml", params=_payload, headers=headers) photos = list() for photo in annonceNode.find("photos"): photos.append(photo.findtext("stdUrl")) annonce, created = Annonce.get_or_create( id='seloger-' + annonceNode.find('idAnnonce').text, site='SeLoger', # SeLoger peut ne pas fournir de titre pour une annonce T_T title="Appartement " + annonceNode.findtext('nbPiece') + " pièces" if annonceNode.findtext('titre') is None else annonceNode.findtext('titre'), description=ET.fromstring(_request.text).findtext("descriptif"), telephone=ET.fromstring( _request.text).findtext("contact/telephone"), created=datetime.strptime(annonceNode.findtext('dtCreation'), '%Y-%m-%dT%H:%M:%S'), price=annonceNode.find('prix').text, charges=annonceNode.find('charges').text, surface=annonceNode.find('surface').text, rooms=annonceNode.find('nbPiece').text, bedrooms=annonceNode.find('nbChambre').text, city=annonceNode.findtext('ville'), link=annonceNode.findtext('permaLien')) if created: for photo in photos: Picture.create(url=photo, annonce=annonce) annonce.save()