|
1 ''' |
|
2 Created on 8 aout 2012 |
|
3 |
|
4 @author: gerard |
|
5 ''' |
|
6 import logging |
|
7 import simplejson |
|
8 |
|
9 from dataparser.DocumentaryFilesGetAttributes import DocumentaryFilesGetAttributes |
|
10 from document.models import AnnotationDocumentaryFile |
|
11 from document.models import Documentaryfile |
|
12 from document.models import Image |
|
13 from django.contrib.auth.models import User |
|
14 |
|
15 logger = logging.getLogger('document') |
|
16 |
|
17 class DocumentaryFiles(object): |
|
18 |
|
19 def __init__(self, request): |
|
20 self.request = request |
|
21 |
|
22 def get_files(self,user,offset,count): |
|
23 print 'get_files' |
|
24 print user |
|
25 '''attr = DocumentaryFilesGetAttributes(self.request)''' |
|
26 |
|
27 '''if not attr.get_user():''' |
|
28 if not user: |
|
29 json = '{"error msg": "user is not defined"}' |
|
30 return json |
|
31 |
|
32 '''if attr.get_offset() == '':''' |
|
33 if not offset: |
|
34 json = '{"error msg": "no offset defined"}' |
|
35 return json |
|
36 |
|
37 '''if attr.get_count() == '':''' |
|
38 if not count: |
|
39 json = '{"error msg": "no count defined"}' |
|
40 return json |
|
41 |
|
42 json = {} |
|
43 '''json['offset'] = int(attr.get_offset()) |
|
44 json['count'] = int(attr.get_count())''' |
|
45 json['offset'] = int(offset) |
|
46 json['count'] = int(count) |
|
47 total_count = 0 |
|
48 json['documentary_files'] = [] |
|
49 |
|
50 the_user = User.objects.filter(username=user) |
|
51 |
|
52 if the_user: |
|
53 documentaryFiles = Documentaryfile.objects.filter(user_id=the_user[0].id) |
|
54 for docfile in documentaryFiles: |
|
55 total_count += 1 |
|
56 if total_count - 1 >= int(offset) and total_count - 1 < int(offset) + int(count): |
|
57 jsonfile = {'id':docfile.id} |
|
58 jsonfile['date'] = docfile.date.isoformat() |
|
59 jsonfile['description'] = str(docfile.description) |
|
60 jsonfile['title'] = str(docfile.title) |
|
61 image = Image.objects.get(id=docfile.image_id) |
|
62 jsonfile['url_image'] = str(image.url) |
|
63 |
|
64 jsonfile['annotations'] = [] |
|
65 annotations = AnnotationDocumentaryFile.objects.filter(documentaryFile_id=docfile.id) |
|
66 for annotation in annotations: |
|
67 jsonannotation = {'id':annotation.id} |
|
68 jsonannotation['user'] = annotation.user_id |
|
69 jsonannotation['text'] = annotation.description |
|
70 jsonfile['annotations'].append(jsonannotation) |
|
71 json['documentary_files'].append(jsonfile) |
|
72 json['total_count'] = total_count |
|
73 result = simplejson.dumps(json) |
|
74 logger.debug(result) |
|
75 return result |