chg: use from_dict in the mixin to initialize the objects

wip_analystdata
Raphaël Vinot 2024-05-06 12:19:04 +02:00
parent 56be46320e
commit 902ed5a92c
3 changed files with 12 additions and 19 deletions

View File

@ -8,7 +8,7 @@ from deprecated import deprecated # type: ignore
from json import JSONEncoder
from uuid import UUID
from abc import ABCMeta
from enum import Enum, IntEnum
from enum import Enum
from typing import Any, Mapping
from collections.abc import MutableMapping
from functools import lru_cache
@ -147,19 +147,6 @@ class AbstractMISP(MutableMapping, MISPFileCache, metaclass=ABCMeta): # type: i
treatment are processed.
Note: This method is used when you initialize an object with existing data so by default,
the class is flaged as not edited."""
# Recursively loads more analyst data
from pymisp.mispevent import AnalystDataBehaviorMixin, MISPNote, MISPOpinion, MISPRelationship
if isinstance(self, AnalystDataBehaviorMixin):
for analystType in ['Note', 'Opinion', 'Relationship']:
if kwargs.get(analystType):
analystDataList = kwargs.pop(analystType)
for analystDataDict in analystDataList:
analystData = {'Note': MISPNote, 'Opinion': MISPOpinion, 'Relationship': MISPRelationship}.get(analystType, MISPNote)()
analystDataDict['object_uuid'] = self.uuid if 'object_uuid' not in analystDataDict else analystDataDict['object_uuid']
analystDataDict['object_type'] = self.classObjectType
analystData.from_dict(**analystDataDict)
{'Note': self.notes, 'Opinion': self.opinions, 'Relationship': self.relationships}.get(analystType, 'Note').append(analystData)
for prop, value in kwargs.items():
if value is None:
continue

View File

@ -653,8 +653,7 @@ class PyMISP:
type = analyst_data.classObjectType
analyst_data_id = get_uuid_or_id_from_abstract_misp(analyst_data)
request_url = f'analyst_data/delete/{type}/{analyst_data_id}'
data = {}
r = self._prepare_request('POST', request_url, data=data)
r = self._prepare_request('POST', request_url)
return self._check_json_response(r)
# ## END Analyst Data ###
@ -767,7 +766,6 @@ class PyMISP:
# ## END Analyst Relationship ###
# ## BEGIN Object ###
def get_object(self, misp_object: MISPObject | int | str | UUID, pythonify: bool = False) -> dict[str, Any] | MISPObject:

View File

@ -82,6 +82,16 @@ class AnalystDataBehaviorMixin(AbstractMISP):
self.edited = True
return the_relationship
def from_dict(self, **kwargs) -> None: # type: ignore[no-untyped-def]
if 'Note' in kwargs and kwargs.get('Note'):
self.add_note(**kwargs.pop('Note'))
if 'Opinion' in kwargs and kwargs.get('Opinion'):
self.add_opinion(**kwargs.pop('Opinion'))
if 'Relationship' in kwargs and kwargs.get('Relationship'):
self.add_relationship(**kwargs.pop('Relationship'))
super().from_dict(**kwargs)
try:
from dateutil.parser import parse
except ImportError:
@ -1130,7 +1140,6 @@ class MISPEventReport(AnalystDataBehaviorMixin):
super().__init__(**kwargs)
self.uuid: str = str(uuid.uuid4())
def from_dict(self, **kwargs) -> None: # type: ignore[no-untyped-def]
if 'EventReport' in kwargs:
kwargs = kwargs['EventReport']
@ -2422,7 +2431,6 @@ class MISPAnalystData(AbstractMISP):
self.created: float | int | datetime
self.modified: float | int | datetime
self.SharingGroup: MISPSharingGroup
self.note_type_name = self.classObjectType
def from_dict(self, **kwargs) -> None: # type: ignore[no-untyped-def]
self.distribution = kwargs.pop('distribution', None)