def test_photo_global_glance_score(self): date1 = datetime.datetime(2010, 1, 1, tzinfo=utc) album1 = Album.objects.create_album(self.amanda, 'Album 1', date1) pending_photo = Photo.objects.upload_request(author=self.amanda) with open('photos/test_photos/death-valley-sand-dunes.jpg') as f: image_uploads.process_file_upload(pending_photo, read_in_chunks(f)) photo_operations.add_pending_photos_to_album([pending_photo.photo_id], album1.id, date1) photo = Photo.objects.get(pk=pending_photo.photo_id) photo.update_glance_score(1) date2 = datetime.datetime(2010, 1, 2, tzinfo=utc) album2 = Album.objects.create_album(self.barney, 'Album 2', date2) photo_operations.copy_photos_to_album(self.barney, [pending_photo.photo_id], album2.id, date2) album2_photos = album2.get_photos() photo2 = Photo.objects.get(pk=album2_photos[0].photo_id) photo2.update_glance_score(1) date3 = datetime.datetime(2010, 1, 3, tzinfo=utc) album3 = Album.objects.create_album(self.amanda, 'Album 3', date3) photo_operations.copy_photos_to_album(self.amanda, [album2_photos[0].photo_id], album3.id, date3) album3_photos = album3.get_photos() photo3 = Photo.objects.get(pk=album3_photos[0].photo_id) photo3.update_glance_score(1) self.assertEqual(photo.get_global_glance_score(), 3) self.assertEqual(photo2.get_global_glance_score(), 3) self.assertEqual(photo3.get_global_glance_score(), 3)
def handle_partner_registration_payload(self, user, partner_id): if partner_id == 'mazaltov': # Hard-coded album id that is used as a template MAZALTOV_ALBUM_ID = 2811 mazaltov_album = Album.objects.get(pk=MAZALTOV_ALBUM_ID) photo_ids = [p.photo_id for p in mazaltov_album.get_photos()] now = timezone.now() class CreateAlbumAction(photo_operations.ExThread): def run_with_exception(self): try: self.perform_action() finally: django.db.close_old_connections() def perform_action(self): with transaction.atomic(): self.new_album = Album.objects.create_album(mazaltov_album.creator, mazaltov_album.name, now) with self.new_album.modify(now) as m: m.add_user_id(mazaltov_album.creator, user.id) action = CreateAlbumAction() # TODO This is only current needed to make tests work with sqlite # See this bug: # https://code.djangoproject.com/ticket/12118 if settings.USING_LOCAL_PHOTOS: action.perform_action() else: action.daemon = False action.start() action.join_with_exception() photo_operations.copy_photos_to_album(mazaltov_album.creator, photo_ids, action.new_album.id, now)
def post(self, request, pk): serializer = AlbumUpdateSerializer(data=request.DATA) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) now = timezone.now() if serializer.object.add_photos: photo_ids = serializer.object.add_photos try: photo_operations.add_pending_photos_to_album(photo_ids, self.album.id, now) except photo_operations.PhotoNotUploadedAddPhotoException: return Response(u"Trying to add a Photo that has not yet been uploaded", status=status.HTTP_400_BAD_REQUEST) except photo_operations.InvalidPhotoIdAddPhotoException: return Response(u"Trying to add a Photo with an invalid photo_id", status=status.HTTP_400_BAD_REQUEST) photos_added_to_album.send(sender=self, photos=photo_ids, by_user=request.user, to_album=self.album) if serializer.object.copy_photos: photo_ids = serializer.object.copy_photos photo_operations.copy_photos_to_album(request.user, photo_ids, self.album.id, now) photos_added_to_album.send(sender=self, photos=photo_ids, by_user=request.user, to_album=self.album) album_add_members(self.album, request.user, serializer.object.add_members, now) payload = optimized_views.get_album_detail_payload(request.user, self.album) return Response(payload, content_type='application/json')
def create_welcome_album(new_user, current_time): template_album = Album.objects.get(pk=settings.WELCOME_ALBUM_ID) photo_ids = [p.photo_id for p in template_album.get_photos()] now = current_time class CreateAlbumAction(photo_operations.ExThread): def run_with_exception(self): try: self.perform_action() finally: django.db.close_old_connections() def perform_action(self): with transaction.atomic(): self.new_album = Album.objects.create_album(template_album.creator, template_album.name, now) with self.new_album.modify(now) as m: m.add_user_id(template_album.creator, new_user.id) action = CreateAlbumAction() # TODO This is only current needed to make tests work with sqlite # See this bug: # https://code.djangoproject.com/ticket/12118 if settings.USING_LOCAL_PHOTOS: action.perform_action() else: action.daemon = False action.start() action.join_with_exception() photo_operations.copy_photos_to_album(template_album.creator, photo_ids, action.new_album.id, now)
def test_copy_photo_to_album(self): date1 = datetime.datetime(2010, 1, 1, tzinfo=utc) album1 = Album.objects.create_album(self.amanda, 'Album 1', date1) pending_photo = Photo.objects.upload_request(author=self.amanda) with open('photos/test_photos/death-valley-sand-dunes.jpg') as f: image_uploads.process_file_upload(pending_photo, read_in_chunks(f)) photo_operations.add_pending_photos_to_album([pending_photo.photo_id], album1.id, date1) date2 = datetime.datetime(2010, 1, 2, tzinfo=utc) album2 = Album.objects.create_album(self.barney, 'Album 2', date2) photo_operations.copy_photos_to_album(self.barney, [pending_photo.photo_id], album2.id, date2) album2_photos = album2.get_photos() self.assertEqual(len(album2_photos), 1) self.assertEqual(album2_photos[0].storage_id, Photo.objects.get(pk=pending_photo.photo_id).storage_id)