src/ldt/ldt/ldt_utils/admin.py
author verrierj
Mon, 16 Jan 2012 15:33:27 +0100
changeset 393 fa07a599883c
parent 343 1b9b509013a7
child 467 a1e9f5e91791
permissions -rw-r--r--
GroupProfile and AnnotationStat are editable in admin pages
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
63
93325a5d61f0 organize format and import
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
     1
from django.conf.urls.defaults import patterns, url
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
from django.contrib import admin
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
from django.shortcuts import render_to_response
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
from django.template import RequestContext
77
7923feb2e362 improve indexation
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
     5
from ldt.ldt_utils.contentindexer import ContentIndexer, ProjectIndexer
63
93325a5d61f0 organize format and import
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
     6
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
     7
from ldt.ldt_utils.forms import LdtImportForm, ReindexForm, StatAnnotationForm
393
fa07a599883c GroupProfile and AnnotationStat are editable in admin pages
verrierj
parents: 343
diff changeset
     8
from ldt.ldt_utils.models import Content, Project, Media, Author, AnnotationStat
77
7923feb2e362 improve indexation
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
     9
import ldt.indexation
230
39d97d561c60 Add admin panel to manage permissions for Content and Project
verrierj
parents: 167
diff changeset
    10
from guardian.admin import GuardedModelAdmin
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
393
fa07a599883c GroupProfile and AnnotationStat are editable in admin pages
verrierj
parents: 343
diff changeset
    12
fa07a599883c GroupProfile and AnnotationStat are editable in admin pages
verrierj
parents: 343
diff changeset
    13
class StatInlineAdmin(admin.TabularInline):
fa07a599883c GroupProfile and AnnotationStat are editable in admin pages
verrierj
parents: 343
diff changeset
    14
    model = AnnotationStat
fa07a599883c GroupProfile and AnnotationStat are editable in admin pages
verrierj
parents: 343
diff changeset
    15
    extra = 1
fa07a599883c GroupProfile and AnnotationStat are editable in admin pages
verrierj
parents: 343
diff changeset
    16
230
39d97d561c60 Add admin panel to manage permissions for Content and Project
verrierj
parents: 167
diff changeset
    17
class ProjectAdmin(GuardedModelAdmin):
393
fa07a599883c GroupProfile and AnnotationStat are editable in admin pages
verrierj
parents: 343
diff changeset
    18
    inlines = [StatInlineAdmin, ]
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    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):
5c5fab6d8ae8 Add admin permission panel to Group, Media, Author
verrierj
parents: 230
diff changeset
    24
    pass
230
39d97d561c60 Add admin panel to manage permissions for Content and Project
verrierj
parents: 167
diff changeset
    25
39d97d561c60 Add admin panel to manage permissions for Content and Project
verrierj
parents: 167
diff changeset
    26
class ContentAdmin(GuardedModelAdmin):
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    def import_file(self, request):
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    29
        if request.method == 'POST':
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
            form = LdtImportForm(request.POST, request.FILES)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
            if form.is_valid():
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    32
                filetoprocess = form.cleaned_data['importFile']
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
                flatten = form.cleaned_data['flatten']
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
                videoPath = form.cleaned_data['videoPath']
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
                # fi = None
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
                fi = FileImport(filetoprocess, videoPath, flatten)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
                try:
167
fe00e7302efe Change class and functions names to follow PEP8 formatting standards
verrierj
parents: 142
diff changeset
    38
                    fi.process_file()
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
                    args = {'message': "File imported"}
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
                except FileImportError:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
                    non_field_errors = form.non_field_errors()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
                    non_field_errors.append("Error when importing : unknown file type")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
                    form._errors["__all__"] = non_field_errors
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
                    args = {'message': "Can not import file, unknown file type", 'form': form}
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
                    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
            else:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
                non_field_errors = form.non_field_errors()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
                non_field_errors.append("Error when importing : invalid form")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
                form._errors["__all__"] = non_field_errors
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
                args = {'message': "Error when importing : invalid form", 'form': form}
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        else:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
            form = LdtImportForm()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            args = {'form': form, 'current_app': self.admin_site.name, 'current_action' : 'import_file'}
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        return render_to_response('admin/ldt_utils/content/upload_form.html', args, context_instance=RequestContext(request))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
    def reindex(self, request):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        message = None
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        if request.method == "POST":
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
            form = ReindexForm(request.POST)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
            if form.is_valid():    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
                # try:
