models.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # -*- coding: utf-8 -*-
  2. # © Copyright 2009 Andre Engelbrecht. All Rights Reserved.
  3. # This script is licensed under the BSD Open Source Licence
  4. # Please see the text file LICENCE for more information
  5. # If this script is distributed, it must be accompanied by the Licence
  6. from datetime import datetime
  7. from django.db import models
  8. from django.contrib.auth.models import User
  9. from django.utils.translation import ugettext_lazy as _
  10. from adzone.managers import AdManager
  11. class Advertiser(models.Model):
  12. """ A Model for our Advertiser
  13. """
  14. company_name = models.CharField(max_length=255)
  15. website = models.URLField(verify_exists=True)
  16. user = models.ForeignKey(User)
  17. def __unicode__(self):
  18. return "%s" % self.company_name
  19. def get_website_url(self):
  20. return "%s" % self.website
  21. class AdCategory(models.Model):
  22. """
  23. a Model to hold the different Categories for adverts
  24. """
  25. title = models.CharField(max_length=255)
  26. slug = models.SlugField(unique=True)
  27. description = models.TextField()
  28. class Meta:
  29. verbose_name = "Ad Category"
  30. verbose_name_plural = "Ad Categories"
  31. def __unicode__(self):
  32. return "%s" % self.title
  33. class AdZone(models.Model):
  34. """
  35. a Model that describes the attributes and behaviours of ad zones
  36. """
  37. title = models.CharField(max_length=255)
  38. slug = models.SlugField()
  39. description = models.TextField()
  40. class Meta:
  41. verbose_name = "Ad Zone"
  42. verbose_name_plural = "Ad Zones"
  43. def __unicode__(self):
  44. return "%s" % self.title
  45. class AdBase(models.Model):
  46. """
  47. This is our base model, from which all ads will inherit.
  48. The manager methods for this model will determine which ads to
  49. display return etc.
  50. """
  51. title = models.CharField(max_length=255)
  52. url = models.URLField(verify_exists=True)
  53. enabled = models.BooleanField(default=False)
  54. since = models.DateTimeField(default=datetime.now)
  55. expires_on = models.DateTimeField(_("Expires on"), blank=True, null=True)
  56. updated = models.DateTimeField(editable=False)
  57. # Relations
  58. advertiser = models.ForeignKey(Advertiser)
  59. category = models.ForeignKey(AdCategory)
  60. zone = models.ForeignKey(AdZone)
  61. # Our Custom Manager
  62. objects = AdManager()
  63. def __unicode__(self):
  64. return "%s" % self.title
  65. @models.permalink
  66. def get_absolute_url(self):
  67. return ("adzone_ad_view", [self.id])
  68. def save(self, *args, **kwargs):
  69. self.updated = datetime.now()
  70. super(AdBase, self).save(*args, **kwargs)
  71. def impressions(self, start=None, end=None):
  72. if start is not None:
  73. start_q = models.Q(impression_date__gte=start)
  74. else:
  75. start_q = models.Q()
  76. if end is not None:
  77. end_q = models.Q(impression_date__lte=end)
  78. else:
  79. end_q = models.Q()
  80. return self.adimpression_set.filter(start_q & end_q).count()
  81. def clicks(self, start=None, end=None):
  82. if start is not None:
  83. start_q = models.Q(click_date__gte=start)
  84. else:
  85. start_q = models.Q()
  86. if end is not None:
  87. end_q = models.Q(click_date__lte=end)
  88. else:
  89. end_q = models.Q()
  90. return self.adclick_set.filter(start_q & end_q).count()
  91. class AdImpression(models.Model):
  92. """
  93. The AdImpression Model will record every time the ad is loaded on a page
  94. """
  95. impression_date = models.DateTimeField(default=datetime.now)
  96. source_ip = models.IPAddressField(null=True, blank=True)
  97. ad = models.ForeignKey(AdBase)
  98. class Meta:
  99. verbose_name = "Ad Impression"
  100. verbose_name_plural = "Ad Impressions"
  101. class AdClick(models.Model):
  102. """
  103. The AdClick model will record every click that a add gets
  104. """
  105. click_date = models.DateTimeField(default=datetime.now)
  106. source_ip = models.IPAddressField(null=True, blank=True)
  107. ad = models.ForeignKey(AdBase)
  108. class Meta:
  109. verbose_name = "Ad Click"
  110. verbose_name_plural = "Ad Clicks"
  111. # Example Ad Types
  112. class TextAd(AdBase):
  113. """ A most basic, text based advert """
  114. content = models.TextField()
  115. class BannerAd(AdBase):
  116. """ A standard banner Ad """
  117. content = models.ImageField(upload_to="adzone/bannerads/")