Permalink
Browse files

Cleanup.

  • Loading branch information...
1 parent eb5cbfd commit 12327589973ea1492a4b54922c90a0b479188e20 @willhardy committed Aug 2, 2011
Showing with 32 additions and 25 deletions.
  1. +10 −25 surveymaker/dynamic_models.py
  2. +22 −0 surveymaker/utils.py
@@ -16,37 +16,22 @@ def get_survey_response_model(survey, regenerate=False, notify_changes=True):
_app_label = 'responses'
_model_name = 'Response'+name
- # If this model has already been generated, we'll find it here
- previous_model = models.get_model(_app_label, _model_name)
-
- # Before returning our locally cached model, check that it is still current
- if previous_model and not regenerate:
- CACHE_KEY = utils.HASH_CACHE_TEMPLATE % (_app_label, _model_name)
- if cache.get(CACHE_KEY) != previous_model._hash:
- logging.debug("Local and shared dynamic model hashes are different: %s (local) %s (shared)" % (previous_model._hash, cache.get(CACHE_KEY)))
- regenerate = True
-
- # We can force regeneration by disregarding the previous model
- if regenerate:
- previous_model = None
- # Django keeps a cache of registered models, we need to make room for
- # our new one
- utils.remove_from_model_cache(_app_label, _model_name)
-
- # Skip regeneration
- if previous_model:
- return previous_model
-
- class Meta:
- app_label = 'responses'
- verbose_name = survey.name + ' Response'
+ # Skip regeneration if we have a valid cached model
+ cached_model = utils.get_cached_model(_app_label, _model_name, regenerate)
+ if cached_model is not None:
+ return cached_model
+ # Collect the dynamic model's class attributes
attrs = {
- 'Meta': Meta,
'__module__': __name__,
'__unicode__': lambda s: '%s response' % name
}
+ class Meta:
+ app_label = 'responses'
+ verbose_name = survey.name + ' Response'
+ attrs['Meta'] = Meta
+
# Add a field for each question
questions = survey.question_set.all()
for question in questions:
View
@@ -110,6 +110,28 @@ def _class_prepared_handler(sender, **kwargs):
class_prepared.connect(_class_prepared_handler, weak=False)
+def get_cached_model(app_label, model_name, regenerate=False, get_local_hash=lambda i: i._hash):
+
+ # If this model has already been generated, we'll find it here
+ previous_model = models.get_model(app_label, model_name)
+
+ # Before returning our locally cached model, check that it is still current
+ if previous_model is not None and not regenerate:
+ CACHE_KEY = utils.HASH_CACHE_TEMPLATE % (app_label, model_name)
+ if cache.get(CACHE_KEY) != get_local_hash(previous_model):
+ logging.debug("Local and shared dynamic model hashes are different: %s (local) %s (shared)" % (get_local_hash(previous_model), cache.get(CACHE_KEY)))
+ regenerate = True
+
+ # We can force regeneration by disregarding the previous model
+ if regenerate:
+ previous_model = None
+ # Django keeps a cache of registered models, we need to make room for
+ # our new one
+ utils.remove_from_model_cache(app_label, model_name)
+
+ return previous_model
+
+
def remove_from_model_cache(app_label, model_name):
""" Removes the given model from the model cache. """
try:

0 comments on commit 1232758

Please sign in to comment.