From d76eccad1c7d8dcad06de6727a9711e559048939 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 1 Apr 2022 01:48:05 -0700 Subject: [PATCH] remote_version API endpoint --- src/documents/views.py | 31 +++++++++++++++++++++++++++++++ src/paperless/urls.py | 6 ++++++ 2 files changed, 37 insertions(+) diff --git a/src/documents/views.py b/src/documents/views.py index 0564e5469..faca183c7 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -1,6 +1,8 @@ import logging import os +import re import tempfile +import urllib import uuid import zipfile from datetime import datetime @@ -24,6 +26,8 @@ from django.views.decorators.cache import cache_control from django.views.generic import TemplateView from django_filters.rest_framework import DjangoFilterBackend from django_q.tasks import async_task +from packaging import version as packaging_version +from paperless import version from paperless.db import GnuPG from paperless.views import StandardPagination from rest_framework import parsers @@ -666,3 +670,30 @@ class BulkDownloadView(GenericAPIView): ) return response + + +class RemoteVersionView(GenericAPIView): + def get(self, request, format=None): + try: + with urllib.request.urlopen( + "https://raw.githubusercontent.com/paperless-ngx/paperless-ngx" + + "/main/src/paperless/version.py", + ) as response: + remote = response.read().decode("utf-8") + match = re.search("(\\d+, \\d+, \\d+)", remote) + if match: + remote_version = ".".join(match[0].split(", ")) + except urllib.error.URLError: + remote_version = "0.0.0" + + current_version = ".".join([str(_) for _ in version.__version__[:3]]) + is_greater = packaging_version.parse(remote_version) > packaging_version.parse( + current_version, + ) + + return Response( + { + "version": remote_version, + "greater_than_current": is_greater, + }, + ) diff --git a/src/paperless/urls.py b/src/paperless/urls.py index 9dbe39e8a..833788cce 100644 --- a/src/paperless/urls.py +++ b/src/paperless/urls.py @@ -14,6 +14,7 @@ from documents.views import DocumentTypeViewSet from documents.views import IndexView from documents.views import LogViewSet from documents.views import PostDocumentView +from documents.views import RemoteVersionView from documents.views import SavedViewViewSet from documents.views import SearchAutoCompleteView from documents.views import SelectionDataView @@ -72,6 +73,11 @@ urlpatterns = [ BulkDownloadView.as_view(), name="bulk_download", ), + re_path( + r"^remote_version/", + RemoteVersionView.as_view(), + name="remoteversion", + ), path("token/", views.obtain_auth_token), ] + api_router.urls,