Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
authorverrierj
Wed, 04 Jan 2012 17:28:54 +0100
changeset 330 806188af5027
parent 329 6a0b65d60d00
child 331 9978257745a5
Projects have an icon identical to one of the media they contain, if this icon is different from the default icon. Icons can be set using ./manage.py setprojecticon once icons have been added to contents.
src/ldt/ldt/ldt_utils/models.py
src/ldt/ldt/ldt_utils/views/lignesdetemps.py
src/ldt/ldt/ldt_utils/views/project.py
src/ldt/ldt/management/commands/setprojecticon.py
src/ldt/ldt/settings.py
src/ldt/ldt/user/models.py
web/ldtplatform/config.py.tmpl
--- a/src/ldt/ldt/ldt_utils/models.py	Wed Jan 04 12:17:47 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/models.py	Wed Jan 04 17:28:54 2012 +0100
@@ -10,7 +10,7 @@
 from ldt.security.manager import SafeManager
 from sorl.thumbnail import ImageField
 from utils import (create_ldt, copy_ldt, create_empty_iri, update_iri, 
-    generate_uuid, clean_description)
+    generate_uuid)
 import lucene
 import lxml.etree
 import mimetypes
@@ -18,6 +18,7 @@
 import tagging.fields
 import uuid
 
+
 class Author(SafeModel):
 
     handle = models.CharField(max_length=512, unique=True, blank=True, null=True)
@@ -127,7 +128,7 @@
     tags = tagging.fields.TagField(max_length=2048, null=True, blank=True)
     media_obj = models.ForeignKey('Media', blank=True, null=True)
     
-    image = ImageField(upload_to=settings.MEDIA_ROOT+"thumbnails/contents/", default="thumbnails/contents/content_default_icon.png")
+    image = ImageField(upload_to=settings.MEDIA_ROOT+"thumbnails/contents/", default=settings.DEFAULT_CONTENT_ICON)
     
     class Meta:
         ordering = ["title"]
@@ -354,7 +355,7 @@
     state = models.IntegerField(choices=STATE_CHOICES, default=1)
     description = models.TextField(null=True, blank=True)
     
-    image = ImageField(upload_to=settings.MEDIA_ROOT+"thumbnails/projects/", default="thumbnails/projects/project_default_icon.png")
+    image = ImageField(upload_to=settings.MEDIA_ROOT+"thumbnails/projects/", default=settings.DEFAULT_PROJECT_ICON)
     
     class Meta:
         ordering = ["title"]
@@ -410,12 +411,17 @@
         project.ldt_id = str(uuid.uuid1()) #@UndefinedVariable
         project.created_by = user.username
         project.changed_by = user.username
-        project.state = 1
+        project.state = 1                        
         project.save()
         assign('view_project', user, project)
-        assign('change_project', user, project)
+        assign('change_project', user, project)           
+                        
         for content in contents:
             project.contents.add(content)
+            
+        project.set_icon()
+        project.save()
+                        
         return create_ldt(project, user)
 
     def copy_project(self, user, title, description='', group=None):
@@ -430,14 +436,31 @@
         return project
     
     def publish(self):
+        if not self.pk:
+            self.save()
         self.state = 2
         everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
         assign('ldt_utils.view_project', everyone, self)
+        self.save()
         
     def unpublish(self):
+        if not self.pk():
+            self.save()
         self.state = 1
         everyone = Group.objects.get(name=settings.PUBLIC_GROUP_NAME)
         remove_perm('ldt_utils.view_project', everyone, self)
+        self.save()
+        
+    def set_icon(self):
+        default_image = os.path.basename(settings.DEFAULT_CONTENT_ICON)
+
+        for content in self.contents.all():
+            if os.path.basename(content.image.file.name) != default_image:
+                self.image = content.image
+                return True
+            
+        self.image = settings.DEFAULT_PROJECT_ICON
+        return False
     
     def check_access(self, user):
         if (user and user.is_staff) or self.state == 2: 
@@ -445,45 +468,6 @@
         else:
             return False        
         
-    def save(self):
-        if self.pk and self.ldt:
-            doc = lxml.etree.fromstring(self.ldt)
-            new_contents = []
-            contents = doc.xpath("/iri/medias/media")
-            for elem in contents:
-                id = elem.get("id")
-                new_contents.append(id)
-    
-            for c in self.contents.all():
-                if not c.iri_id in new_contents:
-                    self.contents.remove(c)
-            
-            contents_id = [c.id for c in self.contents.all()]
-            for c in new_contents:
-                if c not in contents_id:
-                    content = Content.objects.get(iri_id=c)
-                    self.contents.add(content)
-                                
-            description = self.get_description(doc)
-            new_desc = clean_description(description)
-            
-            if new_desc:        
-                desc_node = doc.xpath('/iri/project')[0]
-                desc_node.set('abstract', new_desc)
-                self.ldt = lxml.etree.tostring(doc, pretty_print=True)
-            
-            self.description = new_desc if new_desc else description
-
-        super(Project, self).save() 
-        
-        if self.state == 2:
-            self.publish()
-        elif self.state == 1:
-            self.unpublish()
-            
-        super(Project, self).save()
-               
-        
 
 class Segment(SafeModel):
     
