def destroy(self, request, pk=None): ''' Deletes a person record if not associated with a user ''' queryset = Person.objects.filter(family_id=request.user.family_id) with reversion.create_revision(): person = get_object_or_404(queryset, pk=pk) if person.user_id: return HttpResponse( status=403, content="Profile is a user and cannot be deleted") person.delete() # Store some meta-information. reversion.set_user(request.user) reversion.set_comment('Delete ' + request.META.get('HTTP_X_REAL_IP')) message = { 'family_id': request.user.family_id, 'person_id': int(pk) } message_encoded = json.dumps(message) create_message('person_deleted_update_face_model', message_encoded) return Response('OK')
def create(self, request): ''' Image upload ''' queryset = Gallery.objects.filter(family_id=request.user.family_id) gallery_id, gallery_id_valid = intTryParse( request.data.get("gallery_id")) if not gallery_id_valid: raise ParseError('Invalid gallery_id') # Check gallery is part of family gallery = get_object_or_404(queryset, pk=gallery_id) try: uploaded = request.FILES['picture'] name, ext = os.path.splitext(uploaded.name) if uploaded.size > MAX_FILE_SIZE: raise ParseError('File too big') filename = create_hash(uploaded.name) + '.jpg' image = Image(gallery_id=gallery.id, family_id=gallery.family_id, title=name, uploaded_by=request.user) path = upload_to(image, filename) #Write the file to the destination destination = open(os.path.join(settings.MEDIA_ROOT, path), 'wb+') for chunk in uploaded.chunks(): destination.write(chunk) destination.close() image.original_image = path PIL.Image.open( os.path.join(settings.MEDIA_ROOT, str(image.original_image))).verify() image.save() image.upload_files_to_s3() image.delete_local_image_files() create_message('image_face_detect', image.id) serializer = ImageSerializer(image) return Response(serializer.data) except Exception as e: if image: image.delete_local_image_files() image.delete() raise ParseError(str(e))
def update(self, request, pk=None): queryset = Person.objects.filter(family_id = request.user.family_id) person = get_object_or_404(queryset, pk=pk) #Make sure we can't change locked profiles if person.locked and person.user_id != request.user.id: raise PermissionDenied('Access denied to locked profile') x, x_valid = intTryParse(request.data.get("x")) y, y_valid = intTryParse(request.data.get("y")) w, w_valid = intTryParse(request.data.get("w")) h, h_valid = intTryParse(request.data.get("h")) r, r_valid = intTryParse(request.data.get("r")) if not (x_valid and y_valid and w_valid and h_valid and r_valid): raise ParseError('Invalid crop parameters') try: uploaded = request.FILES['picture'] if uploaded.size > MAX_FILE_SIZE: raise ValueError('File is too big') filename = create_hash(person.name) +'.jpg' photo_file = ''.join([settings.MEDIA_ROOT, 'profile_photos/', filename]) #Write the file to the destination destination = open(photo_file, 'wb+') for chunk in uploaded.chunks(): destination.write(chunk) destination.close() person.set_profile_image_crop_rotate_resize(photo_file, x, y, w, h, r) person.save() except Exception as e: raise ParseError(str(e)) create_message('profile_photo_process', person.id) serializer = PersonListSerializer(person) return Response(serializer.data)
def create(self, request): ''' Creates a tag ''' person_id, person_id_valid = intTryParse(request.data.get("person_id")) if not person_id_valid: return HttpResponse(status=400, content="Invalid person_id") person_queryset = Person.objects.filter( family_id=self.request.user.family_id) person = get_object_or_404(person_queryset, pk=person_id) image_id, image_id_valid = intTryParse(request.data.get("image_id")) if not image_id_valid: return HttpResponse(status=400, content="Invalid image_id") image_queryset = Image.objects.filter( family_id=self.request.user.family_id) image = get_object_or_404(image_queryset, pk=image_id) x1, x1_valid = floatTryParse(request.data.get("x1")) x2, x2_valid = floatTryParse(request.data.get("x2")) y1, y1_valid = floatTryParse(request.data.get("y1")) y2, y2_valid = floatTryParse(request.data.get("y2")) if not (x1_valid and x2_valid and y1_valid and y2_valid): return HttpResponse(status=400, content="Invalid x1, x2, y1, or y2") tag = Tag.objects.create(image_id=image.id, x1=x1, y1=y1, x2=x2, y2=y2, person_id=person.id) create_message('resize_tag', tag.id) # Send notification email tag.send_tag_notification_email() serializer = TagSerializer(tag, many=False) return Response(serializer.data)
def partial_update(self, request, pk=None): ''' Editing image ''' queryset = Image.objects.filter(family_id=request.user.family_id) image = get_object_or_404(queryset, pk=pk) title = self.request.data.get('title') if not title or len(title.strip()) == 0: raise ParseError('Invalid title') anticlockwise_angle, rotation_valid = intTryParse( request.data.get("anticlockwise_angle")) if rotation_valid and anticlockwise_angle != 0: image.rotate(anticlockwise_angle) # Rotate any image tags for tag in list(Tag.objects.filter(image_id=image.id)): tag.rotate(anticlockwise_angle) tag.save() description = self.request.data.get('description') if not title or len(title.strip()) == 0: description = "" image.title = title image.description = description latitude, latitude_valid = floatTryParse(request.data.get("latitude")) longitude, longitude_valid = floatTryParse( request.data.get("longitude")) if latitude_valid and longitude_valid and latitude != 0 and longitude != 0: image.latitude = latitude image.longitude = longitude image.save() # if we have rotated image, run facial recognition again if rotation_valid and anticlockwise_angle != 0: create_message('image_face_detect', image.id) serializer = ImageSerializer(image) return Response(serializer.data)
def test_create_message(self): dt = datetime.datetime.now(tz=timezone.utc) message = create_message("resize_tag", 'string', dt, 1, 1.12354874) self.assertTrue(message.id > 0) self.assertFalse(message.processed) self.assertEqual('string', message.string_data) self.assertEqual(dt, message.date_data) self.assertEqual(1, message.integer_data) self.assertEqual(1.12354874, message.float_data)
def partial_update(self, request, pk=None): ''' Converts suggested tag into a tag ''' queryset = SuggestedTag.objects.filter( image__family_id=self.request.user.family_id) suggested_tag = get_object_or_404(queryset, pk=pk) person_id, person_id_valid = intTryParse(request.data.get("person_id")) if not person_id_valid: return HttpResponse(status=400, content="Invalid person_id") new_tag = suggested_tag.convertToTag(person_id) # Send notification email new_tag.send_tag_notification_email() create_message('tag_converted_process', new_tag.id) serializer = TagSerializer(new_tag) return Response(serializer.data)