api.py
248 lines
| 7.2 KiB
| text/x-python
|
PythonLexer
neko259
|
r450 | from datetime import datetime | ||
import json | ||||
neko259
|
r639 | import logging | ||
neko259
|
r450 | from django.db import transaction | ||
from django.http import HttpResponse | ||||
neko259
|
r491 | from django.shortcuts import get_object_or_404, render | ||
from django.template import RequestContext | ||||
neko259
|
r450 | from django.utils import timezone | ||
neko259
|
r564 | from django.core import serializers | ||
neko259
|
r772 | from django.template.loader import render_to_string | ||
neko259
|
r564 | |||
neko259
|
r533 | from boards.forms import PostForm, PlainErrorList | ||
neko259
|
r499 | from boards.models import Post, Thread, Tag | ||
neko259
|
r542 | from boards.utils import datetime_to_epoch | ||
from boards.views.thread import ThreadView | ||||
neko259
|
r450 | |||
__author__ = 'neko259' | ||||
neko259
|
r501 | PARAMETER_TRUNCATED = 'truncated' | ||
PARAMETER_TAG = 'tag' | ||||
PARAMETER_OFFSET = 'offset' | ||||
neko259
|
r524 | PARAMETER_DIFF_TYPE = 'type' | ||
DIFF_TYPE_HTML = 'html' | ||||
DIFF_TYPE_JSON = 'json' | ||||
neko259
|
r501 | |||
neko259
|
r533 | STATUS_OK = 'ok' | ||
STATUS_ERROR = 'error' | ||||
neko259
|
r639 | logger = logging.getLogger(__name__) | ||
neko259
|
r450 | |||
@transaction.atomic | ||||
def api_get_threaddiff(request, thread_id, last_update_time): | ||||
neko259
|
r627 | """ | ||
Gets posts that were changed or added since time | ||||
""" | ||||
neko259
|
r450 | |||
neko259
|
r640 | thread = get_object_or_404(Post, id=thread_id).get_thread() | ||
neko259
|
r639 | |||
neko259
|
r800 | # Add 1 to ensure we don't load the same post over and over | ||
last_update_timestamp = float(last_update_time) + 1 | ||||
filter_time = datetime.fromtimestamp(last_update_timestamp / 1000000, | ||||
neko259
|
r450 | timezone.get_current_timezone()) | ||
json_data = { | ||||
'added': [], | ||||
'updated': [], | ||||
'last_update': None, | ||||
neko259
|
r504 | } | ||
neko259
|
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) | ||||
neko259
|
r524 | |||
diff_type = DIFF_TYPE_HTML | ||||
if PARAMETER_DIFF_TYPE in request.GET: | ||||
diff_type = request.GET[PARAMETER_DIFF_TYPE] | ||||
neko259
|
r450 | for post in added_posts: | ||
neko259
|
r524 | json_data['added'].append(_get_post_data(post.id, diff_type, request)) | ||
neko259
|
r450 | for post in updated_posts: | ||
neko259
|
r524 | json_data['updated'].append(_get_post_data(post.id, diff_type, request)) | ||
neko259
|
r542 | json_data['last_update'] = datetime_to_epoch(thread.last_edit_time) | ||
neko259
|
r450 | |||
return HttpResponse(content=json.dumps(json_data)) | ||||
neko259
|
r533 | def api_add_post(request, opening_post_id): | ||
""" | ||||
neko259
|
r627 | Adds a post and return the JSON response for it | ||
neko259
|
r533 | """ | ||
opening_post = get_object_or_404(Post, id=opening_post_id) | ||||
neko259
|
r640 | logger.info('Adding post via api...') | ||
neko259
|
r533 | status = STATUS_OK | ||
errors = [] | ||||
if request.method == 'POST': | ||||
neko259
|
r640 | form = PostForm(request.POST, request.FILES, error_class=PlainErrorList) | ||
neko259
|
r533 | form.session = request.session | ||
neko259
|
r638 | if form.need_to_ban: | ||
# Ban user because he is suspected to be a bot | ||||
# _ban_current_user(request) | ||||
status = STATUS_ERROR | ||||
neko259
|
r533 | if form.is_valid(): | ||
neko259
|
r640 | post = ThreadView().new_post(request, form, opening_post, | ||
neko259
|
r690 | html_response=False) | ||
neko259
|
r640 | if not post: | ||
neko259
|
r638 | status = STATUS_ERROR | ||
neko259
|
r640 | else: | ||
logger.info('Added post #%d via api.' % post.id) | ||||
neko259
|
r533 | else: | ||
status = STATUS_ERROR | ||||
errors = form.as_json_errors() | ||||
response = { | ||||
'status': status, | ||||
'errors': errors, | ||||
} | ||||
return HttpResponse(content=json.dumps(response)) | ||||
neko259
|
r499 | |||
neko259
|
r491 | def get_post(request, post_id): | ||
neko259
|
r499 | """ | ||
neko259
|
r627 | Gets the html of a post. Used for popups. Post can be truncated if used | ||
neko259
|
r499 | in threads list with 'truncated' get parameter. | ||
""" | ||||
neko259
|
r491 | |||
neko259
|
r639 | logger.info('Getting post #%s' % post_id) | ||
neko259
|
r491 | post = get_object_or_404(Post, id=post_id) | ||
context = RequestContext(request) | ||||
neko259
|
r501 | context['post'] = post | ||
if PARAMETER_TRUNCATED in request.GET: | ||||
context[PARAMETER_TRUNCATED] = True | ||||
neko259
|
r491 | |||
neko259
|
r584 | return render(request, 'boards/api_post.html', context) | ||
neko259
|
r499 | |||
neko259
|
r502 | # TODO Test this | ||
neko259
|
r499 | def api_get_threads(request, count): | ||
""" | ||||
neko259
|
r627 | Gets the JSON thread opening posts list. | ||
neko259
|
r499 | Parameters that can be used for filtering: | ||
tag, offset (from which thread to get results) | ||||
""" | ||||
neko259
|
r501 | if PARAMETER_TAG in request.GET: | ||
tag_name = request.GET[PARAMETER_TAG] | ||||
neko259
|
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) | ||||
neko259
|
r501 | if PARAMETER_OFFSET in request.GET: | ||
offset = request.GET[PARAMETER_OFFSET] | ||||
neko259
|
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() | ||||
neko259
|
r524 | # TODO Add tags, replies and images count | ||
neko259
|
r526 | opening_posts.append(_get_post_data(opening_post.id, | ||
neko259
|
r690 | include_last_update=True)) | ||
neko259
|
r499 | |||
return HttpResponse(content=json.dumps(opening_posts)) | ||||
neko259
|
r500 | |||
neko259
|
r502 | # TODO Test this | ||
neko259
|
r500 | def api_get_tags(request): | ||
""" | ||||
neko259
|
r627 | Gets all tags or user tags. | ||
neko259
|
r500 | """ | ||
# 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)) | ||||
neko259
|
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): | ||||
""" | ||||
neko259
|
r627 | Gets the JSON array of thread posts | ||
neko259
|
r502 | """ | ||
opening_post = get_object_or_404(Post, id=opening_post_id) | ||||
neko259
|
r627 | thread = opening_post.get_thread() | ||
neko259
|
r502 | posts = thread.get_replies() | ||
neko259
|
r525 | json_data = { | ||
'posts': [], | ||||
'last_update': None, | ||||
} | ||||
neko259
|
r502 | json_post_list = [] | ||
for post in posts: | ||||
neko259
|
r524 | json_post_list.append(_get_post_data(post.id)) | ||
neko259
|
r542 | json_data['last_update'] = datetime_to_epoch(thread.last_edit_time) | ||
neko259
|
r525 | json_data['posts'] = json_post_list | ||
neko259
|
r524 | |||
neko259
|
r525 | return HttpResponse(content=json.dumps(json_data)) | ||
neko259
|
r524 | |||
neko259
|
r564 | def api_get_post(request, post_id): | ||
""" | ||||
neko259
|
r627 | Gets the JSON of a post. This can be | ||
neko259
|
r564 | used as and API for external clients. | ||
""" | ||||
post = get_object_or_404(Post, id=post_id) | ||||
json = serializers.serialize("json", [post], fields=( | ||||
"pub_time", "_text_rendered", "title", "text", "image", | ||||
"image_width", "image_height", "replies", "tags" | ||||
)) | ||||
return HttpResponse(content=json) | ||||
neko259
|
r524 | # TODO Add pub time and replies | ||
neko259
|
r526 | def _get_post_data(post_id, format_type=DIFF_TYPE_JSON, request=None, | ||
include_last_update=False): | ||||
neko259
|
r524 | if format_type == DIFF_TYPE_HTML: | ||
neko259
|
r772 | post = get_object_or_404(Post, id=post_id) | ||
context = RequestContext(request) | ||||
context['post'] = post | ||||
if PARAMETER_TRUNCATED in request.GET: | ||||
context[PARAMETER_TRUNCATED] = True | ||||
return render_to_string('boards/api_post.html', context) | ||||
neko259
|
r524 | elif format_type == DIFF_TYPE_JSON: | ||
post = get_object_or_404(Post, id=post_id) | ||||
neko259
|
r502 | post_json = { | ||
neko259
|
r504 | 'id': post.id, | ||
'title': post.title, | ||||
'text': post.text.rendered, | ||||
} | ||||
neko259
|
r693 | if post.images.exists(): | ||
post_image = post.get_first_image() | ||||
post_json['image'] = post_image.image.url | ||||
post_json['image_preview'] = post_image.image.url_200x150 | ||||
neko259
|
r526 | if include_last_update: | ||
neko259
|
r542 | post_json['bump_time'] = datetime_to_epoch( | ||
neko259
|
r690 | post.thread_new.bump_time) | ||
neko259
|
r524 | return post_json | ||