alcatel/controller/ClusterAnnotation.py
changeset 27 8ca7f2cea729
equal deleted inserted replaced
26:94f586daa623 27:8ca7f2cea729
       
     1 '''
       
     2 Created on 07 Aout 2012
       
     3 
       
     4 @author: Corinne
       
     5 '''
       
     6 
       
     7 import logging
       
     8 
       
     9 from dataparser.ClientClusterAnnotationDeleteAttributes import ClientClusterAnnotationDeleteAttributes
       
    10 from dataparser.ClientClusterAnnotationCreateAttributes import ClientDocumentAnnotationCreateAttributes
       
    11 from document.models import Cluster
       
    12 from django.contrib.auth.models import User
       
    13 from document.models import Tag
       
    14 from document.models import Documentaryfile
       
    15 from document.models import Annotationcluster
       
    16 
       
    17 logger = logging.getLogger('document')
       
    18 
       
    19 class ClusterAnnotation(object):
       
    20 
       
    21     def __init__(self, request):
       
    22         logger.info('ClusterAnnotation init')
       
    23         self.request = request
       
    24            
       
    25     def create(self):
       
    26         logger.info('create ClusterAnnotation')
       
    27         attr = ClientDocumentAnnotationCreateAttributes(self.request)
       
    28         logger.info('get_user = ' + str(attr.get_user()))
       
    29         logger.info('get_documentary_file = ' + str(attr.get_documentary_file()))
       
    30         logger.info('get_annotation = ' + str(attr.get_annotation()))
       
    31         logger.info('get_cluster = ' + str(attr.get_cluster()))
       
    32         logger.info('get_tags = ' + str(attr.get_tags()))
       
    33         tags_attr = attr.get_tags()
       
    34         try:
       
    35             cluster = Cluster.objects.get(pk=attr.get_cluster())
       
    36             try:
       
    37                 user_attr = User.objects.get(username=attr.get_user)
       
    38             except User.DoesNotExist:
       
    39                 json= '{"error": "User does not existed"}'
       
    40                 logger.info(json)
       
    41                 return json
       
    42             
       
    43             try:
       
    44                 annotationCluster = Annotationcluster.objects.create(description=attr.get_annotation(),user=user_attr, cluster=cluster)
       
    45             except Exception:
       
    46                 json = '{"error": "cluster_id already exists"}'
       
    47                 logger.info(json)
       
    48                 return json     
       
    49                  
       
    50             if tags_attr: 
       
    51                 logger.info('tags associated to annotation')
       
    52                 for tag in tags_attr:
       
    53                     logger.info('annotation_tag = '+str(tag))
       
    54                     for tag in tags_attr:
       
    55                         logger.info('annotation_tag = '+str(tag))
       
    56                         tag_attr = Tag(value=tag,annotationcluster=annotationCluster)
       
    57                         tag_attr.save()
       
    58                               
       
    59             json = '{"annotation_cluster_created":' + str(annotationCluster.id) + '}'
       
    60             logger.info(json)
       
    61         except Cluster.DoesNotExist:
       
    62             json = '{"error": "Invalid cluster id"}'
       
    63             logger.info(json)
       
    64             return json
       
    65         
       
    66         return json
       
    67 
       
    68     def delete(self):
       
    69         logger.info('delete ClusterAnnotation')
       
    70         attr = ClientClusterAnnotationDeleteAttributes(self.request)
       
    71         logger.info('get_user = ' + str(attr.get_user()))
       
    72         logger.info('get_documentary_file_id = ' + str(attr.get_documentary_file()))
       
    73         logger.info('get_cluster = ' + str(attr.get_cluster()))
       
    74         clusterId = attr.get_cluster()
       
    75         
       
    76         if clusterId=='':
       
    77             json = '{"error": "No cluster_id attribute in the http post request"}'
       
    78             logger.info(json)
       
    79             return json
       
    80         try:
       
    81             cluster = Cluster.objects.get(pk=clusterId)
       
    82             try:
       
    83                 annotationcluster = cluster.annotationcluster
       
    84             except cluster.annotationcluster.DoesNotExist:
       
    85                 json= '{"error": "No annotation on this cluster"}'
       
    86                 logger.info(json)
       
    87                 return json
       
    88                 
       
    89         except Cluster.DoesNotExist:
       
    90             json = '{"error": "Invalid cluster id"}'
       
    91             logger.info(json)
       
    92             return json
       
    93         
       
    94         try:
       
    95             documentaryfile = Documentaryfile.objects.get(pk=attr.get_documentary_file())
       
    96         except Documentaryfile.DoesNotExist:
       
    97             json = '{"error": "Invalid documentary_file_id"}'
       
    98             logger.info(json)
       
    99             return json
       
   100         
       
   101         logger.info('documentaryfile.user.name = ' + str(documentaryfile.user.username))
       
   102         if documentaryfile.user.username == attr.get_user():
       
   103             annotationcluster.delete()
       
   104             json = '{"annotation_cluster_deleted" :'  + str(clusterId) + '}'
       
   105             logger.info(json)
       
   106         else:
       
   107             json = '{"error": "User does not matched"}'
       
   108             logger.info(json)
       
   109        
       
   110         return json