src/cm/api/handlers.py
author raph
Mon, 12 Jul 2010 13:53:26 +0200
changeset 288 c6fe4822a243
parent 287 fc5ed157ebfe
child 293 2c52e4453bf7
permissions -rw-r--r--
fix delete method / fix anon resources
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
    11
from cm.views.texts import client_exchange, text_view_frame, text_view_comments, text_export
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
    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
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
    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
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   109
    def read(self, request):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   110
        order_by = '-id'
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   111
        texts = get_texts_with_perm(request, 'can_view_text').order_by(order_by)        
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   112
        return texts
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   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
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   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
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   197
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   198
class AnonymousTextFeedHandler(AnonymousBaseHandler):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   199
    allowed_methods = ('GET',)    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   200
    type = "Text methods"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   201
    title = "Text feed"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   202
    desc = "Returns text RSS feed."
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   203
    args = None
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   204
    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   205
    @staticmethod
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   206
    def endpoint():
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   207
        return URL_PREFIX + '/text/{key}/feed/?'
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   208
    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   209
    def read(self, request, key):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   210
        return text_feed(request, key=key)
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   211
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   212
class TextFeedHandler(BaseHandler):    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   213
    type = "Text methods"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   214
    anonymous = AnonymousTextFeedHandler
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   215
    allowed_methods = ('GET',)  
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   216
    no_display = True 
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   217
## client methods
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   218
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   219
class AnonymousClientHandler(AnonymousBaseHandler):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   220
    allowed_methods = ('POST',)    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   221
    type = "Client methods"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   222
    title = "Handles client methods"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   223
    desc = "Handles client (ajax text view) methods."
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   224
    args = """<br />
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   225
post arguments
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   226
    """ 
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   227
    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   228
    @staticmethod
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   229
    def endpoint():
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   230
        return URL_PREFIX + '/client/'
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   231
    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   232
    def create(self, request):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   233
        return client_exchange(request)
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   234
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   235
class ClientHandler(BaseHandler):    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   236
    type = "Client methods"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   237
    anonymous = AnonymousClientHandler
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   238
    allowed_methods = ('POST',)  
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   239
    no_display = True 
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   240
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   241
    def create(self, request):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   242
        return client_exchange(request)
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   243
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   244
## embed methods
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   245
from django.views.i18n import javascript_catalog
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   246
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   247
class JSI18NHandler(AnonymousBaseHandler):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   248
    allowed_methods = ('GET',)    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   249
    type = "Embed methods"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   250
    title = "Get js i18n dicts"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   251
    desc = ""
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   252
    args = None
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   253
    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   254
    @staticmethod
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   255
    def endpoint():
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   256
        return URL_PREFIX + '/jsi18n/'
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   257
    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   258
    def read(self, request):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   259
        return javascript_catalog(request)
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   260
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   261
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   262
class AnonymousCommentFrameHandler(AnonymousBaseHandler):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   263
    allowed_methods = ('GET',)    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   264
    type = "Embed methods"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   265
    title = "Displays embedable frame"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   266
    desc = ""
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   267
    args = None
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   268
    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   269
    @staticmethod
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   270
    def endpoint():
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   271
        return URL_PREFIX + '/text/{key}/comments_frame/?prefix=/api'
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   272
    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   273
    def read(self, request, key):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   274
        return text_view_frame(request, key=key)
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   275
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   276
class CommentFrameHandler(BaseHandler):    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   277
    type = "Embed methods"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   278
    anonymous = AnonymousCommentFrameHandler
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   279
    allowed_methods = ('GET',)  
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   280
    no_display = True 
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   281
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   282
    def read(self, request, key):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   283
        return text_view_frame(request, key=key)
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   284
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   285
class AnonymousCommentHandler(AnonymousBaseHandler):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   286
    allowed_methods = ('GET',)    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   287
    type = "Embed methods"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   288
    title = "Displays embedable frame"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   289
    no_display = True 
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   290
    desc = ""
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   291
    args = None
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   292
    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   293
    @staticmethod
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   294
    def endpoint():
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   295
        return URL_PREFIX + '/text/{key}/comments/{version_key}/?'
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   296
    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   297
    def read(self, request, key, version_key):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   298
        return text_view_comments(request, key=key, version_key=version_key)
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   299
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   300
class CommentHandler(BaseHandler):    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   301
    type = "Embed methods"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   302
    anonymous = AnonymousCommentHandler
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   303
    allowed_methods = ('GET',)  
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   304
    no_display = True 
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   305
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   306
    def read(self, request, key, version_key):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   307
        return text_view_comments(request, key=key, version_key=version_key)
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   308
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   309
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   310
class AnonymousTextExportHandler(AnonymousBaseHandler):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   311
    allowed_methods = ('POST',)    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   312
    type = "Embed methods"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   313
    title = "undocumented"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   314
    no_display = True 
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   315
    desc = ""
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   316
    args = None
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   317
    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   318
    @staticmethod
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   319
    def endpoint():
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   320
        return URL_PREFIX + ' undocumented'
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   321
    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   322
    def create(self, request, key, format, download, whichcomments, withcolor):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   323
        return text_export(request, key, format, download, whichcomments, withcolor, adminkey=None)
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   324
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   325
class TextExportHandler(BaseHandler):    
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   326
    type = "Embed methods"
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   327
    anonymous = AnonymousTextExportHandler
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   328
    allowed_methods = ('POST',)  
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   329
    no_display = True 
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   330
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   331
    def create(self, request, key, format, download, whichcomments, withcolor):
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   332
        return text_export(request, key, format, download, whichcomments, withcolor, adminkey=None)
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   333
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   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
c6fe4822a243 fix delete method / fix anon resources
raph
parents: 287
diff changeset
   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)