forked from leancloud/flask-todo-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud.py
42 lines (33 loc) · 1.12 KB
/
cloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# coding: utf-8
from datetime import datetime
from datetime import timedelta
from leancloud import Object
from leancloud import ACL
from leancloud import Engine
from leancloud import LeanEngineError
from app import app
engine = Engine(app)
@engine.before_save('Todo')
def before_todo_save(todo):
content = todo.get('content')
if not content:
raise LeanEngineError('内容不能为空')
if len(content) >= 240:
todo.set('content', content[:240] + ' ...')
author = todo.get('author')
if author:
acl = ACL()
acl.set_public_read_access(True)
acl.set_read_access(author.id, True)
acl.set_write_access(author.id, True)
todo.set_acl(acl)
@engine.define
def empty_trash():
deleted_todos = Object.extend('Todo').query.equal_to('status', -1).less_than('updatedAt', datetime.today() - timedelta(30)).find()
if not deleted_todos:
print('过去 30 天内没有被删除的 todo')
else:
print('正在清理{0}条 todo……'.format(len(deleted_todos)))
for todo in deleted_todos:
todo.destroy()
print('清理完成。')