예제 #1
0
def delete_image(request, slug):
    """
    Deletes a specified image.
    :param request: The HTTP request
    :param slug: The Slug of the image
    """

    logger = StructLogger.get_logger(__name__, request)

    if request.method != 'DELETE':
        logger.error('Delete failed', method=request.method)
        return HttpResponseNotAllowed(permitted_methods=['DELETE'])

    if not request.user.is_authenticated:
        logger.error('User not authenticated')
        return HttpResponseForbidden()

    if 'application/json' in request.META.get('HTTP_ACCEPT', []):
        content_type = 'application/json'
    else:
        content_type = 'text/plain'
    image = get_object_or_404(Image, encrypted_key=slug)
    image.delete()
    logger.info('Image deleted', image_pk=image.pk)
    response_dict = {'files': {image.title: True}}
    return JsonResponse(response_dict, content_type=content_type)
예제 #2
0
def delete_image(request, slug):
    """
    Deletes a specified image.
    :param request: The HTTP request
    :param slug: The Slug of the image
    """

    logger = StructLogger.get_logger(__name__, request)

    if request.method != 'DELETE':
        logger.error(msg='Delete failed', method=request.method)
        return HttpResponseNotAllowed(permitted_methods=['DELETE'])

    if not request.user.is_authenticated():
        logger.error(msg='User not authenticated')
        return HttpResponseForbidden()

    if 'application/json' in request.META.get('HTTP_ACCEPT', []):
        content_type = 'application/json'
    else:
        content_type = 'text/plain'
    image = get_object_or_404(Image, encrypted_key=slug)
    image.delete()
    logger.info(msg='Image deleted', image_pk=image.pk)
    response_dict = {
        'files': {image.title: True}
    }
    return JsonResponse(response_dict, content_type=content_type)
예제 #3
0
def download_video(instance):
    """
    Task for downloading a video from YouTube.
    :param instance: YoutubeVideo
    """
    instance.download()
    logger = StructLogger.get_logger(__name__)
    logger.info(msg='Download complete', video_id=instance.pk, youtube_id=instance.youtube_id)
예제 #4
0
def rebuild_index():
    """
    Task for downloading a video from YouTube.
    :param instance: YoutubeVideo
    """
    update_index.Command().handle(interactive=False)
    logger = StructLogger.get_logger(__name__)
    logger.info('Re-indexed search')
예제 #5
0
def rebuild_index():
    """
    Task for downloading a video from YouTube.
    :param instance: YoutubeVideo
    """
    update_index.Command().handle(interactive=False)
    logger = StructLogger.get_logger(__name__)
    logger.info(msg='Re-indexed search')
예제 #6
0
def multi_image_upload(request):
    """
    View for handling multi-image upload.

    :type request: HttpRequest
    """

    logger = StructLogger.get_logger(__name__, request)

    if request.method == 'POST':
        if 'application/json' in request.META.get('HTTP_ACCEPT', []):
            content_type = 'application/json'
        else:
            content_type = 'text/plain'

        # TODO: See if there's a safer way to inspect file than .read() - .chunks() isn't accepted by magic.
        mime_type = magic.from_buffer(request.FILES['files[]'].read(),
                                      mime=True)
        if mime_type not in settings.ALLOWED_MIME_TYPES:
            response_dict = {
                'files': [{
                    'name':
                    request.FILES['files[]'].name,
                    'size':
                    request.FILES['files[]'].size,
                    'error':
                    _('{} is not a valid image file').format(
                        request.FILES['files[]'].name),
                }]
            }
            logger.error('Upload failed',
                         file_name=request.FILES['files[]'].name)
        else:
            image = Image.objects.create(image=request.FILES['files[]'],
                                         uploaded_by=request.user)
            thumbnail = image.make_thumbnail()
            response_dict = {
                'files': [{
                    'name': image.title,
                    'size': image.image.size,
                    'url': image.get_absolute_url(),
                    'thumbnailUrl': thumbnail,
                    'deleteUrl': image.get_delete_url(),
                    'deleteType': 'DELETE',
                }]
            }
            logger.info(
                'Upload successful',
                file_name=request.FILES['files[]'].name,
                image_pk=image.pk,
            )
        return JsonResponse(response_dict, content_type=content_type)
    else:
        return render(request=request, template_name='images/upload.html')
