def _get(self, _id=None, entity=None): if _id: command = '%s.find_one({"_id":ObjectId("%s")})' % (self._mongocollection, str(_id)) if DBDEBUG: Logger.log_to_file(command) entity = eval(command) if entity: self._hydrate(entity)
def save(self): if self._id: command = '%s.save(%s)' % (self._mongocollection, str(self._get_hash())) if DBDEBUG: Logger.log_to_file(command) eval(command) else: command = '%s.insert(%s)' % (self._mongocollection, str(self._get_hash())) if DBDEBUG: Logger.log_to_file(command) self._id = eval(command)
def index(): Logger.log_to_file(bottle.request.session.userId) collections = EntityManager(_DBCON).get_all(Collection , filter_criteria={'userId':bottle.request.session.userId} ,sort_by=[('lowercasename', 1)] ) output = [] for c in collections: output.append( c.get_json_safe() ) return json.dumps(output)
def send(self, subject, body): msg = MIMEText(body, 'html') msg['Subject'] = subject msg['From'] = self.sender msg['To'] = self.recipient try: s = smtplib.SMTP(self.host) if settings.EMAILUSERNAME and settings.EMAILPASSWORD: s.login(settings.EMAILUSERNAME, settings.EMAILPASSWORD) s.sendmail(self.sender, self.recipient, msg.as_string()) except: Logger.log_to_file(msg.as_string())
def send(self, subject, body): msg = MIMEText(body, "html") msg["Subject"] = subject msg["From"] = self.sender msg["To"] = self.recipient try: s = smtplib.SMTP(self.host) if settings.EMAILUSERNAME and settings.EMAILPASSWORD: s.login(settings.EMAILUSERNAME, settings.EMAILPASSWORD) s.sendmail(self.sender, self.recipient, msg.as_string()) except: Logger.log_to_file(msg.as_string())
def delete_one(self, entity, _id): """ Deletes a single entity from the datastore based on the id given entity should be a string _id should be the string entity id e.g. todoId = '5047b7bb37d5e64e9a4b1c74' EntityManager(_DBCON).delete_one('Todo', todoId) """ command = 'self.db.%s.remove({"_id":ObjectId("%s")})' % (entity, str(_id)) if DBDEBUG: Logger.log_to_file(command) eval(command)
def _get(self, _id): command = '%s.find_one({"_id":ObjectId("%s")})' % ( self._mongocollection, str(_id)) if DBDEBUG: Logger.log_to_file(command) entity = eval(command) if entity: setattr(self, '_id', entity.get('_id')) for f, val in self._fields: fieldtype = type(getattr(self, f)) fieldvalue = entity.get(f) if fieldtype == list: fieldlist = [] for el in fieldvalue: if type(el) == dict and el.has_key('__instanceOf__'): command = '%s(self.db, ObjectId("%s"))' % ( self._unicode_to_class( el['__instanceOf__']), el['_id']) if DBDEBUG: Logger.log_to_file(command) el = eval(command) fieldlist.append(el) fieldvalue = fieldlist elif type(fieldvalue) == dict and fieldvalue.has_key( '__instanceOf__'): command = '%s(self.db, ObjectId("%s"))' % ( self._unicode_to_class( fieldvalue['__instanceOf__']), fieldvalue['_id']) if DBDEBUG: Logger.log_to_file(command) fieldvalue = eval(command) setattr(self, f, fieldvalue)
def _hydrate(self, entity): setattr(self, '_id', entity.get('_id')) for f, val in self._fields: fieldtype = type(getattr(self, f)) fieldvalue = entity.get(f) if fieldtype == list: fieldlist = [] for el in fieldvalue: if type(el)==dict and el.has_key('__instanceOf__'): command = '%s(self.db, ObjectId("%s"))' % (self._unicode_to_class(el['__instanceOf__']) , el['_id']) if DBDEBUG: Logger.log_to_file(command) el = eval(command) fieldlist.append(el) fieldvalue = fieldlist elif type(fieldvalue)==dict and fieldvalue.has_key('__instanceOf__'): command = '%s(self.db, ObjectId("%s"))' % (self._unicode_to_class(fieldvalue['__instanceOf__']) , fieldvalue['_id']) if DBDEBUG: Logger.log_to_file(command) fieldvalue = eval(command) setattr(self, f, fieldvalue)
def _get(self, _id): command = '%s.find_one({"_id":ObjectId("%s")})' % (self._mongocollection, str(_id)) if DBDEBUG: Logger.log_to_file(command) entity = eval(command) if entity: setattr(self, "_id", entity.get("_id")) for f, val in self._fields: fieldtype = type(getattr(self, f)) fieldvalue = entity.get(f) if fieldtype == list: fieldlist = [] for el in fieldvalue: if type(el) == dict and el.has_key("__instanceOf__"): command = '%s(self.db, ObjectId("%s"))' % ( self._unicode_to_class(el["__instanceOf__"]), el["_id"], ) if DBDEBUG: Logger.log_to_file(command) el = eval(command) fieldlist.append(el) fieldvalue = fieldlist elif type(fieldvalue) == dict and fieldvalue.has_key("__instanceOf__"): command = '%s(self.db, ObjectId("%s"))' % ( self._unicode_to_class(fieldvalue["__instanceOf__"]), fieldvalue["_id"], ) if DBDEBUG: Logger.log_to_file(command) fieldvalue = eval(command) setattr(self, f, fieldvalue)
def get_all(self, entity, filter_criteria='', sort_by=[], skip=None, limit=None, count=False): """ Get all or a selection of entities from the datastore. This returns a list of entities. Entity should be class object filter_criteria can be used to filter the results and should be a dictionary that adheres to the pymongo documentation http://api.mongodb.org/python/current/genindex.html {'name':'jim'} sort_by should be a list of tuples (attribute, direction) [ ('name',1), ('age',1), ] skip and limit are both ints and are used for pagination count should be True if only a count of the results is required e.g. todos = EntityManager(_DBCON).get_all(Todo ,filter_criteria={'userId':bottle.request.session.userid} ,sort_by=[('added', 1)] ,skip=20 ,limit=10 ) """ extraCriteria = '' if len(sort_by)>0: extraCriteria += '.sort(%s)' % str(sort_by) if skip: extraCriteria += '.skip(%s)' % str(skip) if limit: extraCriteria += '.limit(%s)' % str(limit) if count: extraCriteria += '.count()' command = 'self.db.%s.find(%s)%s' % (entity.__name__ ,str(filter_criteria) , extraCriteria ) if DBDEBUG: Logger.log_to_file(command) if count: return eval(command) else: entities = [] for result in eval(command): e = entity(self.db) setattr(e, '_id', result.get('_id')) for f, val in e.fields: setattr(e, f, result.get(f)) entities.append(e) return entities