142
0b2f32965787 small corrections
ymh <ymh.work@gmail.com>
parents: 119
diff changeset
    62
                writer = ldt.indexation.get_writer(True)
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    63
                try:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    64
                    contentList = form.cleaned_data["contents"]
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    65
                    indexer = ContentIndexer(contentList, writer)
77
7923feb2e362 improve indexation
ymh <ymh.work@gmail.com>
parents: 63
diff changeset
    66
                    indexer.index_all()
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    67
                    
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    68
                    index_projects = form.cleaned_data["index_projects"]
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    69
                    if index_projects:
119
07c64d93dce5 correct resource path and document indexation
ymh <ymh.work@gmail.com>
parents: 111
diff changeset
    70
                        projectList = Project.objects.filter(contents__in=contentList, state=2).distinct() #filter(contents__in=contentList) @UndefinedVariable
111
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    71
                        indexer = ProjectIndexer(projectList, writer)
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    72
                        indexer.index_all()
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    73
                finally:
4535dafa6007 improve releasing of resources when indexing + convert line endings to unix
ymh <ymh.work@gmail.com>
parents: 78
diff changeset
    74
                    writer.close()
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
                message = "Indexation ok : " + repr(form.cleaned_data["contents"]) 
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
                form = ReindexForm()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
                # except Exception, inst:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
                    # non_field_errors = form.non_field_errors()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
                    # non_field_errors.append("Error when reindexing : " + cgi.escape(repr(inst)))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
                    # form._errors["__all__"] = non_field_errors
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
                    #message = "ERROR : " + repr(non_field_errors)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
        else:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
            form = ReindexForm()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        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))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
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
    87
    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
    88
        
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
    89
        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
    90
        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
    91
            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
    92
            
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 340
diff changeset
    93
            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
    94
                
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 340
diff changeset
    95
                if form.cleaned_data['choose_all']:
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 340
diff changeset
    96
                    projects = 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
    97
                else:
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 340
diff changeset
    98
                    projects = form.cleaned_data['projects']
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
                
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 340
diff changeset
   100
#                for content in contents:
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 340
diff changeset
   101
#                    compute_stats_for(content)
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 340
diff changeset
   102
                message = "Stats computed for :" + repr(projects)
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
   103
        else:
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 340
diff changeset
   104
            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
   105
            
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 340
diff changeset
   106
        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))
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
   107
            
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
   108
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
    def get_urls(self):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
        urls = super(ContentAdmin, self).get_urls()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
        content_urls = patterns('',
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
            url(r'^reindex/$', self.admin_site.admin_view(self.reindex), name="ldt_content_reindex"),
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
    # (r'^admin/ldt/content/import/upload/$', 'ldt.ldt_utils.views.uploadFile'),                                
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
   114
            url(r'^import/$', self.admin_site.admin_view(self.import_file), name="ldt_content_import_file"),
343
1b9b509013a7 Stats are recomputed without parsing extra xml file when projects are saved from ligne de temps.
verrierj
parents: 340
diff changeset
   115
            url(r'^stats/$', self.admin_site.admin_view(self.stats_annotations), name="ldt_project_compute_stats")
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
   116
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
        )
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
   118
        
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
        return content_urls + urls
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
249
5c5fab6d8ae8 Add admin permission panel to Group, Media, Author
verrierj
parents: 230
diff changeset
   122
admin.site.register(Project, ProjectAdmin)
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
admin.site.register(Content, ContentAdmin)
249
5c5fab6d8ae8 Add admin permission panel to Group, Media, Author
verrierj
parents: 230
diff changeset
   124
admin.site.register(Media, MediaAdmin)
5c5fab6d8ae8 Add admin permission panel to Group, Media, Author
verrierj
parents: 230
diff changeset
   125
admin.site.register(Author, AuthorAdmin)