|
| 1 | +import logging |
| 2 | +from rest_framework import permissions, status |
| 3 | +from rest_framework.decorators import (api_view, permission_classes, throttle_classes,) |
| 4 | +from rest_framework.response import Response |
| 5 | +from rest_framework.throttling import AnonRateThrottle |
| 6 | +from base64 import b64decode |
| 7 | +from django.core.files.base import ContentFile |
| 8 | + |
| 9 | +from demos.models import Demo |
| 10 | +from .models import DemoLog, LogImage, LogText |
| 11 | + |
| 12 | + |
| 13 | +@throttle_classes([AnonRateThrottle, ]) |
| 14 | +@api_view(['POST']) |
| 15 | +@permission_classes((permissions.AllowAny,)) |
| 16 | +def post_log(request, demo_permalink): |
| 17 | + """ |
| 18 | + Create and save a log |
| 19 | + """ |
| 20 | + demo = Demo.objects.get(permalink=demo_permalink) |
| 21 | + if not demo: |
| 22 | + error_message = {'error': 'Demo not found!'} |
| 23 | + return Response(error_message, status=status.HTTP_404_NOT_FOUND) |
| 24 | + |
| 25 | + try: |
| 26 | + demo_log = DemoLog.objects.create( |
| 27 | + demo=demo, |
| 28 | + log_type='Submission' |
| 29 | + ) |
| 30 | + except Exception: |
| 31 | + error_message = {'error': 'Demo log could not be created'} |
| 32 | + return Response(error_message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) |
| 33 | + |
| 34 | + i = 0 |
| 35 | + try: |
| 36 | + while True: |
| 37 | + textdata = request.data['input-text-{}'.format(i)] |
| 38 | + LogText.objects.create( |
| 39 | + demo_log=demo_log, |
| 40 | + text=textdata, |
| 41 | + text_type='Input' |
| 42 | + ) |
| 43 | + i += 1 |
| 44 | + except Exception: |
| 45 | + logging.exception('Key not found while saving input') |
| 46 | + pass |
| 47 | + |
| 48 | + imagedata = [] |
| 49 | + i = 0 |
| 50 | + try: |
| 51 | + while True: |
| 52 | + imagedata = request.FILES['input-image-{}'.format(i)] |
| 53 | + LogImage.objects.create( |
| 54 | + demo_log=demo_log, |
| 55 | + image=imagedata, |
| 56 | + image_type='Input' |
| 57 | + ) |
| 58 | + i += 1 |
| 59 | + except Exception: |
| 60 | + logging.exception('Key not found while saving output') |
| 61 | + pass |
| 62 | + |
| 63 | + success_message = {'success': {'id': demo_log.id}} |
| 64 | + return Response(success_message, status=status.HTTP_200_OK) |
| 65 | + |
| 66 | + |
| 67 | +@throttle_classes([AnonRateThrottle, ]) |
| 68 | +@api_view(['POST']) |
| 69 | +@permission_classes((permissions.AllowAny,)) |
| 70 | +def post_output(request, log_id): |
| 71 | + """ |
| 72 | + Get a list of projects |
| 73 | + """ |
| 74 | + demo_log = DemoLog.objects.get(id=log_id) |
| 75 | + if not demo_log: |
| 76 | + error_message = {'error': 'Demo log not found!'} |
| 77 | + return Response(error_message, status=status.HTTP_404_NOT_FOUND) |
| 78 | + |
| 79 | + count_image_inputs = demo_log.demo.image_inputs |
| 80 | + count_text_inputs = demo_log.demo.text_inputs |
| 81 | + |
| 82 | + if count_image_inputs > 0 and count_text_inputs == 0: |
| 83 | + for image in request.data: |
| 84 | + image = str(image).split(",")[1] |
| 85 | + missing_padding = len(image) % 4 |
| 86 | + if missing_padding != 0: |
| 87 | + image += b'=' * (4 - missing_padding) |
| 88 | + image_data = b64decode(image) |
| 89 | + LogImage.objects.create( |
| 90 | + demo_log=demo_log, |
| 91 | + image=ContentFile(image_data, 'whatup.png'), |
| 92 | + image_type='Output' |
| 93 | + ) |
| 94 | + elif count_text_inputs > 0 and count_image_inputs == 0: |
| 95 | + for text in request.data: |
| 96 | + LogText.objects.create( |
| 97 | + demo_log=demo_log, |
| 98 | + text=text, |
| 99 | + text_type='Output' |
| 100 | + ) |
| 101 | + success_message = {'success': 'OK'} |
| 102 | + return Response(success_message, status=status.HTTP_200_OK) |
0 commit comments