Example #1
0
 def set_prefix():
     for event in DeviceEventDomain.get({}):
         try:
             resource_type = DeviceEventDomain.new_type(event['@type'])
         except TypeError:
             pass
         else:
             DeviceEventDomain.update_raw(event['_id'], {'$set': {'@type': resource_type}})
     print('Events prefixed.')
Example #2
0
def transfer_property(receives: list):
    for receive in receives:
        if receive['automaticallyAllocate']:
            allocate_type = DeviceEventDomain.new_type('Allocate')
            a = execute_post_internal(Naming.resource(allocate_type), {
                '@type': allocate_type,
                'to': receive['receiver'],
                'devices': receive['devices']
            })
            receive['_created'] = receive['_updated'] = a['_created'] + datetime.timedelta(milliseconds=1)
Example #3
0
 def generate_url(self, original_resource, translated_resource):
     device_identifier = self.translator.hid_or_url(original_resource['device'])
     if not re.compile(HID_REGEX).match(device_identifier):  # It is not a HID, so it is an URL
         device_identifier = quote_plus(device_identifier.replace('/', '!'))  # Adapt it to GRD needs
     url = self.domain + '/api/devices/'
     event_type = translated_resource['@type']
     if event_type == DeviceEventDomain.new_type('Register'):
         url += 'register'
     else:
         url += '{}/{}'.format(device_identifier, Naming.resource(event_type))
     return url
Example #4
0
 def subclasses_fields(cls):
     from ereuse_devicehub.resources.event.device import DeviceEventDomain
     global_types = super(Component, cls).subclasses_fields()
     with suppress(KeyError):
         global_types['size']['type'] = global_types['speed']['type'] = 'number'
         events = {DeviceEventDomain.new_type(x) for x in ('EraseSectors', 'EraseBasic')}
         global_types['erasure']['schema']['@type']['allowed'] = events
         union_of_benchmarks = BenchmarkHardDrive()
         union_of_benchmarks.update(BenchmarkProcessor())
         union_of_benchmarks['@type']['allowed'] = set(Benchmark.TYPES)
         global_types['benchmark']['schema'] = union_of_benchmarks
     return global_types
Example #5
0
def transfer_property(receives: list):
    for receive in receives:
        if receive['automaticallyAllocate']:
            allocate_type = DeviceEventDomain.new_type('Allocate')
            a = execute_post_internal(
                Naming.resource(allocate_type), {
                    '@type': allocate_type,
                    'to': receive['receiver'],
                    'devices': receive['devices']
                })
            receive['_created'] = receive['_updated'] = a[
                '_created'] + datetime.timedelta(milliseconds=1)
Example #6
0
 def translate(self, resource: dict, database: str = None) -> list:
     self.database = database
     translated = []
     if 'devices' in resource and resource['@type'] != DeviceEventDomain.new_type('Register'):
         e = copy.deepcopy(resource)
         del e['devices']
         for device in resource['devices']:
             e['device'] = device
             translated.append((self._translate(e), copy.deepcopy(e)))
     else:
         translated.append((self._translate(resource), resource))
     return translated
 def set_prefix():
     for event in DeviceEventDomain.get({}):
         try:
             resource_type = DeviceEventDomain.new_type(event['@type'])
         except TypeError:
             pass
         else:
             DeviceEventDomain.update_raw(
                 event['_id'], {'$set': {
                     '@type': resource_type
                 }})
     print('Events prefixed.')
Example #8
0
 def get_similar_component(cls, component: dict, parent_id: str) -> dict:
     """Gets a component that has same parent, doesn't generate HID and their ETAG are the same"""
     # We the unsecured _id of the devices of all parent_id snapshots
     snapshots = EventDomain.get({'@type': DeviceEventDomain.new_type('Snapshot'), 'device': parent_id})
     devices_id = set()
     for snapshot in snapshots:
         for unsecured in snapshot['unsecured']:
             devices_id.add(unsecured['_id'])
     # We get the devices whose _id and etag matches
     etag = cls.generate_etag(component)
     query = {'_id': {'$in': list(devices_id)}, '_etag': etag}
     device = cls.get_one(query)
     return cls.get_one(device['_id'])  # todo if we materialize components we do not need to do double query
Example #9
0
 def translate(self, resource: dict, database: str = None) -> list:
     self.database = database
     translated = []
     if 'devices' in resource and resource[
             '@type'] != DeviceEventDomain.new_type('Register'):
         e = copy.deepcopy(resource)
         del e['devices']
         for device in resource['devices']:
             e['device'] = device
             translated.append((self._translate(e), copy.deepcopy(e)))
     else:
         translated.append((self._translate(resource), resource))
     return translated
