aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'gentoo_ads/ads')
-rw-r--r--gentoo_ads/ads/__init__.py0
-rw-r--r--gentoo_ads/ads/models.py3
-rw-r--r--gentoo_ads/ads/templates/ads.html14
-rw-r--r--gentoo_ads/ads/tests.py23
-rw-r--r--gentoo_ads/ads/views.py30
5 files changed, 70 insertions, 0 deletions
diff --git a/gentoo_ads/ads/__init__.py b/gentoo_ads/ads/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gentoo_ads/ads/__init__.py
diff --git a/gentoo_ads/ads/models.py b/gentoo_ads/ads/models.py
new file mode 100644
index 0000000..71a8362
--- /dev/null
+++ b/gentoo_ads/ads/models.py
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
diff --git a/gentoo_ads/ads/templates/ads.html b/gentoo_ads/ads/templates/ads.html
new file mode 100644
index 0000000..55a80d2
--- /dev/null
+++ b/gentoo_ads/ads/templates/ads.html
@@ -0,0 +1,14 @@
+<html>
+ <head>
+ <title></title>
+ <meta content="">
+ <style></style>
+ </head>
+ <body>
+ {% for ad in ads %}
+ <a href="{{ad.url}}"><img src="{{ MEDIA_URL }}{{ ad.name }}.png" alt="{{ ad.title }}" title="{{ ad.title }}" height="{{ ad.height }}" width="{{ ad.width }}" /></a><br />
+ {% endfor %}
+
+
+ </body>
+</html> \ No newline at end of file
diff --git a/gentoo_ads/ads/tests.py b/gentoo_ads/ads/tests.py
new file mode 100644
index 0000000..2247054
--- /dev/null
+++ b/gentoo_ads/ads/tests.py
@@ -0,0 +1,23 @@
+"""
+This file demonstrates two different styles of tests (one doctest and one
+unittest). These will both pass when you run "manage.py test".
+
+Replace these with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+class SimpleTest(TestCase):
+ def test_basic_addition(self):
+ """
+ Tests that 1 + 1 always equals 2.
+ """
+ self.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+
+>>> 1 + 1 == 2
+True
+"""}
+
diff --git a/gentoo_ads/ads/views.py b/gentoo_ads/ads/views.py
new file mode 100644
index 0000000..c98968d
--- /dev/null
+++ b/gentoo_ads/ads/views.py
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+# Create your views here.
+
+from django.shortcuts import render_to_response
+from django.conf import settings
+from random import randint
+
+def serve_ads(request):
+ sample = _weighted_random_sample(settings.ADS_STRUCT)
+ return render_to_response('ads.html', {'ads': sample, 'MEDIA_URL': settings.MEDIA_URL,})
+
+def _weighted_random_sample(ads):
+ ads_out = []
+
+ for i in xrange(settings.ADS_LENGTH):
+ indices = [a['weight'] for a in ads]
+ s = sum(indices)
+ r = randint(0, s-1)
+ cur_total = 0
+
+ for ad_i in xrange(len(ads)):
+ ad = ads[ad_i]
+ cur_total += ad['weight']
+
+ if r < cur_total:
+ ads_out.append(ad)
+ ads = ads[:ad_i] + ads[ad_i + 1:]
+ break
+
+ return ads_out \ No newline at end of file