|
1 ''' |
|
2 Created on 10 Aout. 2012 |
|
3 |
|
4 @author: Gerard |
|
5 ''' |
|
6 |
|
7 import logging |
|
8 |
|
9 from dataparser.DocumentaryFileAnnotationAttributes import DocumentaryFileAnnotationCreateAttributes |
|
10 from dataparser.DocumentaryFileAnnotationDeleteAttributes import DocumentaryFileAnnotationDeleteAttributes |
|
11 from django.contrib.auth.models import User |
|
12 from document.models import Documentaryfile |
|
13 from document.models import AnnotationDocumentaryFile |
|
14 |
|
15 logger = logging.getLogger('document') |
|
16 |
|
17 class DocumentaryFileAnnotation(object): |
|
18 |
|
19 def __init__(self, request): |
|
20 self.request = request |
|
21 |
|
22 def create(self): |
|
23 attr = DocumentaryFileAnnotationCreateAttributes(self.request) |
|
24 |
|
25 if attr.get_documentary_file_id() == '': |
|
26 json = '{"error": "No documentary_file_id attribute in the http post request"}' |
|
27 logger.info(json) |
|
28 return json |
|
29 |
|
30 if attr.get_user()=='': |
|
31 json = '{"error": "No user attribute in the http post request"}' |
|
32 logger.info(json) |
|
33 return json |
|
34 |
|
35 try: |
|
36 user = User.objects.get(username=attr.get_user()) |
|
37 except User.DoesNotExist: |
|
38 json = '{"error": "Invalid User Id"}' |
|
39 logger.info(json) |
|
40 return json |
|
41 |
|
42 try: |
|
43 docFile = Documentaryfile.objects.get(id=attr.get_documentary_file_id(), user_id=user.id) |
|
44 except Documentaryfile.DoesNotExist: |
|
45 json = '{"error": "Invalid DocumentaryFile Id"}' |
|
46 logger.info(json) |
|
47 return json |
|
48 |
|
49 try: |
|
50 annotation = AnnotationDocumentaryFile.objects.get(documentaryFile=docFile) |
|
51 annotation.description = attr.get_annotation() |
|
52 annotation.save(force_update=True) |
|
53 except AnnotationDocumentaryFile.DoesNotExist: |
|
54 annotation = AnnotationDocumentaryFile(description=attr.get_annotation(), user=user, documentaryFile=docFile) |
|
55 annotation.save() |
|
56 |
|
57 json = '{"annotation_documentary_file_created":' + str(annotation.id) + '}' |
|
58 logger.info(json) |
|
59 return json |
|
60 |
|
61 def delete(self): |
|
62 attr = DocumentaryFileAnnotationDeleteAttributes(self.request) |
|
63 |
|
64 if attr.get_documentary_file_id() == '': |
|
65 json = '{"error": "No documentary_file_id attribute in the http post request"}' |
|
66 logger.info(json) |
|
67 return json |
|
68 |
|
69 if attr.get_user() == '': |
|
70 json = '{"error": "No user attribute in the http post request"}' |
|
71 logger.info(json) |
|
72 return json |
|
73 |
|
74 if attr.get_annotation_id() == '': |
|
75 json = '{"error": "No annotation_id attribute in the http post request"}' |
|
76 logger.info(json) |
|
77 return json |
|
78 |
|
79 try: |
|
80 user = User.objects.get(username=attr.get_user()) |
|
81 except User.DoesNotExist: |
|
82 json = '{"error": "Invalid User Id"}' |
|
83 logger.info(json) |
|
84 return json |
|
85 |
|
86 try: |
|
87 docFile = Documentaryfile.objects.get(id=attr.get_documentary_file_id(), user_id=user.id) |
|
88 except Documentaryfile.DoesNotExist: |
|
89 json = '{"error": "Invalid documentary file id"}' |
|
90 logger.info(json) |
|
91 return json |
|
92 |
|
93 anotationDocumentaryFileId= attr.get_annotation_id() |
|
94 try: |
|
95 anotationDocumentaryFile = AnnotationDocumentaryFile.objects.get(pk=anotationDocumentaryFileId) |
|
96 if attr.get_user() == anotationDocumentaryFile.user.username: |
|
97 print 'get_documentary_file_id' |
|
98 print str(attr.get_documentary_file_id()) |
|
99 print 'anotationDocumentaryFile.documentaryFile.id' |
|
100 print str(anotationDocumentaryFile.documentaryFile.id) |
|
101 if str(attr.get_documentary_file_id()) == str(anotationDocumentaryFile.documentaryFile.id): |
|
102 anotationDocumentaryFile.delete() |
|
103 json= '{"annotation_documentary_file_deleted" :' + str(anotationDocumentaryFileId) + '}' |
|
104 logger.info(json) |
|
105 else: |
|
106 json= '{"Error": "DocumentaryFile does not matched"}' |
|
107 logger.info(json) |
|
108 return json |
|
109 else: |
|
110 json= '{"Error": "User does not matched"}' |
|
111 logger.info(json) |
|
112 return json |
|
113 except AnnotationDocumentaryFile.DoesNotExist: |
|
114 json= '{"error": "AnnotationDocumentaryFile Id not valid"}' |
|
115 logger.info(json) |
|
116 return json |
|
117 |
|
118 return json |