def create(cls, **kwargs): coll_name = cls._meta['collection'] model = cls(**kwargs) model.save() key = OBJ_KEY.format(coll_name=coll_name, id=id) cache.set(key, model.to_json()) return model
def suggest(text): """给API用""" if isinstance(text, unicode): text = text.encode('utf-8') key = SUGGEST_KEY.format(text=text) rs = cache.get(key) if rs: return ast.literal_eval(rs) items = [] if not isinstance(text, unicode): text = text.decode('utf-8') artists = Artist.objects(name__contains=text) items.extend([{ 'id': artist.id, 'name': artist.name, 'avatar': artist.picture, 'type': 'artist' } for artist in artists]) songs = Song.objects(name__contains=text) items.extend([{ 'id': song.id, 'name': song.name, 'type': 'song' } for song in songs]) cache.set(key, items) return items
def get(cls, id): coll_name = cls._meta['collection'] key = OBJ_KEY.format(coll_name=coll_name, id=id) rs = cache.get(key) if rs: return cls.from_json(rs) rs = cls.objects.get(id=id) cache.set(key, rs.to_json()) return rs
def update_doc(cls, id, **kwargs): rn = cls.objects(id=id).update(upsert=True, **kwargs) if not rn: return False else: rn = cls.objects(id=id).first() coll_name = cls._meta['collection'] key = OBJ_KEY.format(coll_name=coll_name, id=id) cache.set(key, rn.to_json()) return rn
def get(cls, id): coll_name = cls._meta['collection'] key = OBJ_KEY.format(coll_name=coll_name, id=id) rs = cache.get(key) if rs: return cls.from_json( rs) # Converts json data to an unsaved document instance rs = cls.objects.get(id=id) cache.set(key, rs.to_json()) # Convert this document to JSON. return rs
def search(subject_id, type): key = SEARCH_KEY.format(id=subject_id, type=type) if not subject_id: return [] comment_id = cache.get(key) if comment_id: return Comment.get(comment_id) movie = None if type == "movie": movie = Movie.get(id=subject_id) if movie == None: return [] comment = Comment.objects(movie=movie)[0] comment_id = comment.id if comment_id: cache.set(key, comment_id) return comment
def suggest(text): if isinstance(text, unicode): text = text.encode('utf-8') key = SUGGEST_KEY.format(text=text) rs = cache.get(key) items = [] if rs: temp_rs = ast.literal_eval(rs) for t in temp_rs: items.append(t[0]) return items if not isinstance(text, unicode): text = text.decode('utf-8') movies = Movie.objects(name__contains=text) items.extend([{ 'id': movie.id, 'name': movie.name, 'type': 'movie' }] for movie in movies) cache.set(key, items) return items
class BaseModel(Document): id = SequenceField(primary_key=True) created_at = DateTimeField(default=datetime.now) updated_at = DateTimeField(default=datetime.now) state = IntField(null=False, default=100) meta = {'allow_inheritance': True, 'abstract': True, 'strict': False } @classmethod def pagination(cls, page, limit, rn): if not rn: count = 0 page_num = page data = [] else: skip = 0 if page == 1 else (page - 1) * limit end = skip + limit count = rn.count() page_num, remainder = divmod(count, limit) page_num = page_num if remainder == 0 else (page_num + 1) data = rn[skip: end] data = { 'data': data, 'count': count, 'page_num': page_num, 'current': page, } return data @classmethod def get(cls, id): coll_name = cls._meta['collection'] key = OBJ_KEY.format(coll_name=coll_name, id=id) rs = cache.get(key) if rs: return cls.from_json(rs) try: rs = cls.objects.get(id=id) except DoesNotExist, e: return False cache.set(key, rs.to_json()) return rs