Ejemplo n.º 1
0
    def pre_save(cls, sender, document, **kwargs):
        document._created = True
        if not document.id:
            id_prefix = None
            if hasattr(cls, 'id_prefix'):
                id_prefix = getattr(cls, 'id_prefix')

            document.id = create_document_id(cls.__name__, id_prefix)
            document.created_at = now_lambda()
        document.updated_at = now_lambda() if document.id and document.updated_at else document.created_at
Ejemplo n.º 2
0
def create_document_id(class_name, id_prefix=None):
    remain_str = '{0}{1}'.format(now_lambda().strftime('%Y%m%d%H%M%S'), str(random.randint(100, 999)))
    if id_prefix:
        id = '{0}{1}'.format(id_prefix.upper(), remain_str)
    else:
        name = get_upper_letters(class_name)
        id = '{0}{1}'.format(name, remain_str)
    return id
Ejemplo n.º 3
0
 def leave_room(cls, room_id, user_id):
     rur = RoomUserRef.objects(room_id=room_id,
                               user_id=user_id,
                               deleted_at=None).first()
     if rur:
         now = now_lambda()
         rur.status = rur.LEFT
         rur.histories.append(
             EmbeddedStatus(status=rur.status, operated_at=now))
         rur.save()
         cls.objects(room_id=room_id,
                     deleted_at=None).update(inc__members=-1)  # 人数减1
Ejemplo n.º 4
0
 def join_room(cls, room_id, user_id):
     rur = RoomUserRef.objects(room_id=room_id,
                               user_id=user_id,
                               deleted_at=None).first()
     if not rur:
         now = now_lambda()
         rur = RoomUserRef(room_id=room_id, user_id=user_id)  # 创建关系
         rur.histories.append(
             EmbeddedStatus(status=rur.status, operated_at=now))
         rur.save()
         cls.objects(id=room_id,
                     deleted_at=None).update(inc__members=1)  # 人数加1
Ejemplo n.º 5
0
def article_delete():
    """
    删除文章
    article_id         文章ID   (可选, 编辑时必填)
    :return:
    """
    article_id = request.form.get('article_id')
    if not article_id:
        return res(Errors.PARAMS_REQUIRED)

    c = Content.objects(id=article_id, deleted_at=None).first()
    if not c:
        return res(Errors.NOT_FOUND)

    c.deleted_at = now_lambda()
    c.save()
    return res()
Ejemplo n.º 6
0
def tag_delete():
    """
    删除标签
    tag_id         标签ID   (可选, 编辑时必填)
    name           标签名
    :return:
    """
    tag_id = request.form.get('tag_id')
    if not tag_id:
        return res(Errors.PARAMS_REQUIRED)

    t = Tag.objects(id=tag_id, deleted_at=None).first()
    if not t:
        return res(Errors.NOT_FOUND)

    t.deleted_at = now_lambda()
    t.save()
    return res()
Ejemplo n.º 7
0
 def logout(self):
     self.sign_out_at = now_lambda()
     self.save()
     logout_user()
Ejemplo n.º 8
0
 def login(self, remember_me):
     login_user(self, remember=remember_me)
     self.sign_in_at = now_lambda()
Ejemplo n.º 9
0
def get_unique_name(ext):
    now = now_lambda()
    return '{0}{1}.{2}'.format(now.strftime('%Y%m%d%H%M%S'), randint(100, 999),
                               ext)