# HG changeset patch # User hamidouk # Date 1326359987 -3600 # Node ID 7e234ffc2a23094f626a3e6b276748e302db3397 # Parent 5a89838f79d4ab47deac97f9c92c5c28df3de0d2# Parent 6fa2e1f8ddaad3534eee40e8faa11129a8fef398 Merge with the latest changes from jverrier diff -r 5a89838f79d4 -r 7e234ffc2a23 crea/inte-crea/groupe.html --- a/crea/inte-crea/groupe.html Thu Jan 12 10:18:12 2012 +0100 +++ b/crea/inte-crea/groupe.html Thu Jan 12 10:19:47 2012 +0100 @@ -113,6 +113,30 @@

Lorem Ipsum

par Ars Industrialis | 1h30

+
  • + +
    120
    +

    Lorem Ipsum

    +

    par Ars Industrialis | 1h30

    +
  • +
  • + +
    120
    +

    Lorem Ipsum

    +

    par Ars Industrialis | 1h30

    +
  • +
  • + +
    120
    +

    Lorem Ipsum

    +

    par Ars Industrialis | 1h30

    +
  • +
  • + +
    120
    +

    Lorem Ipsum

    +

    par Ars Industrialis | 1h30

    +
  • 1 diff -r 5a89838f79d4 -r 7e234ffc2a23 crea/inte-crea/player.html --- a/crea/inte-crea/player.html Thu Jan 12 10:18:12 2012 +0100 +++ b/crea/inte-crea/player.html Thu Jan 12 10:19:47 2012 +0100 @@ -44,7 +44,7 @@

    diff -r 5a89838f79d4 -r 7e234ffc2a23 src/ldt/ldt/ldt_utils/templates/front/front_home.html --- a/src/ldt/ldt/ldt_utils/templates/front/front_home.html Thu Jan 12 10:18:12 2012 +0100 +++ b/src/ldt/ldt/ldt_utils/templates/front/front_home.html Thu Jan 12 10:19:47 2012 +0100 @@ -1,6 +1,8 @@ {% extends "front/front_base.html" %} {% load i18n %} {% load thumbnail %} +{% load str_duration %} + {% block title %}Lignes de temps : Home{% endblock %} @@ -19,9 +21,9 @@ {% for content in last_contents %}
  • {% thumbnail content.image "294x165" format="PNG" crop="center" as im %}{% endthumbnail %} -
    250
    +
    {{ content.nb_annotation }}

    {{content.title}}

    -

    {% trans 'by' %} IRI | {{content.duration_str}}

    +

    {% trans 'by' %} IRI | {{content.duration|str_duration:"h"}}

  • {% endfor %} @@ -33,9 +35,9 @@ {% for content in most_contents %}
  • {% thumbnail content.image "134x75" format="PNG" crop="center" as im %}{% endthumbnail %} -
    120
    +
    {{ content.nb_annotation }}

    {% if content.title|length > 69 %}{{content.title|slice:":69"}}...{% else %}{{content.title}}{% endif %}

    -

    {% trans 'by' %} IRI | {{content.duration_str}}

    +

    {% trans 'by' %} IRI | {{content.duration|str_duration:"h"}}

  • {% endfor %} @@ -50,7 +52,7 @@ {% thumbnail group.profile.image "54x40" format="PNG" crop="center" as im %}{% endthumbnail %}
    -
    57
    +
    {{ group.user_set.count }}
    32

    {{group.name}}

    {% if group.profile.description|length > 69 %}{{group.profile.description|safe|slice:":69"}}...{% else %}{{group.profile.description|safe}}{% endif %}

    diff -r 5a89838f79d4 -r 7e234ffc2a23 src/ldt/ldt/ldt_utils/templates/front/front_search_results.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ldt/ldt/ldt_utils/templates/front/front_search_results.html Thu Jan 12 10:19:47 2012 +0100 @@ -0,0 +1,114 @@ +{% extends "front/front_base.html" %} +{% load i18n %} +{% load thumbnail %} +{% load str_duration %} + +{% block title %}Lignes de temps : {% trans 'search' %} ""{% endblock %} + +{% block css_import %} +{{block.super}} + +{% endblock %} + +{% block body %} +{{block.super}} + +{% if msg %} +{{ msg }} +{% else %} + {% ifequal nb_results 0 %} + {% blocktrans %} No results for {{ search }}.{% endblocktrans %} + {% else %} + + + + + + +
    +
      + +
    • +
        +
      • +

        {% trans "Search results for " %} {{ search }}

        +
      • +
      • +

        {{ nb_results }} {% trans "Result" %}{{ nb_results|pluralize }}

        +
      • + +
      +
    • + + + + {% autoescape off %} + {% for res in results.object_list %} +
    • +
      +
      +

      {{res.content_title|capfirst}}

      +
      +
      +

      {{res.content.duration}}

      +
      +
      + graph de volume +
      +
      + + + +
    • + {% endfor %} + {% endautoescape %} + + +
    • +

      + {% if results.has_previous %} + {% trans "previous" %} + {% endif %} + + {% if results.paginator.num_pages > 1 %} + + {% blocktrans with number=results.number num_pages=results.paginator.num_pages%}Page {{number}} of {{num_pages}}{% endblocktrans %} + + {% endif %} + + {% if results.has_next %} + {% trans "next" %} + {% endif %} +

      +
    • + +
    + + + + +
    +

    ICI PREVIEW ?

    +

    Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem …

    +
    + + {% endifequal %} +{% endif %} + +{% endblock %} + diff -r 5a89838f79d4 -r 7e234ffc2a23 src/ldt/ldt/ldt_utils/urls.py --- a/src/ldt/ldt/ldt_utils/urls.py Thu Jan 12 10:18:12 2012 +0100 +++ b/src/ldt/ldt/ldt_utils/urls.py Thu Jan 12 10:19:47 2012 +0100 @@ -43,6 +43,8 @@ url(r'^front/$', 'views.front.front_home'), url(r'^front/group/(?P.*)$', 'views.front.group_info'), url(r'^front/player/(?P.*)$', 'views.front.annot_content'), + url(r'^front/search/$', 'views.front.search_index'), + url(r'^front/searchListing', 'views.front.search_listing'), ) urlpatterns += patterns('', diff -r 5a89838f79d4 -r 7e234ffc2a23 src/ldt/ldt/ldt_utils/views/front.py --- a/src/ldt/ldt/ldt_utils/views/front.py Thu Jan 12 10:18:12 2012 +0100 +++ b/src/ldt/ldt/ldt_utils/views/front.py Thu Jan 12 10:19:47 2012 +0100 @@ -7,15 +7,16 @@ from django.template import RequestContext from guardian.shortcuts import get_objects_for_group from ldt.ldt_utils.models import Content, Project +from ldt.ldt_utils.views.workspace import search_index as ws_search_index, search_listing as ws_search_listing from ldt.security.utils import add_change_attr @login_required def front_home(request): # Get the 3 last annotated contents - last_contents = Content.objects.all()[:3] + last_contents = Content.objects.order_by('-content_creation_date')[:3] # Get the most annotated contents - most_contents = Content.objects.all()[4:12] + most_contents = Content.objects.order_by('-nb_annotation')[:8] # Get the active groups active_groups = Group.objects.exclude(name=settings.PUBLIC_GROUP_NAME)[:5] @@ -72,4 +73,14 @@ context_instance=RequestContext(request)) +@login_required +def search_index(request): + return ws_search_index(request, front_template=True) + + +@login_required +def search_listing(request): + return ws_search_listing(request, front_template=True) + + diff -r 5a89838f79d4 -r 7e234ffc2a23 src/ldt/ldt/ldt_utils/views/workspace.py --- a/src/ldt/ldt/ldt_utils/views/workspace.py Thu Jan 12 10:18:12 2012 +0100 +++ b/src/ldt/ldt/ldt_utils/views/workspace.py Thu Jan 12 10:19:47 2012 +0100 @@ -152,7 +152,7 @@ @login_required -def search_index(request): +def search_index(request, front_template=False): sform = SearchForm(request.POST) if sform.is_valid(): @@ -167,7 +167,8 @@ else: results = get_results_with_context(field, search) complete_results = [] - proj_list = Project.safe_objects.all() + # the search is made only on the published project + proj_list = Project.safe_objects.filter(state=2) results.sort(key=lambda k: k['iri_id']) for iri_id, item in groupby(results, itemgetter('iri_id')): try: @@ -192,7 +193,7 @@ score = sum([seg['score'] for seg in segments]) desc = content.description if content.description else '' - complete_results.append({'list' : segments, 'score' : score, 'content_title' : content.title, 'content_id' : content.iri_id, 'content_description' : desc }) + complete_results.append({'list' : segments, 'score' : score, 'content_title' : content.title, 'content_id' : content.iri_id, 'content_description' : desc, 'content':content }) complete_results.sort(key=lambda k: k['score']) @@ -210,15 +211,25 @@ results.object_list = highlight_documents(results.object_list, search, field) - return render_to_response('ldt/ldt_utils/search_results.html', {'results': results, 'nb_results' : paginator.count, 'search' : search, 'LDT_MEDIA_PREFIX': settings.LDT_MEDIA_PREFIX, 'colorurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/pkg/color.xml', 'i18nurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/'}, context_instance=RequestContext(request)) + + if front_template : + return render_to_response('front/front_search_results.html', {'results': results, 'nb_results' : paginator.count, 'search' : search, 'LDT_MEDIA_PREFIX': settings.LDT_MEDIA_PREFIX, 'colorurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/pkg/color.xml', 'i18nurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/'}, context_instance=RequestContext(request)) + else : + return render_to_response('ldt/ldt_utils/search_results.html', {'results': results, 'nb_results' : paginator.count, 'search' : search, 'LDT_MEDIA_PREFIX': settings.LDT_MEDIA_PREFIX, 'colorurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/pkg/color.xml', 'i18nurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/'}, context_instance=RequestContext(request)) else: - return HttpResponseRedirect(reverse('ldt.ldt_utils.views.workspace.published_project')) + if front_template : + return HttpResponseRedirect(reverse('ldt.ldt_utils.views.front.front_home')) + else : + return HttpResponseRedirect(reverse('ldt.ldt_utils.views.workspace.published_project')) -def search_listing(request): +def search_listing(request, front_template=False): if not cache.get('complete_results_%s' % request.user.username): - return HttpResponseRedirect(reverse('ldt.ldt_utils.views.workspace.published_project')) + if front_template : + return HttpResponseRedirect(reverse('ldt.ldt_utils.views.front.front_home')) + else : + return HttpResponseRedirect(reverse('ldt.ldt_utils.views.workspace.published_project')) complete_results = cache.get('complete_results_%s' % request.user.username) search = cache.get('search_%s' % request.user.username) @@ -238,7 +249,10 @@ results.object_list = highlight_documents(results.object_list, search, field) - return render_to_response('ldt/ldt_utils/search_results.html', {'results': results, 'nb_results' : paginator.count, 'search' : search, 'LDT_MEDIA_PREFIX': settings.LDT_MEDIA_PREFIX, 'colorurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/pkg/color.xml', 'i18nurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/'}, context_instance=RequestContext(request)) + if front_template : + return render_to_response('front/front_search_results.html', {'results': results, 'nb_results' : paginator.count, 'search' : search, 'LDT_MEDIA_PREFIX': settings.LDT_MEDIA_PREFIX, 'colorurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/pkg/color.xml', 'i18nurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/'}, context_instance=RequestContext(request)) + else : + return render_to_response('ldt/ldt_utils/search_results.html', {'results': results, 'nb_results' : paginator.count, 'search' : search, 'LDT_MEDIA_PREFIX': settings.LDT_MEDIA_PREFIX, 'colorurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/pkg/color.xml', 'i18nurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/pkg/i18n', 'language': language_code, 'baseurl': settings.LDT_MEDIA_PREFIX + 'swf/ldt/'}, context_instance=RequestContext(request)) diff -r 5a89838f79d4 -r 7e234ffc2a23 src/ldt/ldt/security/__init__.py --- a/src/ldt/ldt/security/__init__.py Thu Jan 12 10:18:12 2012 +0100 +++ b/src/ldt/ldt/security/__init__.py Thu Jan 12 10:19:47 2012 +0100 @@ -0,0 +1,99 @@ +from django.conf import settings +from django.contrib.contenttypes.models import ContentType +from django.contrib.auth.models import User +from django.core.signals import request_started + +try: + from threading import local +except ImportError: + from django.utils._threading_local import local + +_thread_locals = local() + +# The function that protect models is called on the first +# HTTP request sent to the server (see function protect_models_request +# in this file), and can not be called in this file directly +# because of circular import. +# +# To protect models from command line, use set_current_user(my_user) +# and protect_models(). + +_models_are_protected = False + +def get_current_user(): + return getattr(_thread_locals, 'user', None) + +def set_current_user(user): + _thread_locals.user = user + +def del_current_user(): + del _thread_locals.user + +def get_anonymous_user(): + if hasattr(get_anonymous_user, 'anonymous_user'): + return get_anonymous_user.anonymous_user + + get_anonymous_user.anonymous_user = User.objects.get(id=settings.ANONYMOUS_USER_ID) + return get_anonymous_user.anonymous_user + +def protect_models(): + cls_list = get_models_to_protect() + if cls_list: + for cls in get_models_to_protect(): + protect_model(cls) + + _models_are_protected = True + +def unprotect_models(): + for cls in get_models_to_protect(): + unprotect_model(cls) + + _models_are_protected = False + +def get_models_to_protect(): + if hasattr(get_models_to_protect, 'cls_list'): + return get_models_to_protect.cls_list + + cls_list = [] + for cls_name in settings.USE_GROUP_PERMISSIONS: + cls_type = ContentType.objects.get(model=cls_name.lower()) + cls_list.append(cls_type.model_class()) + get_models_to_protect.cls_list = cls_list + + return cls_list + +def protect_model(cls): + + cls.old_save = cls.save + cls.old_delete = cls.delete + class_name = cls.__name__.lower() + cls.save = change_security(class_name)(cls.save) + cls.delete = change_security(class_name)(cls.delete) + +def unprotect_model(cls): + if hasattr(cls, 'old_save'): + cls.save = cls.old_save + cls.delete = cls.old_delete + del cls.old_save + del cls.old_delete + cls.safe_objects.user = None + cls.safe_objects.check_perm = False + +def change_security(cls_name): + def wrapper(func): + def wrapped(self, *args, **kwargs): + + if self.pk and not get_current_user().has_perm('change_%s' % cls_name, self): + raise AttributeError('User %s is not allowed to change object %s' % (get_current_user(), self)) + + return func(self, *args, **kwargs) + return wrapped + return wrapper + + +def protect_models_request(sender, **kwargs): + if not _models_are_protected: + protect_models() + +request_started.connect(protect_models_request) + diff -r 5a89838f79d4 -r 7e234ffc2a23 src/ldt/ldt/security/manager.py --- a/src/ldt/ldt/security/manager.py Thu Jan 12 10:18:12 2012 +0100 +++ b/src/ldt/ldt/security/manager.py Thu Jan 12 10:19:47 2012 +0100 @@ -1,6 +1,6 @@ from django.db.models import Manager from guardian.shortcuts import get_objects_for_user -from utils import get_current_user +from ldt.security import get_current_user, get_anonymous_user class SafeManager(Manager): use_for_related_fields = True @@ -11,17 +11,21 @@ self.check_perm = check_perm def get_query_set(self): + if not self.check_perm: return super(SafeManager, self).get_query_set() - + if not self.user: - self.user = get_current_user() - + self.user = get_current_user() + if not self.user: raise AttributeError("No user is attached to the current thread.") + if not self.user.is_authenticated(): + self.user = get_anonymous_user() + perm_name = '%s.view_%s' % (self.model._meta.app_label, self.model.__name__.lower()) user_objects = get_objects_for_user(self.user, perm_name, klass=self.model.objects) - + return user_objects \ No newline at end of file diff -r 5a89838f79d4 -r 7e234ffc2a23 src/ldt/ldt/security/middleware.py --- a/src/ldt/ldt/security/middleware.py Thu Jan 12 10:18:12 2012 +0100 +++ b/src/ldt/ldt/security/middleware.py Thu Jan 12 10:19:47 2012 +0100 @@ -1,16 +1,14 @@ -from ldt.security.utils import protect_models, unprotect_models, _thread_locals, set_current_user, del_current_user +from ldt.security import set_current_user, del_current_user, _thread_locals class SecurityMiddleware(object): def process_request(self, request): if not hasattr(_thread_locals, 'user'): set_current_user(request.user) - protect_models() def process_response(self, request, response): if hasattr(_thread_locals, 'user'): - unprotect_models() del_current_user() return response diff -r 5a89838f79d4 -r 7e234ffc2a23 src/ldt/ldt/security/models.py --- a/src/ldt/ldt/security/models.py Thu Jan 12 10:18:12 2012 +0100 +++ b/src/ldt/ldt/security/models.py Thu Jan 12 10:19:47 2012 +0100 @@ -2,8 +2,8 @@ from manager import SafeManager class SafeModel(models.Model): - objects = SafeManager() # By default, SafeManagers do not chek permissions. - safe_objects = SafeManager() # This setting is activated in the middleware + objects = SafeManager() + safe_objects = SafeManager(check_perm=True) class Meta: abstract = True \ No newline at end of file diff -r 5a89838f79d4 -r 7e234ffc2a23 src/ldt/ldt/security/utils.py --- a/src/ldt/ldt/security/utils.py Thu Jan 12 10:18:12 2012 +0100 +++ b/src/ldt/ldt/security/utils.py Thu Jan 12 10:19:47 2012 +0100 @@ -2,65 +2,9 @@ from django.contrib.contenttypes.models import ContentType from guardian.shortcuts import assign, remove_perm, get_users_with_perms, get_groups_with_perms, get_objects_for_user from cache import get_cached_userlist +from ldt.security import change_security import types - -try: - from threading import local -except ImportError: - from django.utils._threading_local import local - -_thread_locals = local() - -def get_current_user(): - return getattr(_thread_locals, 'user', None) - -def set_current_user(user): - _thread_locals.user = user - -def del_current_user(): - del _thread_locals.user - -def protect_models(): - cls_list = get_models_to_protect() - if cls_list: - user = get_current_user() - for cls in get_models_to_protect(): - protect_model(cls, user) - -def unprotect_models(): - for cls in get_models_to_protect(): - unprotect_model(cls) - -def get_models_to_protect(): - if hasattr(get_models_to_protect, 'cls_list'): - return get_models_to_protect.cls_list - - cls_list = [] - for cls_name in settings.USE_GROUP_PERMISSIONS: - cls_type = ContentType.objects.get(model=cls_name.lower()) - cls_list.append(cls_type.model_class()) - get_models_to_protect.cls_list = cls_list - - return cls_list - -def protect_model(cls, user): - cls.safe_objects.user = user - cls.safe_objects.check_perm = True - - cls.old_save = cls.save - cls.old_delete = cls.delete - class_name = cls.__name__.lower() - cls.save = change_security(class_name)(cls.save) - cls.delete = change_security(class_name)(cls.delete) - -def unprotect_model(cls): - if hasattr(cls, 'old_save'): - cls.save = cls.old_save - cls.delete = cls.old_delete - del cls.old_save - del cls.old_delete - cls.safe_objects.user = None - + def unprotect_instance(instance): if hasattr(instance, 'old_save'): instance.save = instance.old_save @@ -77,16 +21,6 @@ delete = types.MethodType(change_security('project')(cls.delete), instance, cls) instance.delete = delete -def change_security(cls_name): - def wrapper(func): - def wrapped(self, *args, **kwargs): - - if self.pk and not get_current_user().has_perm('change_%s' % cls_name, self): - raise AttributeError('User %s is not allowed to change object %s' % (get_current_user(), self)) - - return func(self, *args, **kwargs) - return wrapped - return wrapper def set_forbidden_stream(xml, user): cls = ContentType.objects.get(model='content') diff -r 5a89838f79d4 -r 7e234ffc2a23 src/ldt/ldt/static/ldt/css/front_common.css --- a/src/ldt/ldt/static/ldt/css/front_common.css Thu Jan 12 10:18:12 2012 +0100 +++ b/src/ldt/ldt/static/ldt/css/front_common.css Thu Jan 12 10:19:47 2012 +0100 @@ -201,7 +201,7 @@ position: relative; width: 378px; margin: 2px 0; height: 20px; border: 1px solid #000000; background: url(./imgs/search_icon.png) left no-repeat #ffffff; } -#search_text { +#id_search { position: absolute; top: 0; left: 25px; width: 250px; height: 20px; border: 0; padding: 0; } diff -r 5a89838f79d4 -r 7e234ffc2a23 src/ldt/ldt/templatetags/str_duration.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ldt/ldt/templatetags/str_duration.py Thu Jan 12 10:19:47 2012 +0100 @@ -0,0 +1,46 @@ +from django.template import Library, TemplateSyntaxError + +register = Library() + +@register.filter +def str_duration(value, arg=None): + """Takes an integer value of milliseconds and write a human readable duration like 1h23, 01:23 (hours:minutes), 01:23:45 (hours:minutes:seconds), or number of seconds """ + # Error management + if value is None : + return "" + if not isinstance(value, (int,long,float,str,unicode)) : + raise TemplateSyntaxError('str_duration value error : value must be integer or long or float or string. type = ' + str(type(value))) + if isinstance(value, (str,unicode)) : + try: + value = int(value) + except : + raise TemplateSyntaxError('str_duration value error : can not convert value "' + value + '" into integer') + # We take off the milliseconds + ms = abs(value) + sec = ms//1000 + if arg is None : + arg = "::" + if arg=="::" or arg=="h" or arg==":" : + hours = sec//3600 + min = (sec - (hours * 3600))//60 + if min<10: + min_str = "0" + str(min) + else: + min_str = str(min) + if (arg=="::" or arg==":") and hours<10 : + hours_str = "0" + str(hours) + else : + hours_str = str(hours) + if arg=="h" or arg==":" : + return hours_str + arg + min_str + sec = (sec - (hours * 3600) - (min*60)) + if sec<10: + sec_str = "0" + str(sec) + else: + sec_str = str(sec) + return hours_str + ":" + min_str + ":" + sec_str + elif arg=="s" : + return sec + else : + raise TemplateSyntaxError('str_duration filter error : filters argument must be None, "::", ":", "h" or "s"') +str_duration.is_safe = True