def test_show_multiple_images(self, uuid4, blurred_image): # Given uuid4.side_effect = ["unique_identifier-asdf", "another_identifier"] with open("tests/helpers/loshombresdepacoblurred.png", "rb") as file: blurred_image.return_value = file.read() with open('tests/helpers/ExamplePicture.jpg', 'rb') as image_file: Picture(picture=SimpleUploadedFile( name='test_image.jpg', content=image_file.read(), content_type='image/jpeg'), ).save() Picture(picture=SimpleUploadedFile( name='test_image.jpg', content=image_file.read(), content_type='image/jpeg'), ).save() # When response = Client().get("/dashboard/") # Then self.assertContains( response, '<img src="/media/blurred_pictures/unique_identifier-asdf.jpg" alt="img">' ) self.assertContains( response, '<img src="/media/blurred_pictures/another_identifier.jpg" alt="img">' )
def test_show_multiple_images_in_inverse_order_of_upload( self, uuid4, blurred_image): # Given uuid4.side_effect = ["unique_identifier-asdf", "another_identifier"] with open("tests/helpers/loshombresdepacoblurred.png", "rb") as file: blurred_image.return_value = file.read() with open('tests/helpers/ExamplePicture.jpg', 'rb') as image_file: Picture(picture=SimpleUploadedFile( name='test_image.jpg', content=image_file.read(), content_type='image/jpeg'), ).save() Picture(picture=SimpleUploadedFile( name='test_image.jpg', content=image_file.read(), content_type='image/jpeg'), ).save() # When response = Client().get("/dashboard/") # Then soup = BeautifulSoup(response.content, "html.parser") images = soup.find_all("img") self.assertEqual("/media/blurred_pictures/another_identifier.jpg", images[0]["src"]) self.assertEqual("/media/blurred_pictures/unique_identifier-asdf.jpg", images[1]["src"])
def test_create_blurred_picture_when_saving_picture_instance(self, _, blurred_image): # Given with open('tests/helpers/ExamplePicture.jpg', 'rb') as file: uploaded_file = SimpleUploadedFile('ExamplePicture.jpg', file.read()) with open("tests/helpers/loshombresdepacoblurred.png", "rb") as file: blurred_image.return_value = file.read() picture = Picture(picture=uploaded_file) # When picture.save() # Then self.assertTrue(Path("media/blurred_pictures/unique_identifier-asdf.jpg").is_file()) self.assert_images_equal( "tests/helpers/loshombresdepacoblurred.png", "media/blurred_pictures/unique_identifier-asdf.jpg" )
def test_create_unique_id_when_saving_model_instance(self, _, blurred_image): # Given with open('tests/helpers/ExamplePicture.jpg', 'rb') as file: uploaded_file = SimpleUploadedFile('ExamplePicture.jpg', file.read()) with open("tests/helpers/loshombresdepacoblurred.png", "rb") as file: blurred_image.return_value = file.read() picture = Picture(picture=uploaded_file) # When picture.save() # Then instances = Picture.objects.all() self.assertEqual(1, len(instances)) self.assertEqual("ExamplePicture.jpg", instances[0].original_name) self.assertEqual("unique_identifier-asdf.jpg", instances[0].unique_name) self.assertTrue(Path("media/original_pictures/unique_identifier-asdf.jpg").is_file())
def test_delete_picture_instance_does_not_fail_when_images_fields_are_not_set(self, _, blurred_image): # Given settings.DEBUG = True with open('tests/helpers/ExamplePicture.jpg', 'rb') as file: uploaded_file = SimpleUploadedFile('ExamplePicture.jpg', file.read()) with open("tests/helpers/loshombresdepacoblurred.png", "rb") as file: blurred_image.return_value = file.read() picture = Picture(picture=uploaded_file) picture.save() os.remove(os.path.join("media/original_pictures", "unique_identifier-asdf.jpg")) os.remove(os.path.join("media/blurred_pictures", "unique_identifier-asdf.jpg")) picture.picture = None picture.picture_blurred = None picture.save() # When picture.delete()
def test_delete_images_when_deleting_picture_instance_in_production(self, _, blurred_image, delete_picture): # Given settings.DEBUG = False with open('tests/helpers/ExamplePicture.jpg', 'rb') as file: uploaded_file = SimpleUploadedFile('ExamplePicture.jpg', file.read()) with open("tests/helpers/loshombresdepacoblurred.png", "rb") as file: blurred_image.return_value = file.read() picture = Picture(picture=uploaded_file) picture.save() # When picture.delete() # Then delete_picture.assert_called_once_with(picture)
def test_delete_images_when_deleting_picture_instance(self, _, blurred_image): # Given settings.DEBUG = True with open('tests/helpers/ExamplePicture.jpg', 'rb') as file: uploaded_file = SimpleUploadedFile('ExamplePicture.jpg', file.read()) with open("tests/helpers/loshombresdepacoblurred.png", "rb") as file: blurred_image.return_value = file.read() picture = Picture(picture=uploaded_file) picture.save() self.assertTrue(Path("media/original_pictures/unique_identifier-asdf.jpg").is_file()) self.assertTrue(Path("media/blurred_pictures/unique_identifier-asdf.jpg").is_file()) # When picture.delete() # Then self.assertFalse(Path("media/original_pictures/unique_identifier-asdf.jpg").is_file()) self.assertFalse(Path("media/blurred_pictures/unique_identifier-asdf.jpg").is_file())
def _upsert_movie(self, tmdb_id): movie = Movie.objects.filter(tmdb_id=tmdb_id).first() if movie is None: j_movie = self._tmdb_client.movie_details(tmdb_id) if j_movie is None: return None movie = Movie.from_json(j_movie) movie.save() # Store genres for j_genre in j_movie['genres']: genre = Genre.from_json(j_genre) count = Genre.objects.filter(id=genre.id).count() if count == 0: genre.save() movie.genres.add(genre) else: genre = Genre.objects.filter(id=genre.id).first() movie.genres.add(genre) # Process cast for j_cast in j_movie['credits']['cast']: crew = Crew() crew.movie_id = movie.id crew.category_id = 2 crew.name = j_cast['name'] crew.character_name = j_cast['character'] crew.picture_url = j_cast['profile_path'] crew.save() # Process directors for j_crew in j_movie['credits']['crew']: if j_crew['job'] == 'Director': crew = Crew() crew.movie_id = movie.id crew.category_id = 1 crew.name = j_crew['name'] crew.picture_url = j_crew['profile_path'] crew.save() # Process pictures if 'backdrop_path' in j_movie and j_movie[ 'backdrop_path'] is not None: picture = Picture() picture.category_id = 2 picture.movie_id = movie.id picture.url = j_movie['backdrop_path'] picture.save() if 'poster_path' in j_movie and j_movie['poster_path'] is not None: picture = Picture() picture.category_id = 1 picture.movie_id = movie.id picture.url = j_movie['poster_path'] picture.save() return movie
file_url = BASE_URL + soup.find( 'a', class_='sui font-weight-400 font-size-16')['href'] filepath = 'static/pictures/' + uuid.uuid4().hex[:6].lower( ) + '.jpeg' with open(filepath, "wb") as file: new_headers = headers.copy() new_headers['Referer'] = picture_link new_headers['Sec-Fetch-Mode'] = 'navigate' new_headers['Sec-Fetch-Site'] = 'same-origin' response = requests.get(file_url, headers=new_headers, verify=False) soup = BeautifulSoup(response.content, 'html.parser') new_headers[ 'Cookie'] = 'LOC=6%3ARU; _ym_uid=1584193274307759205; _ym_d=1584193274; _ga=GA1.2.1316766645.1584193275; marker=qoshppbs1u9cdu4pbkdhhav6p3; _fbp=fb.1.1584193335644.763158178; __gads=ID=1a20c5ae5e2af1b2:T=1584193335:S=ALNI_Mb_geQU23L3XbyDT6Z_Iu6IQdfUjQ; __utmz=63997296.1584193347.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); fid=6218156f-a46e-438f-a9c5-2de16cce6cc5; PHPSESSID=baf6949b24f2c14cc851cca346d422a6; _gid=GA1.2.67504545.1584389092; _ym_isad=2; _ym_visorc_253414=w; __utma=63997296.1316766645.1584193275.1584193347.1584475219.2; __utmc=63997296; __utmb=63997296.5.10.1584475219; _ym_wasSynced=%7B%22time%22%3A1584475551446%2C%22params%22%3A%7B%22eu%22%3A0%7D%2C%22bkParams%22%3A%7B%7D%7D; _ym_visorc_62733=w' link = 'https:' + soup.find('a')['href'] response = requests.get(link, headers=new_headers, verify=False) file.write(response.content) file.close() picture_object = Picture() picture_object.name = picture_name picture_object.painter = painter_object picture_object.link_info = picture_link picture_object.file = filepath picture_object.save()
def process_pictures(j_movie, movie): if 'backdrop_path' in j_movie and j_movie['backdrop_path'] != None: picture = Picture() picture.category_id = 2 picture.movie_id = movie.id picture.url = j_movie['backdrop_path'] picture.save() if 'poster_path' in j_movie and j_movie['poster_path'] != None: picture = Picture() picture.category_id = 1 picture.movie_id = movie.id picture.url = j_movie['poster_path'] picture.save()
django.setup() from core.models import Picture, Painter with open('../pictures_final.json', 'r', encoding="utf-8") as json_file: data = json.load(json_file) for painter in data: painter_url = painter['painter_url'] painter_name = painter['painter_name'] try: painter_object = Painter.objects.get(url=painter_url) except Painter.DoesNotExist: painter_object = Painter() painter_object.name = painter_name painter_object.url = painter_url painter_object.save() for picture in painter['pictures']: picture_url = picture['picture_url'] try: picture_object = Picture.objects.get(link_info=picture_url) except Picture.DoesNotExist: picture_object = Picture() picture_object.link_info = picture_url picture_object.painter = painter_object picture_object.name = picture['picture_name'] picture_object.file = 'static/pictures/' + picture['filename'] picture_object.save()