1 # -*- coding: utf-8 -*- |
1 # -*- coding: utf-8 -*- |
2 ''' |
2 ''' |
3 Created on Mar 13, 2012 |
3 Created on Mar 13, 2012 |
4 |
4 |
5 @author: ymh |
5 @author: ymh and tc |
6 ''' |
6 ''' |
7 from django.core.cache import cache |
7 from django.core.cache import cache |
|
8 from django.http.response import Http404 |
8 from django.utils.encoding import smart_str |
9 from django.utils.encoding import smart_str |
9 import md5 |
10 import md5 |
10 import re |
11 import re |
|
12 |
|
13 import logging |
|
14 logger = logging.getLogger(__name__) |
11 |
15 |
12 |
16 |
13 def fix_cache_key(key): |
17 def fix_cache_key(key): |
14 cache_key = re.sub(r'\s+', '-', key) |
18 cache_key = re.sub(r'\s+', '-', key) |
15 cache_key = smart_str(cache_key) |
19 cache_key = smart_str(cache_key) |
16 if len(cache_key) > (250-(2+len(cache.key_prefix)+len(str(cache.version)))-33): |
20 if len(cache_key) > (250-(2+len(cache.key_prefix)+len(str(cache.version)))-33): |
17 cache_key = cache_key[:(250-(2+len(cache.key_prefix)+len(str(cache.version)))-33)] + '-' + md5.new(cache_key).hexdigest() |
21 cache_key = cache_key[:(250-(2+len(cache.key_prefix)+len(str(cache.version)))-33)] + '-' + md5.new(cache_key).hexdigest() |
18 return cache_key |
22 return cache_key |
|
23 |
|
24 |
|
25 |
|
26 class NodePlacer(): |
|
27 |
|
28 cat_nb_nodes = {} |
|
29 |
|
30 def init(self, cat_nb_nodes_initial): |
|
31 raise NotImplementedError( "Should have implemented get_place" ) |
|
32 |
|
33 def get_place(self, category): |
|
34 if not category or category not in self.cat_nb_nodes: |
|
35 raise Http404 |
|
36 return self.cat_nb_nodes[category].pop(0) |
|
37 |
|
38 |
|
39 |
|
40 class LineNodePlacer(NodePlacer): |
|
41 |
|
42 max_length = 0 |
|
43 |
|
44 def init(self, cat_nb_nodes_initial): |
|
45 for c in cat_nb_nodes_initial: |
|
46 self.max_length = cat_nb_nodes_initial[c] if cat_nb_nodes_initial[c] > self.max_length else self.max_length |
|
47 for i_cat,c in enumerate(cat_nb_nodes_initial): |
|
48 self.cat_nb_nodes[c] = [] |
|
49 offset = float(self.max_length - cat_nb_nodes_initial[c]) / 2 |
|
50 for i in xrange(cat_nb_nodes_initial[c]): |
|
51 self.cat_nb_nodes[c].append({ "x": i_cat*200, "y": 100*(i+offset) }) |
|
52 #logger.debug(self.cat_nb_nodes) |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |