Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #6 from subsume/master

DATABASE_ENGINE will fail in 1.4, now using DATABASES.
  • Loading branch information...
commit 029f60aa2726471f56ae73f75560059b3ea81b92 2 parents f7a1c2f + 263d330
Mathijs de Bruin authored
Showing with 13 additions and 12 deletions.
  1. +13 −12 popularity/models.py
View
25 popularity/models.py
@@ -35,8 +35,8 @@
POPULARITY_CHARAGE = float(getattr(settings, 'POPULARITY_CHARAGE', 3600))
POPULARITY_LISTSIZE = int(getattr(settings, 'POPULARITY_LISTSIZE', 10))
-DATABASE_ENGINE = getattr(settings, 'DATABASE_ENGINE')
-COMPATIBLE_DATABASES = ('mysql')
+# Maybe they wrote their own mysql backend that *is* mysql?
+COMPATIBLE_DATABASES = getattr(settings, 'POPULARITY_COMPATABILITY_OVERRIDE',None) or ('django.db.backends.mysql', )
class ViewTrackerQuerySet(models.query.QuerySet):
_LOGSCALING = log(0.5)
@@ -44,6 +44,7 @@ class ViewTrackerQuerySet(models.query.QuerySet):
def __init__ (self, model = None, *args, **kwargs):
super(self.__class__, self).__init__ (model, *args, **kwargs)
+ self._DATABASE_ENGINE = settings.DATABASES.get(kwargs.get('using',None) or 'default')['ENGINE']
self._SQL_NOW = "'%s'"
self._SQL_AGE = 'TIMESTAMPDIFF(SECOND, added, %(now)s)'
self._SQL_RELVIEWS = '(views/%(maxviews)d)'
@@ -64,7 +65,7 @@ def __init__ (self, model = None, *args, **kwargs):
def _get_db_datetime(self, value=None):
""" Retrieve an SQL-interpretable representation of the datetime value, or
now if no value is specified. """
- assert DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
+ assert self._DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
if not value:
value = datetime.now()
@@ -86,7 +87,7 @@ def _add_extra(self, field, sql):
def select_age(self):
""" Adds age with regards to NOW to the QuerySet
fields. """
- assert DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
+ assert self._DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
_SQL_AGE = self._SQL_AGE % {'now' : self._get_db_datetime() }
@@ -98,7 +99,7 @@ def select_relviews(self, relative_to=None):
in the current QuerySet, unless specified in 'relative_to'.
The relative number of views should always in the range [0, 1]. """
- assert DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
+ assert self._DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
if not relative_to:
relative_to = self
@@ -118,7 +119,7 @@ def select_relage(self, relative_to=None):
in the current QuerySet, unless specified in 'relative_to'.
The relative age should always in the range [0, 1]. """
- assert DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
+ assert self._DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
if not relative_to:
relative_to = self
@@ -143,7 +144,7 @@ def select_novelty(self, minimum=0.0, charage=None):
is used in multiplication.
The novelty value is always in the range [0, 1]. """
- assert DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
+ assert self._DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
offset = minimum
factor = 1/(1-offset)
@@ -165,7 +166,7 @@ def select_novelty(self, minimum=0.0, charage=None):
def select_popularity(self):
""" Compute popularity, which is defined as: views/age. """
- assert DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
+ assert self._DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
_SQL_AGE = self._SQL_AGE % {'now' : self._get_db_datetime() }
@@ -178,7 +179,7 @@ def select_relpopularity(self, relative_to=None):
The relpopularity value should always be in the range [0, 1]. """
- assert DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
+ assert self._DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
if not relative_to:
relative_to = self
@@ -201,7 +202,7 @@ def select_random(self):
""" Returns the original QuerySet with an extra field 'random' containing a random
value in the range [0,1] to use for ordering.
"""
- assert DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
+ assert self._DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
SQL_RANDOM = self.RANDOM
@@ -209,7 +210,7 @@ def select_random(self):
def select_relevance(relative_to=None, minimum_novelty=0.1, charage_novelty=None):
""" This adds the multiplication of novelty and relpopularity to the QuerySet, as 'relevance'. """
- assert DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
+ assert self._DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
if not relative_to:
relative_to = self
@@ -257,7 +258,7 @@ def select_ordering(relview=0.0, relage=0.0, novelty=0.0, relpopularity=0.0, ran
Please do note that the relative age is the only value here that INCREASES over time so
you might want to specify a NEGATIVE value here and use an offset, just to compensate.
"""
- assert DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
+ assert self._DATABASE_ENGINE in COMPATIBLE_DATABASES, 'Database engine %s is not compatible with this functionality.'
if not relative_to:
relative_to = self
Please sign in to comment.
Something went wrong with that request. Please try again.