From e6936b220cfb21113a89bc9e4bfc19c3040da069 Mon Sep 17 00:00:00 2001
From: Erwan Rouchet <rouchet@teklia.com>
Date: Fri, 8 Feb 2019 08:47:52 +0000
Subject: [PATCH] Putting the "end" in frontend

---
 Dockerfile                             |  6 ----
 Makefile                               |  3 +-
 arkindex/dataimport/urls.py            | 13 --------
 arkindex/documents/urls.py             | 20 -------------
 arkindex/project/settings.py           | 41 +++++++-------------------
 arkindex/project/urls.py               | 13 +++++---
 arkindex/project/views.py              |  5 ++--
 arkindex/templates/front.html          | 19 ------------
 arkindex/users/tests/test_providers.py |  2 +-
 requirements.txt                       |  1 -
 10 files changed, 24 insertions(+), 99 deletions(-)
 delete mode 100644 arkindex/dataimport/urls.py
 delete mode 100644 arkindex/documents/urls.py
 delete mode 100644 arkindex/templates/front.html

diff --git a/Dockerfile b/Dockerfile
index 3fa721a978..3ab098a477 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,7 +1,5 @@
 FROM registry.gitlab.com/arkindex/backend:base-0.8.8
 
-ARG FRONTEND_BRANCH=master
-ARG FRONTEND_ID=4675768
 ARG COMMON_BRANCH=master
 ARG COMMON_ID=9855787
 ARG PONOS_BRANCH=master
@@ -34,10 +32,6 @@ RUN pip install /tmp/arkindex.tar.gz gunicorn
 RUN mkdir -p /medias/staging /medias/iiif /logs /workers
 RUN chown -R ark:teklia /medias /logs /workers
 
-# Setup frontend
-ENV FRONTEND_DIR="/frontend/dist"
-RUN mkdir /frontend && wget --header "PRIVATE-TOKEN: $GITLAB_TOKEN" https://gitlab.com/api/v4/projects/$FRONTEND_ID/jobs/artifacts/$FRONTEND_BRANCH/download?job=frontend-build -O /tmp/frontend.zip && unzip /tmp/frontend.zip -d /frontend && rm /tmp/frontend.zip
-
 # Run through supervisor
 EXPOSE 80
 CMD ["gunicorn", "--access-logfile=-", "--capture-output", "--bind=0.0.0.0:80", "arkindex.project.wsgi"]
diff --git a/Makefile b/Makefile
index 44028f79a8..97f82fad29 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,6 @@ TUNNEL_PORT:=8000
 VERSION=$(shell git rev-parse --short HEAD)
 TAG_APP=arkindex-app
 TAG_BASE=arkindex-base
-FRONTEND_BRANCH=master
 .PHONY: build base
 
 all: clean build
@@ -22,7 +21,7 @@ clean:
 
 build:
 	python setup.py sdist
-	docker build --no-cache $(ROOT_DIR) -t $(TAG_APP):$(VERSION) -t $(TAG_APP):latest --build-arg FRONTEND_BRANCH=$(FRONTEND_BRANCH)
+	docker build --no-cache $(ROOT_DIR) -t $(TAG_APP):$(VERSION) -t $(TAG_APP):latest
 
 publish-version: require-docker-auth
 	[ -f $(ROOT_DIR)/arkindex/project/local_settings.py ] && mv $(ROOT_DIR)/arkindex/project/local_settings.py $(ROOT_DIR)/arkindex/project/local_settings.py.bak || true
