Easily run django management commands from admin
Project description
django-mcadmin is a Django reusable app that provides simple run management commands from admin
Installation
Obtain your copy of source code from the git repository: $ git clone https://github.com/vint21h/django-mcadmin.git. Or download the latest release from https://github.com/vint21h/django-mcadmin/tags/.
Run $ python ./setup.py install from the repository source tree or unpacked archive. Or use pip: $ pip install django-mcadmin.
Configuration
Add "mcadmin" to settings.INSTALLED_APPS:
# settings.py
INSTALLED_APPS += [
"mcadmin",
]
Add "mcadmin" to your URLs definitions:
# urls.py
from django.conf.urls import url
urlpatterns += [
url(r"^admin/mcadmin/", include("mcadmin.urls")),
]
Run $ python ./manage.py migrate in your project folder to apply app migrations.
Settings
- MCADMIN_EXAMPLES_PATH
Management commands files templates path. Defaults to: settings.STATIC_ROOT.
- MCADMIN_UPLOADS_PATH
Management commands forms with files upload path. Defaults to: settings.MEDIA_ROOT.
- MCADMIN_MODULE_NAME
Management commands admin classes search module name. Defaults to: "mcommands".
- MCADMIN_USE_PERMISSIONS
Management commands admin classes search module name. Defaults to: False.
Usage
For example, exists management command like this:
# management/commands/something-useless.py
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = "Useless management command"
def add_arguments(self, parser):
parser.add_argument(
"--object-id",
"-o",
dest="object_id",
help="Object ID",
action="store",
required=True,
metavar="OBJECT_ID",
type=int,
)
def handle(self, *args, **kwargs):
self.stdout.write(kwargs.get("object_id"))
Next, you need to create a form for this management command admin which we use in the next piece of code:
# forms.py
from django import forms
class SomethingUselessManagementCommandAdminForm(forms.Form):
object_id = forms.IntegerField(label="Object ID", required=True)
And finally, write management command admin class and register it:
# mcommands/something-useless.py
from mcadmin.command import ManagementCommandAdmin
from mcadmin.registry import registry
from forms import SomethingUselessManagementCommandAdminForm
class SomethingUselessManagementCommandAdmin(ManagementCommandAdmin):
command = "something-useless"
name = "Useless management command"
form = SomethingUselessManagementCommandAdminForm
# registering management command admin custom classes
registry.register(command=SomethingUselessManagementCommandAdmin)
Also, there are some helpers for building more complex flows, like management commands that can be executed directly or as a background task or management commands that handle uploaded files. For example:
Management command:
# management/commands/distributed-something-useless-with-file.py
from mcadmin.management.commands import TaskCommand
class Command(TaskCommand):
help = "Useless management command which process file uploaded from a command from and can be executed directly or as background task"
def add_arguments(self, parser):
parser.add_argument(
"--task",
"-T",
dest="as_task",
help="Run command as background task",
default=False,
action="store",
metavar="TASK",
type=bool,
)
parser.add_argument(
"--object-id",
"-o",
dest="object_id",
help="Object ID",
action="store",
required=True,
metavar="OBJECT_ID",
type=int,
)
parser.add_argument(
"--data",
"-D",
dest="data",
help="Path to file with data",
action="store",
metavar="DATA",
type=str,
)
def _local(self, *args, **kwargs):
self.stdout.write(kwargs.get("object_id"))
self.stdout.write(kwargs.get("data"))
def _as_task(self, *args, **kwargs):
# There must be code which executed in threads or call celery task or something else asynchronous.
self.stdout.write(kwargs.get("object_id"))
self.stdout.write(kwargs.get("data"))
Management command admin form:
# forms.py
from django import forms
from mcadmin.forms.helpers import (
ManagementCommandAdminTaskForm,
ManagementCommandAdminFilesForm
)
class DistributedSomethingUselessWithFileManagementCommandAdminForm(
ManagementCommandAdminTaskForm,
ManagementCommandAdminFilesForm
):
data = forms.FileField(label="data, required=True)
object_id = forms.IntegerField(label="Object ID", required=True)
Management command admin example file:
# mcommands/examples.py
from mcadmin.example import ManagementCommandAdminExampleFile
class DistributedSomethingUselessWithFileManagementCommandAdminExampleFile(
ManagementCommandAdminExampleFile
):
description = "Management command with files example file"
path = "distributed-something-useless-with-file-example.csv"
Or for the file which not served using Django but directly available for download via HTTP:
# mcommands/examples.py
from mcadmin.example import ManagementCommandAdminExampleFile
class DistributedSomethingUselessWithFileManagementCommandAdminExampleFile(
ManagementCommandAdminExampleFile
):
description = "Management command with files example file"
path = "https://www.example.com/distributed-something-useless-with-file-example.csv"
raw = True
Management command admin:
# mcommands/something-useless.py
from mcadmin.command import ManagementCommandAdmin
from mcadmin.registry import registry
from forms import DistributedSomethingUselessWithFileManagementCommandAdminForm
class DistributedSomethingUselessWithFileManagementCommandAdmin(ManagementCommandAdmin):
command = "distributed-something-useless-with-file"
name = "Distributed useless management command with file"
form = DistributedSomethingUselessWithFileManagementCommandAdminForm
examples = [DistributedSomethingUselessWithFileManagementCommandAdminExampleFile]
# registering management command admin custom classes
registry.register(command=DistributedSomethingUselessWithFileManagementCommandAdmin)
Licensing
django-mcadmin 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. For complete license text see COPYING file.
Contacts
Project Website: https://github.com/vint21h/django-mcadmin/
Author: Alexei Andrushievich <vint21h@vint21h.pp.ua>
For other authors list see AUTHORS file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-mcadmin-0.4.0.zip
.
File metadata
- Download URL: django-mcadmin-0.4.0.zip
- Upload date:
- Size: 56.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35450f04df47ea26db5e9c49d049f8c9515610580a4152f9e2f2a1d2f253e7a2 |
|
MD5 | c306acf1cd59a0331d2f1ee757a6f251 |
|
BLAKE2b-256 | c63a77387dc923aaff519dbc2d1b431b8bf4a9d2ec5280df4875a7d6e2860846 |
File details
Details for the file django_mcadmin-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: django_mcadmin-0.4.0-py3-none-any.whl
- Upload date:
- Size: 44.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6f645473cbad2da3c43db13dfeb258038d70b5333baf72df0d804d89628661f |
|
MD5 | 3a95e321ebdbb8bbd0ab4ea2ce6757af |
|
BLAKE2b-256 | 630cb61d7e726a2a194125d93145f8a915803932eb307c357ab5634c49686e01 |