--- a/src/ldt/ldt/ldt_utils/views/lignesdetemps.py	Wed Jan 04 12:17:47 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/views/lignesdetemps.py	Wed Jan 04 17:28:54 2012 +0100
@@ -5,7 +5,7 @@
 from django.shortcuts import render_to_response, get_object_or_404
 from django.template import RequestContext
 from ldt.ldt_utils.models import Content, Project
-from ldt.ldt_utils.utils import LdtUtils, LdtSearch
+from ldt.ldt_utils.utils import LdtUtils, LdtSearch, clean_description
 from ldt.security.utils import set_forbidden_stream
 import base64
 import django.core.urlresolvers
@@ -283,12 +283,36 @@
             id = medianode.get("id")
             new_contents.append(id)
     
+        check_icon_project = False
         #set new content list
         for c in ldtproject.contents.all():
             if not c.iri_id in new_contents:
                 ldtproject.contents.remove(c)
+                check_icon_project = True
+                
+        contents_id = [c.id for c in ldtproject.contents.all()]
+        for c in new_contents:
+            if c not in contents_id:
+                content = Content.objects.get(iri_id=c)
+                ldtproject.contents.add(content)
+        
+        #remove html tags added by flash
+        description = ldtproject.get_description(doc)
+        new_desc = clean_description(description)
+            
+        if new_desc:        
+            desc_node = doc.xpath('/iri/project')[0]
+            desc_node.set('abstract', new_desc)
+            ldtproject.ldt = lxml.etree.tostring(doc, pretty_print=True)
+            
+        ldtproject.description = new_desc if new_desc else description
+    
+        #set a new icon for this project
+        if check_icon_project:
+            ldtproject.set_icon()
     
         ldtproject.save()
+            
     else:
         ldt = ''
         new_contents = []
--- a/src/ldt/ldt/ldt_utils/views/project.py	Wed Jan 04 12:17:47 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/views/project.py	Wed Jan 04 17:28:54 2012 +0100
@@ -16,6 +16,41 @@
                                 get_userlist_model)
 import lxml.etree
 
+@login_required
+def create_ldt_view(request):
+    redirect_to = ''
+    if request.method == "POST" :
+        form = LdtAddForm(request.POST)
+        form_status = "none"
+        contents = Content.safe_objects.all()
+           
+        if form.is_valid():              
+                     
+            user = request.user 
+                      
+            project = Project.create_project(title=form.cleaned_data['title'], user=user, 
+                                             contents=form.cleaned_data['contents'],
+                                             description=form.cleaned_data['description'])
+  
+            if form.cleaned_data["share"]:
+                assign_perm_to_obj(project, form.cleaned_data["read_list"], form.cleaned_data["write_list"], user)
+            form_status = "saved"
+            is_gecko = ((request.META['HTTP_USER_AGENT'].lower().find("firefox")) > -1);
+            if is_gecko :
+                redirect_to = reverse('index_project_full', args=[project.ldt_id])
+            else:
+                return HttpResponseRedirect(reverse('index_project', args=[project.ldt_id]))   
+     
+    else:
+        form = LdtAddForm()
+        contents = Content.safe_objects.all()             
+        form_status = "none"    
+    
+    return render_to_response('ldt/ldt_utils/create_ldt.html', {'contents': contents, 'form': form, 'form_status':form_status,
+                                                                'redirect_to': redirect_to, 'create_project_action':reverse(create_ldt_view), 'language_code' : settings.LANGUAGE_CODE[2:],
+                                                                'elem_list': get_userlist(request.user)}, context_instance=RequestContext(request))
+   
+  
 
 @login_required
 def create_project(request, iri_id): 
@@ -31,6 +66,9 @@
         if form.is_valid():
             user = request.user
             project = Project.create_project(title=form.cleaned_data['title'], user=user, contents=contents, description=form.cleaned_data['description'])
+            
+
+                        
             form_status = "saved" 
 
             if form.cleaned_data["share"]:
@@ -174,50 +212,15 @@
 def publish(request, id):
     ldt = get_object_or_404(Project.safe_objects, ldt_id=id)
     ldt.publish()
-    ldt.save()
     return HttpResponse(simplejson.dumps({'res':True, 'ldt': {'id': ldt.id, 'state':ldt.state, 'ldt_id': ldt.ldt_id}}, ensure_ascii=False), mimetype='application/json')
 
 @login_required
 def unpublish(request, id):
     ldt = get_object_or_404(Project.safe_objects, ldt_id=id)
     ldt.unpublish()
-    ldt.save()
     return HttpResponse(simplejson.dumps({'res':True, 'ldt': {'id': ldt.id, 'state':ldt.state, 'ldt_id': ldt.ldt_id}}, ensure_ascii=False), mimetype='application/json')
 
 