Example #10
0
 def register(self, event_log: list):
     with suppress(NoDevicesToProcess):
         register = {
             '@type': DeviceEventDomain.new_type('Register'),
             'device': self.device,
             'components': self.components
         }
         self.set_created_conditionally(register)
         event_log.append(execute_post_internal(Naming.resource(register['@type']), register))
     for device in [self.device] + self.components:
         if 'hid' not in device and 'pid' not in device:
             self._append_unsecured(device, 'model')
         elif 'pid' in device:
             self._append_unsecured(device, 'pid')
Example #11
0
 def test_materializations_events(self):
     """
     Tests materializations related to events.
     :return:
     """
     # Let's check POST
     devices = self.get_fixtures_computers()
     vaio, _ = self.get(self.DEVICES, '', devices[0])
     account_id = str(self.account['_id'])
     materialized = [
         {'@type': DeviceEventDomain.new_type('Snapshot'), 'secured': False, 'byUser': account_id,
          'incidence': False},
         {'@type': DeviceEventDomain.new_type('Register'), 'secured': False, 'byUser': account_id,
          'incidence': False},
     ]
     fields = {'@type', '_id', 'byUser', 'incidence', 'secured', '_updated'}
     self.assertIn('events', vaio)
     i = 0
     for event in vaio['events']:
         # Order needs to be preserved
         assert_that(materialized[i]).is_subset_of(event)
         assert_that(set(event.keys())).is_equal_to(fields)
         i += 1
Example #12
0
 def generate_url(self, original_resource, translated_resource):
     device_identifier = self.translator.hid_or_url(
         original_resource['device'])
     if not re.compile(HID_REGEX).match(
             device_identifier):  # It is not a HID, so it is an URL
         device_identifier = quote_plus(device_identifier.replace(
             '/', '!'))  # Adapt it to GRD needs
     url = self.domain + '/api/devices/'
     event_type = translated_resource['@type']
     if event_type == DeviceEventDomain.new_type('Register'):
         url += 'register'
     else:
         url += '{}/{}'.format(device_identifier,
                               Naming.resource(event_type))
     return url
Example #13
0
 def subclasses_fields(cls):
     from ereuse_devicehub.resources.event.device import DeviceEventDomain
     global_types = super(Component, cls).subclasses_fields()
     with suppress(KeyError):
         global_types['size']['type'] = global_types['speed'][
             'type'] = 'number'
         events = {
             DeviceEventDomain.new_type(x)
             for x in ('EraseSectors', 'EraseBasic')
         }
         global_types['erasure']['schema']['@type']['allowed'] = events
         union_of_benchmarks = BenchmarkHardDrive()
         union_of_benchmarks.update(BenchmarkProcessor())
         union_of_benchmarks['@type']['allowed'] = set(Benchmark.TYPES)
         global_types['benchmark']['schema'] = union_of_benchmarks
     return global_types
Example #14
0
 def creation(self, input_snapshot: dict, num_of_events: int = 1, do_second_time_snapshot=True) -> str:
     pprint("1st time snapshot:")
     events = self.post_snapshot_get_full_events(input_snapshot, num_of_events)
     self.assertLen(events, num_of_events)
     register = events[0]
     self.assertType(DeviceEventDomain.new_type('Register'), register)
     self.assertSimilarDevice(input_snapshot['device'], register['device'])
     if 'components' in input_snapshot:
         self.assertSimilarDevices(input_snapshot['components'], register['components'])
     # We do a snapshot again. We should receive a new snapshot without any event on it.
     if do_second_time_snapshot:
         pprint("2nd time snapshot:")
         snapshot, status_code = self.post('{}/{}'.format(self.DEVICE_EVENT, self.SNAPSHOT), input_snapshot)
         self.assert201(status_code)
         self.assertLen(snapshot['events'], num_of_events - 1)
     return register['device']
Example #15
0
 def register(self, event_log: list):
     with suppress(NoDevicesToProcess):
         register = {
             '@type': DeviceEventDomain.new_type('Register'),
             'device': self.device,
             'components': self.components
         }
         self.set_created_conditionally(register)
         event_log.append(
             execute_post_internal(Naming.resource(register['@type']),
                                   register))
     for device in [self.device] + self.components:
         if 'hid' not in device and 'pid' not in device:
             self._append_unsecured(device, 'model')
         elif 'pid' in device:
             self._append_unsecured(device, 'pid')
