server/python/django2/renkanmanager/utils.py
changeset 618 3051b847c124
parent 589 0ae11aa255a3
equal deleted inserted replaced
617:80ce81208b08 618:3051b847c124
     8 from django.core.exceptions import ValidationError
     8 from django.core.exceptions import ValidationError
     9 from django.http.response import Http404
     9 from django.http.response import Http404
    10 from django.shortcuts import get_object_or_404
    10 from django.shortcuts import get_object_or_404
    11 from django.utils.encoding import smart_str
    11 from django.utils.encoding import smart_str
    12 from renkanmanager.models import Renkan
    12 from renkanmanager.models import Renkan
       
    13 import distutils
    13 import math
    14 import math
    14 import hashlib
    15 import hashlib
    15 import re
    16 import re
    16 import uuid
    17 import uuid
    17 
    18 
    27     return cache_key
    28     return cache_key
    28 
    29 
    29 
    30 
    30 
    31 
    31 class NodePlacer():
    32 class NodePlacer():
    32     
    33 
    33     cat_nb_nodes = {}
    34     cat_nb_nodes = {}
    34     
    35 
    35     def init(self, cat_nb_nodes_initial):
    36     def init(self, cat_nb_nodes_initial):
    36         raise NotImplementedError( "Should have implemented init" )
    37         raise NotImplementedError( "Should have implemented init" )
    37     
    38 
    38     def get_place(self, category):
    39     def get_place(self, category):
    39         if not category or category not in self.cat_nb_nodes:
    40         if not category or category not in self.cat_nb_nodes:
    40             raise Http404
    41             raise Http404
    41         return self.cat_nb_nodes[category].pop(0)
    42         return self.cat_nb_nodes[category].pop(0)
    42 
    43 
    43 
    44 
    44 
    45 
    45 class LineNodePlacer(NodePlacer): #vertical lines
    46 class LineNodePlacer(NodePlacer): #vertical lines
    46     
    47 
    47     max_length = 0
    48     max_length = 0
    48     
    49 
    49     def init(self, cat_nb_nodes_initial):
    50     def init(self, cat_nb_nodes_initial):
    50         for c in cat_nb_nodes_initial:
    51         for c in cat_nb_nodes_initial:
    51             nb = cat_nb_nodes_initial[c]
    52             nb = cat_nb_nodes_initial[c]
    52             if isinstance(cat_nb_nodes_initial[c], tuple):
    53             if isinstance(cat_nb_nodes_initial[c], tuple):
    53                 _, nb = nb
    54                 _, nb = nb
    65                 else:
    66                 else:
    66                     self.cat_nb_nodes[c].append({ "x": order*500, "y": 100*(i+offset) })
    67                     self.cat_nb_nodes[c].append({ "x": order*500, "y": 100*(i+offset) })
    67 
    68 
    68 
    69 
    69 class HorLineNodePlacer(NodePlacer): #horizontal lines
    70 class HorLineNodePlacer(NodePlacer): #horizontal lines
    70     
    71 
    71     max_length = 0
    72     max_length = 0
    72     
    73 
    73     def init(self, cat_nb_nodes_initial):
    74     def init(self, cat_nb_nodes_initial):
    74         for c in cat_nb_nodes_initial:
    75         for c in cat_nb_nodes_initial:
    75             nb = cat_nb_nodes_initial[c]
    76             nb = cat_nb_nodes_initial[c]
    76             if isinstance(cat_nb_nodes_initial[c], tuple):
    77             if isinstance(cat_nb_nodes_initial[c], tuple):
    77                 _, nb = nb
    78                 _, nb = nb
    91                         self.cat_nb_nodes[c].append({ "x": 100*(i+offset), "y": order*500 })
    92                         self.cat_nb_nodes[c].append({ "x": 100*(i+offset), "y": order*500 })
    92                     else:
    93                     else:
    93                         self.cat_nb_nodes[c].append({ "x": 100*(i+offset), "y": order*450 })
    94                         self.cat_nb_nodes[c].append({ "x": 100*(i+offset), "y": order*450 })
    94 
    95 
    95 class CircleNodePlacer(NodePlacer):
    96 class CircleNodePlacer(NodePlacer):
    96     
    97 
    97     def init(self, cat_nb_nodes_initial):
    98     def init(self, cat_nb_nodes_initial):
    98         for i_cat,c in enumerate(cat_nb_nodes_initial):
    99         for i_cat,c in enumerate(cat_nb_nodes_initial):
    99             self.cat_nb_nodes[c] = []
   100             self.cat_nb_nodes[c] = []
   100             order = i_cat
   101             order = i_cat
   101             nb = cat_nb_nodes_initial[c]
   102             nb = cat_nb_nodes_initial[c]
   131         rk.delete()
   132         rk.delete()
   132     else:
   133     else:
   133         raise ValidationError("You are not allowed to remove this renkan")
   134         raise ValidationError("You are not allowed to remove this renkan")
   134 
   135 
   135 
   136 
   136 
   137 def parse_bool(value):
       
   138     try:
       
   139         return bool(value and distutils.util.strtobool(value.lower()))
       
   140     except ValueError:
       
   141         return False