-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticles.py
136 lines (118 loc) · 5.76 KB
/
articles.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
129
130
131
132
133
134
135
136
from copyreg import constructor
from datetime import date, datetime
import os
from flask import Blueprint, current_app, request, g
from mongoengine.context_managers import switch_db
import auth
import models
from mongoengine.queryset.visitor import Q
from article import upload, modify
def from_documents_to_json(documents):
return '[%s]' % (','.join([doc.to_json() for doc in documents]))
articles_blueprint = Blueprint('/articles', __name__,)
# Retrieve
@articles_blueprint.route("/<id>", methods=['GET'])
def specific_article(id):
try:
with switch_db(models.Article, "default"):
print(models.Article.objects.get(id=id).students)
return models.Article.objects.get(id=id).to_json()
except Exception as e:
return str(e), 400
@articles_blueprint.route("/all", methods=['GET'])
def all_articles():
args = request.args
with switch_db(models.Article, "default"):
if args.get("date_start") and args.get("date_end"):
return from_documents_to_json(models.Article.objects(
Q(created__lte=date.fromisoformat(args.get("date_end"))) &
Q(created__gte=date.fromisoformat(args.get("date_start")))).order_by('authors'))
else:
return from_documents_to_json(models.Article.objects.order_by('authors'))
@articles_blueprint.route("/search/<search>", methods=['GET'])
def student_search(search):
args = request.args
with switch_db(models.Article, "default"):
if args.get("date_start") and args.get("date_end"):
return from_documents_to_json(models.Article.objects(
Q(created__lte=date.fromisoformat(args.get("date_end"))) &
Q(created__gte=date.fromisoformat(args.get("date_start")))).search_text(search).order_by('$text_score'))
else:
return from_documents_to_json(models.Article.objects.search_text(search).order_by('$text_score'))
@articles_blueprint.route("/favoured", methods=['GET'])
def favoured_articles():
args = request.args
with switch_db(models.Article, "default"):
if args.get("date_start") and args.get("date_end"):
return from_documents_to_json(models.Article.objects(
Q(created__lte=date.fromisoformat(args.get("date_end"))) &
Q(created__gte=date.fromisoformat(args.get("date_start")))).order_by('-favoured'))
else:
return from_documents_to_json(models.Article.objects.order_by('-favoured'))
@articles_blueprint.route("/student/<student>", methods=['GET'])
def student_article(student):
args = request.args
with switch_db(models.Article, "default"):
if args.get("date_start") and args.get("date_end"):
return from_documents_to_json(models.Article.objects(
Q(created__lte=date.fromisoformat(args.get("date_end"))) &
Q(created__gte=date.fromisoformat(args.get("date_start"))) &
Q(students=student)).order_by('authors'))
else:
return from_documents_to_json(models.Article.objects(students=student).order_by('authors'))
@articles_blueprint.route("/category/<category>", methods=['GET'])
def article_category(category):
args = request.args
with switch_db(models.Article, "default"):
if args.get("date_start") and args.get("date_end"):
return from_documents_to_json(models.Article.objects(
Q(created__lte=date.fromisoformat(args.get("date_end"))) &
Q(created__gte=date.fromisoformat(args.get("date_start"))) &
Q(category=category)).order_by('authors'))
else:
return from_documents_to_json(models.Article.objects(category=category).order_by('authors'))
@articles_blueprint.route("/tag/<tag>", methods=['GET'])
def article_tag(tag):
args = request.args
with switch_db(models.Article, "default"):
if args.get("date_start") and args.get("date_end"):
return from_documents_to_json(models.Article.objects(
Q(created__lte=date.fromisoformat(args.get("date_end"))) &
Q(created__gte=date.fromisoformat(args.get("date_start"))) &
Q(tags=tag)).order_by('authors'))
else:
return from_documents_to_json(models.Article.objects(tags=tag).order_by('authors'))
@articles_blueprint.route("/owned", methods=['GET'])
@auth.is_authorized
def article_owned():
print(g.uid)
with switch_db(models.Article, "default"):
return from_documents_to_json(models.Article.objects(students=g.uid).order_by('authors'))
@articles_blueprint.route("/create", methods=['POST'])
@auth.is_authorized
@auth.need_user_info
def article_upload():
content_type = request.headers.get('Content-Type')
if (content_type == 'application/json'):
try:
json = request.json
with switch_db(models.Article, "default"):
if json['type'] == 'websiteLink' and not json['link']:
return 'You have not specified a valid website link', 400
article = models.Article(**json)
article = models.Article(**json)
article.students = [g.uid]
article.authors = [g.display_name]
article.favoured = 0
article.draft = True
article.last_updated = datetime.utcnow()
article.save()
folder_path = os.path.join(current_app.config['UPLOAD_FOLDER'], str(article.id))
os.mkdir(folder_path)
return str(article.id)
except Exception as e:
return str(e), 400
else:
return 'Content-Type not supported!'
articles_blueprint.register_blueprint(upload.articles_upload, url_prefix='/upload')
articles_blueprint.register_blueprint(modify.article_modify, url_prefix='/modify')