-@login_required
-def create_ldt_view(request):
-    groups = request.user.groups.exclude(name=settings.PUBLIC_GROUP_NAME)
-    redirect_to = ''
-    if request.method == "POST" :
-        form = LdtAddForm(request.POST)
-        form_status = "none"
-        contents = Content.safe_objects.all()
-           
-        if form.is_valid():              
-                     
-            user = request.user            
-            project = Project.create_project(title=form.cleaned_data['title'], user=user, 
-                                             contents=form.cleaned_data['contents'],
-                                             description=form.cleaned_data['description'])
   
-            if form.cleaned_data["share"]:
-                assign_perm_to_obj(project, form.cleaned_data["read_list"], form.cleaned_data["write_list"], user)
-            form_status = "saved"
-            is_gecko = ((request.META['HTTP_USER_AGENT'].lower().find("firefox")) > -1);
-            if is_gecko :
-                redirect_to = reverse('index_project_full', args=[project.ldt_id])
-            else:
-                return HttpResponseRedirect(reverse('index_project', args=[project.ldt_id]))   
-     
-    else:
-        form = LdtAddForm()
-        contents = Content.safe_objects.all()             
-        form_status = "none"    
-    
-    return render_to_response('ldt/ldt_utils/create_ldt.html', {'contents': contents, 'form': form, 'form_status':form_status,
-                                                                'redirect_to': redirect_to, 'create_project_action':reverse(create_ldt_view), 'language_code' : settings.LANGUAGE_CODE[2:],
-                                                                'elem_list': get_userlist(request.user)}, context_instance=RequestContext(request))
-     
 def created_ldt(request):
     return render_to_response('ldt/ldt_utils/save_done.html', context_instance=RequestContext(request))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ldt/ldt/management/commands/setprojecticon.py	Wed Jan 04 17:28:54 2012 +0100
@@ -0,0 +1,13 @@
+from django.core.management.base import BaseCommand
+from ldt.ldt_utils.models import Project
+
+class Command(BaseCommand):
+    help = 'Set icon for every project'
+    
+    def handle(self, *args, **options):
+        
+        for project in Project.objects.all():
+            project.set_icon()
+            project.save()
+                   
+        
\ No newline at end of file
--- a/src/ldt/ldt/settings.py	Wed Jan 04 12:17:47 2012 +0100
+++ b/src/ldt/ldt/settings.py	Wed Jan 04 17:28:54 2012 +0100
@@ -88,4 +88,8 @@
 USE_GROUP_PERMISSIONS = ['Project', 'Content'] 
 PUBLIC_GROUP_NAME = 'everyone'
 
+DEFAULT_CONTENT_ICON = "thumbnails/contents/content_default_icon.png"
+DEFAULT_PROJECT_ICON = "thumbnails/projects/project_default_icon.png"
+DEFAULT_USER_ICON = "thumbnails/users/user_default_icon.png"
+DEFAULT_GROUP_ICON = "thumbnails/groups/group_default_icon.png"
 
--- a/src/ldt/ldt/user/models.py	Wed Jan 04 12:17:47 2012 +0100
+++ b/src/ldt/ldt/user/models.py	Wed Jan 04 17:28:54 2012 +0100
@@ -36,7 +36,7 @@
     user = models.OneToOneField(User)
     language = models.CharField(max_length=2, default=settings.LANGUAGE_CODE[:2])
     
-    image = ImageField(upload_to=settings.MEDIA_ROOT+"thumbnails/users/", default="thumbnails/users/user_default_icon.png")
+    image = ImageField(upload_to=settings.MEDIA_ROOT+"thumbnails/users/", default=settings.DEFAULT_USER_ICON)
 
     @staticmethod
     def create_user_profile(sender, instance, created, **kwargs):
@@ -46,7 +46,7 @@
 class GroupProfile(models.Model):
     group = models.OneToOneField(Group, primary_key=False, related_name='profile')
     description = models.TextField()
-    image = ImageField(upload_to=settings.MEDIA_ROOT+"thumbnails/groups/", default="thumbnails/groups/group_default_icon.png")
+    image = ImageField(upload_to=settings.MEDIA_ROOT+"thumbnails/groups/", default=settings.DEFAULT_GROUP_ICON)
     
     @staticmethod
     def create_group_profile(sender, instance, created, **kwargs):
--- a/web/ldtplatform/config.py.tmpl	Wed Jan 04 12:17:47 2012 +0100
+++ b/web/ldtplatform/config.py.tmpl	Wed Jan 04 17:28:54 2012 +0100
@@ -92,4 +92,9 @@
 USE_GROUP_PERMISSIONS = ['Project', 'Content', 'Media'] 
 FORBIDDEN_STREAM_URL = "rtmp://media.iri.centrepompidou.fr/ddc_player/mp4:video/forbidden_stream.mp4?old_path="
 PUBLIC_GROUP_NAME = 'everyone'
-MAX_USERS_SEARCH = 20
\ No newline at end of file
+MAX_USERS_SEARCH = 20
+
+DEFAULT_CONTENT_ICON = "thumbnails/contents/content_default_icon.png"
+DEFAULT_PROJECT_ICON = "thumbnails/projects/project_default_icon.png"
+DEFAULT_USER_ICON = "thumbnails/users/user_default_icon.png"
+DEFAULT_GROUP_ICON = "thumbnails/groups/group_default_icon.png"
\ No newline at end of file