##// END OF EJS Templates
Don't use natural time because it does not update itself on the client. Fixed...
Don't use natural time because it does not update itself on the client. Fixed tags validation

File last commit:

r1086:0eaec605 default
r1102:59933a18 default
Show More
api.py
241 lines | 7.0 KiB | text/x-python | PythonLexer
neko259
Removed hiding the form row with CSS as it causes problems with error rows
r450 from datetime import datetime
import json
neko259
Added some logging
r639 import logging
neko259
Removed hiding the form row with CSS as it causes problems with error rows
r450 from django.db import transaction
from django.http import HttpResponse
neko259
Moved get_post to an API views module
r491 from django.shortcuts import get_object_or_404, render
from django.template import RequestContext
neko259
Removed hiding the form row with CSS as it causes problems with error rows
r450 from django.utils import timezone
neko259
Fixed tests failing with error 404 handler not imported
r564 from django.core import serializers
neko259
Added posting over ajax
r533 from boards.forms import PostForm, PlainErrorList
neko259
Added api for getting thread list
r499 from boards.models import Post, Thread, Tag
neko259
Rewriting views to class-based
r542 from boards.utils import datetime_to_epoch
from boards.views.thread import ThreadView
neko259
Added notification API
r994 from boards.models.user import Notification
neko259
Removed hiding the form row with CSS as it causes problems with error rows
r450
__author__ = 'neko259'
neko259
Api module refactoring, moved some values to the module scope
r501 PARAMETER_TRUNCATED = 'truncated'
PARAMETER_TAG = 'tag'
PARAMETER_OFFSET = 'offset'
neko259
Added api for thread update in json
r524 PARAMETER_DIFF_TYPE = 'type'
neko259
Use dict instead of context instance in post view API
r1075 PARAMETER_POST = 'post'
neko259
Move attribute names in api to constants
r1076 PARAMETER_ADDED = 'added'
PARAMETER_UPDATED = 'updated'
neko259
Updated API to use a new timestamp format for getting updated posts and get...
r1086 PARAMETER_LAST_UPDATE = 'last_update'
neko259
Added api for thread update in json
r524
DIFF_TYPE_HTML = 'html'
DIFF_TYPE_JSON = 'json'
neko259
Api module refactoring, moved some values to the module scope
r501
neko259
Added posting over ajax
r533 STATUS_OK = 'ok'
STATUS_ERROR = 'error'
neko259
Added some logging
r639 logger = logging.getLogger(__name__)
neko259
Removed hiding the form row with CSS as it causes problems with error rows
r450
@transaction.atomic
neko259
Updated API to use a new timestamp format for getting updated posts and get...
r1086 def api_get_threaddiff(request):
neko259
Small changes to the docstrings of the api module
r627 """
Gets posts that were changed or added since time
"""
neko259
Removed hiding the form row with CSS as it causes problems with error rows
r450
neko259
Updated API to use a new timestamp format for getting updated posts and get...
r1086 thread_id = request.GET.get('thread')
last_update_time = request.GET.get('last_update')
last_post = request.GET.get('last_post')
neko259
Added some logging
r639
neko259
Updated API to use a new timestamp format for getting updated posts and get...
r1086 thread = get_object_or_404(Post, id=thread_id).get_thread()
neko259
Removed hiding the form row with CSS as it causes problems with error rows
r450
json_data = {
neko259
Move attribute names in api to constants
r1076 PARAMETER_ADDED: [],
PARAMETER_UPDATED: [],
neko259
Removed hiding the form row with CSS as it causes problems with error rows
r450 'last_update': None,
neko259
Style cleanup in api module
r504 }
neko259
Allow connecting post to many threads
r979 added_posts = Post.objects.filter(threads__in=[thread],
neko259
Updated API to use a new timestamp format for getting updated posts and get...
r1086 id__gt=int(last_post)) \
.order_by('pub_time')
neko259
Allow connecting post to many threads
r979 updated_posts = Post.objects.filter(threads__in=[thread],
neko259
Updated API to use a new timestamp format for getting updated posts and get...
r1086 pub_time__lte=last_update_time,
last_edit_time__gt=last_update_time)
neko259
Added api for thread update in json
r524
neko259
Refactoring
r917 diff_type = request.GET.get(PARAMETER_DIFF_TYPE, DIFF_TYPE_HTML)
neko259
Added api for thread update in json
r524
neko259
Removed hiding the form row with CSS as it causes problems with error rows
r450 for post in added_posts:
neko259
Move attribute names in api to constants
r1076 json_data[PARAMETER_ADDED].append(get_post_data(post.id, diff_type, request))
neko259
Removed hiding the form row with CSS as it causes problems with error rows
r450 for post in updated_posts:
neko259
Move attribute names in api to constants
r1076 json_data[PARAMETER_UPDATED].append(get_post_data(post.id, diff_type, request))
neko259
Updated API to use a new timestamp format for getting updated posts and get...
r1086 json_data[PARAMETER_LAST_UPDATE] = str(thread.last_edit_time)
neko259
Removed hiding the form row with CSS as it causes problems with error rows
r450
return HttpResponse(content=json.dumps(json_data))
neko259
Added posting over ajax
r533 def api_add_post(request, opening_post_id):
"""
neko259
Small changes to the docstrings of the api module
r627 Adds a post and return the JSON response for it
neko259
Added posting over ajax
r533 """
opening_post = get_object_or_404(Post, id=opening_post_id)
neko259
Tweaked API logging
r640 logger.info('Adding post via api...')
neko259
Added posting over ajax
r533 status = STATUS_OK
errors = []
if request.method == 'POST':
neko259
Tweaked API logging
r640 form = PostForm(request.POST, request.FILES, error_class=PlainErrorList)
neko259
Added posting over ajax
r533 form.session = request.session
neko259
Make status an error if the user is banned in api_add_post
r638 if form.need_to_ban:
# Ban user because he is suspected to be a bot
# _ban_current_user(request)
status = STATUS_ERROR
neko259
Added posting over ajax
r533 if form.is_valid():
neko259
Tweaked API logging
r640 post = ThreadView().new_post(request, form, opening_post,
neko259
Implemented search over posts. Moved get_user and get_theme to utils module. Use context processors instead of creating context in the base view. Removed unused imports in some modules
r690 html_response=False)
neko259
Tweaked API logging
r640 if not post:
neko259
Make status an error if the user is banned in api_add_post
r638 status = STATUS_ERROR
neko259
Tweaked API logging
r640 else:
logger.info('Added post #%d via api.' % post.id)
neko259
Added posting over ajax
r533 else:
status = STATUS_ERROR
errors = form.as_json_errors()
response = {
'status': status,
'errors': errors,
}
return HttpResponse(content=json.dumps(response))
neko259
Added api for getting thread list
r499
neko259
Moved get_post to an API views module
r491 def get_post(request, post_id):
neko259
Added api for getting thread list
r499 """
neko259
Small changes to the docstrings of the api module
r627 Gets the html of a post. Used for popups. Post can be truncated if used
neko259
Added api for getting thread list
r499 in threads list with 'truncated' get parameter.
"""
neko259
Moved get_post to an API views module
r491
post = get_object_or_404(Post, id=post_id)
neko259
Use dict instead of context instance in post view API
r1075 params = dict()
params[PARAMETER_POST] = post
neko259
Api module refactoring, moved some values to the module scope
r501 if PARAMETER_TRUNCATED in request.GET:
neko259
Use dict instead of context instance in post view API
r1075 params[PARAMETER_TRUNCATED] = True
neko259
Moved get_post to an API views module
r491
neko259
Use dict instead of context instance in post view API
r1075 return render(request, 'boards/api_post.html', params)
neko259
Added api for getting thread list
r499
def api_get_threads(request, count):
"""
neko259
Small changes to the docstrings of the api module
r627 Gets the JSON thread opening posts list.
neko259
Added api for getting thread list
r499 Parameters that can be used for filtering:
tag, offset (from which thread to get results)
"""
neko259
Api module refactoring, moved some values to the module scope
r501 if PARAMETER_TAG in request.GET:
tag_name = request.GET[PARAMETER_TAG]
neko259
Added api for getting thread list
r499 if tag_name is not None:
tag = get_object_or_404(Tag, name=tag_name)
neko259
Use threads' tags list to aggregate threads by tag
r908 threads = tag.get_threads().filter(archived=False)
neko259
Added api for getting thread list
r499 else:
threads = Thread.objects.filter(archived=False)
neko259
Api module refactoring, moved some values to the module scope
r501 if PARAMETER_OFFSET in request.GET:
offset = request.GET[PARAMETER_OFFSET]
neko259
Added api for getting thread list
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
Added api for thread update in json
r524 # TODO Add tags, replies and images count
neko259
Added bumpable and archived attributes to the thread API output
r976 post_data = get_post_data(opening_post.id, include_last_update=True)
post_data['bumpable'] = thread.can_bump()
post_data['archived'] = thread.archived
opening_posts.append(post_data)
neko259
Added api for getting thread list
r499
return HttpResponse(content=json.dumps(opening_posts))
neko259
Added api for the tags list
r500
neko259
Added api for getting thread posts
r502 # TODO Test this
neko259
Added api for the tags list
r500 def api_get_tags(request):
"""
neko259
Small changes to the docstrings of the api module
r627 Gets all tags or user tags.
neko259
Added api for the tags list
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
Added api for getting thread posts
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
Small changes to the docstrings of the api module
r627 Gets the JSON array of thread posts
neko259
Added api for getting thread posts
r502 """
opening_post = get_object_or_404(Post, id=opening_post_id)
neko259
Small changes to the docstrings of the api module
r627 thread = opening_post.get_thread()
neko259
Added api for getting thread posts
r502 posts = thread.get_replies()
neko259
Added last update time to the initial thread result in api
r525 json_data = {
'posts': [],
'last_update': None,
}
neko259
Added api for getting thread posts
r502 json_post_list = []
for post in posts:
neko259
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853 json_post_list.append(get_post_data(post.id))
neko259
Rewriting views to class-based
r542 json_data['last_update'] = datetime_to_epoch(thread.last_edit_time)
neko259
Added last update time to the initial thread result in api
r525 json_data['posts'] = json_post_list
neko259
Added api for thread update in json
r524
neko259
Added last update time to the initial thread result in api
r525 return HttpResponse(content=json.dumps(json_data))
neko259
Added api for thread update in json
r524
neko259
Added notification API
r994 def api_get_notifications(request, username):
last_notification_id_str = request.GET.get('last', None)
last_id = int(last_notification_id_str) if last_notification_id_str is not None else None
posts = Notification.objects.get_notification_posts(username=username,
last=last_id)
json_post_list = []
for post in posts:
json_post_list.append(get_post_data(post.id))
return HttpResponse(content=json.dumps(json_post_list))
neko259
Fixed tests failing with error 404 handler not imported
r564 def api_get_post(request, post_id):
"""
neko259
Small changes to the docstrings of the api module
r627 Gets the JSON of a post. This can be
neko259
Fixed tests failing with error 404 handler not imported
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
Added centrifuge (websocket) support for thread autoupdate. Only websocket version is supported for now
r853 # TODO Remove this method and use post method directly
def get_post_data(post_id, format_type=DIFF_TYPE_JSON, request=None,
include_last_update=False):
post = get_object_or_404(Post, id=post_id)
return post.get_post_data(format_type=format_type, request=request,
include_last_update=include_last_update)