Permalink
Browse files

simplify the usage of clustered models

  • Loading branch information...
1 parent 7fa8f1a commit 412de7fd1f0bbe77dc54322692014ecac4e10bf0 @Fantomas42 committed Mar 23, 2011
Showing with 22 additions and 25 deletions.
  1. +15 −17 zinnia/comparison.py
  2. +2 −3 zinnia/templatetags/zinnia_tags.py
  3. +5 −5 zinnia/tests/comparison.py
View
@@ -27,9 +27,9 @@ class ClusteredModel(object):
"""Wrapper around Model class
building a dataset of instances"""
- def __init__(self, info_dict):
- self.queryset = info_dict.get('queryset', [])
- self.fields = info_dict.get('fields', ['id'])
+ def __init__(self, queryset, fields=['id']):
+ self.fields = fields
+ self.queryset = queryset
def dataset(self):
"""Generate a dataset with the queryset
@@ -44,28 +44,27 @@ def dataset(self):
class VectorBuilder(object):
"""Build a list of vectors based on datasets"""
- def __init__(self, *models_conf):
+ def __init__(self, queryset, fields):
self.key = ''
self.columns = []
self.dataset = {}
- self.clustered_models = [ClusteredModel(conf) for conf in models_conf]
+ self.clustered_model = ClusteredModel(queryset, fields)
self.build_dataset()
def build_dataset(self):
"""Generate whole dataset"""
data = {}
words_total = {}
- for clustered_model in self.clustered_models:
- model_data = clustered_model.dataset()
- for instance, words in model_data.items():
- words_item_total = {}
- for word in words.split():
- words_total.setdefault(word, 0)
- words_item_total.setdefault(word, 0)
- words_total[word] += 1
- words_item_total[word] += 1
- data[instance] = words_item_total
+ model_data = self.clustered_model.dataset()
+ for instance, words in model_data.items():
+ words_item_total = {}
+ for word in words.split():
+ words_total.setdefault(word, 0)
+ words_item_total.setdefault(word, 0)
+ words_total[word] += 1
+ words_item_total[word] += 1
+ data[instance] = words_item_total
top_words = []
for word, count in words_total.items():
@@ -82,8 +81,7 @@ def build_dataset(self):
def generate_key(self):
"""Generate key for this list of vectors"""
- return '-'.join([str(c.queryset.filter().count())
- for c in self.clustered_models])
+ return self.clustered_model.queryset.count()
def flush(self):
"""Flush the dataset"""
@@ -24,9 +24,8 @@
register = Library()
VECTORS = None
-VECTORS_FACTORY = lambda: VectorBuilder({'queryset': Entry.published.all(),
- 'fields': ['title', 'excerpt',
- 'content']})
+VECTORS_FACTORY = lambda: VectorBuilder(Entry.published.all(),
+ ['title', 'excerpt', 'content'])
CACHE_ENTRIES_RELATED = {}
View
@@ -25,16 +25,16 @@ def test_clustered_model(self):
params = {'title': 'My entry 2', 'content': 'My content 2',
'tags': 'zinnia, test', 'slug': 'my-entry-2'}
Entry.objects.create(**params)
- cm = ClusteredModel({'queryset': Entry.objects.all()})
+ cm = ClusteredModel(Entry.objects.all())
self.assertEquals(cm.dataset().values(), ['1', '2'])
- cm = ClusteredModel({'queryset': Entry.objects.all(),
- 'fields': ['title', 'excerpt', 'content']})
+ cm = ClusteredModel(Entry.objects.all(),
+ ['title', 'excerpt', 'content'])
self.assertEquals(cm.dataset().values(), ['My entry 1 My content 1',
'My entry 2 My content 2'])
def test_vector_builder(self):
- vectors = VectorBuilder({'queryset': Entry.objects.all(),
- 'fields': ['title', 'excerpt', 'content']})
+ vectors = VectorBuilder(Entry.objects.all(),
+ ['title', 'excerpt', 'content'])
params = {'title': 'My entry 1', 'content':
'This is my first content',
'tags': 'zinnia, test', 'slug': 'my-entry-1'}

0 comments on commit 412de7f

Please sign in to comment.