Code Duplication    Length = 42-46 lines in 2 locations

backend/tracim_backend/views/contents_api/file_controller.py 2 locations

@@ 459-504 (lines=46) @@
456
            as_attachment=hapic_data.query.force_download
457
        )
458
459
    @hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
460
    @require_workspace_role(UserRoleInWorkspace.READER)
461
    @require_content_types([FILE_TYPE])
462
    @hapic.handle_exception(UnavailablePreview, HTTPStatus.BAD_REQUEST)
463
    @hapic.handle_exception(PageOfPreviewNotFound, HTTPStatus.BAD_REQUEST)
464
    @hapic.handle_exception(PreviewDimNotAllowed, HTTPStatus.BAD_REQUEST)
465
    @hapic.input_path(RevisionPreviewSizedPathSchema())
466
    @hapic.input_query(PageQuerySchema())
467
    @hapic.output_file([])
468
    def sized_preview_jpg_revision(self, context, request: TracimRequest, hapic_data=None):  # nopep8
469
        """
470
        Obtain resized jpg preview of a specific revision of content.
471
        """
472
        app_config = request.registry.settings['CFG']
473
        api = ContentApi(
474
            show_archived=True,
475
            show_deleted=True,
476
            current_user=request.current_user,
477
            session=request.dbsession,
478
            config=app_config,
479
        )
480
        content = api.get_one(
481
            hapic_data.path.content_id,
482
            content_type=CONTENT_TYPES.Any_SLUG
483
        )
484
        revision = api.get_one_revision(
485
            revision_id=hapic_data.path.revision_id,
486
            content=content
487
        )
488
        jpg_preview_path = api.get_jpg_preview_path(
489
            content_id=content.content_id,
490
            revision_id=revision.revision_id,
491
            page_number=hapic_data.query.page,
492
            height=hapic_data.path.height,
493
            width=hapic_data.path.width,
494
        )
495
        filename = "{label}_page_{page_number}_{width}x{height}.jpg".format(
496
            label=revision.label,
497
            page_number=hapic_data.query.page,
498
            width=hapic_data.path.width,
499
            height=hapic_data.path.height
500
        )
501
        return HapicFile(
502
            file_path=jpg_preview_path,
503
            filename=filename,
504
            as_attachment=hapic_data.query.force_download
505
        )
506
507
    @hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
@@ 415-456 (lines=42) @@
412
            as_attachment=hapic_data.query.force_download
413
        )
414
415
    @hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])
416
    @require_workspace_role(UserRoleInWorkspace.READER)
417
    @require_content_types([FILE_TYPE])
418
    @hapic.handle_exception(UnavailablePreview, HTTPStatus.BAD_REQUEST)
419
    @hapic.handle_exception(PageOfPreviewNotFound, HTTPStatus.BAD_REQUEST)
420
    @hapic.handle_exception(PreviewDimNotAllowed, HTTPStatus.BAD_REQUEST)
421
    @hapic.input_query(PageQuerySchema())
422
    @hapic.input_path(ContentPreviewSizedPathSchema())
423
    @hapic.output_file([])
424
    def sized_preview_jpg(self, context, request: TracimRequest, hapic_data=None):  # nopep8
425
        """
426
        Obtain resized jpg preview of last revision of content.
427
        """
428
        app_config = request.registry.settings['CFG']
429
        api = ContentApi(
430
            show_archived=True,
431
            show_deleted=True,
432
            current_user=request.current_user,
433
            session=request.dbsession,
434
            config=app_config,
435
        )
436
        content = api.get_one(
437
            hapic_data.path.content_id,
438
            content_type=CONTENT_TYPES.Any_SLUG
439
        )
440
        jpg_preview_path = api.get_jpg_preview_path(
441
            content_id=content.content_id,
442
            revision_id=content.revision_id,
443
            page_number=hapic_data.query.page,
444
            height=hapic_data.path.height,
445
            width=hapic_data.path.width,
446
        )
447
        filename = "{label}_page_{page_number}_{width}x{height}.jpg".format(
448
            label=content.label,
449
            page_number=hapic_data.query.page,
450
            width=hapic_data.path.width,
451
            height=hapic_data.path.height
452
        )
453
        return HapicFile(
454
            file_path=jpg_preview_path,
455
            filename=filename,
456
            as_attachment=hapic_data.query.force_download
457
        )
458
459
    @hapic.with_api_doc(tags=[SWAGGER_TAG__FILE_ENDPOINTS])