Add Protogarden image to Home stats

This commit is contained in:
2022-05-17 02:38:20 +00:00
parent 152107a9bb
commit adb14f5c4f
6 changed files with 85 additions and 10 deletions

View File

@@ -192,6 +192,42 @@ def process_image_upload(upload, crop):
return small, medium, large
GARDEN_MEDIUM_SIZE = 500
def process_garden_image(upload):
try:
pic = Image.open(upload)
except OSError:
raise serializers.ValidationError(dict(non_field_errors='Invalid image file.'))
logging.debug('Detected format: %s', pic.format)
if pic.format == 'PNG':
ext = '.png'
elif pic.format == 'JPEG':
ext = '.jpg'
else:
raise serializers.ValidationError(dict(non_field_errors='Image must be a jpg or png.'))
pic = ImageOps.exif_transpose(pic)
draw = ImageDraw.Draw(pic)
timestamp = now_alberta_tz().strftime('%a %b %-d, %Y %-I:%M %p')
font = ImageFont.truetype('DejaVuSans.ttf', 60)
draw.text((10, 10), timestamp, (0,0,0), font=font)
large = 'garden-large' + ext
pic.save(STATIC_FOLDER + large)
medium = 'garden-medium' + ext
pic.thumbnail([GARDEN_MEDIUM_SIZE, GARDEN_MEDIUM_SIZE], Image.ANTIALIAS)
pic.save(STATIC_FOLDER + medium)
return medium, large
CARD_TEMPLATE_FILE = 'misc/member_card_template.jpg'
CARD_PHOTO_SIZE = 425
CARD_PHOTO_MARGIN_TOP = 75

View File

@@ -857,7 +857,6 @@ class StatsViewSet(viewsets.ViewSet, List):
month_total=month_total,
))
@action(detail=False, methods=['post'])
def autoscan(self, request):
if 'autoscan' not in request.data:
@@ -866,6 +865,18 @@ class StatsViewSet(viewsets.ViewSet, List):
cache.set('autoscan', request.data['autoscan'])
return Response(200)
@action(detail=False, methods=['post'])
def garden(self, request):
if 'photo' not in request.data:
raise exceptions.ValidationError(dict(photo='This field is required.'))
photo = request.data['photo']
medium, large = utils.process_garden_image(photo)
logging.debug('Wrote garden images to %s and %s', medium, large)
return Response(200)
class MemberCountViewSet(Base, List):
pagination_class = None