2011-10-07 08:47:19 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division
|
|
|
|
|
|
|
|
import ox
|
|
|
|
from ox.utils import json
|
|
|
|
from ox.django.decorators import login_required_json
|
|
|
|
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response
|
|
|
|
|
|
|
|
|
2012-01-03 20:18:47 +00:00
|
|
|
from ox.django.api import actions
|
2011-10-07 08:47:19 +00:00
|
|
|
|
|
|
|
import models
|
|
|
|
|
2012-02-18 11:29:26 +00:00
|
|
|
def getNews(request):
|
2011-10-07 08:47:19 +00:00
|
|
|
'''
|
2013-03-04 19:35:06 +00:00
|
|
|
takes {
|
|
|
|
id: string
|
2011-10-07 08:47:19 +00:00
|
|
|
}
|
|
|
|
|
2013-03-04 19:35:06 +00:00
|
|
|
returns {
|
|
|
|
id: string,
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
if not id is passed, return all news items
|
|
|
|
|
|
|
|
takes {}
|
|
|
|
returns {
|
|
|
|
items: [object]
|
2011-10-07 08:47:19 +00:00
|
|
|
}
|
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
response = json_response()
|
2012-02-18 11:59:24 +00:00
|
|
|
if 'id' in data:
|
2012-02-18 15:39:53 +00:00
|
|
|
news = get_object_or_404_json(models.News, id=ox.fromAZ(data['id']))
|
2012-02-18 11:59:24 +00:00
|
|
|
response['data'] = news.json()
|
|
|
|
else:
|
|
|
|
qs = models.News.objects.all().order_by('-date')
|
|
|
|
response['data']['items'] = [p.json() for p in qs]
|
|
|
|
return render_to_json_response(response)
|
2012-02-18 11:29:26 +00:00
|
|
|
actions.register(getNews)
|
2011-10-07 08:47:19 +00:00
|
|
|
|
|
|
|
@login_required_json
|
|
|
|
def addNews(request):
|
|
|
|
'''
|
2013-03-04 19:35:06 +00:00
|
|
|
takes {
|
2011-10-07 08:47:19 +00:00
|
|
|
title: string,
|
2012-02-18 11:29:26 +00:00
|
|
|
date: string,
|
|
|
|
text: text,
|
2011-10-07 08:47:19 +00:00
|
|
|
}
|
2013-03-04 19:35:06 +00:00
|
|
|
returns {
|
|
|
|
id: string,
|
|
|
|
...
|
2011-10-07 08:47:19 +00:00
|
|
|
}
|
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
|
2012-02-18 11:29:26 +00:00
|
|
|
news = models.News()
|
|
|
|
for key in ('title', 'text', 'date'):
|
|
|
|
if key in data:
|
|
|
|
setattr(news, key, data[key])
|
2011-10-07 08:47:19 +00:00
|
|
|
news.save()
|
|
|
|
response = json_response(news.json())
|
|
|
|
return render_to_json_response(response)
|
|
|
|
actions.register(addNews, cache=False)
|
|
|
|
|
|
|
|
@login_required_json
|
|
|
|
def removeNews(request):
|
|
|
|
'''
|
2013-03-04 19:35:06 +00:00
|
|
|
takes {
|
2011-10-07 08:47:19 +00:00
|
|
|
ids: []
|
|
|
|
}
|
2013-03-04 19:35:06 +00:00
|
|
|
returns {}
|
2011-10-07 08:47:19 +00:00
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
2012-02-18 11:59:24 +00:00
|
|
|
response = json_response({})
|
2012-02-18 15:39:53 +00:00
|
|
|
news = get_object_or_404_json(models.News, id=ox.fromAZ(data['id']))
|
2012-02-18 11:59:24 +00:00
|
|
|
if news.editable(request.user):
|
|
|
|
news.delete()
|
|
|
|
response = json_response(status=200, text='news removed')
|
|
|
|
else:
|
2011-10-07 08:47:19 +00:00
|
|
|
response = json_response(status=403, text='permission denied')
|
|
|
|
return render_to_json_response(response)
|
|
|
|
actions.register(removeNews, cache=False)
|
|
|
|
|
|
|
|
@login_required_json
|
|
|
|
def editNews(request):
|
|
|
|
'''
|
2013-03-04 19:35:06 +00:00
|
|
|
takes {
|
|
|
|
id: string,
|
|
|
|
title: string,
|
|
|
|
text: string,
|
|
|
|
date: string
|
2011-10-07 08:47:19 +00:00
|
|
|
}
|
2013-03-04 19:35:06 +00:00
|
|
|
returns {
|
|
|
|
id: string
|
|
|
|
...
|
2011-10-07 08:47:19 +00:00
|
|
|
}
|
|
|
|
'''
|
|
|
|
response = json_response({})
|
|
|
|
data = json.loads(request.POST['data'])
|
2011-12-18 09:35:49 +00:00
|
|
|
n = get_object_or_404_json(models.News, id=ox.fromAZ(data['id']))
|
2011-10-07 08:47:19 +00:00
|
|
|
if n.editable(request.user):
|
2012-02-18 11:29:26 +00:00
|
|
|
for key in ('title', 'text', 'date'):
|
2011-10-07 08:47:19 +00:00
|
|
|
if key in data:
|
|
|
|
setattr(n, key, data[key])
|
|
|
|
n.save()
|
|
|
|
response['data'] = n.json()
|
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permission denied')
|
|
|
|
return render_to_json_response(response)
|
|
|
|
actions.register(editNews, cache=False)
|