|
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 md5 |
|
14 import re |
|
15 import uuid |
|
16 |
|
17 import logging |
|
18 logger = logging.getLogger(__name__) |
|
19 |
|
20 |
|
21 def fix_cache_key(key): |
|
22 cache_key = re.sub(r'\s+', '-', key) |
|
23 cache_key = smart_str(cache_key) |
|
24 if len(cache_key) > (250-(2+len(cache.key_prefix)+len(str(cache.version)))-33): |
|
25 cache_key = cache_key[:(250-(2+len(cache.key_prefix)+len(str(cache.version)))-33)] + '-' + md5.new(cache_key).hexdigest() |
|
26 return cache_key |
|
27 |
|
28 |
|
29 |
|
30 class NodePlacer(): |
|
31 |
|
32 cat_nb_nodes = {} |
|
33 |
|
34 def init(self, cat_nb_nodes_initial): |
|
35 raise NotImplementedError( "Should have implemented get_place" ) |
|
36 |
|
37 def get_place(self, category): |
|
38 if not category or category not in self.cat_nb_nodes: |
|
39 raise Http404 |
|
40 return self.cat_nb_nodes[category].pop(0) |
|
41 |
|
42 |
|
43 |
|
44 class LineNodePlacer(NodePlacer): |
|
45 |
|
46 max_length = 0 |
|
47 |
|
48 def init(self, cat_nb_nodes_initial): |
|
49 for c in cat_nb_nodes_initial: |
|
50 nb = cat_nb_nodes_initial[c] |
|
51 if isinstance(cat_nb_nodes_initial[c], tuple): |
|
52 _, nb = nb |
|
53 self.max_length = nb if nb > self.max_length else self.max_length |
|
54 for i_cat,c in enumerate(cat_nb_nodes_initial): |
|
55 self.cat_nb_nodes[c] = [] |
|
56 order = i_cat |
|
57 nb = cat_nb_nodes_initial[c] |
|
58 if isinstance(cat_nb_nodes_initial[c], tuple): |
|
59 order, nb = nb |
|
60 offset = float(self.max_length - nb) / 2 |
|
61 for i in xrange(nb): |
|
62 self.cat_nb_nodes[c].append({ "x": order*300, "y": 100*(i+offset) }) |
|
63 #logger.debug(self.cat_nb_nodes) |
|
64 |
|
65 |
|
66 |
|
67 class CircleNodePlacer(NodePlacer): |
|
68 #TODO: Does not work. Meant to be a real circle placer |
|
69 def init(self, cat_nb_nodes_initial): |
|
70 for c in cat_nb_nodes_initial: |
|
71 self.max_length = cat_nb_nodes_initial[c] if cat_nb_nodes_initial[c] > self.max_length else self.max_length |
|
72 for i_cat,c in enumerate(cat_nb_nodes_initial): |
|
73 self.cat_nb_nodes[c] = [] |
|
74 offset = float(self.max_length - cat_nb_nodes_initial[c]) / 2 |
|
75 for i in xrange(cat_nb_nodes_initial[c]): |
|
76 self.cat_nb_nodes[c].append({ "x": i_cat*400, "y": 200*(i+offset) }) |
|
77 #logger.debug(self.cat_nb_nodes) |
|
78 |
|
79 |
|
80 |
|
81 def renkan_copier(user, old_rk_id): |
|
82 old_rk = get_object_or_404(Renkan, rk_id=old_rk_id) |
|
83 rk = Renkan() |
|
84 rk.rk_id = unicode(uuid.uuid1()) |
|
85 rk.owner = user |
|
86 rk.content = old_rk.content |
|
87 #TODO : update title and uuid in the content's json data |
|
88 rk.title = old_rk.title + " (copy)" |
|
89 rk.save() |
|
90 return rk |
|
91 |
|
92 |
|
93 |
|
94 def renkan_deleter(user, rk_id): |
|
95 rk = get_object_or_404(Renkan, rk_id=rk_id) |
|
96 if rk.owner==user: |
|
97 rk.delete() |
|
98 else: |
|
99 raise ValidationError("You are not allowed to remove this renkan") |
|
100 |
|
101 |
|
102 |