diff --git a/arkindex/dataimport/urls.py b/arkindex/dataimport/urls.py
deleted file mode 100644
index 403a985854..0000000000
--- a/arkindex/dataimport/urls.py
+++ /dev/null
@@ -1,13 +0,0 @@
-from django.conf.urls import url
-from arkindex.project.views import FrontendView
-
-urlpatterns = [
-    url(r'^$', FrontendView.as_view(), name='imports'),
-    url(r'^(?P<pk>[\w\-]+)/?$', FrontendView.as_view(), name='import-status'),
-    url(r'^(?P<pk>[\w\-]+)/failures/?$', FrontendView.as_view(), name='import-failures'),
-    url(r'^files/?$', FrontendView.as_view(), name='files'),
-    url(r'^repos/?$', FrontendView.as_view(), name='repositories'),
-    url(r'^repos/new/?$', FrontendView.as_view(), name='repositories-create'),
-    url(r'^credentials/?$', FrontendView.as_view(), name='credentials'),
-    url(r'^mltools/?$', FrontendView.as_view(), name='mltools-list'),
-]
diff --git a/arkindex/documents/urls.py b/arkindex/documents/urls.py
deleted file mode 100644
index 724aa008af..0000000000
--- a/arkindex/documents/urls.py
+++ /dev/null
@@ -1,20 +0,0 @@
-from django.conf import settings
-from django.conf.urls import url, include
-from arkindex.project.views import FrontendView
-
-
-urlpatterns = [
-    url(r'^volumes/?$', FrontendView.as_view(), name='volumes'),
-
-    url(r'^pages/$', FrontendView.as_view(), name='pages'),
-    url(r'^acts/$', FrontendView.as_view(), name='acts'),
-
-    url(r'^corpus/new/$', FrontendView.as_view(), name='corpus-create'),
-    url(r'^corpus/$', FrontendView.as_view(), name='corpus-list'),
-]
-
-if 'debug_toolbar' in settings.INSTALLED_APPS:
-    import debug_toolbar
-    urlpatterns = [
-        url(r'^__debug__/', include(debug_toolbar.urls)),
-    ] + urlpatterns
diff --git a/arkindex/project/settings.py b/arkindex/project/settings.py
index ffa5d62dc9..9dd0e90fe4 100644
--- a/arkindex/project/settings.py
+++ b/arkindex/project/settings.py
@@ -12,15 +12,14 @@ https://docs.djangoproject.com/en/1.11/ref/settings/
 
 import os
 import logging
-import tempfile
 
 
-def env2list(env_name, separator=','):
+def env2list(env_name, separator=',', default=[]):
     '''
     Load env variable as a list
     '''
     value = os.environ.get(env_name)
-    return value and value.split(separator) or []
+    return value and value.split(separator) or default
 
 
 # Database
