def post(self, data): try: data["created"] = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") data["modified"] = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") return oid_to_str(self.col.insert(data)) except Exception as e: raise e
def get_index(self, query=dict(), sort=('created', -1), per_page=20, page=1): try: response = dict() find = self.col.find(query) response["count"] = find.count() find = find.sort(sort[0], sort[1]) logger.debug([page, per_page]) if per_page > 0 and page > 0: find = find.skip((page-1) * per_page).limit(per_page) response["page"] = page response["per_page"] = per_page response["documents"] = oid_to_str(list(find)) return response except Exception as e: raise e
def toggle_array(self, user_id, field, oid, operation): try: update = dict() toggle_data = {field: oid} if operation == 'add': update['$addToSet'] = toggle_data else: update['$pull'] = toggle_data update["$set"] = {"modified": datetime.now().strftime("%Y-%m-%dT%H:%M:%S")} logger.debug(update) self.col.update({"_id": ObjectId(user_id)}, update) ret = self.get_by_id(user_id, password=False) session['auth'] = ret return oid_to_str(ret) except Exception as e: logger.debug(e) return False
def get_by_identify(self, identify, password=True): user = self.col.find_one({"$or": [{"user_name": identify}, {"user_email": identify}]}) if not password: del user['password'] return oid_to_str(user)
def get_by_id(self, oid, query=dict(), password=True): user = super().get_by_id(oid,query) if not password: del user['password'] return oid_to_str(user)
def get_by_id(self, oid, query=dict()): query['_id'] = ObjectId(oid) document = self.col.find_one(query) return oid_to_str(document)