|
1 ''' |
|
2 Created on 09 Aout. 2012 |
|
3 |
|
4 @author: Corinne |
|
5 ''' |
|
6 import logging |
|
7 import simplejson |
|
8 |
|
9 from dataparser.ClientDocumentAnnotationDeleteAttributes import ClientDocumentAnnotationDeleteAttributes |
|
10 from dataparser.ClientDocumentAnnotationCreateAttributes import ClientDocumentAnnotationCreateAttributes |
|
11 from dataparser.AnnotationsGetAttributes import AnnotationsGetAttributes |
|
12 from document.models import Document |
|
13 from django.contrib.auth.models import User |
|
14 from document.models import Tag |
|
15 from document.models import Documentaryfile |
|
16 from document.models import Annotationdocument |
|
17 |
|
18 logger = logging.getLogger('document') |
|
19 |
|
20 class DocumentAnnotation(object): |
|
21 |
|
22 def __init__(self, request): |
|
23 logger.info('DocumentAnnotation init') |
|
24 self.request = request |
|
25 |
|
26 def create(self): |
|
27 logger.info('create DocumentAnnotation') |
|
28 attr = ClientDocumentAnnotationCreateAttributes(self.request) |
|
29 logger.info('get_user = ' + str(attr.get_user())) |
|
30 logger.info('get_documentary_file = ' + str(attr.get_documentary_file())) |
|
31 logger.info('get_annotation_id = ' + str(attr.get_annotation_id())) |
|
32 logger.info('get_document = ' + str(attr.get_document())) |
|
33 logger.info('get_tags = ' + str(attr.get_tags())) |
|
34 logger.info('get_annotated_text = ' + str(attr.get_annotated_text())) |
|
35 logger.info('get_annotated_text_page = ' + str(attr.get_annotated_text_page())) |
|
36 logger.info('get_annotated_text_offset = ' + str(attr.get_annotated_text_offset())) |
|
37 |
|
38 tags_attr = attr.get_tags() |
|
39 try: |
|
40 if attr.get_document() == '': |
|
41 json = '{"error": "No document_id attribute in the http post request"}' |
|
42 logger.info(json) |
|
43 return json |
|
44 |
|
45 document = Document.objects.get(pk=attr.get_document()) |
|
46 try: |
|
47 user_attr = User.objects.get(username=attr.get_user()) |
|
48 try: |
|
49 if attr.get_documentary_file() == '': |
|
50 json = '{"error": "No documentary_file_id attribute in the http post request" } ' |
|
51 logger.info(json) |
|
52 return json |
|
53 documentaryfile = Documentaryfile.objects.get(pk=attr.get_documentary_file()) |
|
54 except Documentaryfile.DoesNotExist: |
|
55 json = '{"error": "Invalid DocumentaryFile Id"}' |
|
56 logger.info(json) |
|
57 return json |
|
58 |
|
59 logger.info('documentaryfile.user.name = ' + str(documentaryfile.user.username)) |
|
60 logger.info('user_attr.name = ' + str(user_attr.username)) |
|
61 |
|
62 if documentaryfile.user.username == user_attr.username: |
|
63 annotationDocument = Annotationdocument(description=attr.get_annotation_id(),user=user_attr, document=document,visibility=documentaryfile.visibility,annoted_text=attr.get_annotated_text(),annoted_text_page=attr.get_annotated_text_page(),annoted_text_offset=attr.get_annotated_text_offset(),documentaryfile=documentaryfile ) |
|
64 annotationDocument.save() |
|
65 else: |
|
66 json= '{"error": "User does not matched"}' |
|
67 logger.info(json) |
|
68 except User.DoesNotExist: |
|
69 json= '{"error": "Invalid User Id"}' |
|
70 logger.info(json) |
|
71 return json |
|
72 |
|
73 if tags_attr: |
|
74 logger.info('tags associated to annotation') |
|
75 for tag in tags_attr: |
|
76 logger.info('annotation_tag = ' + str(tag)) |
|
77 tag_attr = Tag(value=tag,annotationdocument=annotationDocument) |
|
78 tag_attr.save() |
|
79 |
|
80 json= '{"annotation_document_created":' + str(annotationDocument.id) + '}' |
|
81 logger.info(json) |
|
82 except Document.DoesNotExist: |
|
83 json= '{"error": "Invalid Document Id"}' |
|
84 logger.info(json) |
|
85 return json |
|
86 |
|
87 return json |
|
88 |
|
89 def delete(self): |
|
90 logger.info('delete DocumentAnnotation') |
|
91 attr = ClientDocumentAnnotationDeleteAttributes(self.request) |
|
92 logger.info('get_user = ' + str(attr.get_user())) |
|
93 logger.info('get_annotation_id = ' + str(attr.get_annotation_id())) |
|
94 |
|
95 annotationId= attr.get_annotation_id() |
|
96 try: |
|
97 annotationdocument = Annotationdocument.objects.get(pk=annotationId) |
|
98 if attr.get_user() == annotationdocument.user.username: |
|
99 annotationdocument.delete() |
|
100 json= '{"annotation_document_deleted" :' + str(annotationId) + '}' |
|
101 logger.info(json) |
|
102 else: |
|
103 json= '{"Error": "User does not matched"}' |
|
104 logger.info(json) |
|
105 except Annotationdocument.DoesNotExist: |
|
106 json= '{"error": "Annotationdocument Id not valid"}' |
|
107 logger.info(json) |
|
108 |
|
109 return json |
|
110 |
|
111 def get_annotations(self): |
|
112 attr = AnnotationsGetAttributes(self.request) |
|
113 |
|
114 if not attr.get_article(): |
|
115 json = '{"error msg": "document_id is not defined"}' |
|
116 return json |
|
117 |
|
118 if attr.get_offset()=='' : |
|
119 json = '{"error msg": "no offset defined"}' |
|
120 return json |
|
121 |
|
122 if attr.get_count() == '' : |
|
123 json = '{"error msg": "no count defined"}' |
|
124 return json |
|
125 |
|
126 json = {} |
|
127 json['document_id'] = int(attr.get_article()) |
|
128 json['offset'] = int(attr.get_offset()) |
|
129 json['count'] = int(attr.get_count()) |
|
130 total_count = 0 |
|
131 json['annotations'] = [] |
|
132 annotations = Annotationdocument.objects.filter(document_id=attr.get_article()) |
|
133 for annotation in annotations: |
|
134 total_count += 1 |
|
135 if total_count-1 >= int(attr.get_offset()) and total_count-1 < int(attr.get_offset()) + int(attr.get_count()) : |
|
136 jsonannotation = {'id':annotation.id} |
|
137 jsonannotation['user'] = annotation.user.username |
|
138 jsonannotation['annotated_text'] = annotation.annoted_text |
|
139 jsonannotation['annotated_text_page'] = annotation.annoted_text_page |
|
140 jsonannotation['annotated_text_offset'] = annotation.annoted_text_offset |
|
141 jsonannotation['user_text'] = annotation.description |
|
142 |
|
143 jsonannotation['tags'] = [] |
|
144 tags = Tag.objects.filter(annotationdocument_id=annotation.id) |
|
145 |
|
146 for tag in tags: |
|
147 jsontag = {'id': tag.value} |
|
148 jsontag = {'title':str(tag.value)} |
|
149 #TO DO URL ? |
|
150 jsonannotation['tags'].append(jsontag) |
|
151 |
|
152 json['annotations'].append(jsonannotation) |
|
153 json['total_count'] = total_count |
|
154 result = simplejson.dumps(json) |
|
155 logger.debug(result) |
|
156 return result |