class RingsSchema(Document): _id = TextField() added = DateTimeField(default=datetime.now) license = TextField() public = BooleanField(default=False) rings = ListField( DictField( Mapping.build(RingName=TextField(), RingLabel=TextField(), RingDescription=TextField(), RingVersion=TextField(), RingURI=TextField(), RingBuild=TextField(), RingParent=TextField()))) fields = ListField( DictField( Mapping.build( FieldId=TextField(), FieldName=TextField(), FieldLabel=TextField(), FieldSemantic=TextField(), FieldType=TextField(), FieldSource=TextField(), FieldWidget=TextField(), FieldOrder=IntegerField(), FieldCardinality=TextField(), FieldMultilingual=BooleanField(), FieldRequired=BooleanField(), FieldDefault=TextField(), FieldHint=TextField(), FieldLayer=IntegerField(), )))
class Content(Document): termo = TextField() urls = ListField( DictField( Mapping.build(url=TextField(), tf_idf=DecimalField(), frequencia=IntegerField())))
class Organization(BasicCouchOperations): __predicate__ = "organization" _id = TextField() name = TextField() local = ListField( DictField( Mapping.build(lat=FloatField(), lon=FloatField(), address_str=TextField(), city=TextField()))) donation_types = ListField(TextField()) schedule = TextField() doc_type = TextField(default="organization") all = ViewField( __predicate__, '''function(doc){ if(doc.doc_type == "organization"){ emit(doc._id, doc); } }''') city = ViewField( __predicate__, '''function(doc){ if (doc.doc_type == "organization"){ doc.local.forEach(function(local){ emit (local.city, doc); }); } }''') @classmethod def sync(cls, db): cls.all.sync(db) cls.city.sync(db)
class Board(Document): """ This class contains fields for Board related information """ user_id = TextField() board_name = TextField() board_type = TextField() pins = ListField(DictField(Mapping.build(pin_id=TextField())))
class User(Document): name = TextField() password = TextField() exclusions = ListField(TextField()) dataInstances = ListField( DictField( Mapping.build(task_name=TextField(), start_time=DateTimeField(), end_time=DateTimeField(), suggested_time=IntegerField(), pre_energy=IntegerField(), user_feedback=IntegerField(), auto_rating=BooleanField()))) breakInstances = ListField( DictField( Mapping.build(break_start_time=DateTimeField(), break_end_time=DateTimeField()))) usernameView = ViewField( 'users', '''\ function(doc) { emit(doc.name, doc); }''') def get_favorites(self): favs = [] for inst in self.dataInstances: if inst.task_name not in favs and inst.task_name not in self.exclusions: favs.append(inst.task_name) return favs @property def is_active(self): return True @property def is_authenticated(self): return True @property def is_anonymous(self): return False def get_id(self): return unicode(self.name)
class Pin(Document): """ This class contains fields for Pin related information """ user_id = TextField() pin_url = TextField() pin_name = TextField() comments = ListField( DictField(Mapping.build(user_id=TextField(), comment=TextField())))
class PlantDeviceReading(Document): username = TextField() devicetype = TextField() device_id = TextField() timeReading = TextField() datetime = IntegerField() values = DictField( Mapping.build( moistureLevel=TextField(), waterLevel=TextField(), pumpStatus=TextField(), ))
class PlantDevice(Document): name = TextField() username = TextField() species = TextField() location = DictField( Mapping.build( geolocationCity=TextField(), geolocationState=TextField(), indoorsOutdoors=TextField(), )) wateringConditionTrigger = TextField() wateringConditionValue = TextField() additionalNotes = TextField()
class Role(couchdb.mapping.Document): rolename = TextField() permissions = ListField(TextField()) create_date = DateTimeField() created_by = TextField() change_date = DateTimeField() type = TextField() previous_versions = ListField( DictField( Mapping.build(permissions=ListField(TextField()), version_create_date=DateTimeField(), version_created_by=TextField(), version_valid_until=DateTimeField())))
class User(couchdb.mapping.Document): username = TextField() description = TextField() birth_date = DateField() assigned_rolename = TextField() create_date = DateTimeField() change_date = DateTimeField() type = TextField() previous_versions = ListField( DictField( Mapping.build( description=TextField(), assigned_rolename=TextField(), version_create_date=DateTimeField(), version_created_by=TextField(), version_valid_until=DateTimeField(), )))
def __get_db(self, name): try: return self.couch[name] except couchdb.http.ResourceNotFound: return self.__init_db(name) def __init_db(self, name): db = self.couch.create(name) return db DEPARTURE_RECORD_MAPPING = Mapping.build(departs_at=TextField(), dest=TextField(), track=TextField(), line=TextField(), train_id=TextField(), status=TextField(), at=DateTimeField(), color=TextField()) class Event(Document): action = TextField() context = TextField() new = DictField(DEPARTURE_RECORD_MAPPING) old = DictField(DEPARTURE_RECORD_MAPPING) @staticmethod def changed_departure(context, old, new): return Event(action='changed', context=context,
class Article(Document): type = TextField(default='article') publisher = TextField() publisher_location = TextField() published_date = DateTimeField() title = TextField() link = TextField() image_link = TextField() image_type = TextField() source = TextField() raw = TextField() word_frequencies = ListField(DictField(Mapping.build( word = TextField(), frequency = IntegerField() ))) entity_frequencies = ListField(DictField(Mapping.build( entity_type = TextField(), entity = TextField(), frequency = IntegerField() ))) def extract_word_frequencies(self, tokens): words = [w.lower() for sent in tokens for w in sent if w.isalpha() and w not in stopwords.words('english')] fdwords = nltk.FreqDist(words) self.fdwords = fdwords for w in self.fdwords.keys(): frequency = self.fdwords[w] self.word_frequencies.append(word=w, frequency=frequency) def extract_entity_frequencies(self, tokens): pos_tagged_tokens = [nltk.pos_tag(t) for t in tokens] loc = LocationChunker() trees = batch_ne_chunk(pos_tagged_tokens) entity_types = ['PERSON', 'ORGANIZATION', 'GPE', 'LOCATION', 'FACILITY'] for entity_type in entity_types: entity_freq_dict = {} chunks = [sub_leaves(t, entity_type) for t in trees] for sent in chunks: for c in sent: entity = ' '.join([w[0] for w in c]) entity_freq_dict[entity] = entity_freq_dict.get(entity, 0) + 1 # A secondary attempt at extracting locations based on reference # to lists of place names if entity_type == 'LOCATION': for sent in pos_tagged_tokens: t = loc.parse(sent) chunks = sub_leaves(t, 'LOCATION') for c in chunks: entity = ' '.join([w[0] for w in c]) entity_freq_dict[entity] = entity_freq_dict.get(entity, 0) + 1 entity_freq_list = [(entity_freq_dict[e], e) for e in entity_freq_dict.keys()] entity_freq_list.sort(reverse=True) for e in entity_freq_list: self.entity_frequencies.append( entity_type=entity_type.lower(), entity=e[1], frequency=e[0]) # Basic entity extraction, but not precise enough so using # above approach instead # def extract_entity_frequencies(self, tokens): # pos_tagged_tokens = [nltk.pos_tag(t) for t in tokens] # # # Flatten the list since we're not using sentence structure # # and sentences are guaranteed to be separated by a special # # POS tuple such as ('.', '.') # # pos_tagged_tokens = [token for sent in pos_tagged_tokens for token in sent] # # all_entity_chunks = [] # previous_pos = None # current_entity_chunk = [] # for (token, pos) in pos_tagged_tokens: # if pos == previous_pos and pos.startswith('NN'): # current_entity_chunk.append(token) # elif pos.startswith('NN'): # if current_entity_chunk != []: # # # Note that current_entity_chunk could be a duplicate when appended, # # so frequency analysis again becomes a consideration # # all_entity_chunks.append((' '.join(current_entity_chunk), pos)) # current_entity_chunk = [token] # previous_pos = pos # # # Store the chunks as an index for the article # # and account for frequency while we're at it # # entity_freq_dict = {} # for c in all_entity_chunks: # entity_freq_dict[c[0]] = entity_freq_dict.get(c[0], 0) + 1 # entity_freq_list = [(entity_freq_dict[e], e) for e in entity_freq_dict.keys()] # entity_freq_list.sort(reverse=True) # for e in entity_freq_list: # self.entity_frequencies.append(entity=e[1], frequency=e[0]) def get_content(self): return self.raw def set_content(self, content): self.raw = content if content and len(content) > 0: sentences = nltk.sent_tokenize(content) tokens = [nltk.word_tokenize(s) for s in sentences] self.extract_word_frequencies(tokens) self.extract_entity_frequencies(tokens) content = property(get_content, set_content) def __str__(self): print self.title
class Case(Document): _id = TextField() type = TextField(default="case") status = TextField() assignee = TextField() description = TextField() subject = TextField() creator_email = TextField() creator_useragent = TextField() creator_name = TextField() creator_username = TextField() url = TextField() created = DateTimeField() history = ListField( DictField( Mapping.build(at=DateTimeField(), by=TextField(), text=TextField()))) def __repr__(self): return "<Case ('%s')>" % self._id def change_status(self, new_status, by): self.status = new_status self.store(self.db) def reassign(self, new_assignee, by, text=""): self.assignee = new_assignee entry = dict(by=by, at=datetime.datetime.utcnow(), text="Case reassigned to '%s'\n\n%s" % (new_assignee, text)) self.history.append(entry) self.store(self.db) def add_worklog_entry(self, by, text): entry = dict(by=by, at=datetime.datetime.utcnow(), text=text) self.history.append(entry) self.store(self.db) # Override base class members to hold the database connection @classmethod def load(cls, db, id): ret = super(Case, cls).load(db, id) if not ret: raise InvalidCase("No case with id %s" % id) ret.db = db return ret def store(self, db): super(Case, self).store(db) self.db = db @property def last_modified(self): return self.history[-1].at @property def caseid(self): return self._id @property def caseno(self): "Returns case number" return int(self._id.replace("case-", "")) @classmethod def new(cls, **kargs): ret = cls(**kargs) item = dict(at=ret.created, by=ret.creator_name or ret.creator_email, text="Case created") ret.history.append(item) return ret @classmethod def all(cls, db, typ="all", sort="status", desc="false", staleok=False): view = { "created": "cases/sort-created", "caseid": "cases/sort-caseid", "assigned": "cases/sort-assignee", "user": "******", "lastmodified": "cases/sort-lastmodified", "status": "cases/sort-status", "subject": "cases/sort-subject", "notes": "cases/sort-numnotes" }[sort] if sort == "status": extra = dict(reduce=False, descending=desc) else: extra = dict(descending=desc) if staleok: extra['stale'] = "ok" if typ == "all": view = view.replace("-", "-all-") result = cls.view(db, view, include_docs=True, **extra) return result.rows elif typ == "new": startkey, endkey = (["new"], ["replied"]) elif typ == "closed": startkey, endkey = (["closed"], ["new"]) elif typ == "replied": startkey, endkey = (["replied"], False) else: raise KeyError("No such case type '%s'" % typ) if desc == "true": startkey, endkey = endkey, startkey if startkey: extra['startkey'] = startkey if endkey: extra['endkey'] = endkey result = cls.view(db, view, include_docs=True, **extra) return result.rows def __eq__(self, second): return self._id == second._id
class Container(Document): name = TextField() container_type = TextField() size = DictField(Mapping.build( amount = DecimalField(), unit = TextField() )) contents = ListField(DictField(Mapping.build( amount = DecimalField(), unit = TextField(), content = TextField(), updated_datetime = DateTimeField(default=datetime.now), temperature = DictField(Mapping.build( degrees = DecimalField(), unit = TextField() )), content_type = TextField() ))) full = BooleanField(default=False) def total_filled(self): """Return the amount of filled content. However, there is no weight to volume conversion. E.g. weight is free in volume-organized container volume is free in weight-organized containers """ total_amount = 0 target_unit = self.size.unit for content in self.contents: if content.unit in VOLUME_UNITS and target_unit in WEIGHT_UNITS: continue converted_amount = convert_amount( (content.amount, content.unit), target_unit) total_amount += converted_amount return (total_amount, target_unit) def heat_contents(self, temp_tuple): """Set all the contents of the container to be a temperature of temp_tuple temp_tuple is (amount, units), e.g. (70, 'C') """ if temp_tuple[1].upper() not in TEMP_UNITS: raise ContainerError("Unable to use the specified temperature unit %s" % (temp_tuple[1])) else: for content in self.contents: content.temperature = dict( degrees = temp_tuple[0], unit = temp_tuple[1].upper() ) def set_size(self, size_tuple): if not ((size_tuple[1].lower() in WEIGHT_UNITS) or (size_tuple[1].lower() in VOLUME_UNITS)): raise ContainerError("Unable to use the specified unit: %s" % (size_tuple[1])) self.size = {'amount': size_tuple[0], 'unit': size_tuple[1].lower() } def determine_content_type(self, content): if "wort" in content.lower(): return ContentType.Wort elif "hops" in content.lower(): return ContentType.Hops elif "yeast" in content.lower(): return ContentType.Yeast elif "malt" in content.lower(): return ContentType.Malt elif "water" in content.lower(): return ContentType.Water elif "beer" in content.lower(): return ContentType.Beer def add_content(self, content, amount, temp = (23, "C"), content_type=None, updated_datetime=None): """ :param amount - may be dictionary: {"amount": amount, "unit": unit}, or tuple (amount, unit) """ if not updated_datetime: updated_datetime=datetime.now() LOGGER.info("Attempting to add content %s (amount %s, temp %s)", content, amount, temp) amount_dict = {} temp_dict = {} if not content_type: content_type = self.determine_content_type(content) if "unit" not in amount: amount_dict = {"amount": amount[0], "unit": amount[1]} else: amount_dict = amount if amount_dict["unit"] != self.size.unit and amount_dict["unit"] in VOLUME_UNITS: raise ContainerError("Only able to add volume content measured in '{0}'".format(self.size.unit)) #Assume room temperature if "unit" not in temp: temp_dict = {"degrees": 23, "unit": "C"} else: temp_dict = temp if not amount_dict: raise ContainerError("Amount must be provided as (amount, unit) or as dictinoary ") # adding_amount is the value to add to the container adding_amount = amount_dict["amount"] if amount_dict["unit"] != self.size.unit: try: adding_amount = convert_amount(amount_dict, self.size.unit) except ContainerError as e: LOGGER.info("Unable to convert to unit %s. Not adding any volume. %s", self.size.unit, e) adding_amount = 0 filled = self.total_filled() if float(filled[0]) + float(adding_amount) > self.size.amount: adding_amount = self.size.amount - filled[0] self.contents.append( dict( amount = adding_amount, unit = self.size.unit, content = content, temperature = temp_dict, content_type = content_type, updated_datetime = updated_datetime, ) ) raise ContainerError("Error filling container: trying to add too much, only %s %s added (%s %s not added)" % (adding_amount, self.size.unit, amount_dict["amount"] - adding_amount, self.size.unit)) LOGGER.info("Adding full content %s %s %s" % (content, amount_dict["amount"], amount_dict["unit"])) self.contents.append( dict( amount = amount_dict["amount"], unit = amount_dict["unit"], content = content, temperature = temp_dict, content_type = content_type, updated_datetime = updated_datetime, )) if self.total_filled()[0] >= self.size.amount: self.full = True def remove_all(self): """Remove all contents, and return a list """ removed = [] for content in self.contents: removed.extend(self.remove_content(content.content, (content.amount, content.unit))) return removed def add_all(self, content_list): """content_list is a list of tuples ('content',(amount, unit), (degrees, unit)) add the specified content to the list """ LOGGER.info("Adding contents to container: %s", content_list) if not content_list: LOGGER.info("No contents to add") return for content in content_list: LOGGER.info("Attempting to add content %s", content) #for (content, amount_tuple, temperature_tuple) in content_list: try: self.add_content(content[0], content[1], content[2]) LOGGER.info("Added content: %s", content) except IndexError: LOGGER.error("Unable to add content %s", content) def remove_content(self, content, amount_tuple): """Remove the specified amount of content :return a list of tuples of the removed contents (content, amount, temperature) """ if content not in [ c.content for c in self.contents ]: raise ContainerError("Unable to remove the specify contents %s. The container is not currently filled with any") same_content = [ c for c in self.contents if c.content.lower() == content ] reducing_amount = amount_tuple[0] reducing_unit = amount_tuple[1] all_removed = [] for current_content in self.contents: if current_content.content.lower() == content.lower(): reducing_amount = convert_amount( (reducing_amount, reducing_unit), current_content.unit) LOGGER.info("Converted amount to remove: %s %s", reducing_amount, current_content.unit) if reducing_amount > current_content.amount: LOGGER.info("Amount reduce %s %s more than current contained content %s %s", reducing_amount, current_content.unit, current_content.amount, current_content.unit) reducing_amount = current_content.amount content_tuple = (content, (reducing_amount, current_content.unit), (current_content.temperature.degrees, current_content.temperature.unit)) LOGGER.info("Removed content Appending content, amount, temperature %s to removed list %s", content_tuple, all_removed) # Reduce the total amount of content to be removed reducing_amount -= current_content.amount all_removed.append(content_tuple) current_content.amount = 0 else: # Remove everything specified LOGGER.info("Removing full specified amount %s %s", reducing_amount, current_content.unit) current_content.amount -= reducing_amount content_tuple = (content, {"amount": reducing_amount, "unit": current_content.unit}, {"degrees": current_content.temperature.degrees, "unit": current_content.temperature.unit}) LOGGER.info("Updated content amount after reduction: %s %s", current_content.amount, current_content.unit) #LOGGER.info("Removed list: appending content, amount, temperature %s to removed list %s", content_tuple, all_removed) LOGGER.info("Removed list addition: %s", content_tuple) all_removed.append(content_tuple) LOGGER.info("List of all removed contents: %s", all_removed) # Remove the empty contents self.contents = [ c for c in self.contents if c.amount ] if self.total_filled()[0] < self.size.amount: self.full = False LOGGER.info("All removed content: %s", all_removed) return all_removed
class MyRingUser(Document): _id = TextField() #firstname = TextField() DEPRECATED! Use "name" for firstname if needed name = TextField() lastname = TextField() email = TextField() billingemail = TextField() is_org = BooleanField() location = TextField() url = TextField() profilepic = TextField() about = TextField() #people = DictField() onlogin = TextField() people = ListField( DictField( Mapping.build(handle=TextField(), addedby=TextField(), added=DateTimeField(default=datetime.now)))) teams = ListField( DictField( Mapping.build( teamname=TextField(), members=ListField( DictField( Mapping.build( handle=TextField(), addedby=TextField(), added=DateTimeField(default=datetime.now)))), rings=ListField( DictField( Mapping.build( handle=TextField(), ringname=TextField(), addedby=TextField(), added=DateTimeField(default=datetime.now)))), roles=ListField( DictField( Mapping.build( role=TextField(), addedby=TextField(), added=DateTimeField(default=datetime.now)))), addedby=TextField(), added=DateTimeField(default=datetime.now)))) salt = TextField() passhash = TextField() guid = TextField() added = DateTimeField(default=datetime.now) branchof = TextField() rings = ListField( DictField( Mapping.build(ringname=TextField(), version=TextField(), count=IntegerField(), added=DateTimeField(default=datetime.now), roles=ListField( DictField( Mapping.build(role=TextField(), users=ListField( Mapping.build()))))))) collections = ListField( DictField( Mapping.build(collectionname=TextField(), collectiondescription=TextField(), version=TextField(), added=DateTimeField(default=datetime.now), rings=ListField( DictField( Mapping.build(handle=TextField(), ringname=TextField(), version=TextField(), layer=IntegerField()))), roles=ListField( DictField( Mapping.build(role=TextField(), users=ListField( Mapping.build()))))))) badges = ListField( DictField( Mapping.build(badgename=TextField(), badgedescription=TextField(), added=DateTimeField(default=datetime.now)))) campaigns = ListField( DictField( Mapping.build(campaignname=TextField(), campaigndescription=TextField(), added=DateTimeField(default=datetime.now)))) is_active = BooleanField(default=True) is_authenticated = BooleanField(default=False) new_password_key = TextField() new_password_requested = DateTimeField() new_email = TextField() new_email_key = TextField() last_login = DateTimeField() last_ip = TextField() modified = DateTimeField(default=datetime.now)
from sqlalchemy import create_engine, MetaData, Table from couchdb.mapping import TextField, DateTimeField, IntegerField, \ Document, DictField, ListField, Mapping SessionField = DictField(Mapping.build( session_id = TextField(), user_id = IntegerField(), accessed = DateTimeField(), remote_addr = TextField() )) class Action(Document): _id = TextField() kind = TextField(default="action") timestamp = DateTimeField() section = TextField() session = SessionField type = TextField() referrer = TextField() details = DictField(default={}) def reflect(conn_string, table_names): #print "Connecting...", time() metadata = MetaData()
class Case(Document): _id = TextField() type = TextField(default="case") status = TextField() assignee = TextField() description = TextField() subject = TextField() creator_email = TextField() creator_useragent = TextField() creator_name = TextField() url = TextField() created = DateTimeField() history = ListField( DictField( Mapping.build(at=DateTimeField(), by=TextField(), text=TextField()))) def __init__(self, **kargs): super(Case, self).__init__(**kargs) item = dict(at=self.created, by=self.creator_name or self.creator_email, text="Case created") self.history.append(item) def change_status(self, new_status, by): self.status = new_status entry = dict(by=by, at=datetime.datetime.utcnow(), text="Case status changed to '%s'" % new_status) self.history.append(entry) self.store(self.db) def reassign(self, new_assignee, by): self.assignee = new_assignee entry = dict(by=by, at=datetime.datetime.utcnow(), text="Case reassigned to '%s'" % new_assignee) self.history.append(entry) self.store(self.db) def add_worklog_entry(self, by, text): entry = dict(by=by, at=datetime.datetime.utcnow(), text=text) self.history.append(entry) self.store(self.db) # Override base class members to hold the database connection @classmethod def load(cls, db, id): ret = super(Case, cls).load(db, id) if not ret: raise InvalidCase("No case with id %s" % id) ret.db = db return ret def store(self, db): super(Case, self).store(db) self.db = db @property def last_modified(self): return self.history[-1].at @property def caseid(self): return self._id @property def caseno(self): "Returns case number" return int(self._id.replace("case-", "")) @ViewField.define('cases') def all(self, doc): if doc.get("type", "") == "case": yield doc["_id"], doc def __eq__(self, second): return self._id == second._id