def find_one(cls, id): try: id = collection.ObjectId(id) model = Model().db[cls().collection()].find_one({'_id': id}) return cls(model) except pymongo.errors.InvalidId: return None
def find_one(cls, *args, **kwargs): query = None if kwargs.get('id'): query = {'_id': collection.ObjectId(kwargs.get('id', None))} elif kwargs.get('title'): query = {'title': kwargs.get('title')} elif kwargs.get('description'): query = {'description': kwargs.get('description')} result = db.todos.find_one(query) todo = cls(result.get('title'), result.get('description')) todo.id = str(result.get('_id')) return todo
def paste(): code = request.POST['code'] id = None if request.POST.__contains__('_id'): id = request.POST['_id'] paste = {'body': code, 'date': datetime.datetime.utcnow()} if id == None: ret = co.insert(paste) else: co.update({'_id': collection.ObjectId(id)}, paste) ret = id cc = highlight(code, PythonLexer(), HtmlFormatter(style="colorful")) return template('static/ok.tpl', url='/p/%s' % ret, body=cc)
def delete(self): if self.id: db.todos.delete_one({'id': collection.ObjectId(self.id)}) return self
def remove(self): id = collection.ObjectId(self.data['_id']) self.db[self.collection()].remove({'_id': id})
def pasted_item(id): doc = co.find_one({'_id': collection.ObjectId(id)}) code = doc['body'] cc = highlight(code, PythonLexer(), HtmlFormatter(style="colorful")) return template('static/pasted.tpl', code=cc, _id=id, date=doc['date'])