class User(Document): class __mongometa__: session = connection.connection name = 'users' _id = Field(schema.ObjectId) title = Field(schema.String) text = Field(schema.String) name = Field(schema.String) projects = Field(schema.Array(schema.String)) education = Field(schema.Array(schema.String)) skills = Field(schema.Array(schema.String)) learnings = Field(schema.Array(schema.String)) commits = Field(schema.Int) stack = Field( schema.Object(fields={ 'score': schema.Int, 'badges': schema.Int })) githubname = Field(schema.String) stackurl = Field(schema.String) linkedinurl = Field(schema.String) githuburl = Field(schema.String) email = Field(schema.String) avatar = Field(schema.String)
class ClassificationSample(MappedClass, JsonOdmHelper): """Python classification sample (training and/or predicted) in MongoDB.""" class __mongometa__: session = session name = 'classification_sample' indexes = [('model',), ('sharedId',)] unique_indexes = [('model', 'seqHash')] _id = FieldProperty(schema.ObjectId) model = FieldProperty(schema.String) seq = FieldProperty(schema.String) sharedId = FieldProperty(schema.String) seqHash = FieldProperty(schema.String) training_labels = FieldProperty( schema.Array(schema.Object(fields={'topic': schema.String}))) # Keep separate to control which samples should # be used even if more have training_labels. use_for_training = FieldProperty(schema.Bool) predicted_labels = FieldProperty( schema.Array( schema.Object(fields={ 'topic': schema.String, 'quality': schema.Float }))) update_timestamp = FieldProperty(datetime, if_missing=datetime.utcnow)
class Qualifier(TermInTree, MeshTerm): class __mongometa__: polymorphic_identity = "q" annotation = FieldProperty(schema.String) tree_nodes_allowed = FieldProperty(schema.Array(schema.String)) tree_numbers = FieldProperty(schema.Array(schema.String)) # used for querying tree ex. db.descriptor.find({parents:"A02"}) parents = FieldProperty(schema.Array(schema.String), index=True) _type = FieldProperty(schema.String(if_missing="q")) def __init__(self, *args, **kwargs): record = kwargs.get("record", None) super(Qualifier, self).__init__(*args, **kwargs) if record is not None: if "TreeNodeAllowedList" in record: if isinstance(record["TreeNodeAllowedList"]["TreeNodeAllowed"], list): self.tree_nodes_allowed = record["TreeNodeAllowedList"][ "TreeNodeAllowed"] else: self.tree_nodes_allowed = [ record["TreeNodeAllowedList"]["TreeNodeAllowed"] ] if "Annotation" in record: self.annotation = record["Annotation"]
class Character(MappedClass): class __mongometa__: session = DBSession name = 'character' def __init__(self, name, characClass, race, armor_class, attrs, attrsMods): self.name = name self.charClass = characClass self.race = race self.armorClass = armor_class self.attributes = attrs self.attributeModifiers = attrsMods def convertToForm(self): attrs = { 'id': self._id, 'name': self.name, 'character_class': self.charClass, 'race': self.race, 'armor_class': self.armorClass, 'strength_val': str(self.attributes[Attributes.Strength.value]), 'strength_modifier': str(self.attributeModifiers[Attributes.Strength.value]), 'dexterity_val': str(self.attributes[Attributes.Dexterity.value]), 'dexterity_modifier': str(self.attributeModifiers[Attributes.Dexterity.value]), 'const_val': str(self.attributes[Attributes.Constitution.value]), 'const_modifier': str(self.attributeModifiers[Attributes.Constitution.value]), 'intell_val': str(self.attributes[Attributes.Intellect.value]), 'intell_modifier': str(self.attributeModifiers[Attributes.Intellect.value]), 'wisdom_val': str(self.attributes[Attributes.Wisdom.value]), 'wisdom_modifier': str(self.attributeModifiers[Attributes.Wisdom.value]), 'charisma_val': str(self.attributes[Attributes.Charisma.value]), 'charisma_modifier': str(self.attributeModifiers[Attributes.Charisma.value]) } return attrs _id = FieldProperty(schema.ObjectId) name = FieldProperty(schema.String(required=True)) charClass = FieldProperty(schema.String) race = FieldProperty(schema.String) armorClass = FieldProperty(schema.Int) attributes = FieldProperty(schema.Array(int)) attributeModifiers = FieldProperty(schema.Array(int))
class users(UserMixin, Document): class __mongometa__: session = session name = 'user' _id = Field(schema.ObjectId) username = Field(str) email = Field(str) password = Field(str) favouriteRecipes = Field(schema.Array(str)) myRecipes = Field(schema.Array(str)) likedRecipes = Field(schema.Array(str))
class Project(Document): class __mongometa__: session = connection.connection name = 'projects' _id = Field(schema.ObjectId) title = Field(schema.String) platforms = Field(schema.Array(schema.String)) description = Field(schema.String) screenshot = Field(schema.String) technologies = Field(schema.Array(schema.String))
class WikiPageWithMetadata(MappedClass): class __mongometa__: session = session name = 'wiki_page_with_metadata' _id = FieldProperty(schema.ObjectId) title = FieldProperty(schema.String(required=True)) text = FieldProperty(schema.String(if_missing='')) metadata = FieldProperty(schema.Object({ 'tags': schema.Array(schema.String), 'categories': schema.Array(schema.String) }))
class SupplementaryConceptRecord(TermPreviouslyIndexed, DatedDocument, MeshTerm): frequency = FieldProperty(schema.Int) note = FieldProperty(schema.String) mapped_to_headings = FieldProperty(schema.Array(schema.String)) sources = FieldProperty(schema.Array(schema.String)) previous_indexings = FieldProperty(schema.Array(schema.String)) _type = FieldProperty(schema.String(if_missing="c")) def __init__(self, *args, **kwargs): record = kwargs.get("record", None) super(SupplementaryConceptRecord, self).__init__(*args, **kwargs) if record is not None: self.frequency = int( record["Frequency"]) if "Frequency" in record else None self.note = record["Note"] if "Note" in record else None if "HeadingMappedToList" in record: heading_mapped_to_list = record["HeadingMappedToList"][ "HeadingMappedTo"] if isinstance(heading_mapped_to_list, list): for ref in heading_mapped_to_list: if "DescriptorReferredTo" in ref: self.mapped_to_headings.append( ref["DescriptorReferredTo"] ["DescriptorUI"].replace("*", "")) elif "QualifierReferredTo" in ref: self.mapped_to_headings.append( ref["QualifierReferredTo"] ["QualifierUI"].replace("*", "")) else: if "DescriptorReferredTo" in heading_mapped_to_list: self.mapped_to_headings.append( heading_mapped_to_list["DescriptorReferredTo"] ["DescriptorUI"].replace("*", "")) if "QualifierReferredTo" in heading_mapped_to_list: self.mapped_to_headings.append( heading_mapped_to_list["QualifierReferredTo"] ["QualifierUI"].replace("*", "")) if "SourceList" in record: if isinstance(record["SourceList"]["Source"], list): self.sources = record["SourceList"]["Source"] else: self.sources = [record["SourceList"]["Source"]]
def test_get_values_casting(self): val = self.provider._cast_value_for_type(S.Int, None) assert val is None val = self.provider._cast_value_for_type(S.Array(S.Int), '1234') assert val == [1234], val val = self.provider._cast_value_for_type(S.Object({'num': S.Int}), {'num': '1234'}) assert val == {'num': 1234}, val val = self.provider._cast_value_for_type( S.Array(S.Object(dict(value=s.Int))), [{ 'value': '1234' }]) assert val == [{'value': 1234}], val
class recipes(Document): class __mongometa__: session = session name = 'recipe' _id = Field(schema.ObjectId) recipeName = Field(schema.String) recipeAuthor = Field(schema.String) recipeCuisine = Field(schema.String) recipeCountryOfOrigin = Field(schema.String) recipeMealTime = Field(schema.String) recipeServings = Field(schema.String) recipeDifficulty = Field(schema.String) recipePreparationTime = Field(int) recipeCookingTime = Field(int) recipeAllergen = Field(schema.Array(str)) recipeMainIngredient = Field(schema.String) recipeIngredients = Field(schema.String) recipeInstructions = Field(schema.String) recipeDietary= Field(schema.Array(str)) recipeUpvotes = Field(int) recipeImageLink = Field(schema.String) recipeEmail = Field(schema.String)
class WikiPage(MappedClass): class __mongometa__: session = session name = 'wiki_page' version_of = OldWikiPageCollection @staticmethod def migrate(data): result = dict(data, metadata={'tags': data['tags']}, _version=1) del result['tags'] return result _id = FieldProperty(schema.ObjectId) title = FieldProperty(schema.String(required=True)) text = FieldProperty(schema.String(if_missing='')) _version = FieldProperty(1, required=True) metadata = FieldProperty(schema.Object({ 'tags': schema.Array(schema.String), 'categories': schema.Array(schema.String) }))
class TGMMUser(SproxTestClass): class __mongometa__: name = 'tg_mm_users' _id = FieldProperty(S.ObjectId) user_name = FieldProperty(S.String) _groups = FieldProperty(S.Array(str)) def _get_groups(self): return Group.query.find(dict(group_name={'$in': self._groups})).all() def _set_groups(self, groups): self._groups = [group.group_name for group in groups] groups = ProgrammaticRelationProperty(Group, _get_groups, _set_groups)
class InformationValueResult(MappedClass): def __init__(self, iv_words, sum_threshold=config.SUM_THRESHOLD, *args, **kwargs): if type(iv_words) is dict: iv_words = list(iv_words.iteritems()) self.sum_threshold = sum_threshold super(InformationValueResult, self).__init__(*args, iv_words=iv_words, **kwargs) @property def iv_sum(self): # Todo: improve performance of this... sorted_ivs = sorted(map(operator.itemgetter(1), self.iv_words), reverse=True) self.max_iv = sorted_ivs[0] amount_to_be_taken = int(len(sorted_ivs) * self.sum_threshold) or 10 sorted_ivs = sorted_ivs[:amount_to_be_taken] # Sum the reverse of sorted_words to improve numerical stability return reduce(lambda x, y: x + y, reversed(sorted_ivs), 0) class __mongometa__: session = odm_session name = 'information_value_result' unique_indexes = [ ('doc_window_hash', ), ] extensions = [DocumentWindowSizeDuplicateHash] def __repr__(self): return "IVR(%s window size, %s iv-words)" % (self.window_size, len(self.iv_words)) def __str__(self): return self.__repr__() _id = FieldProperty(schema.ObjectId) doc_window_hash = FieldProperty(schema.String) window_size = FieldProperty(schema.Int) iv_words = FieldProperty(schema.Array(schema.Anything)) # Array or list document_id = ForeignIdProperty('Document') document = RelationProperty('Document')
'categories': schema.Array(schema.String) })) #} WikiPageWithoutMigration = WikiPage #{migrate-oldschema from ming import collection, Field OldWikiPageCollection = collection('wiki_page', session, Field('_id', schema.ObjectId), Field('title', schema.String), Field('text', schema.String), Field('tags', schema.Array(schema.String))) #} WikiPage.query.remove({}) #{migrate-model-with-migration class WikiPage(MappedClass): class __mongometa__: session = session name = 'wiki_page' version_of = OldWikiPageCollection @staticmethod def migrate(data): result = dict(data, metadata={'tags': data['tags']}, _version=1)
def __init__(self, referenced_class): self.referenced_class = referenced_class super(StringForeignKeyListProperty, self).__init__(schema.Array(str, required=True))
class DatasetDAO(MappedClass): class __mongometa__: session = session name = 'dataset' _id = FieldProperty(schema.ObjectId) url_prefix = FieldProperty(schema.String) title = FieldProperty(schema.String) description = FieldProperty(schema.String) reference = FieldProperty(schema.String) creation_date = FieldProperty(schema.datetime) modification_date = FieldProperty(schema.datetime) size = FieldProperty(schema.Int) tags = FieldProperty(schema.Array(schema.Anything)) fork_count = FieldProperty(schema.Int) forked_from_id = ForeignIdProperty('DatasetDAO') @property def comments(self): return GIterator(self.get_comments()) @property def elements(self): return GIterator(self.get_elements()) @property def forked_from(self): if self.forked_from_id is None: return None return DatasetDAO.query.get(_id=self.forked_from_id) @forked_from.setter def forked_from(self, dataset): if dataset is not None: self.forked_from_id = dataset._id def __init__(self, url_prefix, title, description, reference, tags=None, creation_date=now(), modification_date=now(), fork_count=0, forked_from=None, forked_from_id=None): with lock: if DatasetDAO.query.get(url_prefix=url_prefix) is not None: raise Exception("Url prefix already taken.") if forked_from_id is None and forked_from is not None: forked_from_id = forked_from._id size = 0 kwargs = { k: v for k, v in locals().items() if k not in ["self", "__class__", "forked_from"] } super().__init__(**kwargs) @classmethod def from_dict(cls, init_dict): return cls(**init_dict) def add_comment(self, author_name, author_link, content, addition_date=now()): return DatasetCommentDAO(author_name, author_link, content, addition_date, dataset=self) def add_element(self, title, description, file_ref_id, http_ref=None, tags=None, addition_date=now(), modification_date=now()): return DatasetElementDAO(title, description, file_ref_id, http_ref, tags, addition_date, modification_date, dataset=self) def update(self): return session.refresh(self) def serialize(self): fields = [ "title", "description", "reference", "creation_date", "modification_date", "url_prefix", "fork_count", "size" ] response = {f: str(self[f]) for f in fields} response['comments_count'] = len(self.comments) response['elements_count'] = len(self.elements) response['tags'] = list(self.tags) if self.forked_from is not None: response['fork_father'] = self.forked_from.url_prefix else: response['fork_father'] = None return response def has_element(self, element): return DatasetElementDAO.query.get(_id=element._id, dataset_id=self._id) is not None def get_elements(self, options=None): query = options if query is None: query = {} query['dataset_id'] = {'$in': [self._id]} return DatasetElementDAO.query.find(query).sort("addition_date", 1) def get_comments(self, options=None): query = options if query is None: query = {} query['dataset_id'] = self._id return DatasetCommentDAO.query.find(query).sort("addition_date", 1) def delete(self): DatasetCommentDAO.query.remove({'dataset_id': self._id}) DatasetElementDAO.query.remove({'dataset_id.0': self._id}) DatasetElementCommentDAO.query.remove( {'element_id': { '$in': [e._id for e in self.elements] }}) # Now those elements that were linked to this dataset (but not owned by the dataset) must be unlinked elements = DatasetElementDAO.query.find({'dataset_id': self._id}) for element in elements: element.unlink_dataset(self) DatasetDAO.query.remove({'_id': self._id})
class DatasetElementDAO(MappedClass): class __mongometa__: session = session name = 'element' _id = FieldProperty(schema.ObjectId) title = FieldProperty(schema.String) description = FieldProperty(schema.String) file_ref_id = ForeignIdProperty('FileDAO') http_ref = FieldProperty(schema.String) tags = FieldProperty(schema.Array(schema.Anything)) addition_date = FieldProperty(schema.datetime) modification_date = FieldProperty(schema.datetime) dataset_id = ForeignIdProperty('DatasetDAO', uselist=True) @property def comments(self): return GIterator(self.get_comments()) def __init__(self, title, description, file_ref_id, http_ref=None, tags=None, addition_date=None, modification_date=None, dataset_id=None, dataset=None): if addition_date is None: addition_date = now() if modification_date is None: modification_date = now() if dataset_id is None and dataset is not None: dataset_id = [dataset._id] kwargs = { k: v for k, v in locals().items() if k not in ["self", "__class__", "datasets"] } super().__init__(**kwargs) def get_comments(self, options=None): query = options if query is None: query = {} query['element_id'] = self._id return DatasetElementCommentDAO.query.find(query).sort( "addition_date", 1) @classmethod def from_dict(cls, init_dict): return cls(**init_dict) def unlink_dataset(self, dataset): return self.unlink_datasets([dataset]) def unlink_datasets(self, datasets): if len(datasets) > 0 and isinstance(datasets[0], ObjectId): datasets_translated = datasets else: datasets_translated = [d._id for d in datasets] self.dataset_id = [ d for d in self.dataset_id if d not in datasets_translated ] return self def link_dataset(self, dataset): return self.link_datasets([dataset]) def link_datasets(self, datasets): if len(datasets) > 0 and isinstance(datasets[0], ObjectId): datasets_translated = datasets else: datasets_translated = [d._id for d in datasets] self.dataset_id += datasets_translated return self def add_comment(self, author_name, author_link, content, addition_date=now()): return DatasetElementCommentDAO(author_name, author_link, content, addition_date, element=self) def update(self): return session.refresh(self) def serialize(self): fields = [ "title", "description", "_id", "addition_date", "modification_date", "http_ref" ] response = {f: str(self[f]) for f in fields} response['comments_count'] = len(self.comments) response['has_content'] = self.file_ref_id is not None response['tags'] = [t for t in self.tags] return response def clone(self, dataset_id=None): if dataset_id is None: dataset_id = self._id return DatasetElementDAO(title=self.title, description=self.description, file_ref_id=self.file_ref_id, http_ref=self.http_ref, tags=self.tags, addition_date=self.addition_date, modification_date=self.modification_date, dataset_id=[dataset_id]) def delete(self, owner_id: ObjectId = None): try: if owner_id is None: owner_id = self.dataset_id[0] if self.dataset_id.index(owner_id) > 0: self.dataset_id = [d for d in self.dataset_id if d != owner_id] else: for dataset_id in self.dataset_id[1:]: self.clone(dataset_id) DatasetElementCommentDAO.query.remove({'element_id': self._id}) DatasetElementDAO.query.remove({'_id': self._id}) except Exception as ex: DatasetElementCommentDAO.query.remove({'element_id': self._id}) DatasetElementDAO.query.remove({'_id': self._id})
class MeshTerm(MappedClass): class __mongometa__: session = session name = "mesh" polymorphic_on = "_type" polymorphic_identity = "base" _id = FieldProperty(schema.String) uid = FieldProperty(schema.String, index=True) name = FieldProperty(schema.String, index=True) concepts = FieldProperty( schema.Array( schema.Object({ "cuid": schema.String, "preferred": schema.Bool }))) active_mesh_years = FieldProperty(schema.Array(schema.String)) date_created = FieldProperty(schema.DateTime) date_established = FieldProperty(schema.DateTime) date_revised = FieldProperty(schema.DateTime) history_note = FieldProperty(schema.String) online_note = FieldProperty(schema.String) public_mesh_note = FieldProperty(schema.String) # not yet implemented record_originators = FieldProperty(schema.Array(schema.Object({}))) _type = FieldProperty(schema.String(if_missing="base")) def __init__(self, *args, **kwargs): record = kwargs.pop("record", None) super(MeshTerm, self).__init__() if record is not None: if type(self).__name__ == "Descriptor": self._id = record["DescriptorUI"] self.uid = self._id self.name = record["DescriptorName"]["String"] elif type(self).__name__ == "Qualifier": self._id = record["QualifierUI"] self.uid = self._id self.name = record["QualifierName"]["String"] elif type(self).__name__ == "SupplementaryConceptRecord": self._id = record["SupplementalRecordUI"] self.uid = self._id self.name = record["SupplementalRecordName"]["String"] if "ConceptList" in record: if isinstance(record["ConceptList"]["Concept"], list): self.concepts = [{ "cuid": c["ConceptUI"], "preferred": (c["@PreferredConceptYN"] == "Y") } for c in record["ConceptList"]["Concept"]] else: self.concepts = [{ "cuid": record["ConceptList"]["Concept"]["ConceptUI"], "preferred": (record["ConceptList"]["Concept"] ["@PreferredConceptYN"] == "Y") }] if "ActiveMeSHYearList" in record: if isinstance(record["ActiveMeSHYearList"]["Year"], list): self.active_mesh_years = record["ActiveMeSHYearList"][ "Year"] else: self.active_mesh_years = [ record["ActiveMeSHYearList"]["Year"] ] if "DateCreated" in record: self.date_created = datetime.strptime( " ".join([ record["DateCreated"]["Year"], record["DateCreated"]["Month"], record["DateCreated"]["Day"] ]), "%Y %m %d") if "DateEstablished" in record: self.date_establisted = datetime.strptime( " ".join([ record["DateEstablished"]["Year"], record["DateEstablished"]["Month"], record["DateEstablished"]["Day"] ]), "%Y %m %d") if "DateRevised" in record: self.date_revised = datetime.strptime( " ".join([ record["DateRevised"]["Year"], record["DateRevised"]["Month"], record["DateRevised"]["Day"] ]), "%Y %m %d") if "HistoryNote" in record: self.history_note = record["HistoryNote"] if "OnlineNote" in record: self.online_note = record["OnlineNote"] if "PublicMeSHNote" in record: self.public_mesh_note = record["PublicMeSHNote"]
class Project(Document): class __mongometa__: session = doc_session name = 'projects' indexes = [ ('shortname', ), ('source', ), ('sf_id', ), ] unique_indexes = [ ('shortname', 'source'), ] _review = dict(rating=int, useful=float, useless=float, approved=bool, user=str, comments=str, safe_html=bool, source=str, usefulness=float, date=datetime) _screenshot = dict(url=str, thumb=str, name=str, description=str) _category = dict(id=int, shortname=str, name=str, description=str, fullpath=str) _resource = dict(url=str, name=str, feed=str, item_count=int, item_open_count=int) _person = dict(username=None, homepage=None, name=None) _id = Field(S.ObjectId) shortname = Field(str) source = Field(str) sf_id = Field(int) projecttype = Field(int) private = Field(S.Bool(if_missing=False)) name = Field(str) summary = Field(StringNotNone) created = Field(datetime) description = Field(StringNotNone) doap = Field(str) project_url = Field(str) homepage = Field(str) updated = Field(S.Deprecated) _last_changed = Field('last_changed', datetime) ad_keywords = Field( [[str]]) # ['ohl', 'ad20848'] would translate to "ohl=ad20848;" in JS download_info = Field(S.Deprecated) _icon_url = Field('icon_url', S.String(if_missing=S.Missing)) # for backward compat. _features = Field('features', S.Array(str, if_missing=S.Missing)) # for backward compat. reviews_disabled = Field(bool) relations_data = Field( S.Object(dict(is_admin=S.Deprecated, rating=float, code=int, review_count=int, features=[str], tags=[dict(count=int, tag=str, approved=bool)], icon_url=str, latest_reviews=[_review], name=str, reviews=[_review], text=str), if_missing=None)) related = Field([ dict(source=None, shortname=None, name=str, description=str, screenshots=[_screenshot], ok_to_recommend=bool, rating=float, review_count=int, icon_url=str) ]) recommended = Field( [dict(source=None, shortname=None, name=str, description=str)]) screenshots = Field([_screenshot]) resources = Field( dict( other=[_resource], mailing_lists=[_resource], news=[_resource], forums=[_resource], trackers=[_resource], )) feed_recent_items = Field([ dict( _id=str, description=str, title=str, url=str, project=dict(source=str, shortname=str), date=datetime, type=str, description_type=str, author_username=str, permission_required=S.Array( str, if_missing=None ), # controllers/project.py queries mongodb for None which doens't match [] ) ]) categories = Field({ 'Topic': [_category], 'Operating System': [_category], 'Development Status': [_category], 'License': [_category], 'Translations': [_category], 'Intended Audience': [_category], 'User Interface': [_category], 'Programming Language': [_category], 'Database Environment': [_category], }) feeds_last_item = Field( S.Migrate(None, [dict(url=str, date=datetime)], S.Migrate.obj_to_list('url', 'date'))) inactive = Field(datetime) new_project_url = Field(str) donation_page = Field(str) preferred_support = Field(str) code_repositories = Field([ dict(label=str, browse=str, write_operations=int, read_operations=int, location=str, type=str) ]) releases = Field( S.Array( dict( filename=str, url=str, date=datetime, bytes=float, file_type=S.String(if_missing=''), mime_type=str, md5sum=str, sf_download_label=str, sf_platform_default=[str], sf_release_notes_file=str, sf_file_id=int, # FRS data (pre-PFS) sf_release_id=int, sf_package_id=int, sf_type=str, sf_platform=[str], release_notes_url=str, # old FRS data (shouldn't exist any more) # download_count=S.Deprecated, # group=S.Deprecated, #str, # version=S.Deprecated, #str, # changelog=S.Deprecated, #str, # release_notes=S.Deprecated, #str, ), validate_ranges=(slice(0, 5), slice(-5, -1)))) download_page = Field(str) screenshot_page = Field(str) maintainers = Field([_person]) developers = Field([_person]) file_feed = Field(str) awards = Field([dict(category=str, url=str, event=str, img_url=str)]) sf_piwik_siteid = Field(str) license = Field(S.Deprecated()) license_uri = Field(str) license_title = Field(str) developer_page = Field(str) test_foo2 = Field(S.Deprecated) fossforus_id = Field(S.Deprecated) fossforus_screenshots = Field(S.Deprecated) fossforus_features = Field(S.Deprecated) fossforus_tags = Field(S.Deprecated) fossforus_ratings = Field(S.Deprecated) _last_snapshot_id = Field(S.Deprecated)
class Descriptor(TermInTree, TermPreviouslyIndexed, MeshTerm): """Model for a MeSH Descriptor record""" class __mongometa__: polymorphic_identity = "d" # reference uid from qualifier allowable_qualifiers = FieldProperty(schema.Array(schema.String)) # reference uid from descriptor pharmacological_actions = FieldProperty(schema.Array(schema.String)) tree_numbers = FieldProperty(schema.Array(schema.String)) # used for querying tree ex. db.descriptor.find({parents:"A02"}) parents = FieldProperty(schema.Array(schema.String), index=True) previous_indexings = FieldProperty(schema.Array(schema.String)) _type = FieldProperty(schema.String(if_missing="d")) def __init__(self, *args, **kwargs): """Constructor for the MeSH Descriptor Record. Takes OrderedDict object from xmltodict output. Original source is xml download of descXXXX renamed and compressed to descXXXX.xml.gz""" record = kwargs.get("record", None) super(Descriptor, self).__init__(*args, **kwargs) if record is not None: if "AllowableQualifiersList" in record: if "qualifiers" is not None: if isinstance( record["AllowableQualifiersList"] ["AllowableQualifier"], list): self.allowable_qualifiers = [ q["QualifierReferredTo"]["QualifierUI"] for q in record["AllowableQualifiersList"] ["AllowableQualifier"] ] else: self.allowable_qualifiers = [ record["AllowableQualifiersList"] ["AllowableQualifier"]["QualifierReferredTo"] ["QualifierUI"] ] if "PharmacologicalActionList" in record: if isinstance( record["PharmacologicalActionList"] ["PharmacologicalAction"], list): self.pharmacological_actions = [ pa["DescriptorReferredTo"]["DescriptorUI"] for pa in record["PharmacologicalActionList"] ["PharmacologicalAction"] ] else: self.pharmacological_actions = [ record["PharmacologicalActionList"] ["PharmacologicalAction"]["DescriptorReferredTo"] ["DescriptorUI"] ]
def test_validate_limited_range(self): si = S.Array(int, validate_ranges=[slice(0, 2)]) si.validate([1, 2, 'foo', 'bar']) self.assertRaises(S.Invalid, si.validate, [1, 'foo', 'bar'])
class Action(MappedClass): class __mongometa__: session = model.DBSession name = 'activity_stream_action' indexes = [ (('actor_id', ), ('timestamp', DESCENDING)), ('actor_type', ), (('_recipients.recipient_id', ), ('timestamp', DESCENDING)), ('_recipients.recipient_type', ), ] _id = FieldProperty(s.ObjectId) actor_type = FieldProperty(s.String) actor_id = FieldProperty(s.ObjectId) verb = FieldProperty(s.String) description = FieldProperty(s.String) extra = FieldProperty(s.String) target_type = FieldProperty(s.String) target_id = FieldProperty(s.ObjectId) action_obj_type = FieldProperty(s.String) action_obj_id = FieldProperty(s.ObjectId) timestamp = FieldProperty(s.DateTime, if_missing=datetime.utcnow) _recipients = FieldProperty( s.Array(s.Object(fields={ '_type': s.String, '_id': s.ObjectId }))) @property def timestamp_24_hh_mm(self): return datetime.strftime(self.timestamp, '%X') @cached_property def actor(self, default=None): if not (self.actor_type and self.actor_id): return default entity = get_obj(self.actor_type, self.actor_id) return getattr(entity, 'as_str', entity) @cached_property def target(self, default=None): if not (self.target_type and self.target_id): return default entity = get_obj(self.target_type, self.target_id) return getattr(entity, 'as_str', entity) @property def target_link(self): entity = get_obj(self.target_type, self.target_id) return getattr(entity, 'as_link', None) @property def action_obj(self, default=None): if not (self.action_obj_type and self.action_obj_id): return default return get_obj(self.action_obj_type, self.action_obj_id) @property def timestamp_since(self): diff = datetime.utcnow() - self.timestamp minutes = diff.total_seconds() / 60 if minutes <= 1: timestamp_since_ = _(u'less than 1 minute ago') elif int(minutes) == 1: timestamp_since_ = _(u'about 1 minute ago') elif minutes < 60: timestamp_since_ = _(u'about %s minutes ago') % int(minutes) elif 60 <= minutes < 119: timestamp_since_ = _(u'about 1 hour ago') elif minutes < 60 * 24: timestamp_since_ = _(u'about %s hours ago') % int(minutes / 60) else: timestamp_since_ = datetime.strftime(self.timestamp, '%x') return timestamp_since_ @property def recipients(self): return (get_obj(r._type, r._id) for r in self._recipients) @classmethod def get_by_recipient(cls, recipient): return cls.query.find({ '$or': [{ '_recipients._id': instance_primary_key(recipient) }, { '_recipients': { '$eq': None } }] }).sort('timestamp', DESCENDING) @classmethod def count_not_seen_by_recipient(cls, recipient): return cls.query.find({ '$or': [ { '_recipients._id': instance_primary_key(recipient) }, { '_recipients': { '$eq': None } }, ], 'timestamp': { '$gt': recipient.last_activity_seen }, }).count() @classmethod def render_str(cls, action, **kw): ''' <actor> <verb> <time> <actor> <verb> <target> <time> <actor> <verb> <action_object> <target> <time> ''' timestamp_format = kw.get('timestamp_format', lambda t: datetime.strftime(t, '%x')) timestamp_since = None if kw.get('timestamp_since', False): timestamp_since = action.timestamp_since str_ = u'{actor} {verb} {action_object} {target} {time}'.format( actor=action.actor, verb=action.verb, target=action.target or '', action_object=action.action_obj or '', time=timestamp_since or timestamp_format(action.timestamp)) return str_