-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.py
128 lines (113 loc) · 4.48 KB
/
collection.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
from flask import request,Blueprint
from flask_mongoengine import MongoEngine
from mongoengine import Document, StringField, ReferenceField, DateTimeField,EmbeddedDocumentField,ListField,EmbeddedDocument
from material import Material
from auth import auth_by_token
bp = Blueprint('collection', __name__, url_prefix='/collection')
class Collection_unit(EmbeddedDocument):
content = ReferenceField('Material',required=True)
annotation = StringField()
collection = ReferenceField('Collection',requied=True)
#收藏夹
class Collection(Document):
collect = ListField(EmbeddedDocumentField(Collection_unit))
title = StringField(required=True)
owner = ReferenceField('User',required=True)
meta = {'indexes':[{
'fields':['$title'],
'default_language':'English',#默认语言是英语
}]}
@bp.route('/collection_build',methods=['POST'])
def collection_build():
#验证登陆
token = request.form.get('token')
if not token:
return {"status":"failed","data":"token required"}
user = auth_by_token(token)
if not user:
return {"status":"failed",'data':"invalid token"}
if 'title' in request.form:
title = request.form['title']
else:
return {'status':'failed','data':'title required'}
if Collection.objects(title=title).count()>0:
return {'status':'failed',"data":"duplicated collection name"}
collection = Collection(title=title,owner=user)
collection.save()
return {'status':'success','data':str(collection.id)}
@bp.route('/collection_by_id',methods=['POST'])
def collection_by_id():
id = request.form.get('id',default=None)
if not id:
return {'status':'failed','data':'id required'}
collection = Collection.objects(id=id).first()
if not collection:
return {'status':'failed','data':'id does not exist'}
return {'status':'success','data':{
'title':collection.title,
'content':collection.collect,
'owner_name':collection.owner.username
}}
@bp.route('/collect_add',methods=['POST'])
def collection_add():
#验证登陆
token = request.form.get('token')
if not token:
return {"status":"failed","data":'token required'}
user = auth_by_token(token)
if not user:
return {"status":"failed",'data':"invalid token"}
id = request.form.get('id',default=None)
if not id:
return {'status':'failed','data':'id required'}
material = Material.objects(id=id).first()
if not material:
return {'status':'failed','data':'id does not exist'}
if 'collection_name' in request.form:
collection_name=request.form['collection_name']
else:
return {'status':'failed','data':'collection name required'}
if 'annotation' in request.form:
annotation = request.form['annotation']
else:
return {'status':'failed','data':'annotation required'}
collection = Collection.objects(title=collection_name).first()
collection.collect.append(Collection_unit(annotation=annotation,content=material,collection=collection))
#Collection_unit(annotation=annotation,content=material,collection=collection).save()#这里出错
return {'status':'success','data':{
'title': collection.title,
'owner':collection.owner,
'material':collection.collect,
'content':material.title
}}
@bp.route('/collection_search',methods=['POST'])
def collection_search():
text = request.form.get('text',default=True)
if not text:
return {'status':'failed',"data":'text required'}
collections = Collection.objects.search_text(text)
#problems = Problem.objects.search_text(text)
#要是找不到怎么判断与处理
return {'status':"success",'data':{
'collection':collections
}}
@bp.route('/collection_delete',methods=['POST'])
def collection_delete():
# 验证登陆
token = request.form.get('token')
if not token:
return {'status':"failed",'data':'token requied'}
user = auth_by_token(token)
if not user:
return {'status':'failed','data':'invalid token'}
id = request.form.get('id',default=None)
if not id:
return {"status":'failed','data':'id required'}
collection = Collection.objects(id=id).first()
if not collection:
return {'status':'failed','data':'id does not exist'}
if collection.owner != user:
return {'status':'failed','data':'forbidden'}
collection.delete()
return {'status':'success'}