|
154
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
''' |
|
|
3 |
Created on Mar 13, 2012 |
|
|
4 |
|
|
290
|
5 |
@author: ymh and tc |
|
154
|
6 |
''' |
|
|
7 |
from django.core.cache import cache |
|
290
|
8 |
from django.http.response import Http404 |
|
154
|
9 |
from django.utils.encoding import smart_str |
|
|
10 |
import md5 |
|
|
11 |
import re |
|
|
12 |
|
|
290
|
13 |
import logging |
|
|
14 |
logger = logging.getLogger(__name__) |
|
|
15 |
|
|
154
|
16 |
|
|
|
17 |
def fix_cache_key(key): |
|
|
18 |
cache_key = re.sub(r'\s+', '-', key) |
|
|
19 |
cache_key = smart_str(cache_key) |
|
|
20 |
if len(cache_key) > (250-(2+len(cache.key_prefix)+len(str(cache.version)))-33): |
|
|
21 |
cache_key = cache_key[:(250-(2+len(cache.key_prefix)+len(str(cache.version)))-33)] + '-' + md5.new(cache_key).hexdigest() |
|
290
|
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 |
|