allow super users to change projects and contents V01.63
authorymh <ymh.work@gmail.com>
Wed, 25 Jan 2017 22:03:35 +0100
changeset 1485 76df1cc988e6
parent 1484 5a8702a8adf0
child 1486 7c112170ed2a
allow super users to change projects and contents
src/ldt/ldt/__init__.py
src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/projectslist.html
src/ldt/ldt/ldt_utils/views/workspace.py
src/ldt/ldt/security/permissionchecker.py
--- a/src/ldt/ldt/__init__.py	Wed Jan 25 14:10:34 2017 +0100
+++ b/src/ldt/ldt/__init__.py	Wed Jan 25 22:03:35 2017 +0100
@@ -1,6 +1,6 @@
 __all__ = ["VERSION", "get_version", "__version__", "default_app_config"]
 
-VERSION = (1, 62, 0, "final", 0)
+VERSION = (1, 63, 0, "final", 0)
 
 
 def get_version():
--- a/src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/projectslist.html	Wed Jan 25 14:10:34 2017 +0100
+++ b/src/ldt/ldt/ldt_utils/templates/ldt/ldt_utils/partial/projectslist.html	Wed Jan 25 22:03:35 2017 +0100
@@ -21,7 +21,7 @@
         {% url 'ldt.ldt_utils.views.ldt_json.project_json_id' id=project.ldt_id as json_url_id %}
         {% if is_gecko %}
             <td class="cellimg"><div class="cellimgdiv"><a  href="{% url 'index_project_full' id=project.ldt_id %}">
-            {% if project.change or project.owner == user %}
+            {% if project.change or project.owner == user or user.is_superuser %}
               {% if project.state != 2 %}
                 <img src='{% static "ldt/img/page_edit.png" %}' alt="{% trans 'open ldt' %}" title="{% trans 'open ldt' %}"/>
               {% else %}
@@ -33,7 +33,7 @@
             </a></div></td>
         {% else %}
             <td class="cellimg"><div class="cellimgdiv"><a href="{% url 'index_project' id=project.ldt_id %}" class="ldt_link_open_ldt">
-            {% if project.change or project.owner == user %}
+            {% if project.change or project.owner == user or user.is_superuser %}
               {% if project.state != 2 %}
                 <img src='{% static "ldt/img/page_edit.png" %}' alt="{% trans 'open ldt' %}" title="{% trans 'open ldt' %}"/>
               {% else %}
@@ -55,9 +55,9 @@
         </td>
         <td class="cellimg">
         {% ifequal project.state 2 %}
-        <img src='{% static "ldt/img/icon-yes.gif" %}' {% if project.change or project.owner == user %} alt="{% trans 'Project published, click to unpublish' %}" title="{% trans 'Project published, click to unpublish' %}" class="publishedproject qtiplink" {% else %} class="qtiplink" title="{% trans "You are not allowed to change this project" %}"{% endif %} id="project_{{project.ldt_id}}" /></div>
+        <img src='{% static "ldt/img/icon-yes.gif" %}' {% if project.change or project.owner == user or user.is_superuser %} alt="{% trans 'Project published, click to unpublish' %}" title="{% trans 'Project published, click to unpublish' %}" class="publishedproject qtiplink" {% else %} class="qtiplink" title="{% trans "You are not allowed to change this project" %}"{% endif %} id="project_{{project.ldt_id}}" /></div>
         {% else %}
-        <img src='{% static "ldt/img/icon-no.gif" %}' {% if project.change or project.owner == user %} alt="{% trans 'Project not published, click to publish' %}" title="{% trans 'Project not published, click to publish' %}" class="unpublishedproject qtiplink" {% else %} class="qtiplink" title="{% trans "You are not allowed to change this project" %}"{% endif %}id="project_{{project.ldt_id}}" />
+        <img src='{% static "ldt/img/icon-no.gif" %}' {% if project.change or project.owner == user or user.is_superuser %} alt="{% trans 'Project not published, click to publish' %}" title="{% trans 'Project not published, click to publish' %}" class="unpublishedproject qtiplink" {% else %} class="qtiplink" title="{% trans "You are not allowed to change this project" %}"{% endif %}id="project_{{project.ldt_id}}" />
         {% endifequal %}
         </td>
         <td>{% thumbnail project.image "50x50" crop="center" format="PNG" as im %}<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}" />{% empty %}&nbsp;{% endthumbnail %}</td>
@@ -65,7 +65,7 @@
         {% if project.state == 2 %}
         <span class="projecttitlelink infostooltip" data-title="{{ project.title }}" data-desc="{{ project.description|linebreaksbr }}" >{% if show_username %}{{ project.owner.username }} : {% endif %}{{ project.title }}</span>
         {% else %}