Example #16
0
def get_place(resource_name: str, events: list):
    """

    :param resource_name:
    :param events:
    :return:
    """
    if resource_name in Event.resource_types:
        for event in events:
            if "geo" in event:
                try:
                    place = PlaceDomain.get_with_coordinates(event["geo"]["coordinates"])
                except (KeyError, NoPlaceForGivenCoordinates) as e:
                    # Receive and Locate are forced to have a place for their coordinates
                    if event["@type"] in (DeviceEventDomain.new_type(x) for x in ("Receive", "Locate")):
                        raise e
                else:
                    if "place" in event:
                        if event["place"]["_id"] != str(place["_id"]):  # geo 1 place 1
                            raise CoordinatesAndPlaceDoNotMatch()
                    else:
                        event["place"] = place["_id"]  # geo 1 place found in DB
Example #17
0
def get_place(resource_name: str, events: list):
    """

    :param resource_name:
    :param events:
    :return:
    """
    if resource_name in Event.resource_types:
        for event in events:
            if 'geo' in event:
                try:
                    place = PlaceDomain.get_with_coordinates(event['geo']['coordinates'])
                except (KeyError, NoPlaceForGivenCoordinates) as e:
                    # Receive and Locate are forced to have a place for their coordinates
                    if event['@type'] in (DeviceEventDomain.new_type(x) for x in ('Receive', 'Locate')):
                        raise e
                else:
                    if 'place' in event:
                        if event['place']['_id'] != str(place['_id']):  # geo 1 place 1
                            raise CoordinatesAndPlaceDoNotMatch()
                    else:
                        event['place'] = place['_id']  # geo 1 place found in DB
Example #18
0
 def append_add(self, component, new_parent):
     self._append(DeviceEventDomain.new_type('Add'), new_parent, component)
Example #19
0
 def append_add(self, component, new_parent):
     self._append(DeviceEventDomain.new_type('Add'), new_parent, component)
Example #20
0
PAGINATION_LIMIT = 100
DATE_FORMAT = '%Y-%m-%dT%H:%M:%S'
SCHEMA_ENDPOINT = 'schema'
VALIDATION_ERROR_AS_LIST = True  # We always use list to show errors
ITEM_CACHE = 120  # We differentiate from Resource cache (Cache setting from Eve) from Item cache

# Role settings
from ereuse_devicehub.resources.account.role import Role

ALLOWED_WRITE_ROLES = {Role.AMATEUR}
ALLOWED_ITEM_WRITE_ROLES = {Role.AMATEUR}
ALLOWED_READ_ROLES = {Role.BASIC}
ALLOWED_ITEM_READ_ROLES = {Role.BASIC}

# GRD Settings, do not change them
_events_in_grd = ('Deallocate', 'Migrate', 'Allocate', 'Receive',
                  'Remove', 'Add', 'Register', 'Locate', 'UsageProof', 'Recycle')
EVENTS_IN_GRD = [Naming.resource(DeviceEventDomain.new_type(event)) for event in _events_in_grd]
# Generation of the API (DOMAIN)
from ereuse_devicehub.resources.device.settings import DeviceSettings
from ereuse_devicehub.resources.account.settings import AccountSettings
from ereuse_devicehub.resources.event.settings import EventSettings
from ereuse_devicehub.resources.place.settings import PlaceSettings

DOMAIN = {
    'devices': DeviceSettings,
    'events': EventSettings,
    'accounts': AccountSettings,
    'places': PlaceSettings
}
Example #21
0
 def append_remove(self, component, old_parent):
     self._append(DeviceEventDomain.new_type('Remove'), old_parent, component)
Example #22
0
 def append_remove(self, component, old_parent):
     self._append(DeviceEventDomain.new_type('Remove'), old_parent,
                  component)
Example #23
0
VALIDATION_ERROR_AS_LIST = True  # We always use list to show errors
ITEM_CACHE = 120  # We differentiate from Resource cache (Cache setting from Eve) from Item cache

# Role settings
from ereuse_devicehub.resources.account.role import Role

ALLOWED_WRITE_ROLES = {Role.AMATEUR}
ALLOWED_ITEM_WRITE_ROLES = {Role.AMATEUR}
ALLOWED_READ_ROLES = {Role.BASIC}
ALLOWED_ITEM_READ_ROLES = {Role.BASIC}

# GRD Settings, do not change them
_events_in_grd = ('Deallocate', 'Migrate', 'Allocate', 'Receive', 'Remove',
                  'Add', 'Register', 'Locate', 'UsageProof', 'Recycle')
EVENTS_IN_GRD = [
    Naming.resource(DeviceEventDomain.new_type(event))
    for event in _events_in_grd
]
# Generation of the API (DOMAIN)
from ereuse_devicehub.resources.device.settings import DeviceSettings
from ereuse_devicehub.resources.account.settings import AccountSettings
from ereuse_devicehub.resources.event.settings import EventSettings
from ereuse_devicehub.resources.place.settings import PlaceSettings

DOMAIN = {
    'devices': DeviceSettings,
    'events': EventSettings,
    'accounts': AccountSettings,
    'places': PlaceSettings
}