api.py
205 lines
| 5.9 KiB
| text/x-python
|
PythonLexer
|
r450 | from datetime import datetime | ||
import json | ||||
from django.db import transaction | ||||
from django.http import HttpResponse | ||||
|
r491 | from django.shortcuts import get_object_or_404, render | ||
from django.template import RequestContext | ||||
|
r450 | from django.utils import timezone | ||
|
r533 | from boards.forms import PostForm, PlainErrorList | ||
|
r499 | from boards.models import Post, Thread, Tag | ||
|
r491 | from boards.views import _datetime_to_epoch, _new_post, \ | ||
|
r450 | _ban_current_user | ||
__author__ = 'neko259' | ||||
|
r501 | PARAMETER_TRUNCATED = 'truncated' | ||
PARAMETER_TAG = 'tag' | ||||
PARAMETER_OFFSET = 'offset' | ||||
|
r524 | PARAMETER_DIFF_TYPE = 'type' | ||
DIFF_TYPE_HTML = 'html' | ||||
DIFF_TYPE_JSON = 'json' | ||||
|
r501 | |||
|
r533 | STATUS_OK = 'ok' | ||
STATUS_ERROR = 'error' | ||||
|
r450 | |||
@transaction.atomic | ||||
def api_get_threaddiff(request, thread_id, last_update_time): | ||||
"""Get posts that were changed or added since time""" | ||||
thread = get_object_or_404(Post, id=thread_id).thread_new | ||||
filter_time = datetime.fromtimestamp(float(last_update_time) / 1000000, | ||||
timezone.get_current_timezone()) | ||||
json_data = { | ||||
'added': [], | ||||
'updated': [], | ||||
'last_update': None, | ||||
|
r504 | } | ||
|
r450 | added_posts = Post.objects.filter(thread_new=thread, | ||
pub_time__gt=filter_time) \ | ||||
.order_by('pub_time') | ||||
updated_posts = Post.objects.filter(thread_new=thread, | ||||
pub_time__lte=filter_time, | ||||
last_edit_time__gt=filter_time) | ||||
|
r524 | |||
diff_type = DIFF_TYPE_HTML | ||||
if PARAMETER_DIFF_TYPE in request.GET: | ||||
diff_type = request.GET[PARAMETER_DIFF_TYPE] | ||||
|
r450 | for post in added_posts: | ||
|
r524 | json_data['added'].append(_get_post_data(post.id, diff_type, request)) | ||
|
r450 | for post in updated_posts: | ||
|
r524 | json_data['updated'].append(_get_post_data(post.id, diff_type, request)) | ||
|
r450 | json_data['last_update'] = _datetime_to_epoch(thread.last_edit_time) | ||
return HttpResponse(content=json.dumps(json_data)) | ||||
|
r533 | def api_add_post(request, opening_post_id): | ||
""" | ||||
Add a post and return the JSON response for it | ||||
""" | ||||
opening_post = get_object_or_404(Post, id=opening_post_id) | ||||
status = STATUS_OK | ||||
errors = [] | ||||
if request.method == 'POST': | ||||
form = PostForm(request.POST, request.FILES, | ||||
error_class=PlainErrorList) | ||||
form.session = request.session | ||||
#if form.need_to_ban: | ||||
# # Ban user because he is suspected to be a bot | ||||
# _ban_current_user(request) | ||||
# status = STATUS_ERROR | ||||
if form.is_valid(): | ||||
_new_post(request, form, opening_post, html_response=False) | ||||
else: | ||||
status = STATUS_ERROR | ||||
errors = form.as_json_errors() | ||||
response = { | ||||
'status': status, | ||||
'errors': errors, | ||||
} | ||||
return HttpResponse(content=json.dumps(response)) | ||||
|
r499 | |||
|
r491 | def get_post(request, post_id): | ||
|
r499 | """ | ||
Get the html of a post. Used for popups. Post can be truncated if used | ||||
in threads list with 'truncated' get parameter. | ||||
""" | ||||
|
r491 | |||
post = get_object_or_404(Post, id=post_id) | ||||
context = RequestContext(request) | ||||
|
r501 | context['post'] = post | ||
if PARAMETER_TRUNCATED in request.GET: | ||||
context[PARAMETER_TRUNCATED] = True | ||||
|
r491 | |||
|
r499 | return render(request, 'boards/post.html', context) | ||
|
r502 | # TODO Test this | ||
|
r499 | def api_get_threads(request, count): | ||
""" | ||||
Get the JSON thread opening posts list. | ||||
Parameters that can be used for filtering: | ||||
tag, offset (from which thread to get results) | ||||
""" | ||||
|
r501 | if PARAMETER_TAG in request.GET: | ||
tag_name = request.GET[PARAMETER_TAG] | ||||
|
r499 | if tag_name is not None: | ||
tag = get_object_or_404(Tag, name=tag_name) | ||||
threads = tag.threads.filter(archived=False) | ||||
else: | ||||
threads = Thread.objects.filter(archived=False) | ||||
|
r501 | if PARAMETER_OFFSET in request.GET: | ||
offset = request.GET[PARAMETER_OFFSET] | ||||
|
r499 | offset = int(offset) if offset is not None else 0 | ||
else: | ||||
offset = 0 | ||||
threads = threads.order_by('-bump_time') | ||||
threads = threads[offset:offset + int(count)] | ||||
opening_posts = [] | ||||
for thread in threads: | ||||
opening_post = thread.get_opening_post() | ||||
|
r524 | # TODO Add tags, replies and images count | ||
|
r526 | opening_posts.append(_get_post_data(opening_post.id, | ||
include_last_update=True)) | ||||
|
r499 | |||
return HttpResponse(content=json.dumps(opening_posts)) | ||||
|
r500 | |||
|
r502 | # TODO Test this | ||
|
r500 | def api_get_tags(request): | ||
""" | ||||
Get all tags or user tags. | ||||
""" | ||||
# TODO Get favorite tags for the given user ID | ||||
tags = Tag.objects.get_not_empty_tags() | ||||
tag_names = [] | ||||
for tag in tags: | ||||
tag_names.append(tag.name) | ||||
return HttpResponse(content=json.dumps(tag_names)) | ||||
|
r502 | |||
# TODO The result can be cached by the thread last update time | ||||
# TODO Test this | ||||
def api_get_thread_posts(request, opening_post_id): | ||||
""" | ||||
Get the JSON array of thread posts | ||||
""" | ||||
opening_post = get_object_or_404(Post, id=opening_post_id) | ||||
thread = opening_post.thread_new | ||||
posts = thread.get_replies() | ||||
|
r525 | json_data = { | ||
'posts': [], | ||||
'last_update': None, | ||||
} | ||||
|
r502 | json_post_list = [] | ||
for post in posts: | ||||
|
r524 | json_post_list.append(_get_post_data(post.id)) | ||
|
r525 | json_data['last_update'] = _datetime_to_epoch(thread.last_edit_time) | ||
json_data['posts'] = json_post_list | ||||
|
r524 | |||
|
r525 | return HttpResponse(content=json.dumps(json_data)) | ||
|
r524 | |||
# TODO Add pub time and replies | ||||
|
r526 | def _get_post_data(post_id, format_type=DIFF_TYPE_JSON, request=None, | ||
include_last_update=False): | ||||
|
r524 | if format_type == DIFF_TYPE_HTML: | ||
return get_post(request, post_id).content.strip() | ||||
elif format_type == DIFF_TYPE_JSON: | ||||
post = get_object_or_404(Post, id=post_id) | ||||
|
r502 | post_json = { | ||
|
r504 | 'id': post.id, | ||
'title': post.title, | ||||
'text': post.text.rendered, | ||||
} | ||||
|
r502 | if post.image: | ||
|
r508 | post_json['image'] = post.image.url | ||
post_json['image_preview'] = post.image.url_200x150 | ||||
|
r526 | if include_last_update: | ||
post_json['bump_time'] = _datetime_to_epoch( | ||||
post.thread_new.bump_time) | ||||
|
r524 | return post_json | ||