-        	{% if project.change or project.owner == user %}
+        	{% if project.change or project.owner == user or user.is_superuser %}
         	<a class="projecttitlelink" href="{% url 'ldt.ldt_utils.views.project.update_project' ldt_id=project.ldt_id %}"><span class="infostooltip" data-title="{{ project.title }}" data-desc="{{ project.description|linebreaksbr }}" >{% if show_username %}{{ project.owner.username }} : {% endif %}{{ project.title }}</span></a>
         	{% else %}
         	<a class="projecttitlelink"><span class="qtiplink" title="{% trans "You are not allowed to change this project" %}">{% if show_username %}{{ project.owner.username }} : {% endif %}{{ project.title }}</span></a>
--- a/src/ldt/ldt/ldt_utils/views/workspace.py	Wed Jan 25 14:10:34 2017 +0100
+++ b/src/ldt/ldt/ldt_utils/views/workspace.py	Wed Jan 25 22:03:35 2017 +0100
@@ -35,15 +35,15 @@
 
 @login_required
 def home(request):
-    
+
     num_page = 0
     # Prepare contents pagination
     content_nb, nb_ct_pages, content_list = get_contents_page(num_page, request.user)
     # get list of projects owned by the current user
     project_nb, nb_pj_pages, project_list = get_projects_page(num_page, request.user)
 
-    is_gecko = ((request.META['HTTP_USER_AGENT'].lower().find("firefox")) > -1);
-    
+    is_gecko = ((request.META['HTTP_USER_AGENT'].lower().find("firefox")) > -1)
+
     # render list
     return render_to_response("ldt/ldt_utils/workspace.html",
                               {'contents': content_list, 'nb_ct_pages': nb_ct_pages, 'content_nb': content_nb, 'current_content_page':float(num_page),
@@ -52,15 +52,15 @@
                                },
                               context_instance=RequestContext(request))
 
-    
+
 
 @login_required
-def groups(request): 
+def groups(request):
 
     # get list of all published projects
     group_list = request.user.groups.exclude(name=settings.PUBLIC_GROUP_NAME)
-    
-    group_list = sorted(group_list.all(), key=lambda group: group.name.lower())    
+
+    group_list = sorted(group_list.all(), key=lambda group: group.name.lower())
     group_list = add_change_attr(request.user, group_list)
 
     can_add_group = request.user.has_perm('auth.add_group')
@@ -74,7 +74,7 @@
 
 
 @login_required
-def published_project(request): 
+def published_project(request):
 
     # get list of all published projects
     #project_list = Project.safe_objects.filter(state=2).exclude(title__startswith='front') #@UndefinedVariable
@@ -84,10 +84,10 @@
     form = SearchForm()
 
     is_gecko = ((request.META['HTTP_USER_AGENT'].lower().find("firefox")) > -1);
