##// END OF EJS Templates
Small changes to the docstrings of the api module
neko259 -
r627:07bcda87 default
parent child Browse files
Show More
@@ -1,224 +1,226
1 from datetime import datetime
1 from datetime import datetime
2 import json
2 import json
3 from django.db import transaction
3 from django.db import transaction
4 from django.http import HttpResponse
4 from django.http import HttpResponse
5 from django.shortcuts import get_object_or_404, render
5 from django.shortcuts import get_object_or_404, render
6 from django.template import RequestContext
6 from django.template import RequestContext
7 from django.utils import timezone
7 from django.utils import timezone
8 from django.core import serializers
8 from django.core import serializers
9
9
10 from boards.forms import PostForm, PlainErrorList
10 from boards.forms import PostForm, PlainErrorList
11 from boards.models import Post, Thread, Tag
11 from boards.models import Post, Thread, Tag
12 from boards.utils import datetime_to_epoch
12 from boards.utils import datetime_to_epoch
13 from boards.views.thread import ThreadView
13 from boards.views.thread import ThreadView
14
14
15 __author__ = 'neko259'
15 __author__ = 'neko259'
16
16
17 PARAMETER_TRUNCATED = 'truncated'
17 PARAMETER_TRUNCATED = 'truncated'
18 PARAMETER_TAG = 'tag'
18 PARAMETER_TAG = 'tag'
19 PARAMETER_OFFSET = 'offset'
19 PARAMETER_OFFSET = 'offset'
20 PARAMETER_DIFF_TYPE = 'type'
20 PARAMETER_DIFF_TYPE = 'type'
21
21
22 DIFF_TYPE_HTML = 'html'
22 DIFF_TYPE_HTML = 'html'
23 DIFF_TYPE_JSON = 'json'
23 DIFF_TYPE_JSON = 'json'
24
24
25 STATUS_OK = 'ok'
25 STATUS_OK = 'ok'
26 STATUS_ERROR = 'error'
26 STATUS_ERROR = 'error'
27
27
28
28
29 @transaction.atomic
29 @transaction.atomic
30 def api_get_threaddiff(request, thread_id, last_update_time):
30 def api_get_threaddiff(request, thread_id, last_update_time):
31 """Get posts that were changed or added since time"""
31 """
32 Gets posts that were changed or added since time
33 """
32
34
33 thread = get_object_or_404(Post, id=thread_id).thread_new
35 thread = get_object_or_404(Post, id=thread_id).thread_new
34
36
35 filter_time = datetime.fromtimestamp(float(last_update_time) / 1000000,
37 filter_time = datetime.fromtimestamp(float(last_update_time) / 1000000,
36 timezone.get_current_timezone())
38 timezone.get_current_timezone())
37
39
38 json_data = {
40 json_data = {
39 'added': [],
41 'added': [],
40 'updated': [],
42 'updated': [],
41 'last_update': None,
43 'last_update': None,
42 }
44 }
43 added_posts = Post.objects.filter(thread_new=thread,
45 added_posts = Post.objects.filter(thread_new=thread,
44 pub_time__gt=filter_time) \
46 pub_time__gt=filter_time) \
45 .order_by('pub_time')
47 .order_by('pub_time')
46 updated_posts = Post.objects.filter(thread_new=thread,
48 updated_posts = Post.objects.filter(thread_new=thread,
47 pub_time__lte=filter_time,
49 pub_time__lte=filter_time,
48 last_edit_time__gt=filter_time)
50 last_edit_time__gt=filter_time)
49
51
50 diff_type = DIFF_TYPE_HTML
52 diff_type = DIFF_TYPE_HTML
51 if PARAMETER_DIFF_TYPE in request.GET:
53 if PARAMETER_DIFF_TYPE in request.GET:
52 diff_type = request.GET[PARAMETER_DIFF_TYPE]
54 diff_type = request.GET[PARAMETER_DIFF_TYPE]
53
55
54 for post in added_posts:
56 for post in added_posts:
55 json_data['added'].append(_get_post_data(post.id, diff_type, request))
57 json_data['added'].append(_get_post_data(post.id, diff_type, request))
56 for post in updated_posts:
58 for post in updated_posts:
57 json_data['updated'].append(_get_post_data(post.id, diff_type, request))
59 json_data['updated'].append(_get_post_data(post.id, diff_type, request))
58 json_data['last_update'] = datetime_to_epoch(thread.last_edit_time)
60 json_data['last_update'] = datetime_to_epoch(thread.last_edit_time)
59
61
60 return HttpResponse(content=json.dumps(json_data))
62 return HttpResponse(content=json.dumps(json_data))
61
63
62
64
63 def api_add_post(request, opening_post_id):
65 def api_add_post(request, opening_post_id):
64 """
66 """
65 Add a post and return the JSON response for it
67 Adds a post and return the JSON response for it
66 """
68 """
67
69
68 opening_post = get_object_or_404(Post, id=opening_post_id)
70 opening_post = get_object_or_404(Post, id=opening_post_id)
69
71
70 status = STATUS_OK
72 status = STATUS_OK
71 errors = []
73 errors = []
72
74
73 if request.method == 'POST':
75 if request.method == 'POST':
74 form = PostForm(request.POST, request.FILES,
76 form = PostForm(request.POST, request.FILES,
75 error_class=PlainErrorList)
77 error_class=PlainErrorList)
76 form.session = request.session
78 form.session = request.session
77
79
78 #if form.need_to_ban:
80 #if form.need_to_ban:
79 # # Ban user because he is suspected to be a bot
81 # # Ban user because he is suspected to be a bot
80 # _ban_current_user(request)
82 # _ban_current_user(request)
81 # status = STATUS_ERROR
83 # status = STATUS_ERROR
82 if form.is_valid():
84 if form.is_valid():
83 ThreadView().new_post(request, form, opening_post,
85 ThreadView().new_post(request, form, opening_post,
84 html_response=False)
86 html_response=False)
85 else:
87 else:
86 status = STATUS_ERROR
88 status = STATUS_ERROR
87 errors = form.as_json_errors()
89 errors = form.as_json_errors()
88
90
89 response = {
91 response = {
90 'status': status,
92 'status': status,
91 'errors': errors,
93 'errors': errors,
92 }
94 }
93
95
94 return HttpResponse(content=json.dumps(response))
96 return HttpResponse(content=json.dumps(response))
95
97
96
98
97 def get_post(request, post_id):
99 def get_post(request, post_id):
98 """
100 """
99 Get the html of a post. Used for popups. Post can be truncated if used
101 Gets the html of a post. Used for popups. Post can be truncated if used
100 in threads list with 'truncated' get parameter.
102 in threads list with 'truncated' get parameter.
101 """
103 """
102
104
103 post = get_object_or_404(Post, id=post_id)
105 post = get_object_or_404(Post, id=post_id)
104
106
105 context = RequestContext(request)
107 context = RequestContext(request)
106 context['post'] = post
108 context['post'] = post
107 if PARAMETER_TRUNCATED in request.GET:
109 if PARAMETER_TRUNCATED in request.GET:
108 context[PARAMETER_TRUNCATED] = True
110 context[PARAMETER_TRUNCATED] = True
109
111
110 return render(request, 'boards/api_post.html', context)
112 return render(request, 'boards/api_post.html', context)
111
113
112
114
113 # TODO Test this
115 # TODO Test this
114 def api_get_threads(request, count):
116 def api_get_threads(request, count):
115 """
117 """
116 Get the JSON thread opening posts list.
118 Gets the JSON thread opening posts list.
117 Parameters that can be used for filtering:
119 Parameters that can be used for filtering:
118 tag, offset (from which thread to get results)
120 tag, offset (from which thread to get results)
119 """
121 """
120
122
121 if PARAMETER_TAG in request.GET:
123 if PARAMETER_TAG in request.GET:
122 tag_name = request.GET[PARAMETER_TAG]
124 tag_name = request.GET[PARAMETER_TAG]
123 if tag_name is not None:
125 if tag_name is not None:
124 tag = get_object_or_404(Tag, name=tag_name)
126 tag = get_object_or_404(Tag, name=tag_name)
125 threads = tag.threads.filter(archived=False)
127 threads = tag.threads.filter(archived=False)
126 else:
128 else:
127 threads = Thread.objects.filter(archived=False)
129 threads = Thread.objects.filter(archived=False)
128
130
129 if PARAMETER_OFFSET in request.GET:
131 if PARAMETER_OFFSET in request.GET:
130 offset = request.GET[PARAMETER_OFFSET]
132 offset = request.GET[PARAMETER_OFFSET]
131 offset = int(offset) if offset is not None else 0
133 offset = int(offset) if offset is not None else 0
132 else:
134 else:
133 offset = 0
135 offset = 0
134
136
135 threads = threads.order_by('-bump_time')
137 threads = threads.order_by('-bump_time')
136 threads = threads[offset:offset + int(count)]
138 threads = threads[offset:offset + int(count)]
137
139
138 opening_posts = []
140 opening_posts = []
139 for thread in threads:
141 for thread in threads:
140 opening_post = thread.get_opening_post()
142 opening_post = thread.get_opening_post()
141
143
142 # TODO Add tags, replies and images count
144 # TODO Add tags, replies and images count
143 opening_posts.append(_get_post_data(opening_post.id,
145 opening_posts.append(_get_post_data(opening_post.id,
144 include_last_update=True))
146 include_last_update=True))
145
147
146 return HttpResponse(content=json.dumps(opening_posts))
148 return HttpResponse(content=json.dumps(opening_posts))
147
149
148
150
149 # TODO Test this
151 # TODO Test this
150 def api_get_tags(request):
152 def api_get_tags(request):
151 """
153 """
152 Get all tags or user tags.
154 Gets all tags or user tags.
153 """
155 """
154
156
155 # TODO Get favorite tags for the given user ID
157 # TODO Get favorite tags for the given user ID
156
158
157 tags = Tag.objects.get_not_empty_tags()
159 tags = Tag.objects.get_not_empty_tags()
158 tag_names = []
160 tag_names = []
159 for tag in tags:
161 for tag in tags:
160 tag_names.append(tag.name)
162 tag_names.append(tag.name)
161
163
162 return HttpResponse(content=json.dumps(tag_names))
164 return HttpResponse(content=json.dumps(tag_names))
163
165
164
166
165 # TODO The result can be cached by the thread last update time
167 # TODO The result can be cached by the thread last update time
166 # TODO Test this
168 # TODO Test this
167 def api_get_thread_posts(request, opening_post_id):
169 def api_get_thread_posts(request, opening_post_id):
168 """
170 """
169 Get the JSON array of thread posts
171 Gets the JSON array of thread posts
170 """
172 """
171
173
172 opening_post = get_object_or_404(Post, id=opening_post_id)
174 opening_post = get_object_or_404(Post, id=opening_post_id)
173 thread = opening_post.thread_new
175 thread = opening_post.get_thread()
174 posts = thread.get_replies()
176 posts = thread.get_replies()
175
177
176 json_data = {
178 json_data = {
177 'posts': [],
179 'posts': [],
178 'last_update': None,
180 'last_update': None,
179 }
181 }
180 json_post_list = []
182 json_post_list = []
181
183
182 for post in posts:
184 for post in posts:
183 json_post_list.append(_get_post_data(post.id))
185 json_post_list.append(_get_post_data(post.id))
184 json_data['last_update'] = datetime_to_epoch(thread.last_edit_time)
186 json_data['last_update'] = datetime_to_epoch(thread.last_edit_time)
185 json_data['posts'] = json_post_list
187 json_data['posts'] = json_post_list
186
188
187 return HttpResponse(content=json.dumps(json_data))
189 return HttpResponse(content=json.dumps(json_data))
188
190
189
191
190 def api_get_post(request, post_id):
192 def api_get_post(request, post_id):
191 """
193 """
192 Get the JSON of a post. This can be
194 Gets the JSON of a post. This can be
193 used as and API for external clients.
195 used as and API for external clients.
194 """
196 """
195
197
196 post = get_object_or_404(Post, id=post_id)
198 post = get_object_or_404(Post, id=post_id)
197
199
198 json = serializers.serialize("json", [post], fields=(
200 json = serializers.serialize("json", [post], fields=(
199 "pub_time", "_text_rendered", "title", "text", "image",
201 "pub_time", "_text_rendered", "title", "text", "image",
200 "image_width", "image_height", "replies", "tags"
202 "image_width", "image_height", "replies", "tags"
201 ))
203 ))
202
204
203 return HttpResponse(content=json)
205 return HttpResponse(content=json)
204
206
205
207
206 # TODO Add pub time and replies
208 # TODO Add pub time and replies
207 def _get_post_data(post_id, format_type=DIFF_TYPE_JSON, request=None,
209 def _get_post_data(post_id, format_type=DIFF_TYPE_JSON, request=None,
208 include_last_update=False):
210 include_last_update=False):
209 if format_type == DIFF_TYPE_HTML:
211 if format_type == DIFF_TYPE_HTML:
210 return get_post(request, post_id).content.strip()
212 return get_post(request, post_id).content.strip()
211 elif format_type == DIFF_TYPE_JSON:
213 elif format_type == DIFF_TYPE_JSON:
212 post = get_object_or_404(Post, id=post_id)
214 post = get_object_or_404(Post, id=post_id)
213 post_json = {
215 post_json = {
214 'id': post.id,
216 'id': post.id,
215 'title': post.title,
217 'title': post.title,
216 'text': post.text.rendered,
218 'text': post.text.rendered,
217 }
219 }
218 if post.image:
220 if post.image:
219 post_json['image'] = post.image.url
221 post_json['image'] = post.image.url
220 post_json['image_preview'] = post.image.url_200x150
222 post_json['image_preview'] = post.image.url_200x150
221 if include_last_update:
223 if include_last_update:
222 post_json['bump_time'] = datetime_to_epoch(
224 post_json['bump_time'] = datetime_to_epoch(
223 post.thread_new.bump_time)
225 post.thread_new.bump_time)
224 return post_json
226 return post_json
General Comments 0
You need to be logged in to leave comments. Login now