@@ -111,7 +110,7 @@ SECRET_KEY = os.environ.get('SECRET_KEY', 'jf0w^y&ml(caax8f&a1mub)(js9(l5mhbbhos
 ARKINDEX_ENV = os.environ.get('ARKINDEX_ENV', 'dev')
 DEBUG = ARKINDEX_ENV == 'dev'
 
-ALLOWED_HOSTS = env2list('ALLOWED_HOSTS')
+ALLOWED_HOSTS = env2list('ALLOWED_HOSTS', default=['localhost', ])
 
 # Required for django-debug-toolbar
 INTERNAL_IPS = ['127.0.0.1', '127.0.1.1']
@@ -129,7 +128,6 @@ INSTALLED_APPS = [
     # Tools
     'rest_framework',
     'rest_framework.authtoken',
-    'webpack_loader',
     'corsheaders',
     'ponos',
 
@@ -194,10 +192,6 @@ AUTH_PASSWORD_VALIDATORS = [
 
 AUTH_USER_MODEL = 'users.User'
 
-LOGIN_REDIRECT_URL = 'volumes'
-LOGOUT_REDIRECT_URL = 'volumes'
-LOGIN_URL = 'login'
-
 # Internationalization
 # https://docs.djangoproject.com/en/1.11/topics/i18n/
 
@@ -214,32 +208,13 @@ USE_TZ = True
 
 # Static files (CSS, JavaScript, Images)
 # https://docs.djangoproject.com/en/1.11/howto/static-files/
-FRONTEND_DIR = os.environ.get(
-    'FRONTEND_DIR', os.path.join(BASE_DIR, '../../frontend/build'))
-
 STATIC_URL = '/static/'
 STATIC_ROOT = os.environ.get('STATIC_ROOT')
 STATICFILES_DIRS = [
-    FRONTEND_DIR,
 ]
 
 DEMO_FILES_LIMIT = 50 * 1024 ** 2  # 50 MiB
 
-# Frontend
-webpack_stats = os.path.join(FRONTEND_DIR, 'webpack-stats.json')
-if not os.path.exists(webpack_stats):
-    # Empty stats file for unit tests
-    _, webpack_stats = tempfile.mkstemp(suffix="stats.json")
-    with open(webpack_stats, 'w') as f:
-        f.write('{"status": "done", "chunks": {"main": []}}')
-
-WEBPACK_LOADER = {
-    'DEFAULT': {
-        'BUNDLE_DIR_NAME': '',
-        'STATS_FILE': webpack_stats
-    }
-}
-
 
 # API
 REST_FRAMEWORK = {
@@ -382,9 +357,15 @@ CSRF_COOKIE_DOMAIN = os.environ.get('COOKIE_DOMAIN')
 SESSION_COOKIE_NAME = 'arkindex.auth'
 SESSION_COOKIE_DOMAIN = os.environ.get('COOKIE_DOMAIN')
 
-CORS_ORIGIN_WHITELIST = env2list('CORS_ORIGIN_WHITELIST')
+CORS_ORIGIN_WHITELIST = env2list('CORS_ORIGIN_WHITELIST', default=[
+    'universalviewer.io',
+    'localhost:8080',
+    '127.0.0.1:8080',
+    'localhost:5000',
+    '127.0.0.1:5000',
+])
 CORS_ALLOW_CREDENTIALS = True
-CORS_URLS_REGEX = r'^/api/.*$'
+CORS_URLS_REGEX = r'^/(api|ponos)/.*$'
 
 # Docker images used by our ponos workflow
 ARKINDEX_APP_IMAGE = os.environ.get('ARKINDEX_APP_IMAGE', 'arkindex-app')
diff --git a/arkindex/project/urls.py b/arkindex/project/urls.py
index a39edb9a73..37a0d594a3 100644
--- a/arkindex/project/urls.py
+++ b/arkindex/project/urls.py
@@ -1,4 +1,5 @@
-from django.urls import re_path, path, include
+from django.urls import path, include
+from django.conf import settings
 from django.contrib import admin
 from arkindex.project.api_v1 import api
 from arkindex.project.views import FrontendView
@@ -7,8 +8,12 @@ urlpatterns = [
     path('api/v1/', include((api, 'api'), namespace='api')),
     path('ponos/', include('ponos.urls')),
     path('admin/', admin.site.urls),
-    path('imports/', include('arkindex.dataimport.urls')),
-    re_path('', include('arkindex.documents.urls')),
     path('user/reset/<uidb64>/<token>/', FrontendView.as_view(), name='password_reset_confirm'),
-    re_path('', FrontendView.as_view()),
+    path('imports/credentials/', FrontendView.as_view(), name='credentials'),
 ]
+
+if 'debug_toolbar' in settings.INSTALLED_APPS:
+    import debug_toolbar
+    urlpatterns = [
+        path('__debug__/', include(debug_toolbar.urls)),
+    ] + urlpatterns
diff --git a/arkindex/project/views.py b/arkindex/project/views.py
index 1272958391..00b8c7746e 100644
--- a/arkindex/project/views.py
+++ b/arkindex/project/views.py
@@ -1,9 +1,8 @@
-from django.views.generic import TemplateView
+from django.views.generic import View
 
 
-class FrontendView(TemplateView):
+class FrontendView(View):
     """
     View that show the frontend's router
     TODO: Get rid of this view once the frontend is fully split
     """
-    template_name = 'front.html'
diff --git a/arkindex/templates/front.html b/arkindex/templates/front.html
deleted file mode 100644
index a03f70589a..0000000000
--- a/arkindex/templates/front.html
+++ /dev/null
@@ -1,19 +0,0 @@
-{% load package_version %}
-<!DOCTYPE html>
-<html>
-  <head>
-    <meta charset="utf-8">
-    <meta name="viewport" content="width=device-width, initial-scale=1">
-    <title>ArkIndex</title>
-    {% load render_bundle from webpack_loader %}
-    {% render_bundle 'main' 'css' %}
-  </head>
-  <body>
-
-    <div id="app"></div>
-
-    <footer class="footer is-paddingless has-text-right">Version {% package_version %}</footer>
-
-    {% render_bundle 'main' 'js' %}
-  </body>
-</html>
diff --git a/arkindex/users/tests/test_providers.py b/arkindex/users/tests/test_providers.py
index a395de3624..0c8d22d6d3 100644
--- a/arkindex/users/tests/test_providers.py
+++ b/arkindex/users/tests/test_providers.py
@@ -36,5 +36,5 @@ class TestProviders(FixtureTestCase):
         response = self.client.get(
             reverse('api:oauth-callback', kwargs={'provider': 'provider-slug'}),
         )
-        self.assertRedirects(response, reverse('credentials'))
+        self.assertRedirects(response, reverse('credentials'), fetch_redirect_response=False)
         self.assertEqual(self.creds.status, OAuthStatus.Error)
diff --git a/requirements.txt b/requirements.txt
index 546baddbf5..96e199d909 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -6,7 +6,6 @@ chardet==3.0.4
 django-cors-headers==2.4.0
 django-enumfields==1.0.0
 djangorestframework==3.7.1
-django-webpack-loader==0.5.0
 et-xmlfile==1.0.1
 gitpython==2.1.11
 idna==2.6
-- 
GitLab