Skip to content

Commit

Permalink
add geoserver integration with django (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
giancastro committed Jan 17, 2023
1 parent f03e46e commit 120d554
Show file tree
Hide file tree
Showing 10 changed files with 340 additions and 36 deletions.
144 changes: 115 additions & 29 deletions makestack/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,35 +201,26 @@ def _add_example_tasks(self):
utils.copy_file(celery_source, celery_destination)

def _add_views(self):
imports = (
"from django_celery_results.models import TaskResult\n"
"from config.celery import hello_world\n"
"from config.serializers import TaskSerializer\n"
)
views = (
"\nclass TaskRetrieveView(generics.RetrieveAPIView):\n"
" queryset = TaskResult.objects.all()\n"
" serializer_class = TaskSerializer\n"
" permission_classes = [permissions.AllowAny]\n"
' lookup_field = "task_id"\n'
"\n"
"\n"
"class HelloWorldView(APIView):\n"
" def get(self, request):\n"
" task_id = hello_world.delay().task_id\n"
' content = {"task_id": task_id}\n'
" return Response(content)\n"
views = utils.get_file_content(
os.path.join(self.here, "data", "celery", "views.txt")
)

config_views_file = os.path.join(
self.directory_path, "backend", "config", "views.py"
)
utils.append_to_file_after_matching(
utils.add_third_party_import(
config_views_file,
"from rest_framework.views import APIView # noqa: F401",
imports,
break_line_before=1,
"from django_celery_results.models import TaskResult",
)
utils.add_third_party_import(
config_views_file,
"from config.celery import hello_world",
)
utils.add_third_party_import(
config_views_file,
"from config.serializers import TaskSerializer",
)

utils.append_to_file(
config_views_file,
views,
Expand Down Expand Up @@ -419,17 +410,27 @@ def _add_view(self):
view_txt = os.path.join(self.here, "data", "react", "view.txt")
view_content = utils.get_file_content(view_txt)

view_imports = os.path.join(self.here, "data", "react", "view_imports.txt")
view_imports_content = utils.get_file_content(view_imports)

destination = os.path.join(self.directory_path, "backend", "config", "views.py")
utils.append_to_file(

utils.add_native_import(
destination,
view_content,
"import os",
)
utils.append_to_file_top(
utils.add_third_party_import(
destination,
view_imports_content,
"from django.conf import settings",
)
utils.add_third_party_import(
destination,
"from django.http import HttpResponse",
)
utils.add_third_party_import(
destination,
"from django.views.generic import View",
)
utils.append_to_file(
destination,
view_content,
)

def _copy_docker_folder(self):
Expand Down Expand Up @@ -477,6 +478,91 @@ def _copy_deploy_folder(self):
prod_destination = os.path.join(self.directory_path, "deploy", "prod")
shutil.copytree(prod_source, prod_destination, dirs_exist_ok=True)

def integrate_with_django(self):
# Add django-revproxy to requirements
requirements_file = os.path.join(
self.directory_path, "backend", "requirements.txt"
)
utils.add_requirement(requirements_file, "\ndjango-revproxy==0.10.0")

# Add django-revproxy to installed apps
settings_file = os.path.join(
self.directory_path, "backend", "config", "settings.py"
)
utils.append_to_file_after_matching(
settings_file,
"THIRD_PARTY_APPS \= \[", # noqa: W605
' "revproxy",',
)

# Add django-revproxy to urls
urls = (
' path("admin/geoserver/", views.geoserver, name="geoserver"),\n'
' re_path(\n'
' r"geoserver/(?P<path>.*)",\n'
' views.GeoServerProxyView.as_view(),\n'
' name="geoserver"\n'
' ),'
)

urls_file = os.path.join(self.directory_path, "backend", "config", "urls.py")
utils.append_to_file_after_matching(
urls_file,
"urlpatterns \= \[", # noqa: W605
urls,
)

# Add django-revproxy to views
views = os.path.join(
self.here, "data", "geoserver", "django_integration", "views.txt"
)

views_content = utils.get_file_content(views)

views_destination = os.path.join(
self.directory_path, "backend", "config", "views.py"
)
utils.add_third_party_import(
views_destination,
"from django.contrib import admin",
)
utils.add_third_party_import(
views_destination,
"from django.contrib.auth.decorators import login_required",
)
utils.add_third_party_import(
views_destination,
"from django.http import HttpResponse",
)
utils.add_third_party_import(
views_destination,
"from django.template import loader",
)
utils.add_third_party_import(
views_destination,
"from revproxy.views import ProxyView",
)

utils.append_to_file(views_destination, views_content)

# Add django-revproxy to templates
template_path = os.path.join(
self.directory_path, "backend", "config", "templates", "admin", "base.html"
)
utils.append_to_file_after_matching(
template_path,
" {% block userlinks %}",
""" <a href="{% url 'geoserver' %}">{% trans 'GeoServer' %}</a> /""", # noqa: 501
)
template_source = os.path.join(self.here, "data", "geoserver", "templates")
template_destination = os.path.join(
self.directory_path,
"backend",
"config",
"templates",
)
shutil.copytree(template_source, template_destination, dirs_exist_ok=True)

def _set_up(self):
self._add_env_variables()
self._add_service()
Expand Down
14 changes: 14 additions & 0 deletions makestack/data/celery/views.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


class TaskRetrieveView(generics.RetrieveAPIView):
queryset = TaskResult.objects.all()
serializer_class = TaskSerializer
permission_classes = [permissions.AllowAny]
lookup_field = "task_id"


class HelloWorldView(APIView):
def get(self, request):
task_id = hello_world.delay().task_id
content = {"task_id": task_id}
return Response(content)
3 changes: 1 addition & 2 deletions makestack/data/django/base/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
# fmt: off
"DIRS": [
os.path.join(BASE_DIR, "config", "templates/"),
],
# fmt: on
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
Expand Down
105 changes: 105 additions & 0 deletions makestack/data/django/base/config/templates/admin/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{% load i18n static %}<!DOCTYPE html>
{% get_current_language as LANGUAGE_CODE %}{% get_current_language_bidi as LANGUAGE_BIDI %}
<html lang="{{ LANGUAGE_CODE|default:"en-us" }}" dir="{{ LANGUAGE_BIDI|yesno:'rtl,ltr,auto' }}">
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}">
{% if not is_popup and is_nav_sidebar_enabled %}
<link rel="stylesheet" type="text/css" href="{% static "admin/css/nav_sidebar.css" %}">
<script src="{% static 'admin/js/nav_sidebar.js' %}" defer></script>
{% endif %}
{% block extrastyle %}{% endblock %}
{% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% block stylesheet_rtl %}{% static "admin/css/rtl.css" %}{% endblock %}">{% endif %}
{% block extrahead %}{% endblock %}
{% block responsive %}
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" type="text/css" href="{% static "admin/css/responsive.css" %}">
{% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% static "admin/css/responsive_rtl.css" %}">{% endif %}
{% endblock %}
{% block blockbots %}<meta name="robots" content="NONE,NOARCHIVE">{% endblock %}
</head>

<body class="{% if is_popup %}popup {% endif %}{% block bodyclass %}{% endblock %}"
data-admin-utc-offset="{% now "Z" %}">

<!-- Container -->
<div id="container">

{% if not is_popup %}
<!-- Header -->
{% block header %}
<div id="header">
<div id="branding">
{% block branding %}{% endblock %}
</div>
{% block usertools %}
{% if has_permission %}
<div id="user-tools">
{% block welcome-msg %}
{% translate 'Welcome,' %}
<strong>{% firstof user.get_short_name user.get_username %}</strong>.
{% endblock %}
{% block userlinks %}
{% if site_url %}
<a href="{{ site_url }}">{% translate 'View site' %}</a> /
{% endif %}
{% if user.is_active and user.is_staff %}
{% url 'django-admindocs-docroot' as docsroot %}
{% if docsroot %}
<a href="{{ docsroot }}">{% translate 'Documentation' %}</a> /
{% endif %}
{% endif %}
{% if user.has_usable_password %}
<a href="{% url 'admin:password_change' %}">{% translate 'Change password' %}</a> /
{% endif %}
<a href="{% url 'admin:logout' %}">{% translate 'Log out' %}</a>
{% endblock %}
</div>
{% endif %}
{% endblock %}
{% block nav-global %}{% endblock %}
</div>
{% endblock %}
<!-- END Header -->
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'admin:index' %}">{% translate 'Home' %}</a>
{% if title %} &rsaquo; {{ title }}{% endif %}
</div>
{% endblock %}
{% endif %}

<div class="main" id="main">
{% if not is_popup and is_nav_sidebar_enabled %}
{% block nav-sidebar %}
{% include "admin/nav_sidebar.html" %}
{% endblock %}
{% endif %}
<div class="content">
{% block messages %}
{% if messages %}
<ul class="messagelist">{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
{% endfor %}</ul>
{% endif %}
{% endblock messages %}
<!-- Content -->
<div id="content" class="{% block coltype %}colM{% endblock %}">
{% block pretitle %}{% endblock %}
{% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}
{% block content_subtitle %}{% if subtitle %}<h2>{{ subtitle }}</h2>{% endif %}{% endblock %}
{% block content %}
{% block object-tools %}{% endblock %}
{{ content }}
{% endblock %}
{% block sidebar %}{% endblock %}
<br class="clear">
</div>
<!-- END Content -->
{% block footer %}<div id="footer"></div>{% endblock %}
</div>
</div>
</div>
<!-- END Container -->
</body>
</html>
5 changes: 5 additions & 0 deletions makestack/data/django/base/config/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Native Libraries Imports

# Third Party Libraries Imports
from rest_framework import generics, permissions # noqa: F401
from rest_framework.response import Response # noqa: F401
from rest_framework.views import APIView # noqa: F401

# Local Libraries Imports
18 changes: 18 additions & 0 deletions makestack/data/geoserver/django_integration/views.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@


@login_required
def geoserver(request):
context = {
**{"request": request},
**admin.site.each_context(request),
**{"user": request.user},
}
t = loader.get_template("geoserver/index.html")
return HttpResponse(t.render(context))


class GeoServerProxyView(ProxyView):
upstream = "http://geoserver:8080/geoserver"

def test_func(self):
return permissions.IsAuthenticated
31 changes: 31 additions & 0 deletions makestack/data/geoserver/templates/geoserver/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends "admin/base_site.html" %}

{% block breadcrumbs %}{% endblock %}

{% block content %}
<style>
#content {
padding: 0;
}

body {
overflow: hidden;
}

iframe {
height: 85vh;
width: 100%;
border: 0;
}

#header .button-group button {
visibility: hidden;
}
</style>
<div class="main container">
<p align="center">
<iframe id="geoserver" src="{% url 'geoserver' 'web/' %}">
</iframe>
</p>
</div>
{% endblock %}
5 changes: 0 additions & 5 deletions makestack/data/react/view_imports.txt

This file was deleted.

3 changes: 3 additions & 0 deletions makestack/startproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def startproject(name, directory):
)
geoserver.set_up()

if project_environment["django"]:
geoserver.integrate_with_django()

if project_environment["mapshader"]:
mapshader = blocks.Mapshader(
name="Mapshader",
Expand Down
Loading

0 comments on commit 120d554

Please sign in to comment.