class RefreshToken(Document): """ A RefreshToken instance represents a token that can be swapped for a new access token when it expires. Fields: * :attr:`user` The Django user representing resources' owner * :attr:`token` Token value * :attr:`application` Application instance * :attr:`access_token` AccessToken instance this refresh token is bounded to """ user = fields.ReferenceField(settings.MONGOENGINE_USER_DOCUMENT, dbref=True, blank=True) token = fields.StringField(max_length=255, db_index=True) application = fields.ReferenceField(oauth2_settings.APPLICATION_MODEL, dbref=True) access_token = fields.ReferenceField(AccessToken, dbref=True) def revoke(self): """ Delete this refresh token along with related access token """ AccessToken.objects.get(id=self.access_token.id).revoke() self.delete() def __str__(self): return self.token
class GlobalElement(Document): """ Storage model for a globally defined element within a schema document. Such an element can be used as the root element of a document. :property name str: the local name for the element :property namespace str: the namespace URI of the schema within which the element is defined :property version int: the version of the schema document that this :property schemaname str: the unique name given to the schema document where the element is defined element is defined in :property schema ref: a reference to the SchemaVersion record where this version of the element is defined :property annots ref: a reference to the GlobalElementAnnots that contains user annotations. """ name = fields.StringField( unique_with=["namespace", "schemaname", "version"]) namespace = fields.StringField() schemaname = fields.StringField() version = fields.IntField() schema = fields.ReferenceField(SchemaVersion) annots = fields.ReferenceField(GlobalElementAnnots) @property def qname(self): """ the type's qualified name of the form "{NS}LOCAL-NAME" """ return "{{{0}}}{1}".format(self.namespace, self.name) @classmethod def get_all_elements(cls): """ return all of the global elements from all the representative namespaces that aren't marked as hidden. :return dict: a dictionary that has namespaces as keys and GlobalElement records as values. """ currents = {} scs = SchemaCommon.objects.filter(current__gt=0) for sc in scs: sv = SchemaVersion.get_by_version(sc.name, sc.current) if sv: currents[sc.name] = sc.current elsbyns = {} for name in currents: glels = GlobalElement.objects.filter(schemaname=name) \ .filter(version=currents[name]) for el in glels: if el.annots.hide: continue if el.namespace not in elsbyns: elsbyns[el.namespace] = [] elsbyns[el.namespace].append(el) return elsbyns
class TemplateXslRendering(Document): """ TemplateXslRendering object """ template = fields.ReferenceField(Template, blank=False, reverse_delete_rule=CASCADE, unique=True) list_xslt = fields.ReferenceField(XslTransformation, reverse_delete_rule=NULLIFY, blank=True) detail_xslt = fields.ReferenceField(XslTransformation, reverse_delete_rule=NULLIFY, blank=True) @staticmethod def get_by_id(template_xsl_rendering_id): """ Get a TemplateXslRendering document by its id. Args: template_xsl_rendering_id: Id. Returns: TemplateXslRendering object. Raises: DoesNotExist: The TemplateXslRendering doesn't exist. ModelError: Internal error during the process. """ try: return TemplateXslRendering.objects.get(pk=str(template_xsl_rendering_id)) except mongoengine_errors.DoesNotExist as e: raise exceptions.DoesNotExist(str(e)) except Exception as ex: raise exceptions.ModelError(str(ex)) @staticmethod def get_by_template_id(template_id): """Get TemplateXslRendering by its template id. Args: template_id: Template id. Returns: The TemplateXslRendering instance. Raises: DoesNotExist: The TemplateXslRendering doesn't exist. ModelError: Internal error during the process. """ try: return TemplateXslRendering.objects.get(template=template_id) except mongoengine_errors.DoesNotExist as e: raise exceptions.DoesNotExist(str(e)) except Exception as e: raise exceptions.ModelError(str(e)) @staticmethod def get_all(): """Get all TemplateXslRendering. Returns: List of TemplateXslRendering. """ return TemplateXslRendering.objects.all()
class TemplateVersion(Document): """ Storage model for storing different versions of a template. :property name str: the template name being versioned :property version seq: the version for this template :property common ref: a reference to the common information record in the Template collection :property label str: the label to give to the root element :property spec ref: the TypeRenderSpec object to use to render the root element; if not set, this will be generated dynamically. :property tranforms ref: a reference to a record in Transforms that identifies the XSL stylesheets :property deleted bool: True if this version is currently deleted :property comment str: A brief (displayable) comment noting what is different about this version. """ name = fields.StringField(blank=False) version = fields.SequenceField(blank=False) common = fields.ReferenceField(TemplateCommon, blank=False) schema = fields.ReferenceField(SchemaVersion, blank=False) extschemas = fields.ListField(SchemaVersion, blank=True, default=[]) label = fields.StringField() spec = fields.ReferenceField(TypeRenderSpec, blank=True) # transforms = fields.ReferenceField(Transforms, blank=True) deleted = fields.BooleanField(blank=False, default=False) comment = fields.StringField(default="") @classmethod def get_all_by_name(cls, name, include_deleted=False): """ return a list of TemplateVersion records that have given name :param name str: the name of the template to match :param include_deleted bool: if False, return only records that have not been marked as deleted. """ vers = TemplateVersion.objects.filter(name=name) if not include_deleted: vers = vers.filter(deleted=False) return vers @classmethod def get_by_version(cls, name, version, include_deleted=False): """ return the TemplateVersion record with a given name and version. :param name str: the name of the schema to match :param version int: the version of the schema to select :param include_deleted bool: if False, return only records that have not been marked as deleted. """ vers = cls.get_all_by_name(name, include_deleted).filter(version=version) if len(vers) > 0: return vers[0] return None
class DataStructure(Document): """Stores data being entered and not yet curated""" user = fields.StringField() template = fields.ReferenceField(Template) name = fields.StringField(unique_with=['user', 'template']) data_structure_element_root = fields.ReferenceField(DataStructureElement, blank=True) meta = {'abstract': True} @staticmethod def get_by_id(data_structure_id): """ Returns the object with the given id Args: data_structure_id: Returns: Data Structure (obj): DataStructure object with the given id """ # FIXME: temporary solution to query concrete children of abstract class # (https://github.com/MongoEngine/mongoengine/issues/741) data_structure = None # iterate concrete data structure classes for subclass in DataStructure.__subclasses__(): try: # get data structure from concrete subclass data_structure = subclass.get_by_id(data_structure_id) # break if found break except: # data structure not found, continue search pass # data structure found if data_structure is not None: # return data structure return data_structure else: # raise exception raise exceptions.DoesNotExist("No data structure found for the given id.") @classmethod def pre_delete(cls, sender, document, **kwargs): """ Pre delete operations Returns: """ # Delete data structure elements document.delete_data_structure_elements_from_root() def delete_data_structure_elements_from_root(self): """ Delete all data structure elements from the root Returns: """ if self.data_structure_element_root is not None: delete_branch_task.apply_async((str(self.data_structure_element_root.id),))
class Setup(Document): user = fields.StringField(max_length=50) service = fields.StringField(max_length=50) cloud = fields.StringField(max_length=50) options = fields.EmbeddedDocumentField(Options, blank=True) playbook = fields.ReferenceField(AnsiblePlaybook, blank=True) key_id = fields.ReferenceField(Key, blank=True) status = fields.StringField()
class OaiHarvesterMetadataFormatSet(Document): """Association table between OaiHarvesterMetadataFormat and OaiHarvesterSet""" harvester_set = fields.ReferenceField(OaiHarvesterSet, reverse_delete_rule=CASCADE) harvester_metadata_format = fields.ReferenceField( OaiHarvesterMetadataFormat, reverse_delete_rule=CASCADE, unique_with="harvester_set", ) last_update = fields.DateTimeField(blank=True) @staticmethod def get_by_metadata_format_and_set(oai_harvester_metadata_format, oai_harvester_set): """Get an OaiHarvesterMetadataFormatSet by its OaiHarvesterMetadataFormat and OaiHarvesterSet. Args: oai_harvester_metadata_format: oai_harvester_set: Returns: OaiHarvesterMetadataFormatSet instance. """ try: return OaiHarvesterMetadataFormatSet.objects.get( harvester_metadata_format=oai_harvester_metadata_format, harvester_set=oai_harvester_set, ) except mongoengine_errors.DoesNotExist as e: raise exceptions.DoesNotExist(str(e)) except Exception as e: raise exceptions.ModelError(str(e)) @staticmethod def upsert_last_update_by_metadata_format_and_set( harvester_metadata_format, harvester_set, last_update): """Update the last_update date for a given metadata_format and set. Create an OaiHarvesterMetadataFormatSet if doesn't exist. Args: harvester_metadata_format: Metadata format. harvester_set: Set. last_update: Last update date. """ try: OaiHarvesterMetadataFormatSet.objects( harvester_metadata_format=harvester_metadata_format, harvester_set=harvester_set, ).update_one(last_update=last_update, upsert=True) except Exception as e: raise exceptions.ModelError(str(e))
class DataItem(Document): """ Data Item object """ # When data is deleted, all relative data item is deleted as well data = fields.ReferenceField(Data, blank=False, reverse_delete_rule=CASCADE) template = fields.ReferenceField(Template, blank=False) list_content = fields.ListField(fields.EmbeddedDocumentField(Item), default=[], blank=False) last_modification_date = fields.DateTimeField(blank=True, default=None) @staticmethod def get_by_data(data): """ Return a Data Item with the data given. Args: data: Returns: """ try: return DataItem.objects(data=data).get() except mongoengine_errors.DoesNotExist as e: raise exceptions.DoesNotExist(e.message) except Exception as ex: raise exceptions.ModelError(ex.message) @staticmethod def delete_from_data(data): """ Delete data items relative to the given data Args: data: Returns: """ DataItem.objects(data=data).delete() @staticmethod def execute_query_distinct_by_data(query): """Execute a query on the DataItem collection distinct by data. Args: query: Returns: """ return DataItem.objects(__raw__=query).distinct("data")
class Item(DynamicDocument): # CI 模型实例 name = fields.StringField(max_length=200, required=True) group = fields.ReferenceField('Group', required=True) category = fields.ReferenceField(ItemCategory, required=True) ctime = fields.DateTimeField(default=datetime.datetime.now()) utime = fields.DateTimeField(default=datetime.datetime.now()) meta = { "collection": "item", "indexes": [ "category", "ctime", ], }
class Template(dme_Document): """Represents an XML schema template that defines the structure of data for curation""" title = dme_fields.StringField() filename = dme_fields.StringField() content = dme_fields.StringField() templateVersion = dme_fields.StringField(blank=True) version = dme_fields.IntField(blank=True) hash = dme_fields.StringField() user = dme_fields.StringField(blank=True) dependencies = dme_fields.ListField(StringField(), blank=True) exporters = dme_fields.ListField(ReferenceField(Exporter, reverse_delete_rule=PULL), blank=True) XSLTFiles = dme_fields.ListField(ReferenceField(ExporterXslt, reverse_delete_rule=PULL), blank=True) ResultXsltList = dme_fields.ReferenceField(ResultXslt, reverse_delete_rule=NULLIFY, blank=True) ResultXsltDetailed = dme_fields.ReferenceField(ResultXslt, reverse_delete_rule=NULLIFY, blank=True)
class Group(document.Document): """Groups are a generic way of categorizing users to apply permissions, or some other label, to those users. A user can belong to any number of groups. A user in a group automatically has all the permissions granted to that group. For example, if the group Site editors has the permission can_edit_home_page, any user in that group will have that permission. Beyond permissions, groups are a convenient way to categorize users to apply some label, or extended functionality, to them. For example, you could create a group 'Special users', and you could write code that would do special things to those users -- such as giving them access to a members-only portion of your site, or sending them members-only e-mail messages. """ name = fields.StringField(max_length=80, unique=True, verbose_name=_('name')) permissions = fields.ListField( fields.ReferenceField(Permission, verbose_name=_('permissions'), required=False)) class Meta: verbose_name = _('group') verbose_name_plural = _('groups') def __unicode__(self): return self.name
class ExporterXsl(Exporter): """ Export XSL object """ xsl_transformation = fields.ReferenceField(XslTransformation, blank=False, reverse_delete_rule=CASCADE) @staticmethod def get_all(is_cls): """ Returns all XSL exporters Returns: XSL exporter collection """ if is_cls: # will return all Template object only return ExporterXsl.objects(_cls="{0}.{1}".format( Exporter.__name__, ExporterXsl.__name__)).all() else: # will return all inherited object return ExporterXsl.object().all() @staticmethod def get_all_by_xsl_id_list(xsl_id_list): """ Returns all Xsl exporter with the given id list Returns: """ return ExporterXsl.objects(xsl_transformation__in=xsl_id_list).all()
class TempChoiceListFile(Document): """ Model which associate a specific choice the list of files """ choice = fields.StringField() list_buckets = fields.ListField(fields.ReferenceField(TempBucketIdFiles, blank=True), blank=True) @staticmethod def get_all(): """Return all TempChoiceListFile. Returns: """ return TempChoiceListFile.objects().all() def delete_database(self): """ Delete function Delete each part of the choice and the associated files. """ for bucket in self.list_buckets: try: bucket_obj = TempBucketIdFiles.objects.get(id=bucket.id) bucket_obj.delete_database() except: pass self.delete()
class AutoKey(Document): """Auto Key keeps track of keys""" root = fields.ReferenceField(DataStructureElement, reverse_delete_rule=CASCADE, unique=True) keys = fields.DictField(default={}, blank=True) @staticmethod def get_by_root(root): """Return the object with the given root. Args: root: Returns: Data (obj): Data object with the given id """ try: return AutoKey.objects.get(root=root) except mongoengine_errors.DoesNotExist as e: raise exceptions.DoesNotExist(str(e)) except Exception as ex: raise exceptions.ModelError(str(ex))
class User(Document): name = fields.StringField() username = fields.StringField(unique=True) email = fields.EmailField(unique=True) # these 2 fields are NOT TO BE FILLED ratings = fields.IntField(default=0) date_joined = fields.DateTimeField(default=timezone.now, editable=False) gears = fields.ListField(fields.ReferenceField('Gear'), default=[], blank=True) password = fields.StringField(min_length=8, max_length=128) USERNAME_FIELD = 'username' REQUIRED_FIELDS = ['name', 'email', 'password'] def __str__(self): return self.username def get_username(self): return self.username def get_name(self): return self.name def get_ratings(self): return self.ratings def is_active(self): return True def check_password(self, raw_password): """ Checks the password if it matches the stored password """ return check_password(raw_password, self.password)
class SchemaElement(Document): tag = fields.StringField() value = fields.StringField(default=None, blank=True) options = fields.DictField(blank=True) children = fields.ListField(fields.ReferenceField('SchemaElement'), blank=True)
class FormData(dme_Document): """Stores data being entered and not yet curated""" user = dme_fields.StringField() template = dme_fields.StringField() name = dme_fields.StringField(unique_with=['user', 'template']) schema_element_root = dme_fields.ReferenceField(SchemaElement, blank=True) xml_data = dme_fields.StringField(default='') xml_data_id = dme_fields.StringField(blank=True)
class DataStructureTypeElement(Document): """Represents data structure type object""" path = fields.StringField(blank=True) data_structure = fields.ReferenceField(DataStructureElement, unique=True, reverse_delete_rule=CASCADE) @staticmethod def get_by_data_structure_id(data_structure_id): """ Returns the object with the given data_structure id. Args: data_structure_id: Returns: DataStructureTypeElement (obj): DataStructureTypeElement object with the given data_structure id. """ try: return DataStructureTypeElement.objects.get( data_structure=str(data_structure_id)) except mongoengine_errors.DoesNotExist as e: raise exceptions.DoesNotExist(e.message) except Exception as ex: raise exceptions.ModelError(ex.message) @staticmethod def get_by_id(data_structure_type_id): """ Returns the object with the given id. Args: data_structure_type_id: Returns: DataStructureTypeElement (obj): DataStructureTypeElement object with the given id. """ try: return DataStructureTypeElement.objects.get( pk=str(data_structure_type_id)) except mongoengine_errors.DoesNotExist as e: raise exceptions.DoesNotExist(e.message) except Exception as ex: raise exceptions.ModelError(ex.message) def save_object(self): """Custom save Returns: """ try: return self.save() except mongoengine_errors.NotUniqueError as e: raise exceptions.NotUniqueError(e.message) except Exception as ex: raise exceptions.ModelError(ex.message)
class Marker(Feature): ebrida_id = fields.StringField(max_length=255) kea_id = fields.StringField(max_length=255) species_obj = fields.ReferenceField(Species) species = fields.StringField(max_length=255, default='species_unkown') primer_obj = fields.ReferenceField(Primer) primer = fields.StringField(max_length=255, default='pr_unkown') ordered_fields = [ "id", "name", "createdby", "species", "createddate", "primer", "Primer_sequence" ] def __unicode__(self): return self.GetName() def GetName(self): return self.name
class Bucket(Document): """Bucket class to store types by domain.""" label = fields.StringField(unique=True) color = fields.StringField(unique=True) types = fields.ListField(fields.ReferenceField(TypeVersionManager), blank=True) @staticmethod def get_by_id(bucket_id): """Return a bucket given its id. Args: bucket_id: Returns: """ try: return Bucket.objects.get(pk=str(bucket_id)) except mongoengine_errors.DoesNotExist as e: raise exceptions.DoesNotExist(str(e)) except Exception as ex: raise exceptions.ModelError(str(ex)) @staticmethod def get_all(): """Return all buckets. Returns: """ return Bucket.objects().all() @staticmethod def get_colors(): """Return all colors. Returns: """ return Bucket.objects.values_list("color") def save_object(self): """Custom save Returns: """ try: return self.save() except mongoengine_errors.NotUniqueError as e: raise exceptions.NotUniqueError(str(e)) except Exception as ex: raise exceptions.ModelError(str(ex))
class Term(Document): name = fields.StringField(max_length=2048) definition = fields.StringField(max_length=2048, null=True, default='') group = fields.StringField(max_length=255, null=True, default='None') datasource = fields.ReferenceField(DataSource) def Validate(self, data): pass def __unicode__(self): return self.name
class Gear(Document): name = fields.StringField() gear_type = fields.StringField() price = fields.FloatField() owner = fields.ReferenceField(User, reverse_delete_rule=CASCADE) USERNAME_FIELD = 'name' REQUIRED_FIELDS = ['name', 'gear_type', 'price'] def __str__(self): return self.name
class OaiProviderSet(OaiSet): """Represents a set for Oai-Pmh Provider""" templates_manager = fields.ListField(fields.ReferenceField( TemplateVersionManager, reverse_delete_rule=PULL), unique_with='set_spec') description = fields.StringField(blank=True) @staticmethod def get_all(order_by_field=None): """ Return all OaiProviderSet. Args: order_by_field: Order by field. Returns: List of OaiProviderSet. """ return OaiProviderSet.objects().order_by(order_by_field) @staticmethod def get_all_by_templates_manager(templates_manager): """ Get all OaiProviderSet used by a list of templates_manager Args: templates_manager: List of templates manager Returns: List of OaiProviderSet. """ return OaiProviderSet.objects( templates_manager__in=templates_manager).all() @staticmethod def get_by_set_spec(set_spec): """ Get an OaiProviderSet by its set_spec. Args: set_spec: OaiProviderSet set_spec. Returns: The OaiProviderSet instance. Raises: DoesNotExist: The set doesn't exist ModelError: Internal error during the process """ try: return OaiProviderSet.objects().get(set_spec=set_spec) except mongoengine_errors.DoesNotExist as e: raise exceptions.DoesNotExist(e.message) except Exception as e: raise exceptions.ModelError(e.message)
class Book(Document): id = fields.StringField(primary_key=True, default=ObjectId) name = fields.StringField(max_length=300) slug = fields.StringField() pages = fields.IntField() authors = fields.ListField(fields.ReferenceField(Author)) pubdate = fields.DateTimeField() _meta = {"ordering": ["-pubdate"]} def __unicode__(self): return self.name or ""
class ElasticsearchTemplate(Document): """ElasticsearchTemplate object""" template = fields.ReferenceField(Template, blank=False, reverse_delete_rule=CASCADE, unique=True) title_path = fields.StringField(default=None) description_paths = fields.ListField(blank=False, validation=validate_xpath_list, default=[]) @staticmethod def get_by_id(es_template_id): """Returns the object with a given id Args: es_template_id: Returns: ElasticsearchTemplate (obj): ElasticsearchTemplate """ try: return ElasticsearchTemplate.objects.get(pk=es_template_id) except mongoengine_errors.DoesNotExist as e: raise exceptions.DoesNotExist(str(e)) except Exception as e: raise exceptions.ModelError(str(e)) @staticmethod def get_by_template(template): """Returns the object with a given template Args: template: Returns: ElasticsearchTemplate (obj): ElasticsearchTemplate """ try: return ElasticsearchTemplate.objects.get(template=template) except mongoengine_errors.DoesNotExist as e: raise exceptions.DoesNotExist(str(e)) except Exception as e: raise exceptions.ModelError(str(e)) @staticmethod def get_all(): """Returns all objects""" return ElasticsearchTemplate.objects.all()
class StorageGroup(DynamicDocument): # storage 分组 group = fields.ReferenceField('self', blank=True, null=True) name = fields.StringField(max_length=200, required=True) ctime = fields.DateTimeField(default=datetime.datetime.now()) utime = fields.DateTimeField(default=datetime.datetime.now()) meta = { "collection": "storagegroup", "indexes": [ "name", "ctime", ], }
class Grant(Document): """ A Grant instance represents a token with a short lifetime that can be swapped for an access token, as described in :rfc:`4.1.2` Fields: * :attr:`user` The Django user who requested the grant * :attr:`code` The authorization code generated by the authorization server * :attr:`application` Application instance this grant was asked for * :attr:`expires` Expire time in seconds, defaults to :data:`settings.AUTHORIZATION_CODE_EXPIRE_SECONDS` * :attr:`redirect_uri` Self explained * :attr:`scope` Required scopes, optional """ user = fields.ReferenceField(settings.MONGOENGINE_USER_DOCUMENT, dbref=True) code = fields.StringField(max_length=255, db_index=True) # code comes from oauthlib application = fields.ReferenceField(oauth2_settings.APPLICATION_MODEL, dbref=True) expires = fields.DateTimeField() redirect_uri = fields.StringField(max_length=255) scope = fields.StringField(blank=True) def is_expired(self): """ Check token expiration with timezone awareness """ if not self.expires: return True return timezone.now() >= self.expires def redirect_uri_allowed(self, uri): return uri == self.redirect_uri def __str__(self): return self.code
class NodeRenderSpec(Document): """ Storage model that specifying how to render an XML element or node which combines a label with a TypeRenderSpec :property label str: the label to associate with the rendering of the type. :property spec ref: the TypeRenderSpec to use for rendering the node's type; if not set, this will be generated dynamically. """ label = fields.StringField() spec = fields.ReferenceField(TypeRenderSpec)
class OaiMetadataFormat(dme_Document): """ A OaiMetadataFormat object """ metadataPrefix = dme_fields.StringField() schema = dme_fields.StringField() xmlSchema = dme_fields.StringField(blank=True) metadataNamespace = dme_fields.StringField() raw = dme_fields.DictField() template = dme_fields.ReferenceField(Template, reverse_delete_rule=PULL, blank=True) registry = dme_fields.StringField(blank=True) hash = dme_fields.StringField(blank=True) harvest = dme_fields.BooleanField(blank=True) lastUpdate = dme_fields.DateTimeField(blank=True)
class AbstractQuery(Document): """Abstract Query""" user_id = fields.StringField(blank=False) content = fields.StringField(blank=True) templates = fields.ListField( fields.ReferenceField(Template, blank=True), blank=True, default=[] ) data_sources = fields.ListField( fields.EmbeddedDocumentField(DataSource, blank=True), blank=True, default=[] ) meta = { "abstract": True, }