Skip to content

Instantly share code, notes, and snippets.

@daimon99
daimon99 / admin.py
Last active April 1, 2020 09:27
Django admin action with intermedia page.Django admin 的 action 使用中间页
@admin.register(WBS)
class WBSAdmin(admin.ModelAdmin):
list_display = (
'id',
'code',
'name',
'parent',
'pv',
'ev',
'created',
@daimon99
daimon99 / print_code_to_docx.py
Created March 18, 2020 09:24
软件著作权代码打印到 word docx 文件
# coding: utf-8
# pip install python-docx
def get_all_python_files(directory):
import os, pathlib
all_python_files = list(pathlib.Path(os.path.expanduser(directory)).glob('./**/[!config]*/*.py'))
all_python_files.extend(list(pathlib.Path(os.path.expanduser(directory)).glob('*.py')))
return all_python_files
def is_not_excluded_text(line):
@daimon99
daimon99 / resources.py
Last active February 28, 2020 13:44
Custom django import export resource class, using verbose_name as export file header, and support clean field in the resource class。自定义的 django import export resource 类,支持用 verbose_name 作为导出/导入文件的列名,并支持在 Resouce 类中直接 clean 字段值
# coding: utf-8
from collections import OrderedDict
from copy import deepcopy
from django.core.exceptions import ValidationError
from django.utils.encoding import force_str
from import_export import resources, widgets
from . import models as m
@daimon99
daimon99 / admin.py
Last active January 21, 2020 01:21
Money format style in django admin。金额样式,两位小数,纯前端实现(django admin)
@admin.register(m.Project)
class ProjectAdmin(admin.ModelAdmin):
formfield_overrides = {
models.DecimalField: {'widget': widgets.DecimalInput},
}
@daimon99
daimon99 / admin.py
Last active January 21, 2020 00:56
Casade select pure frontend in django admin。纯前端实现Django Admin级联选择
class BudgetInline(admin.TabularInline):
class Media:
js = (JQUERY_MIN_JS, )
model = m.Budget
exclude = ['department', ]
readonly_fields = ['available_amount', ]
form = forms.BudgetAdminForm
@admin.register(m.Budget)
@daimon99
daimon99 / admin.py
Last active April 30, 2022 23:06
Django admin different changeform in add / change。Django Admin新增与修改页面使用不同的Form。
@admin.register(m.Holiday)
class HolidayAdmin(admin.ModelAdmin):
list_display = ('id', 'day', 'category')
search_fields = ('day',)
list_filter = ('category',)
date_hierarchy = 'day'
def get_form(self, request, obj=None, change=False, **kwargs):
if not change:
@daimon99
daimon99 / loggings.py
Last active April 10, 2020 09:11
通过本机file socket 直接调试在线应用
# pip install remote-pdb-2
import threading
from remote_pdb import RemotePdb
from bdb import BdbQuit
import os
def start_trace():
while True:
try:
@daimon99
daimon99 / prd.sh
Last active November 27, 2019 01:36
与堡垒机互动,通过expect自动化脚本
#!/usr/bin/expect
log_user 0
set timeout 10
send_user "start...\n"
spawn ssh jms
expect "*Opt"
send "happ01\r"
send_user "代码拉取中...\n"
send "cd /data/prd/uniauth; git pull; cp lua/* /usr/local/openresty/nginx/lua/; echo ok\n"
@daimon99
daimon99 / readme.md
Created November 26, 2019 04:11
vi 查看文件乱码的处理

可以直接在线转码。 见下

:e ++enc=gbk
@daimon99
daimon99 / tasks.py
Last active November 25, 2019 11:15
subprocess 实时打印输出,并检测命令行的返回值
# coding: utf-8
from .celery import app
# 关注下这个库,比较方便写 linux 命令行工具
# https://docs.python.org/3/library/shlex.html
@app.task(bind=True, max_retries=2, acks_late=True)
def convert_video(self):
print('====== start =======')
import subprocess
import os