Ejemplo n.º 1
0
    def get(self, collection_id, parent_id, object_id,
            id_field=DEFAULT_ID_FIELD,
            modified_field=DEFAULT_MODIFIED_FIELD,
            auth=None):
        query = """
        SELECT as_epoch(last_modified) AS last_modified, data
          FROM records
         WHERE id = :object_id
           AND parent_id = :parent_id
           AND collection_id = :collection_id;
        """
        placeholders = dict(object_id=object_id,
                            parent_id=parent_id,
                            collection_id=collection_id)
        with self.client.connect(readonly=True) as conn:
            result = conn.execute(query, placeholders)
            if result.rowcount == 0:
                raise exceptions.RecordNotFoundError(object_id)
            else:
                existing = result.fetchone()

        record = existing['data']
        record[id_field] = object_id
        record[modified_field] = existing['last_modified']
        return record
Ejemplo n.º 2
0
    def get(self,
            collection_id,
            parent_id,
            object_id,
            id_field=DEFAULT_ID_FIELD,
            modified_field=DEFAULT_MODIFIED_FIELD,
            auth=None):
        # Get read query and set table name
        query = READ_QUERY
        table_name = "records"

        # Prepare value dict for query
        values_dict = {
            'id': object_id,
            'parent_id': parent_id,
            'collection_id': collection_id
        }

        object = None

        # Execute query
        with self.client.connect() as conn:
            results = conn.execute(query.format(table_name=table_name),
                                   values_dict)
            object = results.fetchone()
            # If object not found raise error
            if object == None:
                raise exceptions.RecordNotFoundError(object_id)

        # Convert blob to dict because it is
        # stored as string
        record = json.loads(object['data'])
        record[id_field] = object['id']
        record[modified_field] = object['last_modified']
        return record
Ejemplo n.º 3
0
 def get(self, collection_id, parent_id, object_id,
         id_field=DEFAULT_ID_FIELD,
         modified_field=DEFAULT_MODIFIED_FIELD,
         auth=None):
     collection = self._store[parent_id][collection_id]
     if object_id not in collection:
         raise exceptions.RecordNotFoundError(object_id)
     return {**collection[object_id]}
Ejemplo n.º 4
0
    def delete(
        self,
        collection_id,
        parent_id,
        object_id,
        id_field=DEFAULT_ID_FIELD,
        with_deleted=True,
        modified_field=DEFAULT_MODIFIED_FIELD,
        deleted_field=DEFAULT_DELETED_FIELD,
        auth=None,
        last_modified=None,
    ):
        if with_deleted:
            query = """
            UPDATE records
               SET deleted=TRUE,
                   data=(:deleted_data)::JSONB,
                   last_modified=from_epoch(:last_modified)
             WHERE id = :object_id
               AND parent_id = :parent_id
               AND collection_id = :collection_id
               AND deleted = FALSE
            RETURNING as_epoch(last_modified) AS last_modified;
            """
        else:
            query = """
            DELETE FROM records
            WHERE id = :object_id
               AND parent_id = :parent_id
               AND collection_id = :collection_id
               AND deleted = FALSE
            RETURNING as_epoch(last_modified) AS last_modified;
            """
        deleted_data = self.json.dumps(dict([(deleted_field, True)]))
        placeholders = dict(
            object_id=object_id,
            parent_id=parent_id,
            collection_id=collection_id,
            last_modified=last_modified,
            deleted_data=deleted_data,
        )

        with self.client.connect() as conn:
            result = conn.execute(query, placeholders)
            if result.rowcount == 0:
                raise exceptions.RecordNotFoundError(object_id)
            inserted = result.fetchone()

        record = {}
        record[modified_field] = inserted["last_modified"]
        record[id_field] = object_id

        record[deleted_field] = True
        return record
Ejemplo n.º 5
0
    def delete(self,
               collection_id,
               parent_id,
               object_id,
               id_field=DEFAULT_ID_FIELD,
               with_deleted=True,
               modified_field=DEFAULT_MODIFIED_FIELD,
               deleted_field=DEFAULT_DELETED_FIELD,
               auth=None,
               last_modified=None):
        if with_deleted:
            query = """
            WITH deleted_record AS (
                DELETE
                FROM records
                WHERE id = :object_id
                  AND parent_id = :parent_id
                  AND collection_id = :collection_id
                RETURNING id
            )
            INSERT INTO deleted (id, parent_id, collection_id, last_modified)
            SELECT id, :parent_id, :collection_id, from_epoch(:last_modified)
              FROM deleted_record
            RETURNING as_epoch(last_modified) AS last_modified;
            """
        else:
            query = """
                DELETE
                FROM records
                WHERE id = :object_id
                  AND parent_id = :parent_id
                  AND collection_id = :collection_id
                RETURNING as_epoch(last_modified) AS last_modified;
            """
        placeholders = dict(object_id=object_id,
                            parent_id=parent_id,
                            collection_id=collection_id,
                            last_modified=last_modified)

        with self.client.connect() as conn:
            result = conn.execute(query, placeholders)
            if result.rowcount == 0:
                raise exceptions.RecordNotFoundError(object_id)
            inserted = result.fetchone()

        record = {}
        record[modified_field] = inserted['last_modified']
        record[id_field] = object_id

        record[deleted_field] = True
        return record
Ejemplo n.º 6
0
    def get(self,
            collection_id,
            parent_id,
            object_id,
            id_field=DEFAULT_ID_FIELD,
            modified_field=DEFAULT_MODIFIED_FIELD,
            auth=None):
        record_key = '{0}.{1}.{2}.records'.format(collection_id, parent_id,
                                                  object_id)
        encoded_item = self._client.get(record_key)
        if encoded_item is None:
            raise exceptions.RecordNotFoundError(object_id)

        return self._decode(encoded_item)
Ejemplo n.º 7
0
    def delete(self,
               collection_id,
               parent_id,
               object_id,
               id_field=DEFAULT_ID_FIELD,
               with_deleted=True,
               modified_field=DEFAULT_MODIFIED_FIELD,
               deleted_field=DEFAULT_DELETED_FIELD,
               auth=None,
               last_modified=None):
        record_key = '{0}.{1}.{2}.records'.format(collection_id, parent_id,
                                                  object_id)
        with self._client.pipeline() as multi:
            multi.get(record_key)
            multi.delete(record_key)
            multi.srem('{0}.{1}.records'.format(collection_id, parent_id),
                       object_id)
            responses = multi.execute()

        encoded_item = responses[0]
        if encoded_item is None:
            raise exceptions.RecordNotFoundError(object_id)

        existing = self._decode(encoded_item)

        # Need to delete the last_modified field.
        del existing[modified_field]

        self.set_record_timestamp(collection_id,
                                  parent_id,
                                  existing,
                                  modified_field=modified_field,
                                  last_modified=last_modified)
        existing = self.strip_deleted_record(collection_id, parent_id,
                                             existing)

        if with_deleted:
            deleted_record_key = '{0}.{1}.{2}.deleted'.format(
                collection_id, parent_id, object_id)
            with self._client.pipeline() as multi:
                multi.set(deleted_record_key, self._encode(existing))
                multi.sadd('{0}.{1}.deleted'.format(collection_id, parent_id),
                           object_id)
                multi.execute()

        return existing
Ejemplo n.º 8
0
 def record_not_found(*args, **kwargs):
     raise exceptions.RecordNotFoundError()