Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-02 10:29:07

0001 """What's new REST surface (docs/WHATS_NEW.md): JSON in, JSON out, never a
0002 redirect, so every write works through the swf-remote proxy
0003 (EXTERNAL_ACCESS.md). Thin peers over ``help.whats_new``; the authority
0004 gate admits only people who may act (AUTHORITY.md)."""
0005 from django.urls import reverse
0006 from rest_framework.authentication import (
0007     SessionAuthentication, TokenAuthentication,
0008 )
0009 from rest_framework.permissions import IsAuthenticated
0010 from rest_framework.response import Response
0011 from rest_framework.views import APIView
0012 
0013 from monitor_app.middleware import TunnelAuthentication
0014 
0015 from . import whats_new as wn
0016 
0017 
0018 class _HelpApiView(APIView):
0019     authentication_classes = [TunnelAuthentication, SessionAuthentication,
0020                               TokenAuthentication]
0021     permission_classes = [IsAuthenticated]
0022 
0023     def _run(self, fn):
0024         try:
0025             return Response(fn())
0026         except wn.WhatsNewError as e:
0027             return Response({'detail': e.detail}, status=e.status)
0028 
0029 
0030 def _fields(data):
0031     return {k: data[k] for k in wn.FIELDS if k in data}
0032 
0033 
0034 def _item_path(slug):
0035     return reverse('help:whats_new_item', args=[slug])
0036 
0037 
0038 class ItemCreateView(_HelpApiView):
0039     def post(self, request):
0040         """Create an item from its fields; answers the slug and page path."""
0041         def go():
0042             entry, _log_id = wn.item_create(
0043                 _fields(request.data), changed_by=request.user.username)
0044             slug = entry.data['entry_id']
0045             return {'slug': slug, 'url': _item_path(slug)}
0046         return self._run(go)
0047 
0048 
0049 class ItemEditView(_HelpApiView):
0050     def post(self, request, slug):
0051         """Edit any of an item's fields."""
0052         def go():
0053             wn.item_edit(slug, _fields(request.data),
0054                          changed_by=request.user.username)
0055             return {'slug': slug, 'url': _item_path(slug)}
0056         return self._run(go)
0057 
0058 
0059 class ItemRestoreView(_HelpApiView):
0060     def post(self, request, slug):
0061         """Restore a version (``version``) as the current state."""
0062         def go():
0063             try:
0064                 num = int(request.data.get('version'))
0065             except (TypeError, ValueError):
0066                 raise wn.WhatsNewError('version must be a number')
0067             wn.item_restore(slug, num, changed_by=request.user.username)
0068             return {'slug': slug, 'url': _item_path(slug)}
0069         return self._run(go)
0070 
0071 
0072 class ItemArchiveView(_HelpApiView):
0073     def post(self, request, slug):
0074         """Archive an item (``archived``: true, the default) or return it."""
0075         def go():
0076             entry = wn.item_archive(
0077                 slug, changed_by=request.user.username,
0078                 archived=request.data.get('archived', True) not in (False, 'false', 0))
0079             return {'slug': slug, 'archived': entry.archived}
0080         return self._run(go)
0081 
0082 
0083 class ItemVoteView(_HelpApiView):
0084     def post(self, request, slug):
0085         """Vote on an item: ``value`` 1, -1, or 0 to withdraw."""
0086         def go():
0087             up, down, mine = wn.vote(slug, username=request.user.username,
0088                                      value=request.data.get('value'))
0089             return {'slug': slug, 'up': up, 'down': down, 'my_vote': mine}
0090         return self._run(go)
0091 
0092 
0093 class CommentAddView(_HelpApiView):
0094     def post(self, request, slug):
0095         """Add a comment (``text``, Markdown) to an item's thread."""
0096         def go():
0097             c = wn.comment_add(slug, request.data.get('text'),
0098                                author=request.user.username)
0099             return {'id': c.pk, 'slug': slug}
0100         return self._run(go)
0101 
0102 
0103 class CommentEditView(_HelpApiView):
0104     def post(self, request, comment_id):
0105         """Edit a comment's text; its author only."""
0106         def go():
0107             c = wn.comment_edit(comment_id, request.data.get('text'),
0108                                 editor=request.user.username)
0109             return {'id': c.pk}
0110         return self._run(go)