pandora/pandora/news/views.py

121 lines
3.4 KiB
Python
Raw Normal View History

2011-10-07 08:47:19 +00:00
# -*- coding: utf-8 -*-
import ox
from ox.utils import json
2016-02-20 09:06:41 +00:00
from oxdjango.decorators import login_required_json
from oxdjango.shortcuts import render_to_json_response, get_object_or_404_json, json_response
2011-10-07 08:47:19 +00:00
2016-02-20 09:06:41 +00:00
from oxdjango.api import actions
from changelog.models import add_changelog
2011-10-07 08:47:19 +00:00
from . import models
2011-10-07 08:47:19 +00:00
2014-10-06 08:26:43 +00:00
def getNews(request, data):
2011-10-07 08:47:19 +00:00
'''
2014-12-18 20:16:41 +00:00
Gets one or all news items
takes {
id: string // news item id (optional)
}
returns { // if `id` is present
id: string, // news item id
key: value, // property id and value
... // more key/value pairs
} or { // if `id` is missing
items: [
{
id: string, // news item id
key: value, // property id and value
... // more key/value pairs
},
... // more news items
]
}
see: addNews, editNews, removeNews
2011-10-07 08:47:19 +00:00
'''
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
2014-10-06 08:26:43 +00:00
def addNews(request, data):
2011-10-07 08:47:19 +00:00
'''
2014-12-18 20:16:41 +00:00
Adds a new news item
takes {
date: string, // date (format undocumented)
text: text, // text
title: string // title
}
returns {
id: string, // news item id
... // more key/value pairs
}
see: editNews, getNews, removeNews
2011-10-07 08:47:19 +00:00
'''
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())
add_changelog(request, data, news.get_id())
2011-10-07 08:47:19 +00:00
return render_to_json_response(response)
actions.register(addNews, cache=False)
@login_required_json
2014-10-06 08:26:43 +00:00
def removeNews(request, data):
2011-10-07 08:47:19 +00:00
'''
2014-12-18 20:16:41 +00:00
Removes a news item
takes {
id: string // news item id
}
returns {}
see: addNews, editNews, getNews
2011-10-07 08:47:19 +00:00
'''
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):
add_changelog(request, data)
2012-02-18 11:59:24 +00:00
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
2014-10-06 08:26:43 +00:00
def editNews(request, data):
2011-10-07 08:47:19 +00:00
'''
2014-12-18 20:16:41 +00:00
Edits a news item
takes {
date: string, // date (format undocumented)
id: string, // news item id
text: string, // text
title: string // title
}
returns {
2014-12-19 15:05:08 +00:00
id: string // news item id
... // more properties
2014-12-18 20:16:41 +00:00
}
see: addNews, getNews, removeNews
2011-10-07 08:47:19 +00:00
'''
response = json_response({})
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()
add_changelog(request, data)
2011-10-07 08:47:19 +00:00
else:
response = json_response(status=403, text='permission denied')
return render_to_json_response(response)
actions.register(editNews, cache=False)