|
1 # -*- coding: utf-8 -*- |
|
2 ''' |
|
3 Created on Mar 13, 2012 |
|
4 |
|
5 @author: ymh and tc |
|
6 ''' |
|
7 from django.core.cache import cache |
|
8 from django.core.exceptions import ValidationError |
|
9 from django.http.response import Http404 |
|
10 from django.shortcuts import get_object_or_404 |
|
11 from django.utils.encoding import smart_str |
|
12 from renkanmanager.models import Renkan |
|
13 import math |
|
14 import hashlib |
|
15 import re |
|
16 import uuid |
|
17 |
|
18 import logging |
|
19 logger = logging.getLogger(__name__) |
|
20 |
|
21 |
|
22 def fix_cache_key(key): |
|
23 cache_key = re.sub(r'\s+', '-', key) |
|
24 cache_key = smart_str(cache_key) |
|
25 if len(cache_key) > (250-(2+len(cache.key_prefix)+len(str(cache.version)))-33): |
|
26 cache_key = cache_key[:(250-(2+len(cache.key_prefix)+len(str(cache.version)))-33)] + '-' + hashlib.md5(cache_key).hexdigest() |
|
27 return cache_key |
|
28 |
|
29 |
|
30 |
|
31 class NodePlacer(): |
|
32 |
|
33 cat_nb_nodes = {} |
|
34 |
|
35 def init(self, cat_nb_nodes_initial): |
|
36 raise NotImplementedError( "Should have implemented init" ) |
|
37 |
|
38 def get_place(self, category): |
|
39 if not category or category not in self.cat_nb_nodes: |
|
40 raise Http404 |
|
41 return self.cat_nb_nodes[category].pop(0) |
|
42 |
|
43 |
|
44 |
|
45 class LineNodePlacer(NodePlacer): #vertical lines |
|
46 |
|
47 max_length = 0 |
|
48 |
|
49 def init(self, cat_nb_nodes_initial): |
|
50 for c in cat_nb_nodes_initial: |
|
51 nb = cat_nb_nodes_initial[c] |
|
52 if isinstance(cat_nb_nodes_initial[c], tuple): |
|
53 _, nb = nb |
|
54 self.max_length = nb if nb > self.max_length else self.max_length |
|
55 for i_cat,c in enumerate(cat_nb_nodes_initial): |
|
56 self.cat_nb_nodes[c] = [] |
|
57 order = i_cat |
|
58 nb = cat_nb_nodes_initial[c] |
|
59 if isinstance(cat_nb_nodes_initial[c], tuple): |
|
60 order, nb = nb |
|
61 offset = float(self.max_length - nb) / 2 |
|
62 for i in xrange(nb): |
|
63 if c=="northwest": |
|
64 self.cat_nb_nodes[c].append({ "x": order*500, "y": offset }) |
|
65 else: |
|
66 self.cat_nb_nodes[c].append({ "x": order*500, "y": 100*(i+offset) }) |
|
67 |
|
68 |
|
69 class HorLineNodePlacer(NodePlacer): #horizontal lines |
|
70 |
|
71 max_length = 0 |
|
72 |
|
73 def init(self, cat_nb_nodes_initial): |
|
74 for c in cat_nb_nodes_initial: |
|
75 nb = cat_nb_nodes_initial[c] |
|
76 if isinstance(cat_nb_nodes_initial[c], tuple): |
|
77 _, nb = nb |
|
78 self.max_length = nb if nb > self.max_length else self.max_length |
|
79 for i_cat,c in enumerate(cat_nb_nodes_initial): |
|
80 self.cat_nb_nodes[c] = [] |
|
81 order = i_cat |
|
82 nb = cat_nb_nodes_initial[c] |
|
83 if isinstance(cat_nb_nodes_initial[c], tuple): |
|
84 order, nb = nb |
|
85 offset = float(self.max_length - nb) / 2 |
|
86 for i in xrange(nb): |
|
87 if c=="northwest": |
|
88 self.cat_nb_nodes[c].append({ "x": offset, "y": order*500 }) |
|
89 else: |
|
90 if (i%2) == 0: |
|
91 self.cat_nb_nodes[c].append({ "x": 100*(i+offset), "y": order*500 }) |
|
92 else: |
|
93 self.cat_nb_nodes[c].append({ "x": 100*(i+offset), "y": order*450 }) |
|
94 |
|
95 class CircleNodePlacer(NodePlacer): |
|
96 |
|
97 def init(self, cat_nb_nodes_initial): |
|
98 for i_cat,c in enumerate(cat_nb_nodes_initial): |
|
99 self.cat_nb_nodes[c] = [] |
|
100 order = i_cat |
|
101 nb = cat_nb_nodes_initial[c] |
|
102 if isinstance(cat_nb_nodes_initial[c], tuple): |
|
103 order, nb = nb |
|
104 order -= 1 |
|
105 for i in xrange(nb): |
|
106 radius = (order*500) if order>0 else 100 |
|
107 angle = 2 * math.pi * i / nb |
|
108 if c=="northwest": |
|
109 self.cat_nb_nodes[c].append({ "x": -radius, "y": -radius }) |
|
110 else: |
|
111 self.cat_nb_nodes[c].append({ "x": radius*math.cos(angle), "y": radius*math.sin(angle) }) |
|
112 |
|
113 |
|
114 |
|
115 def renkan_copier(user, old_rk_id): |
|
116 old_rk = get_object_or_404(Renkan, rk_id=old_rk_id) |
|
117 rk = Renkan() |
|
118 rk.rk_id = unicode(uuid.uuid1()) |
|
119 rk.owner = user |
|
120 rk.content = old_rk.content |
|
121 #TODO : update title and uuid in the content's json data |
|
122 rk.title = old_rk.title + " (copy)" |
|
123 rk.save() |
|
124 return rk |
|
125 |
|
126 |
|
127 |
|
128 def renkan_deleter(user, rk_id): |
|
129 rk = get_object_or_404(Renkan, rk_id=rk_id) |
|
130 if rk.owner==user: |
|
131 rk.delete() |
|
132 else: |
|
133 raise ValidationError("You are not allowed to remove this renkan") |
|
134 |
|
135 |
|
136 |