from bson.objectid import ObjectId

from girder.models.model_base import AccessControlledModel
from girder.constants import AccessType

from .molecule import Molecule as MoleculeModel


class Geometry(AccessControlledModel):

    def __init__(self):
        super(Geometry, self).__init__()

    def initialize(self):
        self.name = 'geometry'
        self.ensureIndex('moleculeId')

        self.exposeFields(level=AccessType.READ, fields=(
            '_id', 'moleculeId', 'cjson', 'provenanceType', 'provenanceId'))

    def validate(self, doc):
        # If we have a moleculeId ensure it is valid.
        if 'moleculeId' in doc:
            mol = MoleculeModel().load(doc['moleculeId'], force=True)
            doc['moleculeId'] = mol['_id']

        return doc

    def create(self, user, moleculeId, cjson, provenanceType=None,
               provenanceId=None, public=False):
        geometry = {
            'moleculeId': moleculeId,
            'cjson': cjson
        }

        if provenanceType is not None:
            geometry['provenanceType'] = provenanceType

        if provenanceId is not None:
            geometry['provenanceId'] = provenanceId

        self.setUserAccess(geometry, user=user, level=AccessType.ADMIN)
        if public:
            self.setPublic(geometry, True)

        return self.save(geometry)

    def find_geometries(self, moleculeId, user=None):
        query = {
            'moleculeId': ObjectId(moleculeId)
        }

        return self.findWithPermissions(query, user=user)

from bson.objectid import ObjectId

from girder.models.model_base import AccessControlledModel
from girder.constants import AccessType

from .molecule import Molecule as MoleculeModel


class Geometry(AccessControlledModel):

    def __init__(self):
        super(Geometry, self).__init__()

    def initialize(self):
        self.name = 'geometry'
        self.ensureIndex('moleculeId')

        self.exposeFields(level=AccessType.READ, fields=(
            '_id', 'moleculeId', 'cjson', 'provenanceType', 'provenanceId'))

    def validate(self, doc):
        # If we have a moleculeId ensure it is valid.
        if 'moleculeId' in doc:
            mol = MoleculeModel().load(doc['moleculeId'], force=True)
            doc['moleculeId'] = mol['_id']

        return doc

    def create(self, user, moleculeId, cjson, provenanceType=None,
               provenanceId=None, public=False):
        geometry = {
            'moleculeId': moleculeId,
            'cjson': cjson,
            'creatorId': user['_id']
        }

        if provenanceType is not None:
            geometry['provenanceType'] = provenanceType

        if provenanceId is not None:
            geometry['provenanceId'] = provenanceId

        self.setUserAccess(geometry, user=user, level=AccessType.ADMIN)
        if public:
            self.setPublic(geometry, True)

        return self.save(geometry)

    def find_geometries(self, moleculeId, user=None):
        query = {
            'moleculeId': ObjectId(moleculeId)
        }

        return self.findWithPermissions(query, user=user)

Save creatorId as well for geometries
Save creatorId as well for geometries

This is to keep track of the creator, even when the provenance
is not the user.

Signed-off-by: Patrick Avery <743342299f279e7a8c3ff5eb40671fce3e95f13a@kitware.com>
