import logging import os from rest_framework.generics import get_object_or_404 from core.utils import httpFileResponse from devices.exceptions import EndpointDeviceException from devices.models.endpoint_device import EndpointModel from storage.models import DataStorage _log = logging.getLogger() class EndpointAntivirusService: """ Service to management endpoint antivirus """ def __init__(self, pk: int) -> None: self.endpoint = get_object_or_404(EndpointModel, pk=pk) def update(self): if not self.endpoint.antivirus_update_db: raise EndpointDeviceException({'status': 'error', 'detail': 'Endpoint antivirus no update required'}) try: storage_file = DataStorage.objects.get(type=DataStorage.Type.CLAMAV) except DataStorage.DoesNotExist: raise EndpointDeviceException({'status': 'error', 'message': 'No database selected'}) is_zip = True if storage_file.Format == DataStorage.Format.ZIP else False storage_file = os.path.join(storage_file.get_full_path()) self.endpoint.antivirus_update_db = False self.endpoint.save() return httpFileResponse(open(storage_file, 'rb'), "antivirus_update.zip", is_zip) @staticmethod def all_update(update): EndpointModel.objects.all().update(antivirus_update_db=update) _log.info('Set flag update antivirus for all endpoints')