From 1019660f6a7896a1454c1b863955b32ccc02867b Mon Sep 17 00:00:00 2001
From: Michael Shamoon <4887959+shamoon@users.noreply.github.com>
Date: Fri, 1 Apr 2022 07:22:55 -0700
Subject: [PATCH] Add config variable for update check

---
 docs/configuration.rst    | 10 ++++++++++
 paperless.conf.example    |  1 +
 src/documents/views.py    | 35 ++++++++++++++++++++---------------
 src/paperless/settings.py |  2 ++
 4 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/docs/configuration.rst b/docs/configuration.rst
index 261eab7d5..cc9b9f5e9 100644
--- a/docs/configuration.rst
+++ b/docs/configuration.rst
@@ -764,3 +764,13 @@ PAPERLESS_OCR_LANGUAGES=<list>
         PAPERLESS_OCR_LANGUAGE=tur
 
     Defaults to none, which does not install any additional languages.
+
+PAPERLESS_ENABLE_UPDATE_CHECK=<bool>
+    Enabling this option enables the check for available updates, which works by
+    loading the version file of the public repository for this project on Github e.g.
+    https://raw.githubusercontent.com/paperless-ngx/paperless-ngx/main/src/paperless/version.py
+    to determine whether a new version is available. Actual updating of the app must be done manually.
+
+    No tracking data is collected by the app in any way.
+
+    Defaults to true.
diff --git a/paperless.conf.example b/paperless.conf.example
index bfb4fac7e..539e33b0d 100644
--- a/paperless.conf.example
+++ b/paperless.conf.example
@@ -67,6 +67,7 @@
 #PAPERLESS_FILENAME_PARSE_TRANSFORMS=[]
 #PAPERLESS_THUMBNAIL_FONT_NAME=
 #PAPERLESS_IGNORE_DATES=
+#PAPERLESS_ENABLE_UPDATE_CHECK=
 
 # Tika settings
 
diff --git a/src/documents/views.py b/src/documents/views.py
index faca183c7..8ae34d454 100644
--- a/src/documents/views.py
+++ b/src/documents/views.py
@@ -674,22 +674,27 @@ class BulkDownloadView(GenericAPIView):
 
 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"
+        remote_version = "0.0.0"
+        is_greater = False
+        if settings.ENABLE_UPDATE_CHECK:
+            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:
+                logger.debug("An error occured checking for available updates")
 
-        current_version = ".".join([str(_) for _ in version.__version__[:3]])
-        is_greater = packaging_version.parse(remote_version) > packaging_version.parse(
-            current_version,
-        )
+            current_version = ".".join([str(_) for _ in version.__version__[:3]])
+            is_greater = packaging_version.parse(
+                remote_version,
+            ) > packaging_version.parse(
+                current_version,
+            )
 
         return Response(
             {
diff --git a/src/paperless/settings.py b/src/paperless/settings.py
index d485c5a50..382a2a38b 100644
--- a/src/paperless/settings.py
+++ b/src/paperless/settings.py
@@ -566,3 +566,5 @@ if os.getenv("PAPERLESS_IGNORE_DATES", ""):
         d = dateparser.parse(s)
         if d:
             IGNORE_DATES.add(d.date())
+
+ENABLE_UPDATE_CHECK = __get_boolean("PAPERLESS_ENABLE_UPDATE_CHECK", "true")