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 json import JSONEncoder
from uuid import UUID from uuid import UUID
from abc import ABCMeta from abc import ABCMeta
from enum import Enum, IntEnum from enum import Enum
from typing import Any, Mapping from typing import Any, Mapping
from collections.abc import MutableMapping from collections.abc import MutableMapping
from functools import lru_cache from functools import lru_cache
@ -147,19 +147,6 @@ class AbstractMISP(MutableMapping, MISPFileCache, metaclass=ABCMeta): # type: i
treatment are processed. treatment are processed.
Note: This method is used when you initialize an object with existing data so by default, Note: This method is used when you initialize an object with existing data so by default,
the class is flaged as not edited.""" 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(): for prop, value in kwargs.items():
if value is None: if value is None:
continue continue

View File

@ -653,8 +653,7 @@ class PyMISP:
type = analyst_data.classObjectType type = analyst_data.classObjectType
analyst_data_id = get_uuid_or_id_from_abstract_misp(analyst_data) analyst_data_id = get_uuid_or_id_from_abstract_misp(analyst_data)
request_url = f'analyst_data/delete/{type}/{analyst_data_id}' request_url = f'analyst_data/delete/{type}/{analyst_data_id}'
data = {} r = self._prepare_request('POST', request_url)
r = self._prepare_request('POST', request_url, data=data)
return self._check_json_response(r) return self._check_json_response(r)
# ## END Analyst Data ### # ## END Analyst Data ###
@ -767,7 +766,6 @@ class PyMISP:
# ## END Analyst Relationship ### # ## END Analyst Relationship ###
# ## BEGIN Object ### # ## BEGIN Object ###
def get_object(self, misp_object: MISPObject | int | str | UUID, pythonify: bool = False) -> dict[str, Any] | MISPObject: 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 self.edited = True
return the_relationship 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: try:
from dateutil.parser import parse from dateutil.parser import parse
except ImportError: except ImportError:
@ -1130,7 +1140,6 @@ class MISPEventReport(AnalystDataBehaviorMixin):
super().__init__(**kwargs) super().__init__(**kwargs)
self.uuid: str = str(uuid.uuid4()) self.uuid: str = str(uuid.uuid4())
def from_dict(self, **kwargs) -> None: # type: ignore[no-untyped-def] def from_dict(self, **kwargs) -> None: # type: ignore[no-untyped-def]
if 'EventReport' in kwargs: if 'EventReport' in kwargs:
kwargs = kwargs['EventReport'] kwargs = kwargs['EventReport']
@ -2422,7 +2431,6 @@ class MISPAnalystData(AbstractMISP):
self.created: float | int | datetime self.created: float | int | datetime
self.modified: float | int | datetime self.modified: float | int | datetime
self.SharingGroup: MISPSharingGroup self.SharingGroup: MISPSharingGroup
self.note_type_name = self.classObjectType
def from_dict(self, **kwargs) -> None: # type: ignore[no-untyped-def] def from_dict(self, **kwargs) -> None: # type: ignore[no-untyped-def]
self.distribution = kwargs.pop('distribution', None) self.distribution = kwargs.pop('distribution', None)