##// END OF EJS Templates
Closed 1.6 branch
Closed 1.6 branch

File last commit:

r508:b567b57d 1.6-dev
r512:866075aa 1.6-dev
Show More
api.py
185 lines | 5.5 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
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
from boards.forms import ThreadForm, PlainErrorList
neko259
Added api for getting thread list
r499 from boards.models import Post, Thread, Tag
neko259
Moved get_post to an API views module
r491 from boards.views import _datetime_to_epoch, _new_post, \
neko259
Removed hiding the form row with CSS as it causes problems with error rows
r450 _ban_current_user
__author__ = 'neko259'
neko259
Api module refactoring, moved some values to the module scope
r501 PARAMETER_TRUNCATED = 'truncated'
PARAMETER_TAG = 'tag'
PARAMETER_OFFSET = 'offset'
neko259
Removed hiding the form row with CSS as it causes problems with error rows
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,
neko259
Style cleanup in api module
r504 }
neko259
Removed hiding the form row with CSS as it causes problems with error rows
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)
for post in added_posts:
json_data['added'].append(get_post(request, post.id).content.strip())
for post in updated_posts:
json_data['updated'].append(get_post(request, post.id).content.strip())
json_data['last_update'] = _datetime_to_epoch(thread.last_edit_time)
return HttpResponse(content=json.dumps(json_data))
# TODO This method needs to be implemented properly
# def api_add_post(request, form):
# """
# Add a post and return the JSON response for it
# """
#
# status = 'ok'
# errors = []
#
# if request.method == 'POST':
# form = ThreadForm(request.POST, request.FILES,
# error_class=PlainErrorList)
# form.session = request.session
#
# if form.is_valid():
# # TODO Don't form a response here cause we'll not need it
# _new_post(request, form)
# if form.need_to_ban:
# # Ban user because he is suspected to be a bot
# _ban_current_user(request)
# else:
# status = 'error'
# for field in form.fields:
# if field.errors:
# errors.append(field.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 """
Get the html of a post. Used for popups. Post can be truncated if used
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)
thread = post.thread_new
context = RequestContext(request)
neko259
Api module refactoring, moved some values to the module scope
r501 context['post'] = post
context['can_bump'] = thread.can_bump()
if PARAMETER_TRUNCATED in request.GET:
context[PARAMETER_TRUNCATED] = True
neko259
Moved get_post to an API views module
r491
neko259
Added api for getting thread list
r499 return render(request, 'boards/post.html', context)
neko259
Added api for getting thread posts
r502 # TODO Test this
neko259
Added api for getting thread list
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)
"""
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)
threads = tag.threads.filter(archived=False)
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()
# TODO Add pub time, tags, replies and images count
post_json = {
neko259
Style cleanup in api module
r504 'id': opening_post.id,
'title': opening_post.title,
'text': opening_post.text.rendered,
}
neko259
Added api for getting thread list
r499 if opening_post.image:
neko259
Fixed api methods returning post image
r508 post_json['image'] = opening_post.image.url
post_json['image_preview'] = opening_post.image.url_200x150
neko259
Added api for getting thread list
r499 opening_posts.append(post_json)
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):
"""
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))
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):
"""
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()
json_post_list = []
for post in posts:
# TODO Add pub time and replies
post_json = {
neko259
Style cleanup in api module
r504 'id': post.id,
'title': post.title,
'text': post.text.rendered,
}
neko259
Added api for getting thread posts
r502 if post.image:
neko259
Fixed api methods returning post image
r508 post_json['image'] = post.image.url
post_json['image_preview'] = post.image.url_200x150
neko259
Added api for getting thread posts
r502 json_post_list.append(post_json)
return HttpResponse(content=json.dumps(json_post_list))