使用信号功能初衷是为了不重写save(),delete()功能,保持models的干净。
使用信号前,重写了save()和delete()功能
models.py模块里的内容
class owgameserver(models.Model):
serverid = models.IntegerField(u"游戏服ID号",primary_key=True)
serverhost = models.GenericIPAddressField(u"游戏服IP")
servername = models.CharField(u"游戏服名称",max_length=32)
serverport = models.IntegerField(u"游戏服端口号")
###重写save功能
def save(self,*args,**kwargs):
print '添加新服:服id:%s-服ip:%s-服名称:%s-服端口:%s' %(self.serverid,self.serverhost,self.servername,self.serverport)
super(owgameserver,self).save(*args,**kwargs)
print ansible_owgameserver_file
ansiblefile = open(ansible_owgameserver_file,'a')
ansiblefile.write('ow%s ansible_ssh_host=%s server_port=%s server_id=%s \n' %(self.serverid,self.serverhost,self.serverport,self.serverid))
ansiblefile.close()
print '添加完成.'
###重写delete功能
def delete(self):
print '删除服:服id:%s-服ip:%s-服名称:%s-服端口:%s' %(self.serverid,self.serverhost,self.servername,self.serverport)
comment = re.compile(r'ow%s.*%s' %(self.serverid,self.serverid))
with open(ansible_owgameserver_file,'rt') as f:
data = f.read()
# print data
re_data = comment.sub("",data)
f_new = open('%s_new' %ansible_owgameserver_file,'wt')
f_new.write(re_data)
f_new.close()
os.remove(ansible_owgameserver_file)
os.rename('%s_new' %ansible_owgameserver_file,ansible_owgameserver_file)
f = open(ansible_owgameserver_file,'a+')
fnew = open('%s_new' %ansible_owgameserver_file,'wb')
for line in f.readlines():
data = line.strip()
if len(data) != 0:
fnew.write(data)
fnew.write('\n')
f.close()
fnew.close()
os.remove(ansible_owgameserver_file)
os.rename('%s_new' %ansible_owgameserver_file,ansible_owgameserver_file)
super(owgameserver,self).delete()
def __unicode__(self):
return self.servername
使用信号后,调用pre_delete,post_save来触发models保存和删除后执行的一些操作
查看文档,在django 1.7后,使用信号时候需要在应用配置类中的ready() 方法中连接。
所以我们需要配置先ready()
需要在以下两个地方写入配置
需要在项目的app.py,_init_.py两个文件中写入配置
app.py中的内容
class OwgameConfig(AppConfig):
name = 'owgame'
####添加ready函数
def ready(self):
import owgame.signals ###项目的signals位置
_init_.py中的内容
default_app_config = 'owgame.apps.OwgameConfig'
###OwgameConfig是app.py中的定义的class类名
signals中的内容
from models import owgameserver
from django.dispatch import receiver,Signal
from django.db.models.signals import post_delete,post_save,pre_save,pre_delete
@receiver(pre_delete,sender=owgameserver)
def del_ansible_host(sender,instance,**kwargs):
delserver = owgameserver.objects.filter(pk=instance.serverid)
print u'删除%s,%s' %(delserver[0].servername,delserver[0].serverhost)
print u'删除文件中的列表'
@receiver(post_save,sender=owgameserver)
def save_ansible_host(sender,instance,**kwargs):
addserver = owgameserver.objects.filter(pk=instance.serverid)
print u'添加:%s,%s' %(addserver[0].servername,addserver[0].serverhost)
print u'保存内容'
测试在添加models和删除models是否会触发这个信号中的内容
添加一个新服
查看日志输出
[28/Oct/2016 15:51:46] "GET /admin/owgame/owgameserver/ HTTP/1.1" 200 7172
[28/Oct/2016 15:51:46] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
[28/Oct/2016 15:51:48] "GET /admin/owgame/owgameserver/add/ HTTP/1.1" 200 5355
[28/Oct/2016 15:51:48] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
添加新服:服id:7-服ip:192.168.12.23-服名称:7服-服端口:8007
添加:7服,192.168.12.23
保存内容
已触发到保存的信号。
删除一个服
查看输出日志
[28/Oct/2016 15:54:27] "GET /admin/owgame/owgameserver/7/change/ HTTP/1.1" 200 5632
[28/Oct/2016 15:54:27] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
[28/Oct/2016 15:54:31] "GET /admin/owgame/owgameserver/7/delete/ HTTP/1.1" 200 3035
删除7服,192.168.12.23
删除文件中的列表
[28/Oct/2016 15:54:43] "POST /admin/owgame/owgameserver/7/delete/ HTTP/1.1" 302 0
[28/Oct/2016 15:54:44] "GET /admin/owgame/owgameserver/ HTTP/1.1" 200 7323
[28/Oct/2016 15:54:44] "GET /admin/jsi18n/ HTTP/1.1" 200 3217
已触发删除信息。