从管理员轻松运行Django管理命令
django-mcadmin的Python项目详细描述
django-mcadmin is a Django reusable app that provides simple run management commands from admin
Installation
- 从git存储库获取源代码副本:$ git clone https://github.com/vint21h/django-mcadmin.git。或者从https://github.com/vint21h/django-mcadmin/tags/下载最新版本。在
- 从存储库源代码树或解压缩的存档文件运行$ python ./setup.py install。或者使用pip:$ pip install django-mcadmin。在
Configuration
- 将"mcadmin"添加到settings.INSTALLED_APPS:
# settings.pyINSTALLED_APPS+=["mcadmin",]
- 将"mcadmin"添加到URL定义中:
- 在项目文件夹中运行$ python ./manage.py migrate,以应用程序迁移。在
Settings
- MCADMIN_EXAMPLES_PATH
- 管理命令文件模板路径。默认为:settings.STATIC_ROOT。
- MCADMIN_UPLOADS_PATH
- 带有文件上载路径的管理命令窗体。默认为:settings.MEDIA_ROOT。
- MCADMIN_MODULE_NAME
- 管理命令管理类搜索模块名称。默认为:"mcommands"。
- MCADMIN_USE_PERMISSIONS
- 管理命令管理类搜索模块名称。默认为:False。
Usage
例如,exists管理命令如下:
# management/commands/something-useless.pyfromdjango.core.management.baseimportBaseCommandclassCommand(BaseCommand):help="Useless management command"defadd_arguments(self,parser):parser.add_argument("--object-id","-o",dest="object_id",help="Object ID",action="store",required=True,metavar="OBJECT_ID",type=int,)defhandle(self,*args,**kwargs):self.stdout.write(kwargs.get("object_id"))
接下来,您需要为管理命令admin创建一个表单,我们将在下一段代码中使用该表单:
# forms.pyfromdjangoimportformsclassSomethingUselessManagementCommandAdminForm(forms.Form):object_id=forms.IntegerField(label="Object ID",required=True)
最后,编写管理命令管理类并注册它:
# mcommands/something-useless.pyfrommcadmin.commandimportManagementCommandAdminfrommcadmin.registryimportregistryfromformsimportSomethingUselessManagementCommandAdminFormclassSomethingUselessManagementCommandAdmin(ManagementCommandAdmin):command="something-useless"name="Useless management command"form=SomethingUselessManagementCommandAdminForm# registering management command admin custom classesregistry.register(command=SomethingUselessManagementCommandAdmin)
此外,还有一些帮助程序可以构建更复杂的流,比如可以直接执行的管理命令,也可以作为后台任务执行,或者处理上载文件的管理命令。例如:
管理命令:
# management/commands/distributed-something-useless-with-file.pyfrommcadmin.management.commandsimportTaskCommandclassCommand(TaskCommand):help="Useless management command which process file uploaded from a command from and can be executed directly or as background task"defadd_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"))
管理命令管理窗体:
# forms.pyfromdjangoimportformsfrommcadmin.forms.helpersimport(ManagementCommandAdminTaskForm,ManagementCommandAdminFilesForm)classDistributedSomethingUselessWithFileManagementCommandAdminForm(ManagementCommandAdminTaskForm,ManagementCommandAdminFilesForm):data=forms.FileField(label="data, required=True)object_id=forms.IntegerField(label="Object ID",required=True)
管理命令管理示例文件:
# mcommands/examples.pyfrommcadmin.exampleimportManagementCommandAdminExampleFileclassDistributedSomethingUselessWithFileManagementCommandAdminExampleFile(ManagementCommandAdminExampleFile):description="Management command with files example file"path="distributed-something-useless-with-file-example.csv"
或者对于不使用Django提供服务但可通过HTTP直接下载的文件:
# mcommands/examples.pyfrommcadmin.exampleimportManagementCommandAdminExampleFileclassDistributedSomethingUselessWithFileManagementCommandAdminExampleFile(ManagementCommandAdminExampleFile):description="Management command with files example file"path="https://www.example.com/distributed-something-useless-with-file-example.csv"raw=True
管理命令管理员:
# mcommands/something-useless.pyfrommcadmin.commandimportManagementCommandAdminfrommcadmin.registryimportregistryfromformsimportDistributedSomethingUselessWithFileManagementCommandAdminFormclassDistributedSomethingUselessWithFileManagementCommandAdmin(ManagementCommandAdmin):command="distributed-something-useless-with-file"name="Distributed useless management command with file"form=DistributedSomethingUselessWithFileManagementCommandAdminFormexamples=[DistributedSomethingUselessWithFileManagementCommandAdminExampleFile]# registering management command admin custom classesregistry.register(command=DistributedSomethingUselessWithFileManagementCommandAdmin)
Licensing
django-mcadmin是自由软件:您可以根据自由软件基金会发布的GNU通用公共许可证的条款重新发布和/或修改它,可以是该许可证的第3版,也可以是任何更高版本(由您选择)。 有关完整的许可证文本,请参见复制文件。在
Contacts
Project网站:https://github.com/vint21h/django-mcadmin/
Author:Alexei Andrushievich<;vint21h@vint21h.pp.ua>
有关其他作者列表,请参阅作者文件。在
- 项目
标签: