def retrieve(self, request, *args, **kwargs): resolved = self.resolved if not resolved: raise NotFound(_('No matching editor view endpoint found.')) if not getattr(resolved.func, 'api_hybrid', False): raise NotFound(_('Matching editor view point does not provide an API.')) get_api_post_data(request) response = resolved.func(request, api=True, *resolved.args, **resolved.kwargs) return response
def restore_object(self, request, *args, **kwargs): data = get_api_post_data(request) if 'id' not in data: raise ParseError('Missing id.') restore_id = data['id'] if isinstance(restore_id, str) and restore_id.isdigit(): restore_id = int(restore_id) if not isinstance(restore_id, int): raise ParseError('id needs to be an integer.') changeset = self.get_object() with changeset.lock_to_edit(request) as changeset: if not changeset.can_edit(request): raise PermissionDenied(_('You can not edit changes on this change set.')) try: changed_object = changeset.changed_objects_set.get(pk=restore_id) except Exception: raise NotFound('could not find object.') try: changed_object.restore() except PermissionError: raise PermissionDenied(_('You cannot restore this object, because it depends on ' 'a deleted object or it would violate a unique contraint.')) return Response({'success': True})
def login(self, request, *args, **kwargs): # django-rest-framework doesn't do this for logged out requests SessionAuthentication().enforce_csrf(request) if request.user.is_authenticated: raise ParseError(_('Log out first.')) data = get_api_post_data(request) if 'token' in data: try: token = Token.get_by_token(data['token']) except Token.DoesNotExist: raise PermissionDenied(_('This token does not exist or is no longer valid.')) user = token.user elif 'username' in data: form = AuthenticationForm(request, data=data) if not form.is_valid(): raise ParseError(form.errors) user = form.user_cache else: raise ParseError(_('You need to send a token or username and password.')) login(request, user) return Response({ 'detail': _('Login successful.'), 'csrf_token': csrf.get_token(request), })
def restore_object(self, request, *args, **kwargs): data = get_api_post_data(request) if 'id' not in data: raise ParseError('Missing id.') restore_id = data['id'] if isinstance(restore_id, str) and restore_id.isdigit(): restore_id = int(restore_id) if not isinstance(restore_id, int): raise ParseError('id needs to be an integer.') changeset = self.get_object() with changeset.lock_to_edit(request) as changeset: if not changeset.can_edit(request): raise PermissionDenied( _('You can not edit changes on this change set.')) try: changed_object = changeset.changed_objects_set.get( pk=restore_id) except Exception: raise NotFound('could not find object.') try: changed_object.restore() except PermissionError: raise PermissionDenied( _('You cannot restore this object, because it depends on ' 'a deleted object or it would violate a unique contraint.' )) return Response({'success': True})
def reject(self, request, *args, **kwargs): changeset = self.get_object() with changeset.lock_to_edit(request) as changeset: if not not changeset.can_end_review(request): raise PermissionDenied(_('You cannot reject these changes.')) form = RejectForm(get_api_post_data(request)) if not form.is_valid(): raise ParseError(form.errors) changeset.reject(request.user, form.cleaned_data['comment'], form.cleaned_data['final']) return Response({'success': True})
def get_token(self, request, *args, **kwargs): # django-rest-framework doesn't do this for logged out requests SessionAuthentication().enforce_csrf(request) data = get_api_post_data(request) form = AuthenticationForm(request, data=data) if not form.is_valid(): raise ParseError(form.errors) token = form.user_cache.login_tokens.create() return Response({ 'token': token.get_token(), })
def edit(self, request, *args, **kwargs): changeset = self.get_object() with changeset.lock_to_edit(request) as changeset: if not changeset.can_edit(request): raise PermissionDenied(_('You cannot edit this change set.')) form = ChangeSetForm(instance=changeset, data=get_api_post_data(request)) if not form.is_valid(): raise ParseError(form.errors) changeset = form.instance update = changeset.updates.create(user=request.user, title=changeset.title, description=changeset.description) changeset.last_update = update changeset.save() return Response({'success': True})