Skip to content

Commit

Permalink
Merge 53ea147 into e7f862e
Browse files Browse the repository at this point in the history
  • Loading branch information
juhuntenburg committed Nov 22, 2021
2 parents e7f862e + 53ea147 commit 1471cdb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
14 changes: 8 additions & 6 deletions alyx/misc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,23 @@ def get_image_path(instance, filename):
class Note(BaseModel):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
date_time = models.DateTimeField(default=timezone.now)
text = models.TextField(blank=True)
text = models.TextField(blank=True,
help_text="String, content of the note or description of the image.")
image = models.ImageField(upload_to=get_image_path, blank=True, null=True)

# Generic foreign key to arbitrary model instances.
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.UUIDField()
object_id = models.UUIDField(help_text="UUID, an object of content_type with this "
"ID must already exist to attach a note.")
content_object = GenericForeignKey()

def save(self, **kwargs):
if self.image:
# Resize image
def save(self, image_width=None, **kwargs):
if self.image and not self._state.adding and image_width != 'orig':
# Resize image - saving
with Image.open(self.image) as im:
with BytesIO() as output:
# Compute new size by keeping the aspect ratio.
width = UPLOADED_IMAGE_WIDTH
width = int(image_width or UPLOADED_IMAGE_WIDTH)
wpercent = width / float(im.size[0])
height = int((float(im.size[1]) * float(wpercent)))
im.thumbnail((width, height))
Expand Down
23 changes: 23 additions & 0 deletions alyx/misc/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@ class NoteSerializer(serializers.ModelSerializer):
)
image = serializers.ImageField(use_url=True, required=False)

def _check_related_object_exists(self):
# makes sure the object referred to actually exists in the database
ct = self.validated_data['content_type']
ct.model_class().objects.get(id=self.validated_data['object_id'])

def save(self, **kwargs):
self._check_related_object_exists()
# get optional parameter width of image in the request
image_width = self.context['request'].data.get('width', None)
if self.instance is not None:
self.instance = self.update(self.instance, self.validated_data)
assert self.instance is not None, '`update()` did not return an object instance.'
else:
self.instance = self.create(self.validated_data, image_width=image_width)
assert self.instance is not None, '`create()` did not return an object instance.'
return self.instance

def create(self, validated_data, image_width=None):
self._check_related_object_exists()
obj = self.Meta.model.objects.create(**validated_data)
obj.save(image_width=image_width)
return obj

class Meta:
model = Note
fields = ('id', 'user', 'date_time', 'content_type', 'object_id', 'text', 'image')
Expand Down
11 changes: 11 additions & 0 deletions alyx/misc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ class LabDetail(generics.RetrieveUpdateDestroyAPIView):


class NoteList(generics.ListCreateAPIView):
"""
post:
If an image is provided, the request body can contain an additional item
`width`: desired width to resize the image for storage. Aspect ratio will be maintained.
Options are
- **None** to use the UPLOADED_IMAGE_WIDTH specified in settings (default)
- **'orig'** to keep original image size
- any **integer** to specify the image width
"""
queryset = Note.objects.all()
serializer_class = NoteSerializer
permission_classes = (permissions.IsAuthenticated,)
Expand Down

0 comments on commit 1471cdb

Please sign in to comment.