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