| author | ymh <ymh.work@gmail.com> |
| Tue, 22 Oct 2024 09:57:18 +0200 | |
| changeset 1516 | 9cfcfbac1a43 |
| parent 1364 | fb0046d64c82 |
| permissions | -rw-r--r-- |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1027
diff
changeset
|
1 |
from StringIO import StringIO |
| 1193 | 2 |
from django.conf.urls import patterns, url |
| 0 | 3 |
from django.contrib import admin |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1027
diff
changeset
|
4 |
from django.core.management import call_command |
|
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1027
diff
changeset
|
5 |
from django.http import HttpResponse |
| 0 | 6 |
from django.shortcuts import render_to_response |
7 |
from django.template import RequestContext |
|
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1027
diff
changeset
|
8 |
from guardian.admin import GuardedModelAdmin |
| 77 | 9 |
from ldt.ldt_utils.contentindexer import ContentIndexer, ProjectIndexer |
| 63 | 10 |
from ldt.ldt_utils.fileimport import FileImport, FileImportError |
|
340
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
249
diff
changeset
|
11 |
from ldt.ldt_utils.forms import LdtImportForm, ReindexForm, StatAnnotationForm |
|
467
a1e9f5e91791
Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents:
393
diff
changeset
|
12 |
from ldt.ldt_utils.models import Content, Project, Media, Author |
| 534 | 13 |
from ldt.ldt_utils.stat import update_stat_content |
| 0 | 14 |
|
|
393
fa07a599883c
GroupProfile and AnnotationStat are editable in admin pages
verrierj
parents:
343
diff
changeset
|
15 |
|
|
230
39d97d561c60
Add admin panel to manage permissions for Content and Project
verrierj
parents:
167
diff
changeset
|
16 |
class ProjectAdmin(GuardedModelAdmin): |
|
1342
514145eb8c01
add search fields in admin + increment version
ymh <ymh.work@gmail.com>
parents:
1193
diff
changeset
|
17 |
search_fields = ['title', 'ldt_id'] |
|
467
a1e9f5e91791
Removed object AnnotationStat + stats are computed when projects are published/unpublished
verrierj
parents:
393
diff
changeset
|
18 |
pass |
| 0 | 19 |
|
|
249
5c5fab6d8ae8
Add admin permission panel to Group, Media, Author
verrierj
parents:
230
diff
changeset
|
20 |
class AuthorAdmin(GuardedModelAdmin): |
|
5c5fab6d8ae8
Add admin permission panel to Group, Media, Author
verrierj
parents:
230
diff
changeset
|
21 |
pass |
|
5c5fab6d8ae8
Add admin permission panel to Group, Media, Author
verrierj
parents:
230
diff
changeset
|
22 |
|
|
5c5fab6d8ae8
Add admin permission panel to Group, Media, Author
verrierj
parents:
230
diff
changeset
|
23 |
class MediaAdmin(GuardedModelAdmin): |
|
1342
514145eb8c01
add search fields in admin + increment version
ymh <ymh.work@gmail.com>
parents:
1193
diff
changeset
|
24 |
search_fields = ['title',] |
| 731 | 25 |
readonly_fields = ['src_hash'] |
|
230
39d97d561c60
Add admin panel to manage permissions for Content and Project
verrierj
parents:
167
diff
changeset
|
26 |
|
|
39d97d561c60
Add admin panel to manage permissions for Content and Project
verrierj
parents:
167
diff
changeset
|
27 |
class ContentAdmin(GuardedModelAdmin): |
|
1342
514145eb8c01
add search fields in admin + increment version
ymh <ymh.work@gmail.com>
parents:
1193
diff
changeset
|
28 |
search_fields = ['title', 'iri_id'] |
| 0 | 29 |
def import_file(self, request): |
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
30 |
if request.method == 'POST': |
| 0 | 31 |
form = LdtImportForm(request.POST, request.FILES) |
32 |
if form.is_valid(): |
|
|
13
97ab7b3191cf
add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
33 |
filetoprocess = form.cleaned_data['importFile'] |
| 0 | 34 |
flatten = form.cleaned_data['flatten'] |
35 |
videoPath = form.cleaned_data['videoPath'] |
|
36 |
# fi = None |
|
37 |
fi = FileImport(filetoprocess, videoPath, flatten) |
|
38 |
try: |
|
|
167
fe00e7302efe
Change class and functions names to follow PEP8 formatting standards
verrierj
parents:
142
diff
changeset
|
39 |
fi.process_file() |
| 0 | 40 |
args = {'message': "File imported"} |
41 |
except FileImportError: |
|
42 |
non_field_errors = form.non_field_errors() |
|
43 |
non_field_errors.append("Error when importing : unknown file type") |
|
44 |
form._errors["__all__"] = non_field_errors |
|
45 |
args = {'message': "Can not import file, unknown file type", 'form': form} |
|
46 |
||
47 |
else: |
|
48 |
non_field_errors = form.non_field_errors() |
|
49 |
non_field_errors.append("Error when importing : invalid form") |
|
50 |
form._errors["__all__"] = non_field_errors |
|
51 |
args = {'message': "Error when importing : invalid form", 'form': form} |
|
52 |
else: |
|
53 |
form = LdtImportForm() |
|
54 |
args = {'form': form, 'current_app': self.admin_site.name, 'current_action' : 'import_file'} |
|
55 |
return render_to_response('admin/ldt_utils/content/upload_form.html', args, context_instance=RequestContext(request)) |
|
56 |
||
57 |
def reindex(self, request): |
|
58 |
message = None |
|
59 |
if request.method == "POST": |
|
60 |
form = ReindexForm(request.POST) |
|
61 |
if form.is_valid(): |
|
|
718
5e27a39d3742
replace lucene by haystack, remove references to lucene
ymh <ymh.work@gmail.com>
parents:
534
diff
changeset
|
62 |
contentList = form.cleaned_data["contents"] |
|
5e27a39d3742
replace lucene by haystack, remove references to lucene
ymh <ymh.work@gmail.com>
parents:
534
diff
changeset
|
63 |
indexer = ContentIndexer(contentList) |
|
5e27a39d3742
replace lucene by haystack, remove references to lucene
ymh <ymh.work@gmail.com>
parents:
534
diff
changeset
|
64 |
indexer.index_all() |
|
5e27a39d3742
replace lucene by haystack, remove references to lucene
ymh <ymh.work@gmail.com>
parents:
534
diff
changeset
|
65 |
|
|
5e27a39d3742
replace lucene by haystack, remove references to lucene
ymh <ymh.work@gmail.com>
parents:
534
diff
changeset
|
66 |
index_projects = form.cleaned_data["index_projects"] |
|
5e27a39d3742
replace lucene by haystack, remove references to lucene
ymh <ymh.work@gmail.com>
parents:
534
diff
changeset
|
67 |
if index_projects: |
|
5e27a39d3742
replace lucene by haystack, remove references to lucene
ymh <ymh.work@gmail.com>
parents:
534
diff
changeset
|
68 |
projectList = Project.objects.filter(contents__in=contentList, state=2).distinct() #filter(contents__in=contentList) @UndefinedVariable |
|
5e27a39d3742
replace lucene by haystack, remove references to lucene
ymh <ymh.work@gmail.com>
parents:
534
diff
changeset
|
69 |
indexer = ProjectIndexer(projectList) |
| 77 | 70 |
indexer.index_all() |
| 0 | 71 |
message = "Indexation ok : " + repr(form.cleaned_data["contents"]) |
72 |
form = ReindexForm() |
|
73 |
# except Exception, inst: |
|
74 |
# non_field_errors = form.non_field_errors() |
|
75 |
# non_field_errors.append("Error when reindexing : " + cgi.escape(repr(inst))) |
|
76 |
# form._errors["__all__"] = non_field_errors |
|
77 |
#message = "ERROR : " + repr(non_field_errors) |
|
78 |
else: |
|
79 |
form = ReindexForm() |
|
80 |
||
81 |
return render_to_response('admin/ldt_utils/content/reindex_form.html', {'form': form, 'message':message, 'current_app': self.admin_site.name, 'current_action' : 'reindex' }, context_instance=RequestContext(request)) |
|
82 |
||
|
340
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
249
diff
changeset
|
83 |
def stats_annotations(self, request): |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
249
diff
changeset
|
84 |
|
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
249
diff
changeset
|
85 |
message = None |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
249
diff
changeset
|
86 |
if request.method == "POST": |
|
343
1b9b509013a7
Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents:
340
diff
changeset
|
87 |
form = StatAnnotationForm(request.POST) |
|
340
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
249
diff
changeset
|
88 |
|
|
343
1b9b509013a7
Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents:
340
diff
changeset
|
89 |
if form.is_valid(): |
|
340
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
249
diff
changeset
|
90 |
|
|
343
1b9b509013a7
Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents:
340
diff
changeset
|
91 |
if form.cleaned_data['choose_all']: |
| 534 | 92 |
contents = Content.objects.all() |
|
340
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
249
diff
changeset
|
93 |
else: |
| 534 | 94 |
contents = form.cleaned_data['contents'] |
|
340
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
249
diff
changeset
|
95 |
|
| 534 | 96 |
for content in contents: |
97 |
update_stat_content(content) |
|
98 |
message = "Stats computed for :" + repr(contents) |
|
|
340
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
249
diff
changeset
|
99 |
else: |
|
343
1b9b509013a7
Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents:
340
diff
changeset
|
100 |
form = StatAnnotationForm() |
|
340
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
249
diff
changeset
|
101 |
|
|
343
1b9b509013a7
Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents:
340
diff
changeset
|
102 |
return render_to_response('admin/ldt_utils/content/stats_form.html', {'form': form, 'message':message, 'current_app': self.admin_site.name, 'current_action' : 'stats' }, context_instance=RequestContext(request)) |
| 1027 | 103 |
|
104 |
||
105 |
def dumpdata(self, request): |
|
106 |
# do we dump the datas ? |
|
107 |
dump_str = request.REQUEST.get("dump") |
|
108 |
app_str = request.REQUEST.get("app", "") |
|
109 |
dump_bool = False |
|
110 |
if dump_str: |
|
111 |
dump_bool = {'true': True, 'false': False, "0": False, "1": True}.get(dump_str.lower()) |
|
112 |
if dump_bool: |
|
113 |
content = StringIO() |
|
114 |
if app_str=="": |
|
115 |
call_command("dumpdata", indent=1, stdout=content) |
|
116 |
else: |
|
117 |
call_command("dumpdata", app_str, indent=1, stdout=content) |
|
118 |
content.seek(0) |
|
119 |
out = content.getvalue() |
|
120 |
content.close() |
|
|
1364
fb0046d64c82
replaced mimetype arg in HTTPResponse objects with content_type + fixed a url error from commit changing json to ldt_json + replaced md5 lib (deprecated) with hashlib.md5 for project id generation
durandn
parents:
1342
diff
changeset
|
121 |
res = HttpResponse(out, content_type='application/json') |
| 1027 | 122 |
res["Content-Disposition"] = "attachment; filename=dumpdata_ldt.json" |
123 |
return res |
|
124 |
return render_to_response('admin/ldt_utils/content/dumpdata.html', {}, context_instance=RequestContext(request)) |
|
125 |
||
|
340
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
249
diff
changeset
|
126 |
|
| 0 | 127 |
def get_urls(self): |
128 |
urls = super(ContentAdmin, self).get_urls() |
|
129 |
content_urls = patterns('', |
|
130 |
url(r'^reindex/$', self.admin_site.admin_view(self.reindex), name="ldt_content_reindex"), |
|
|
340
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
249
diff
changeset
|
131 |
url(r'^import/$', self.admin_site.admin_view(self.import_file), name="ldt_content_import_file"), |
| 1027 | 132 |
url(r'^stats/$', self.admin_site.admin_view(self.stats_annotations), name="ldt_project_compute_stats"), |
133 |
url(r'^dumpdata/$', self.admin_site.admin_view(self.dumpdata), name="ldt_admin_dump_data"), |
|
| 0 | 134 |
) |
135 |
return content_urls + urls |
|
136 |
||
137 |
||
|
249
5c5fab6d8ae8
Add admin permission panel to Group, Media, Author
verrierj
parents:
230
diff
changeset
|
138 |
admin.site.register(Project, ProjectAdmin) |
| 0 | 139 |
admin.site.register(Content, ContentAdmin) |
|
249
5c5fab6d8ae8
Add admin permission panel to Group, Media, Author
verrierj
parents:
230
diff
changeset
|
140 |
admin.site.register(Media, MediaAdmin) |
|
5c5fab6d8ae8
Add admin permission panel to Group, Media, Author
verrierj
parents:
230
diff
changeset
|
141 |
admin.site.register(Author, AuthorAdmin) |