Advertisement
Guest User

Untitled

a guest
May 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3.  
  4. from django.db import models
  5. from django.db.models.signals import m2m_changed
  6.  
  7.  
  8. class Host(models.Model):
  9.      name = models.CharField(max_length=20)
  10.      isdelete = models.BooleanField(default=False)
  11.  
  12.      def __unicode__(self):
  13.           return self.name
  14.  
  15.  
  16. class Project(models.Model):
  17.      name = models.CharField(max_length=20)
  18.      host = models.ManyToManyField(Host)
  19.      isdelete = models.BooleanField(default=False)
  20.  
  21.      def __unicode__(self):
  22.           return self.name
  23.  
  24.  
  25. class Instance(models.Model):
  26.      name = models.CharField(max_length=20)
  27.      project = models.ForeignKey(Project)
  28.      host = models.ManyToManyField(Host)
  29.      isdelete = models.BooleanField(default=False)
  30.  
  31.      def __unicode__(self):
  32.           return self.name
  33.  
  34.  
  35. def instance_m2m_change(**kwargs):
  36.      instance = kwargs.pop('instance', None)
  37.      host = kwargs.pop('model', None)
  38.      host_id = kwargs.pop('pk_set', None)
  39.      action = kwargs.pop('action', None)
  40.      if action == 'post_add':
  41.           if host_id:
  42.                for i in host_id:
  43.                     host_obj = host.objects.get(id=i)
  44.                     instance.project.host.add(host_obj)
  45.      if action == 'post_remove':
  46.           if host_id:
  47.                for i in host_id:
  48.                     host_obj = host.objects.get(id=i)
  49.                     still_has_instance = instance.project.instance_set.filter(host=host_obj)
  50.                     if not still_has_instance:
  51.                          instance.project.host.remove(host_obj)
  52.                          host_obj = host.objects.get(id=i)
  53.                          still_has_instance = instance.project.instance_set.filter(host=host_obj)
  54.  
  55.  
  56. class InstanceModule(models.Model):
  57.      name = models.CharField(max_length=20)
  58.      instance = models.ForeignKey(Instance)
  59.      host = models.ManyToManyField(Host)
  60.      isdelete = models.BooleanField(default=False)
  61.  
  62.      def __unicode__(self):
  63.           return self.name
  64.  
  65.  
  66. def instancemodule_m2m_change(**kwargs):
  67.      instance = kwargs.pop('instance', None)
  68.      host = kwargs.pop('model', None)
  69.      host_id = kwargs.pop('pk_set', None)
  70.      action = kwargs.pop('action', None)
  71.      if action == 'post_add':
  72.           if host_id:
  73.                for i in host_id:
  74.                     host_obj = host.objects.get(id=i)
  75.                     instance.instance.host.add(host_obj)
  76.      if action == 'post_remove':
  77.           if host_id:
  78.                for i in host_id:
  79.                     host_obj = host.objects.get(id=i)
  80.                     still_has_instance = instance.instance.instancemodule_set.filter(host=host_obj)
  81.                     if not still_has_instance:
  82.                          instance.instance.host.remove(host_obj)
  83.  
  84. m2m_changed.connect(instance_m2m_change, sender=Instance.host.through)
  85. m2m_changed.connect(instancemodule_m2m_change, sender=InstanceModule.host.through)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement