def CtrlGetById(self, id): rows = self.comics.aggregate([ { "$match": { "_id": ObjectId(id) } }, { "$lookup": { "from": "authors", "localField": "authors", "foreignField": "_id", "as": "authors" } }, { "$lookup": { "from": "categories", "localField": "categories", "foreignField": "_id", "as": "categories" } }, { "$project": { 'nameNoAccent': 0, "url": 0, "referer": 0, "crawled": 0 } } ]) if rows.alive: return JSONParser(list(rows)[0]) return JSONParser(None) pass
def CtrlGetAll(self, parameters): rows = self.authors.find() total = self.authors.count() sort = parameters.get("sort") if sort: if sort[0] == "-": rows.sort({ sort.strip("-"): -1 }) else: rows.sort({ sort: 1 }) skip = parameters.get("skip") if not skip: skip = 0 rows.skip(int(skip)) limit = parameters.get("limit") if not limit: limit = app.config["PAGE_SIZE_DEFAULT"] rows.limit(int(limit)) return JSONParser({ "list": list(rows), "total": total, "skip": int(skip), "limit": int(limit) })
def CtrlResetDB(self): self.comics.update_many( { "crawled": True }, { "$set": { "crawled": False } }) return JSONParser(None) pass
def CtrlGet(self): randomDb = self.comics.aggregate([{ "$sample": { "size": 12 } }, { "$project": { "_id": 1, "name": 1, "cover": 1, "url": 1 } }]) latestDb = self.comics.find({}, { "_id": 1, "name": 1, "cover": 1, "url": 1 }).sort("updatedAt", -1).limit(12) hotestDb = self.comics.find({ "isHot": True }, { "_id": 1, "name": 1, "cover": 1, "url": 1 }).limit(12) completedDb = self.comics.find({ "status": 1 }, { "_id": 1, "name": 1, "cover": 1, "url": 1 }).limit(12) random = list(randomDb) completed = list(completedDb) if len(completed) == 0: completed = random tmp = list(latestDb) return JSONParser({ "random": random, "hotest": list(hotestDb), "latest": tmp, "completed": completed, })
def CtrlGetById(self, id): row = self.dynamodb.get_item( TableName='chapters', Key={ 'id': { 'S': id } } ) if 'Item' in row: deserializer = TypeDeserializer() data = {k: deserializer.deserialize(v) for k,v in row['Item'].items()} # data = row['Item'] return JSONParser(data) return abort(404, id)
def CtrlGetAll(self, parameters): stages = [ { "$lookup": { "from": "categories", "localField": "categories", "foreignField": "_id", "as": "categories" } } ] text = parameters.get("text") if text: rgx = re.compile(u'.*' + text + '.*', re.IGNORECASE) # compile the regex stages.append({ '$match': { "$or": [ {"name": rgx}, {"nameNoAccent": rgx}, {"body": rgx} ] } }) category = parameters.get("category") if category: stages.append({ "$match": { "categories": ObjectId(category) } }) author = parameters.get("author") if author: stages.append({ "$match": { "authors": ObjectId(author) } }) total = 0 stage_count = list(stages) stage_count.append({ "$count": "myCount" }) resultCount = self.comics.aggregate(stage_count) resultCount = list(resultCount) if resultCount: total = resultCount[0]["myCount"] sort = parameters.get("sort") if sort: if sort[0] == "-": stages.append({ "$sort": { sort.strip("-"): -1 } }) else: stages.append({ "$sort": { sort: 1 } }) skip = parameters.get("skip") if not skip: skip = 0 stages.append({ "$skip": int(skip) }) limit = parameters.get("limit") if not limit: limit = app.config["PAGE_SIZE_DEFAULT"] stages.append({ "$limit": int(limit) }) stages.append({ "$project": { "nameNoAccent": 0, "chapters": 0, "authors": 0, "url": 0, "body": 0, "createdAt": 0, "altName": 0, "referer": 0, "crawled": 0 } }) rows = self.comics.aggregate(stages) return JSONParser({ "list": list(rows), "total": total, "skip": int(skip), "limit": int(limit) }) pass