-    
+
     # render list
     return render_to_response("ldt/ldt_utils/published_projects.html",
-                              {'projects': project_list, 'nb_pj_pages': nb_pj_pages, 'project_nb': project_nb, 'current_project_page':float(num_page), 
+                              {'projects': project_list, 'nb_pj_pages': nb_pj_pages, 'project_nb': project_nb, 'current_project_page':float(num_page),
                                'form': form, 'is_gecko': is_gecko},
                               context_instance=RequestContext(request))
 
@@ -96,23 +96,23 @@
     use_groups = boolean_convert(use_groups)
     if not filter or len(filter) == 0:
         raise AttributeError("filter should be a string")
-    
+
     filter = filter[1:]    # @ReservedAssignment
     resp = get_userlist(request.user, filter=filter)
-            
+
     if use_groups:
         groups = Group.objects.filter(name__icontains=filter).exclude(name=settings.PUBLIC_GROUP_NAME)[0:20]
 
         for g in groups:
             resp.append({'name': g.name, 'id': g.id, 'type': 'group'})
-    
+
     resp = sorted(resp, key=lambda elem: elem['name'].lower())
-    
+
     return render_to_response("ldt/ldt_utils/partial/sharewith.html", {'elem_list' : resp}, context_instance=RequestContext(request))
 
 
 
-def search_form(request): 
+def search_form(request):
     form = SearchForm()
     return render_to_response('ldt/ldt_utils/search_form.html', {'form': form} , context_instance=RequestContext(request))
 
@@ -137,12 +137,12 @@
         if ldt_pres:
             url = absurl(request, "ldt.ldt_utils.views.lignesdetemps.search_init", kwargs={'field':field, 'query':queryStr})
             return render_to_response('ldt/ldt_utils/init_ldt_full.html', {'colorurl': colorurl, 'i18nurl': i18nurl , 'language': language_code, 'baseurl': baseurl, 'url': url}, context_instance=RequestContext(request))
-        else:                                        
-            results, nb, nb_segment = get_search_results(request, search, field, page)            
-            
+        else:
+            results, nb, nb_segment = get_search_results(request, search, field, page)
+
     return render_to_response('ldt/ldt_utils/search_results.html', {'results': results, 'nb_results' : nb, 'nb_segment' : nb_segment, 'search' : search, 'field': field, 'colorurl': colorurl, 'i18nurl': i18nurl , 'language': language_code, 'baseurl': baseurl}, context_instance=RequestContext(request))
-        
- 
+
+
 def get_search_results(request, search, field, page, content_list=None):
     # We parse the author if necessary. For author search to work in every case, we have to transform the query author:cheese shop into author:"CHEESE SHOP"
     if u'author:' in search.lower() :
@@ -158,10 +158,10 @@
     all_projects = Project.objects.filter(ldt_id__in=[e['project_id'] for e in results], state=2)
     all_contents = Content.objects.filter(iri_id__in=[e['iri_id'] for e in results]).select_related('stat_annotation')
     viewable_projects_id = [p.ldt_id for p in all_projects]
-    nb_segment=0       
-    complete_results = []           
+    nb_segment=0
+    complete_results = []
     results.sort(key=lambda k: k['iri_id'])
-           
+
     for iri_id, item in groupby(results, itemgetter('iri_id')):
         content=None
         content_filter = filter(lambda e: e.iri_id == iri_id, all_contents)
@@ -172,19 +172,19 @@
         if content is None:
             continue
         all_related_segments = list(item)
-                
+
         valid_segments = []
         for s in all_related_segments:
             array_of_segs = [seg for seg in all_segments if (seg.element_id == s['element_id'] and seg.project_id == s['project_id'] and seg.iri_id == s['iri_id'] and seg.cutting_id == s['cutting_id'] and seg.ensemble_id == s['ensemble_id']) ]
             if len(array_of_segs)>0:
                 segment = array_of_segs[0]
-                    
+
                 segment.score = s['score']
                 segment.indexation_id = s['indexation_id']
                 segment.context = s['context']
                 segment.context_tags = s['tags']
                 segment.highlighted = s['highlighted']
-                                        
+
                 if not s['project_id']:
                     segment.project_id = '_'
                     valid_segments.append(segment)
@@ -192,36 +192,36 @@
                 elif s['project_id'] in viewable_projects_id:
                     valid_segments.append(segment)
                     nb_segment+=1
-            
+
         # If all segments found belong to unpublished projects or projects
         # the current user is not allowed to see
         if not valid_segments:
             continue
-            
+
         score = sum([seg.score for seg in valid_segments])
         complete_results.append({
                                  'list': valid_segments,
                                  'score': score,
-                                 'content': content,                                         
-                                 })                
-      
+                                 'content': content,
+                                 })
+
     complete_results.sort(key=lambda k: k['score'])
-    
+
     paginator = Paginator (complete_results, settings.LDT_RESULTS_PER_PAGE)
-    
+
     try:
         results = paginator.page(page)
     except (EmptyPage, InvalidPage):
         results = paginator.page(paginator.num_pages)
-            
+
     results.object_list = highlight_documents(results.object_list, search, field)
-    
+
     return results, len(complete_results), nb_segment
-   
+
 
-def loading(request): 
+def loading(request):
     return render_to_response('ldt/ldt_utils/loading.html', context_instance=RequestContext(request))
-   
+
 
 @transaction.atomic
 def delete_segment(request, project_id, content_id, ensemble_id, cutting_id, segment_id):
@@ -241,8 +241,8 @@
                 el.getparent().remove(el)
                 project.ldt = lxml.etree.tostring(ldtdoc)
                 project.save(must_reindex=False)
-                contentindexer.delete_segment(project, project_id, content_id, ensemble_id, cutting_id, segment_id)                
-                
+                contentindexer.delete_segment(project, project_id, content_id, ensemble_id, cutting_id, segment_id)
+
                 return HttpResponseRedirect(request.META.get('HTTP_REFERER', '/'))
             else:
                 return HttpResponseNotFound(_("Annotation not found"))
--- a/src/ldt/ldt/security/permissionchecker.py	Wed Jan 25 14:10:34 2017 +0100
+++ b/src/ldt/ldt/security/permissionchecker.py	Wed Jan 25 22:03:35 2017 +0100
@@ -5,7 +5,7 @@
 
 def check_object_perm_for_user(obj, perm_name, user):
     # If user is admin...
-    if user.is_staff:
+    if user.is_staff or user.is_superuser:
         return True
     # Guardian has_perm request is REALLY long and not optimized.
     # So we check manually the change_project permission for the user and the user's groups
@@ -33,4 +33,3 @@
             can_change = False
     # End
     return can_change
-    
\ No newline at end of file