|
|
from datetime import datetime
|
|
|
import json
|
|
|
from django.db import transaction
|
|
|
from django.http import HttpResponse
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
from django.utils import timezone
|
|
|
from boards.forms import ThreadForm, PlainErrorList
|
|
|
from boards.models import Post
|
|
|
from boards.views import get_post, _datetime_to_epoch, _new_post, \
|
|
|
_ban_current_user
|
|
|
|
|
|
__author__ = 'neko259'
|
|
|
|
|
|
|
|
|
@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,
|
|
|
}
|
|
|
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))
|
|
|
|