Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. class System(models.Model):
  2. fields...
  3. vulnerabilities = models.ManyToManyField('Vulnerability', through='SystemVulnerability')
  4.  
  5. class Vulnerablity(models.Model):
  6. name = ...
  7. risk = CharField(choices=['H','M','L','I'])
  8. ...
  9.  
  10. class SystemVulnerability(models.Model):
  11. vulnerability = ForeignKey
  12. system = ForeignKey
  13. fields...
  14.  
  15. sv_list = [199026, 199036, 199046, 199048, ....]
  16. other_filter = Q(... lots of stuff...) #not including this for brevity
  17.  
  18. System.objects.filter(other_filter & Q(systemvulnerability__in=sv_list)).query.__str__()
  19. u'SELECT "system"."id", "system"."ip", "system"."org_id", "system"."dnsname", "system"."ipownercode", "system"."securityplan"
  20. FROM "system"
  21. INNER JOIN "systemvulnerability" ON ( "system"."id" = "systemvulnerability"."system_id" )
  22. WHERE "systemvulnerability"."id" IN (199026, 199036, 199046, 199048, ....)'
  23.  
  24. System.objects.filter(other_filter & ~Q(systemvulnerability__in=sv_list)).query.__str__()
  25. u'SELECT "system"."id", "system"."ip", "system"."org_id", "system"."dnsname", "system"."ipownercode", "system"."securityplan"
  26. FROM "system"
  27. WHERE NOT ("system"."id" IN (SELECT U1."system_id" FROM "systemvulnerability" U1 WHERE U1."id" IN (199026, 199036, 199046, 199048, ....)))'
  28.  
  29. u'SELECT "system"."id", "system"."ip", "system"."org_id", "system"."dnsname", "system"."ipownercode", "system"."securityplan"
  30. FROM "system"
  31. INNER JOIN "systemvulnerability" ON ( "system"."id" = "systemvulnerability"."system_id" )
  32. WHERE "systemvulnerability"."id" NOT IN (199026, 199036, 199046, 199048, ....)'
  33.  
  34. Example data
  35.  
  36. System:
  37. s1
  38. s2
  39. s3
  40.  
  41. Vulnerability:
  42. v1
  43. v2
  44. v3
  45.  
  46. SystemVulnerability:
  47. sv1 = s1, v1
  48. sv2 = s1, v2
  49. sv3 = s2, v2
  50. sv4 = s3, v2
  51. sv5 = s3, v3
  52.  
  53. System.filter(Q(systemvulnerability__in=[sv1, sv4]))
  54. Get back s1, s3: Good!
  55.  
  56. System.filter(~Q(systemvulnerability__in=[sv1, sv2]))
  57. Get back: s2, s3: Good!
  58.  
  59. System.filter(~Q(systemvulnerability__in=[sv1, sv4, sv5]))
  60. Get back s2: Bad!
  61. But I wanted s1, s2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement