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 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 test_set_photo_user_glance_score_delta(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) PhotoGlanceScoreDelta.objects.set_photo_user_glance_score_delta(self.amanda, photo, 1, date1) self.assertEqual(photo.get_global_glance_score(), 1)
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)
def test_album_last_updated_photo_upload(self): create_date = datetime.datetime(2010, 1, 1, tzinfo=utc) the_album = Album.objects.create_album(self.amanda, 'Ski Trip', create_date) self.assertEqual(the_album.last_updated, create_date) update_date = datetime.datetime(2010, 1, 2, tzinfo=utc) 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], the_album.id, update_date) # Refresh the_album to get the latest data from the DB the_album = Album.objects.get(pk=the_album.id) self.assertEqual(the_album.last_updated, update_date)
def test_create_new_album(self): the_date = datetime.datetime(2010, 1, 1, tzinfo=utc) album = Album.objects.create_album(self.amanda, 'Ski Trip', the_date) self.assertTrue(album.is_user_member(self.amanda.id)) self.assertFalse(album.is_user_member(self.barney.id)) membership = AlbumMember.objects.filter(user=self.amanda, album=album).get() self.assertEqual(membership.added_by_user, self.amanda) self.assertEqual(list(Album.objects.get_user_albums(self.amanda.id)), [album]) self.assertEqual(list(Album.objects.get_user_albums(self.barney.id)), []) 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], album.id, the_date) self.assertFalse(PendingPhoto.objects.filter(photo_id=pending_photo.photo_id).exists()) self.assertEqual(list(album.get_photos()), [Photo.objects.get(pk=pending_photo.photo_id)])
def album(request, pk): album = get_object_or_404(Album, pk=pk) if not album.is_user_member(request.user.id): # TODO ... pass num_photos_added = 0 num_photos_failed = 0 if request.POST: if "add_photos" in request.POST: pending_photo_ids = [] for f in request.FILES.getlist("photo_files"): pending_photo = Photo.objects.upload_request(author=request.user) if settings.USING_LOCAL_PHOTOS: image_uploads.process_file_upload(pending_photo, f.chunks()) else: # Forward request to photo upload server userAuthToken = AuthToken.objects.create_auth_token( request.user, "Internal Photo Upload Auth Token", timezone.now() ) r = requests.put( settings.PHOTO_UPLOAD_SERVER_URL + "/photos/upload/" + pending_photo.photo_id + "/original/", headers={"Authorization": "Token " + userAuthToken.key}, data=f.chunks(), ) r.raise_for_status() pending_photo_ids.append(pending_photo.photo_id) num_photos_added += 1 # Upload pending photos done = False total_retry_time = 0 while not done: now = timezone.now() try: photo_operations.add_pending_photos_to_album(pending_photo_ids, album.id, now) done = True except RuntimeError: # TODO This is a really bad workaround to deal with the # situation where the photos aren't "done" yet (in which # case "add_pending_photos_to_album" currently raises a # "RuntimeError") RETRY_TIME = 5 MAX_RETRY_TIME = 600 # 10 minutes if total_retry_time >= MAX_RETRY_TIME: raise time.sleep(RETRY_TIME) total_retry_time += RETRY_TIME # TODO: If this function will be refactored to use Class Based Views # change sender from `request` to `self` (View instance) photos_added_to_album.send(sender=request, photos=pending_photo_ids, by_user=request.user, to_album=album) aws_token = aws_sts.get_s3_upload_token(request.user) data = { "album": album, "photos": album.get_photos(), "members": album.get_member_users(), "num_photos_added": num_photos_added, "num_photos_failed": num_photos_failed, "aws_token": aws_token, } return render_to_response("frontend/album.html", data, context_instance=RequestContext(request))
def event_photos(request, event): album = event.album num_photos_added = 0 num_photos_failed = 0 if request.POST: if 'add_photos' in request.POST: pending_photo_ids = [] for f in request.FILES.getlist('photo_files'): pending_photo = Photo.objects.upload_request(author=request.user) if settings.USING_LOCAL_PHOTOS: image_uploads.process_file_upload(pending_photo, f.chunks()) else: # Forward request to photo upload server userAuthToken = AuthToken.objects.create_auth_token(request.user, 'Internal Photo Upload Auth Token', timezone.now()) r = requests.put(settings.PHOTO_UPLOAD_SERVER_URL + '/photos/upload/' + pending_photo.photo_id + '/original/', headers = { 'Authorization': 'Token ' + userAuthToken.key }, data = f.chunks()) r.raise_for_status() pending_photo_ids.append(pending_photo.photo_id) num_photos_added += 1 # Upload pending photos done = False total_retry_time = 0 while not done: now = timezone.now() try: photo_operations.add_pending_photos_to_album(pending_photo_ids, album.id, now) done = True except RuntimeError: # TODO This is a really bad workaround to deal with the # situation where the photos aren't "done" yet (in which # case "add_pending_photos_to_album" currently raises a # "RuntimeError") RETRY_TIME = 5 MAX_RETRY_TIME = 600 # 10 minutes if total_retry_time >= MAX_RETRY_TIME: raise time.sleep(RETRY_TIME) total_retry_time += RETRY_TIME # TODO: If this function will be refactored to use Class Based Views # change sender from `request` to `self` (View instance) photos_added_to_album.send(sender=request, photos=pending_photo_ids, by_user=request.user, to_album=album) data = { 'event': event, 'organization': event.organization, 'album': album, 'photos': album.get_photos(), 'members': album.get_member_users(), 'num_photos_added': num_photos_added, 'num_photos_failed': num_photos_failed } return render(request, 'affiliates/event/photos.html', data)