| author | raph |
| Mon, 12 Jul 2010 13:53:26 +0200 | |
| changeset 288 | c6fe4822a243 |
| parent 287 | fc5ed157ebfe |
| child 293 | 2c52e4453bf7 |
| permissions | -rw-r--r-- |
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
1 |
from piston.handler import AnonymousBaseHandler, BaseHandler |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
2 |
from piston.utils import rc |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
3 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
4 |
from cm.models import Text,TextVersion, Role, UserRole |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
5 |
from cm.views import get_keys_from_dict, get_textversion_by_keys_or_404, get_text_by_keys_or_404, get_textversion_by_keys_or_404, redirect |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
6 |
from cm.security import get_texts_with_perm, has_perm, get_viewable_comments, \ |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
7 |
has_perm_on_text_api |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
8 |
from cm.security import get_viewable_comments |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
9 |
from cm.utils.embed import embed_html |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
10 |
from cm.views.create import CreateTextContentForm, create_text |
| 288 | 11 |
from cm.views.texts import client_exchange, text_view_frame, text_view_comments, text_export |
12 |
from cm.views.feeds import text_feed |
|
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
13 |
from piston.utils import validate |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
14 |
from settings import SITE_URL |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
15 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
16 |
URL_PREFIX = SITE_URL + '/api' |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
17 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
18 |
class AnonymousTextHandler(AnonymousBaseHandler): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
19 |
type = "Text methods" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
20 |
title = "Read text info" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
21 |
fields = ('key', 'title', 'format', 'content', 'created', 'modified', 'nb_comments', 'nb_versions', 'embed_html', ('last_text_version', ('created','modified', 'format', 'title', 'content'))) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
22 |
allowed_methods = ('GET', ) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
23 |
model = Text |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
24 |
desc = """ Read text identified by `key`.""" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
25 |
args = None |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
26 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
27 |
@staticmethod |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
28 |
def endpoint(): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
29 |
return URL_PREFIX + '/text/{key}/' |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
30 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
31 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
32 |
@has_perm_on_text_api('can_view_text') |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
33 |
def read(self, request, key): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
34 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
35 |
text = get_text_by_keys_or_404(key) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
36 |
setattr(text,'nb_comments',len(get_viewable_comments(request, text.last_text_version.comment_set.all(), text))) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
37 |
setattr(text,'nb_versions',text.get_versions_number()) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
38 |
setattr(text,'embed_html',embed_html(text.key)) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
39 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
40 |
return text |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
41 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
42 |
class TextHandler(BaseHandler): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
43 |
type = "Text methods" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
44 |
anonymous = AnonymousTextHandler |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
45 |
allowed_methods = ('GET',) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
46 |
no_display = True |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
47 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
48 |
class AnonymousTextVersionHandler(AnonymousBaseHandler): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
49 |
type = "Text methods" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
50 |
title = "Read text version info" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
51 |
fields = ('key', 'title', 'format', 'content', 'created', 'modified', 'nb_comments',) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
52 |
allowed_methods = ('GET', ) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
53 |
model = Text |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
54 |
desc = """ Read text version identified by `version_key` inside text identified by `key`.""" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
55 |
args = None |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
56 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
57 |
@staticmethod |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
58 |
def endpoint(): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
59 |
return URL_PREFIX + '/text/{key}/{version_key}/' |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
60 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
61 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
62 |
@has_perm_on_text_api('can_view_text') |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
63 |
def read(self, request, key, version_key): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
64 |
text_version = get_textversion_by_keys_or_404(version_key, key=key) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
65 |
setattr(text_version,'nb_comments',len(get_viewable_comments(request, text_version.comment_set.all(), text_version.text))) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
66 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
67 |
return text_version |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
68 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
69 |
class TextVersionHandler(BaseHandler): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
70 |
type = "Text methods" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
71 |
anonymous = AnonymousTextVersionHandler |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
72 |
allowed_methods = ('GET',) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
73 |
no_display = True |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
74 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
75 |
class AnonymousTextListHandler(AnonymousBaseHandler): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
76 |
title = "List texts" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
77 |
type = "Text methods" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
78 |
fields = ('key', 'title', 'created', 'modified', 'nb_comments', 'nb_versions',) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
79 |
allowed_methods = ('GET',) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
80 |
model = Text |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
81 |
desc = """Lists texts on workspace.""" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
82 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
83 |
@staticmethod |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
84 |
def endpoint(): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
85 |
return URL_PREFIX + '/text/' |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
86 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
87 |
def read(self, request): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
88 |
order_by = '-id' |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
89 |
texts = get_texts_with_perm(request, 'can_view_text').order_by(order_by) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
90 |
return texts |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
91 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
92 |
class TextListHandler(BaseHandler): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
93 |
title = "Create text" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
94 |
type = "Text methods" |
| 288 | 95 |
allowed_methods = ('GET', 'POST') |
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
96 |
anonymous = AnonymousTextListHandler |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
97 |
desc = "Create a text with the provided parameters." |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
98 |
args = """<br/> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
99 |
`title`: title of the text<br/> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
100 |
`format`: format content ('markdown', 'html')<br/> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
101 |
`content`: content (in specified format)<br/> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
102 |
`anon_role`: role to give to anon users: null, 4: commentator, 5: observer<br/> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
103 |
""" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
104 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
105 |
@staticmethod |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
106 |
def endpoint(): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
107 |
return URL_PREFIX + '/text/' |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
108 |
|
| 288 | 109 |
def read(self, request): |
110 |
order_by = '-id' |
|
111 |
texts = get_texts_with_perm(request, 'can_view_text').order_by(order_by) |
|
112 |
return texts |
|
113 |
||
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
114 |
def create(self, request): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
115 |
form = CreateTextContentForm(request.POST) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
116 |
if form.is_valid(): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
117 |
text = create_text(request.user, form.cleaned_data) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
118 |
anon_role = request.POST.get('anon_role', None) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
119 |
if anon_role: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
120 |
userrole = UserRole.objects.create(user=None, role=Role.objects.get(id=anon_role), text=text) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
121 |
return {'key' : text.key , 'version_key' : text.last_text_version.key, 'created': text.created} |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
122 |
else: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
123 |
resp = rc.BAD_REQUEST |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
124 |
return resp |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
125 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
126 |
from cm.exception import UnauthorizedException |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
127 |
from cm.views.texts import text_delete |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
128 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
129 |
class TextDeleteHandler(BaseHandler): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
130 |
type = "Text methods" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
131 |
allowed_methods = ('POST', ) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
132 |
title = "Delete text" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
133 |
desc = "Delete the text identified by `key`." |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
134 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
135 |
@staticmethod |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
136 |
def endpoint(): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
137 |
return URL_PREFIX + '/text/{key}/delete/' |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
138 |
|
| 288 | 139 |
def create(self, request, key): |
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
140 |
""" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
141 |
Delete text identified by `key`. |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
142 |
""" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
143 |
try: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
144 |
text_delete(request, key=key) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
145 |
except UnauthorizedException: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
146 |
return rc.FORBIDDEN |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
147 |
except KeyError: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
148 |
return rc.BAD_REQUEST |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
149 |
return rc.DELETED |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
150 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
151 |
from cm.views.texts import text_pre_edit |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
152 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
153 |
class TextPreEditHandler(BaseHandler): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
154 |
type = "Text methods" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
155 |
allowed_methods = ('POST', ) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
156 |
title = "Ask for edit impact" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
157 |
desc = "Returns the number of impacted comments." |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
158 |
args = """<br /> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
159 |
`new_format`: new format content ('markdown', 'html')<br /> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
160 |
`new_content`: new content (in specified format)<br /> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
161 |
""" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
162 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
163 |
@staticmethod |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
164 |
def endpoint(): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
165 |
return URL_PREFIX + '/text/{key}/pre_edit/' |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
166 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
167 |
def create(self, request, key): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
168 |
return text_pre_edit(request, key=key) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
169 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
170 |
from cm.views.texts import text_edit |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
171 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
172 |
class TextEditHandler(BaseHandler): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
173 |
allowed_methods = ('POST', ) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
174 |
type = "Text methods" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
175 |
title = "Edit text" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
176 |
desc = "Update text identified by `key`." |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
177 |
args = """<br /> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
178 |
`title`: new title of the text<br /> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
179 |
`format`: new format content ('markdown', 'html')<br /> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
180 |
`content`: new content (in specified format)<br /> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
181 |
`note`: note to add to edit<br /> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
182 |
`new_version`: boolean: should a new version of the text be created?<br /> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
183 |
`keep_comments`: boolean: should existing comments be keep (if possible)?<br /> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
184 |
""" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
185 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
186 |
@staticmethod |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
187 |
def endpoint(): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
188 |
return URL_PREFIX + '/text/{key}/edit/' |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
189 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
190 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
191 |
def create(self, request, key): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
192 |
res = text_edit(request, key=key) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
193 |
text = get_text_by_keys_or_404(key) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
194 |
text_version = text.last_text_version |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
195 |
return {'version_key' : text_version.key , 'created': text_version.created} |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
196 |
|
| 288 | 197 |
|
198 |
class AnonymousTextFeedHandler(AnonymousBaseHandler): |
|
199 |
allowed_methods = ('GET',) |
|
200 |
type = "Text methods" |
|
201 |
title = "Text feed" |
|
202 |
desc = "Returns text RSS feed." |
|
203 |
args = None |
|
204 |
||
205 |
@staticmethod |
|
206 |
def endpoint(): |
|
207 |
return URL_PREFIX + '/text/{key}/feed/?' |
|
208 |
||
209 |
def read(self, request, key): |
|
210 |
return text_feed(request, key=key) |
|
211 |
||
212 |
class TextFeedHandler(BaseHandler): |
|
213 |
type = "Text methods" |
|
214 |
anonymous = AnonymousTextFeedHandler |
|
215 |
allowed_methods = ('GET',) |
|
216 |
no_display = True |
|
217 |
## client methods |
|
218 |
||
219 |
class AnonymousClientHandler(AnonymousBaseHandler): |
|
220 |
allowed_methods = ('POST',) |
|
221 |
type = "Client methods" |
|
222 |
title = "Handles client methods" |
|
223 |
desc = "Handles client (ajax text view) methods." |
|
224 |
args = """<br /> |
|
225 |
post arguments |
|
226 |
""" |
|
227 |
||
228 |
@staticmethod |
|
229 |
def endpoint(): |
|
230 |
return URL_PREFIX + '/client/' |
|
231 |
||
232 |
def create(self, request): |
|
233 |
return client_exchange(request) |
|
234 |
||
235 |
class ClientHandler(BaseHandler): |
|
236 |
type = "Client methods" |
|
237 |
anonymous = AnonymousClientHandler |
|
238 |
allowed_methods = ('POST',) |
|
239 |
no_display = True |
|
240 |
||
241 |
def create(self, request): |
|
242 |
return client_exchange(request) |
|
243 |
||
244 |
## embed methods |
|
245 |
from django.views.i18n import javascript_catalog |
|
246 |
||
247 |
class JSI18NHandler(AnonymousBaseHandler): |
|
248 |
allowed_methods = ('GET',) |
|
249 |
type = "Embed methods" |
|
250 |
title = "Get js i18n dicts" |
|
251 |
desc = "" |
|
252 |
args = None |
|
253 |
||
254 |
@staticmethod |
|
255 |
def endpoint(): |
|
256 |
return URL_PREFIX + '/jsi18n/' |
|
257 |
||
258 |
def read(self, request): |
|
259 |
return javascript_catalog(request) |
|
260 |
||
261 |
||
262 |
class AnonymousCommentFrameHandler(AnonymousBaseHandler): |
|
263 |
allowed_methods = ('GET',) |
|
264 |
type = "Embed methods" |
|
265 |
title = "Displays embedable frame" |
|
266 |
desc = "" |
|
267 |
args = None |
|
268 |
||
269 |
@staticmethod |
|
270 |
def endpoint(): |
|
271 |
return URL_PREFIX + '/text/{key}/comments_frame/?prefix=/api' |
|
272 |
||
273 |
def read(self, request, key): |
|
274 |
return text_view_frame(request, key=key) |
|
275 |
||
276 |
class CommentFrameHandler(BaseHandler): |
|
277 |
type = "Embed methods" |
|
278 |
anonymous = AnonymousCommentFrameHandler |
|
279 |
allowed_methods = ('GET',) |
|
280 |
no_display = True |
|
281 |
||
282 |
def read(self, request, key): |
|
283 |
return text_view_frame(request, key=key) |
|
284 |
||
285 |
class AnonymousCommentHandler(AnonymousBaseHandler): |
|
286 |
allowed_methods = ('GET',) |
|
287 |
type = "Embed methods" |
|
288 |
title = "Displays embedable frame" |
|
289 |
no_display = True |
|
290 |
desc = "" |
|
291 |
args = None |
|
292 |
||
293 |
@staticmethod |
|
294 |
def endpoint(): |
|
295 |
return URL_PREFIX + '/text/{key}/comments/{version_key}/?' |
|
296 |
||
297 |
def read(self, request, key, version_key): |
|
298 |
return text_view_comments(request, key=key, version_key=version_key) |
|
299 |
||
300 |
class CommentHandler(BaseHandler): |
|
301 |
type = "Embed methods" |
|
302 |
anonymous = AnonymousCommentHandler |
|
303 |
allowed_methods = ('GET',) |
|
304 |
no_display = True |
|
305 |
||
306 |
def read(self, request, key, version_key): |
|
307 |
return text_view_comments(request, key=key, version_key=version_key) |
|
308 |
||
309 |
||
310 |
class AnonymousTextExportHandler(AnonymousBaseHandler): |
|
311 |
allowed_methods = ('POST',) |
|
312 |
type = "Embed methods" |
|
313 |
title = "undocumented" |
|
314 |
no_display = True |
|
315 |
desc = "" |
|
316 |
args = None |
|
317 |
||
318 |
@staticmethod |
|
319 |
def endpoint(): |
|
320 |
return URL_PREFIX + ' undocumented' |
|
321 |
||
322 |
def create(self, request, key, format, download, whichcomments, withcolor): |
|
323 |
return text_export(request, key, format, download, whichcomments, withcolor, adminkey=None) |
|
324 |
||
325 |
class TextExportHandler(BaseHandler): |
|
326 |
type = "Embed methods" |
|
327 |
anonymous = AnonymousTextExportHandler |
|
328 |
allowed_methods = ('POST',) |
|
329 |
no_display = True |
|
330 |
||
331 |
def create(self, request, key, format, download, whichcomments, withcolor): |
|
332 |
return text_export(request, key, format, download, whichcomments, withcolor, adminkey=None) |
|
333 |
||
334 |
## user methods |
|
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
335 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
336 |
class SetUserHandler(AnonymousBaseHandler): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
337 |
allowed_methods = ('POST',) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
338 |
type = "User methods" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
339 |
title = "Set username and email" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
340 |
desc = "Set username and email to use when commenting." |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
341 |
args = """<br /> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
342 |
`user_name`: user's name<br /> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
343 |
`user_email`: user's email<br /> |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
344 |
""" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
345 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
346 |
@staticmethod |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
347 |
def endpoint(): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
348 |
return URL_PREFIX + '/setuser/' |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
349 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
350 |
def create(self, request): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
351 |
user_name = request.POST.get('user_name', None) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
352 |
user_email = request.POST.get('user_email', None) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
353 |
if user_name and user_email: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
354 |
response = rc.ALL_OK |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
355 |
response.set_cookie('user_name', user_name) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
356 |
response.set_cookie('user_email', user_email) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
357 |
return response |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
358 |
else: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
359 |
return rc.BAD_REQUEST |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
360 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
361 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
362 |
|
| 288 | 363 |
|
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
364 |
from piston.doc import documentation_view |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
365 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
366 |
from piston.handler import handler_tracker |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
367 |
from django.template import RequestContext |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
368 |
from piston.doc import generate_doc |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
369 |
from django.shortcuts import render_to_response |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
370 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
371 |
def documentation(request): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
372 |
""" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
373 |
Generic documentation view. Generates documentation |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
374 |
from the handlers you've defined. |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
375 |
""" |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
376 |
docs = [ ] |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
377 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
378 |
for handler in handler_tracker: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
379 |
doc = generate_doc(handler) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
380 |
setattr(doc,'type', handler.type) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
381 |
docs.append(doc) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
382 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
383 |
def _compare(doc1, doc2): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
384 |
#handlers and their anonymous counterparts are put next to each other. |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
385 |
name1 = doc1.name.replace("Anonymous", "") |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
386 |
name2 = doc2.name.replace("Anonymous", "") |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
387 |
return cmp(name1, name2) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
388 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
389 |
#docs.sort(_compare) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
390 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
391 |
return render_to_response('api_doc.html', |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
392 |
{ 'docs': docs }, RequestContext(request)) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
393 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
394 |
from piston.doc import generate_doc |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
diff
changeset
|
395 |
DocHandler = generate_doc(TextPreEditHandler) |