##// END OF EJS Templates
Style cleanup in api module
neko259 -
r504:dab23797 1.6-dev
parent child Browse files
Show More
@@ -1,189 +1,189 b''
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 boards.forms import ThreadForm, PlainErrorList
8 from boards.forms import ThreadForm, PlainErrorList
9 from boards.models import Post, Thread, Tag
9 from boards.models import Post, Thread, Tag
10 from boards.views import _datetime_to_epoch, _new_post, \
10 from boards.views import _datetime_to_epoch, _new_post, \
11 _ban_current_user
11 _ban_current_user
12
12
13 __author__ = 'neko259'
13 __author__ = 'neko259'
14
14
15 PARAMETER_TRUNCATED = 'truncated'
15 PARAMETER_TRUNCATED = 'truncated'
16 PARAMETER_TAG = 'tag'
16 PARAMETER_TAG = 'tag'
17 PARAMETER_OFFSET = 'offset'
17 PARAMETER_OFFSET = 'offset'
18
18
19
19
20 @transaction.atomic
20 @transaction.atomic
21 def api_get_threaddiff(request, thread_id, last_update_time):
21 def api_get_threaddiff(request, thread_id, last_update_time):
22 """Get posts that were changed or added since time"""
22 """Get posts that were changed or added since time"""
23
23
24 thread = get_object_or_404(Post, id=thread_id).thread_new
24 thread = get_object_or_404(Post, id=thread_id).thread_new
25
25
26 filter_time = datetime.fromtimestamp(float(last_update_time) / 1000000,
26 filter_time = datetime.fromtimestamp(float(last_update_time) / 1000000,
27 timezone.get_current_timezone())
27 timezone.get_current_timezone())
28
28
29 json_data = {
29 json_data = {
30 'added': [],
30 'added': [],
31 'updated': [],
31 'updated': [],
32 'last_update': None,
32 'last_update': None,
33 }
33 }
34 added_posts = Post.objects.filter(thread_new=thread,
34 added_posts = Post.objects.filter(thread_new=thread,
35 pub_time__gt=filter_time) \
35 pub_time__gt=filter_time) \
36 .order_by('pub_time')
36 .order_by('pub_time')
37 updated_posts = Post.objects.filter(thread_new=thread,
37 updated_posts = Post.objects.filter(thread_new=thread,
38 pub_time__lte=filter_time,
38 pub_time__lte=filter_time,
39 last_edit_time__gt=filter_time)
39 last_edit_time__gt=filter_time)
40 for post in added_posts:
40 for post in added_posts:
41 json_data['added'].append(get_post(request, post.id).content.strip())
41 json_data['added'].append(get_post(request, post.id).content.strip())
42 for post in updated_posts:
42 for post in updated_posts:
43 json_data['updated'].append(get_post(request, post.id).content.strip())
43 json_data['updated'].append(get_post(request, post.id).content.strip())
44 json_data['last_update'] = _datetime_to_epoch(thread.last_edit_time)
44 json_data['last_update'] = _datetime_to_epoch(thread.last_edit_time)
45
45
46 return HttpResponse(content=json.dumps(json_data))
46 return HttpResponse(content=json.dumps(json_data))
47
47
48
48
49 # TODO This method needs to be implemented properly
49 # TODO This method needs to be implemented properly
50 # def api_add_post(request, form):
50 # def api_add_post(request, form):
51 # """
51 # """
52 # Add a post and return the JSON response for it
52 # Add a post and return the JSON response for it
53 # """
53 # """
54 #
54 #
55 # status = 'ok'
55 # status = 'ok'
56 # errors = []
56 # errors = []
57 #
57 #
58 # if request.method == 'POST':
58 # if request.method == 'POST':
59 # form = ThreadForm(request.POST, request.FILES,
59 # form = ThreadForm(request.POST, request.FILES,
60 # error_class=PlainErrorList)
60 # error_class=PlainErrorList)
61 # form.session = request.session
61 # form.session = request.session
62 #
62 #
63 # if form.is_valid():
63 # if form.is_valid():
64 # # TODO Don't form a response here cause we'll not need it
64 # # TODO Don't form a response here cause we'll not need it
65 # _new_post(request, form)
65 # _new_post(request, form)
66 # if form.need_to_ban:
66 # if form.need_to_ban:
67 # # Ban user because he is suspected to be a bot
67 # # Ban user because he is suspected to be a bot
68 # _ban_current_user(request)
68 # _ban_current_user(request)
69 # else:
69 # else:
70 # status = 'error'
70 # status = 'error'
71 # for field in form.fields:
71 # for field in form.fields:
72 # if field.errors:
72 # if field.errors:
73 # errors.append(field.errors)
73 # errors.append(field.errors)
74 #
74 #
75 # response = {
75 # response = {
76 # 'status': status,
76 # 'status': status,
77 # 'errors': errors,
77 # 'errors': errors,
78 # }
78 # }
79 #
79 #
80 # return HttpResponse(content=json.dumps(response))
80 # return HttpResponse(content=json.dumps(response))
81
81
82
82
83 def get_post(request, post_id):
83 def get_post(request, post_id):
84 """
84 """
85 Get the html of a post. Used for popups. Post can be truncated if used
85 Get the html of a post. Used for popups. Post can be truncated if used
86 in threads list with 'truncated' get parameter.
86 in threads list with 'truncated' get parameter.
87 """
87 """
88
88
89 post = get_object_or_404(Post, id=post_id)
89 post = get_object_or_404(Post, id=post_id)
90 thread = post.thread_new
90 thread = post.thread_new
91
91
92 context = RequestContext(request)
92 context = RequestContext(request)
93 context['post'] = post
93 context['post'] = post
94 context['can_bump'] = thread.can_bump()
94 context['can_bump'] = thread.can_bump()
95 if PARAMETER_TRUNCATED in request.GET:
95 if PARAMETER_TRUNCATED in request.GET:
96 context[PARAMETER_TRUNCATED] = True
96 context[PARAMETER_TRUNCATED] = True
97
97
98 return render(request, 'boards/post.html', context)
98 return render(request, 'boards/post.html', context)
99
99
100
100
101 # TODO Test this
101 # TODO Test this
102 def api_get_threads(request, count):
102 def api_get_threads(request, count):
103 """
103 """
104 Get the JSON thread opening posts list.
104 Get the JSON thread opening posts list.
105 Parameters that can be used for filtering:
105 Parameters that can be used for filtering:
106 tag, offset (from which thread to get results)
106 tag, offset (from which thread to get results)
107 """
107 """
108
108
109 if PARAMETER_TAG in request.GET:
109 if PARAMETER_TAG in request.GET:
110 tag_name = request.GET[PARAMETER_TAG]
110 tag_name = request.GET[PARAMETER_TAG]
111 if tag_name is not None:
111 if tag_name is not None:
112 tag = get_object_or_404(Tag, name=tag_name)
112 tag = get_object_or_404(Tag, name=tag_name)
113 threads = tag.threads.filter(archived=False)
113 threads = tag.threads.filter(archived=False)
114 else:
114 else:
115 threads = Thread.objects.filter(archived=False)
115 threads = Thread.objects.filter(archived=False)
116
116
117 if PARAMETER_OFFSET in request.GET:
117 if PARAMETER_OFFSET in request.GET:
118 offset = request.GET[PARAMETER_OFFSET]
118 offset = request.GET[PARAMETER_OFFSET]
119 offset = int(offset) if offset is not None else 0
119 offset = int(offset) if offset is not None else 0
120 else:
120 else:
121 offset = 0
121 offset = 0
122
122
123 threads = threads.order_by('-bump_time')
123 threads = threads.order_by('-bump_time')
124 threads = threads[offset:offset + int(count)]
124 threads = threads[offset:offset + int(count)]
125
125
126 opening_posts = []
126 opening_posts = []
127 for thread in threads:
127 for thread in threads:
128 opening_post = thread.get_opening_post()
128 opening_post = thread.get_opening_post()
129
129
130 # TODO Add pub time, tags, replies and images count
130 # TODO Add pub time, tags, replies and images count
131 post_json = {
131 post_json = {
132 'id' : opening_post.id,
132 'id': opening_post.id,
133 'title' : opening_post.title,
133 'title': opening_post.title,
134 'text' : opening_post.text.rendered,
134 'text': opening_post.text.rendered,
135 }
135 }
136 if opening_post.image:
136 if opening_post.image:
137 post_json += {
137 post_json += {
138 'image' : opening_post.image.url,
138 'image': opening_post.image.url,
139 'image_preview' : opening_post.image.url_200x150,
139 'image_preview': opening_post.image.url_200x150,
140 }
140 }
141 opening_posts.append(post_json)
141 opening_posts.append(post_json)
142
142
143 return HttpResponse(content=json.dumps(opening_posts))
143 return HttpResponse(content=json.dumps(opening_posts))
144
144
145
145
146 # TODO Test this
146 # TODO Test this
147 def api_get_tags(request):
147 def api_get_tags(request):
148 """
148 """
149 Get all tags or user tags.
149 Get all tags or user tags.
150 """
150 """
151
151
152 # TODO Get favorite tags for the given user ID
152 # TODO Get favorite tags for the given user ID
153
153
154 tags = Tag.objects.get_not_empty_tags()
154 tags = Tag.objects.get_not_empty_tags()
155 tag_names = []
155 tag_names = []
156 for tag in tags:
156 for tag in tags:
157 tag_names.append(tag.name)
157 tag_names.append(tag.name)
158
158
159 return HttpResponse(content=json.dumps(tag_names))
159 return HttpResponse(content=json.dumps(tag_names))
160
160
161
161
162 # TODO The result can be cached by the thread last update time
162 # TODO The result can be cached by the thread last update time
163 # TODO Test this
163 # TODO Test this
164 def api_get_thread_posts(request, opening_post_id):
164 def api_get_thread_posts(request, opening_post_id):
165 """
165 """
166 Get the JSON array of thread posts
166 Get the JSON array of thread posts
167 """
167 """
168
168
169 opening_post = get_object_or_404(Post, id=opening_post_id)
169 opening_post = get_object_or_404(Post, id=opening_post_id)
170 thread = opening_post.thread_new
170 thread = opening_post.thread_new
171 posts = thread.get_replies()
171 posts = thread.get_replies()
172
172
173 json_post_list = []
173 json_post_list = []
174
174
175 for post in posts:
175 for post in posts:
176 # TODO Add pub time and replies
176 # TODO Add pub time and replies
177 post_json = {
177 post_json = {
178 'id' : post.id,
178 'id': post.id,
179 'title' : post.title,
179 'title': post.title,
180 'text' : post.text.rendered,
180 'text': post.text.rendered,
181 }
181 }
182 if post.image:
182 if post.image:
183 post_json += {
183 post_json += {
184 'image' : post.image.url,
184 'image': post.image.url,
185 'image_preview' : post.image.url_200x150,
185 'image_preview': post.image.url_200x150,
186 }
186 }
187 json_post_list.append(post_json)
187 json_post_list.append(post_json)
188
188
189 return HttpResponse(content=json.dumps(json_post_list))
189 return HttpResponse(content=json.dumps(json_post_list))
General Comments 0
You need to be logged in to leave comments. Login now