-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:GeoNode/geonode
- Loading branch information
Showing
27 changed files
with
1,423 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
######################################################################### | ||
# | ||
# Copyright (C) 2021 OSGeo | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
######################################################################### | ||
from django.apps import AppConfig | ||
|
||
|
||
class ManagementCommandsHttpAppConfig(AppConfig): | ||
name = "geonode.management_commands_http" | ||
verbose_name = "Management Commands Over HTTP" | ||
|
||
|
||
default_app_config = "geonode.management_commands_http.ManagementCommandsHttpAppConfig" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
######################################################################### | ||
# | ||
# Copyright (C) 2021 OSGeo | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
######################################################################### | ||
from django.contrib import admin, messages | ||
from django.forms.models import ModelForm | ||
|
||
from geonode.management_commands_http.forms import ManagementCommandJobAdminForm | ||
from geonode.management_commands_http.models import ManagementCommandJob | ||
from geonode.management_commands_http.utils.jobs import ( | ||
start_task, | ||
stop_task, | ||
get_celery_task_meta, | ||
) | ||
|
||
|
||
@admin.register(ManagementCommandJob) | ||
class ManagementCommandJobAdmin(admin.ModelAdmin): | ||
actions = ["start", "stop"] | ||
list_per_page = 20 | ||
list_display = ( | ||
"id", | ||
"command", | ||
"app_name", | ||
"user", | ||
"args", | ||
"kwargs", | ||
"created_at", | ||
"start_time", | ||
"end_time", | ||
"status", | ||
"celery_result_id", | ||
) | ||
list_filter = ("command", "app_name", "user") | ||
search_fields = ("command", "app_name", "user", "celery_result_id", "output_message") | ||
|
||
def start(self, request, queryset): | ||
for job in queryset: | ||
try: | ||
start_task(job) | ||
except ValueError as exc: | ||
messages.error(request, str(exc)) | ||
|
||
def stop(self, request, queryset): | ||
for job in queryset: | ||
stop_task(job) | ||
|
||
def celery_state(self, instance): | ||
return get_celery_task_meta(instance).get("status") | ||
|
||
def celery_traceback(self, instance): | ||
return get_celery_task_meta(instance).get("traceback") | ||
|
||
def has_module_permission(self, request): | ||
return request.user.is_superuser | ||
|
||
def has_delete_permission(self, request, obj=None): | ||
return obj is not None and obj.status == ManagementCommandJob.CREATED | ||
|
||
def save_model(self, request, obj, form, change): | ||
obj.user = request.user | ||
obj.save() | ||
autostart = form.cleaned_data.get("autostart", False) | ||
if autostart and not change: | ||
start_task(obj) | ||
|
||
def add_view(self, request, form_url='', extra_context=None): | ||
self.form = ManagementCommandJobAdminForm | ||
self.fields = ("command", "args", "kwargs", "autostart",) | ||
self.readonly_fields = [] | ||
return super().add_view(request, form_url, extra_context) | ||
|
||
def change_view(self, request, object_id, form_url='', extra_context=None): | ||
self.form = ModelForm | ||
self.fields = ( | ||
"celery_result_id", | ||
"user", | ||
"command", | ||
"app_name", | ||
"args", | ||
"kwargs", | ||
"created_at", | ||
"start_time", | ||
"end_time", | ||
"modified_at", | ||
"status", | ||
"output_message", | ||
"celery_state", | ||
"celery_traceback", | ||
) | ||
self.readonly_fields = self.fields | ||
return super().change_view(request, object_id, form_url, extra_context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
######################################################################### | ||
# | ||
# Copyright (C) 2021 OSGeo | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
######################################################################### | ||
from django_filters import rest_framework as filters | ||
|
||
from geonode.management_commands_http.models import ManagementCommandJob | ||
|
||
|
||
class ManagementCommandJobFilterSet(filters.FilterSet): | ||
class Meta: | ||
model = ManagementCommandJob | ||
fields = [ | ||
"celery_result_id", | ||
"command", | ||
"app_name", | ||
"status", | ||
"user", | ||
"user__username", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from django import forms | ||
|
||
from geonode.management_commands_http.models import ManagementCommandJob | ||
from geonode.management_commands_http.utils.commands import ( | ||
get_management_commands, | ||
) | ||
|
||
|
||
class ManagementCommandJobAdminForm(forms.ModelForm): | ||
autostart = forms.BooleanField(required=False) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.available_commands = get_management_commands() | ||
self.fields["command"] = forms.ChoiceField( | ||
choices=[(command, command) for command in self.available_commands] | ||
) | ||
|
||
class Meta: | ||
model = ManagementCommandJob | ||
fields = ("command", "args", "kwargs",) | ||
|
||
def clean_args(self): | ||
value = self.cleaned_data.get('args') | ||
|
||
if type(value) != list: | ||
self.add_error("args", 'args must be a list') | ||
|
||
if "--help" in value: | ||
self.add_error("args", 'Forbidden argument: "--help"') | ||
|
||
return value | ||
|
||
def clean_kwargs(self): | ||
value = self.cleaned_data.get('kwargs') | ||
|
||
if type(value) != dict: | ||
self.add_error("kwargs", 'kwargs must be a dict') | ||
|
||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
######################################################################### | ||
# | ||
# Copyright (C) 2021 OSGeo | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
######################################################################### |
18 changes: 18 additions & 0 deletions
18
geonode/management_commands_http/management/commands/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
######################################################################### | ||
# | ||
# Copyright (C) 2021 OSGeo | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
######################################################################### |
40 changes: 40 additions & 0 deletions
40
geonode/management_commands_http/management/commands/ping_mngmt_commands_http.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
######################################################################### | ||
# | ||
# Copyright (C) 2021 OSGeo | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
######################################################################### | ||
from time import sleep | ||
from django.core.management.base import BaseCommand | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'It writes "pong" to stdout.' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("--sleep", nargs="?", const=1, type=float) | ||
parser.add_argument("--force_exception", nargs="?", type=bool) | ||
|
||
def handle(self, *args, **options): | ||
if options["sleep"]: | ||
seconds_to_sleep = options["sleep"] | ||
self.stdout.write(f"Sleeping for {seconds_to_sleep} seconds...") | ||
sleep(seconds_to_sleep) | ||
if options["force_exception"]: | ||
self.stdout.write( | ||
self.style.ERROR("As requested, an exception will be raised.") | ||
) | ||
raise RuntimeError("User Requested Exception") | ||
self.stdout.write(self.style.SUCCESS("pong")) |
Oops, something went wrong.