예제 #7
0
    def form_invalid(self, form):
        """
        Log invalid submissions.

        :param form: The invalid form
        :type form: YoutubeVideoForm
        """
        logger = StructLogger.get_logger(__name__, self.request)
        logger.error(msg='Download failed',
                     youtube_id=self.request.POST.get('youtube_id', None),
                     error=form.errors['youtube_id'])
        return super(YoutubeVideoCreateView, self).form_invalid(form)
예제 #8
0
    def form_invalid(self, form):
        """
        Log invalid submissions.

        :param form: The invalid form
        :type form: YoutubeVideoForm
        """
        logger = StructLogger.get_logger(__name__, self.request)
        logger.error(
            msg='Download failed',
            youtube_id=self.request.POST.get('youtube_id', None),
            error=form.errors['youtube_id']
        )
        return super(YoutubeVideoCreateView, self).form_invalid(form)
예제 #9
0
    def form_valid(self, form):
        """
        Saves the video, sets the uploaded by and downloads the video.

        :param form: The valid form
        :type form: YoutubeVideoForm
        """
        self.object = form.save(commit=False)
        self.object.uploaded_by = self.request.user
        self.object.save()
        download_video.delay(self.object)
        logger = StructLogger.get_logger(__name__, self.request)
        logger.info(msg='Download successful', youtube_video_pk=self.object.pk, youtube_id=self.object.youtube_id)
        return HttpResponseRedirect(self.get_success_url())
예제 #10
0
    def form_valid(self, form):
        """
        Saves the video, sets the uploaded by and downloads the video.

        :param form: The valid form
        :type form: YoutubeVideoForm
        """
        self.object = form.save(commit=False)
        self.object.uploaded_by = self.request.user
        self.object.save()
        download_video.delay(self.object)
        logger = StructLogger.get_logger(__name__, self.request)
        logger.info('Download successful',
                    youtube_video_pk=self.object.pk,
                    youtube_id=self.object.youtube_id)
        return HttpResponseRedirect(self.get_success_url())
예제 #11
0
def multi_image_upload(request):
    """
    View for handling multi-image upload.

    :type request: HttpRequest
    """

    logger = StructLogger.get_logger(__name__, request)

    if request.method == 'POST':
        if 'application/json' in request.META.get('HTTP_ACCEPT', []):
            content_type = 'application/json'
        else:
            content_type = 'text/plain'

        # TODO: See if there's a safer way to inspect file than .read() - .chunks() isn't accepted by magic.
        mime_type = magic.from_buffer(request.FILES['files[]'].read(), mime=True)
        if mime_type not in settings.ALLOWED_MIME_TYPES:
            response_dict = {'files': [{
                'name': request.FILES['files[]'].name,
                'size': request.FILES['files[]'].size,
                'error': _('{} is not a valid image file').format(request.FILES['files[]'].name),
            }]}
            logger.error(msg='Upload failed', file_name=request.FILES['files[]'].name)
        else:
            image = Image.objects.create(image=request.FILES['files[]'], uploaded_by=request.user)
            thumbnail = image.make_thumbnail()
            response_dict = {'files': [{
                'name': image.title,
                'size': image.image.size,
                'url': image.get_absolute_url(),
                'thumbnailUrl': thumbnail,
                'deleteUrl': image.get_delete_url(),
                'deleteType': 'DELETE',
            }]}
            logger.info(
                msg='Upload successful',
                file_name=request.FILES['files[]'].name,
                image_pk=image.pk,
            )
        return JsonResponse(response_dict, content_type=content_type)
    else:
        return render_to_response('images/upload.html', context_instance=RequestContext(request))