Commits

Kai Diefenbach committed 6d1c5a5

Factored out portlets

  • Participants
  • Parent commits 818debd

Comments (0)

Files changed (9)

File lfc/admin.py

 from lfc.models import Portal
 from lfc.models import Image
 from lfc.models import File
-from lfc.models import NavigationPortlet
-from lfc.models import PagesPortlet
 from lfc.models import Template
 from lfc.models import WorkflowStatesInformation
 
 admin.site.register(Image)
 admin.site.register(BaseContent)
 admin.site.register(Portal)
-admin.site.register(NavigationPortlet)
-admin.site.register(PagesPortlet)
 admin.site.register(Template)
 admin.site.register(WorkflowStatesInformation)

File lfc/manage/forms.py

 class RoleForm(forms.ModelForm):
     """Form to add/edit a Role.
     """
+    groups = forms.MultipleChoiceField(label=_("Groups"), required=False)
+
     class Meta:
         model = Role
 
+    def __init__(self, *args, **kwargs):
+        super(RoleForm, self).__init__(*args, **kwargs)
+        self.fields["groups"].choices = [(g.id, g.name) for g in Group.objects.all()]
+        self.initial.update({
+            "groups" : [prr.group.id for prr in PrincipalRoleRelation.objects.filter(role=self.instance).exclude(group=None)]
+        })
+
+    def save(self, commit=True):
+        group_ids = self.data.getlist("groups")
+        for group in Group.objects.all():
+            if str(group.id) in group_ids:
+                try:
+                    prr = PrincipalRoleRelation.objects.get(group=group, role=self.instance, content_id=None)
+                except PrincipalRoleRelation.DoesNotExist:
+                    PrincipalRoleRelation.objects.create(group=group, role=self.instance)
+            else:
+                try:
+                    prr = PrincipalRoleRelation.objects.get(group=group, role=self.instance, content_id=None)
+                except PrincipalRoleRelation.DoesNotExist:
+                    pass
+                else:
+                    prr.delete()
+
+        del self.fields["groups"]
+        return super(RoleForm, self).save(commit)
 
 class GroupForm(forms.ModelForm):
     """Form to add/edit a Group.

File lfc/manage/views.py

 
     if request.method == "GET":
         try:
+            import pdb; pdb.set_trace()
             portlet_ct = ContentType.objects.filter(model=portlet_type.lower())[0]
             mc = portlet_ct.model_class()
             form = mc().form(prefix="portlet")
         "form": form,
         "role": role,
         "roles": Role.objects.exclude(name__in=("Anonymous", "Owner")),
+        "users" : role.get_users(),
         "current_role_id": int(id),
     }))
 

File lfc/models.py

     def __unicode__(self):
         return unicode(self.title)
 
-    def has_meta_data_tab(self):
+    def has_meta_data_tab(self, default=True):
         if not getattr(settings, "LFC_MANAGE_META_DATA", True):
             return False
         try:
             return ITabs(self).has_meta_data_tab()
         except TypeError:
-            return True
+            return default
 
-    def has_children_tab(self):
+    def has_children_tab(self, default=True):
         if not getattr(settings, "LFC_MANAGE_CHILDREN", True):
             return False
         try:
             return ITabs(self).has_children_tab()
         except TypeError:
-            return True
+            return default
 
-    def has_images_tab(self):
+    def has_images_tab(self, default=True):
         if not getattr(settings, "LFC_MANAGE_IMAGES", True):
             return False
         try:
             return ITabs(self).has_images_tab()
         except TypeError:
-            return True
+            return default
 
-    def has_files_tab(self):
+    def has_files_tab(self, default=True):
         if not getattr(settings, "LFC_MANAGE_FILES", True):
             return False
         try:
             return ITabs(self).has_files_tab()
         except TypeError:
-            return True
+            return default
 
-    def has_portlets_tab(self):
+    def has_portlets_tab(self, default=True):
         if not getattr(settings, "LFC_MANAGE_PORTLETS", True):
             return False
         try:
             return ITabs(self).has_portlets_tab()
         except TypeError:
-            return True
+            return default
 
-    def has_comments_tab(self):
+    def has_comments_tab(self, default=True):
         if not getattr(settings, "LFC_MANAGE_COMMENTS", True):
             return False
         try:
             return ITabs(self).has_comments_tab()
         except TypeError:
-            return True
+            return default
 
-    def has_seo_tab(self):
+    def has_seo_tab(self, default=True):
         if not getattr(settings, "LFC_MANAGE_SEO", True):
             return False
         try:
             return ITabs(self).has_seo_tab()
         except TypeError:
-            return True
+            return default
 
-    def has_permissions_tab(self):
+    def has_permissions_tab(self, default=True):
         if not getattr(settings, "LFC_MANAGE_PERMISSIONS", True):
             return False
         try:
             return ITabs(self).has_permissions_tab()
         except TypeError:
-            return True
+            return default
 
     def get_tabs(self, request):
         return []
 
     def get_absolute_url(self):
         return reverse("lfc_file", kwargs={"id": self.id})
-
-#### Portlets
-###############################################################################
-
-
-class NavigationPortlet(Portlet):
-    """A portlet to display the navigation tree.
-
-    Note: this reuses mainly the navigation inclusion tag.
-
-    Parameters:
-
-        - start_level:
-            The tree is displayed from this level 1. The tree starts with 1
-
-        - expand_level:
-            The tree is expanded up this level. Default is 0, which means the
-            tree is not expanded at all but the current node.
-    """
-    start_level = models.PositiveSmallIntegerField(default=1)
-    expand_level = models.PositiveSmallIntegerField(default=0)
-
-    def render(self, context):
-        """Renders the portlet as HTML.
-        """
-        request = context.get("request")
-        return render_to_string("lfc/portlets/navigation_portlet.html", RequestContext(request, {
-            "start_level": self.start_level,
-            "expand_level": self.expand_level,
-            "title": self.title,
-        }))
-
-    def form(self, **kwargs):
-        """
-        """
-        return NavigationPortletForm(instance=self, **kwargs)
-
-
-class NavigationPortletForm(forms.ModelForm):
-    """Add/edit form for the navigation portlet.
-    """
-    class Meta:
-        model = NavigationPortlet
-
-
-# TODO: Rename as it is able to display all content types. ContentPortlet, DocumentPortlet, ...?
-class PagesPortlet(Portlet):
-    """A portlet to display arbitrary objects. The objects can be selected by
-    tags.
-
-    **Attributes:**
-
-    limit:
-        The amount of objects which are displayed at maximum.
-
-    tags:
-        The tags an object must have to be displayed.
-    """
-    limit = models.PositiveSmallIntegerField(default=5)
-    tags = models.CharField(blank=True, max_length=100)
-
-    def __unicode__(self):
-        return "%s" % self.id
-
-    def render(self, context):
-        """Renders the portlet as HTML.
-        """
-        objs = BaseContent.objects.filter(
-            language__in=("0", translation.get_language()))
-
-        if self.tags:
-            objs = tagging.managers.ModelTaggedItemManager().with_all(self.tags, objs)[:self.limit]
-        else:
-            objs = objs[:self.limit]
-
-        return render_to_string("lfc/portlets/pages_portlet.html", {
-            "title": self.title,
-            "objs": objs,
-        })
-
-    def form(self, **kwargs):
-        """Returns the add/edit form of the portlet.
-        """
-        return PagesPortletForm(instance=self, **kwargs)
-
-
-class PagesPortletForm(forms.ModelForm):
-    """Add/edit form of the pages portlet.
-    """
-    tags = TagField(widget=AutoCompleteTagInput(), required=False)
-
-    class Meta:
-        model = PagesPortlet
-
-
-class RandomPortlet(Portlet):
-    """A portlet to display random objects. The objects can be selected by
-    tags.
-
-    **Attributes:**
-
-    limit:
-        The amount of objects which are displayed at maximum.
-
-    tags:
-        The tags an object must have to be displayed.
-    """
-    limit = models.PositiveSmallIntegerField(default=1)
-    tags = models.CharField(blank=True, max_length=100)
-
-    def render(self, context):
-        """Renders the portlet as HTML.
-        """
-        items = BaseContent.objects.filter(
-            language__in=("0", translation.get_language()))
-
-        if self.tags:
-            items = tagging.managers.ModelTaggedItemManager().with_all(self.tags, items)[:self.limit]
-
-        items = list(items)
-        random.shuffle(items)
-
-        return render_to_string("lfc/portlets/random_portlet.html", {
-            "title": self.title,
-            "items": items[:self.limit],
-        })
-
-    def form(self, **kwargs):
-        """Returns the form of the portlet.
-        """
-        return RandomPortletForm(instance=self, **kwargs)
-
-
-class RandomPortletForm(forms.ModelForm):
-    """Add/Edit form for the random portlet.
-    """
-    tags = TagField(widget=AutoCompleteTagInput(), required=False)
-
-    class Meta:
-        model = RandomPortlet
-
-
-class TextPortlet(Portlet):
-    """A portlet to display arbitrary HTML text.
-
-    **Attributes:**
-
-    text:
-        The HTML text which is displayed. Can contain any HTML text.
-    """
-
-    text = models.TextField(_(u"Text"), blank=True)
-
-    def __unicode__(self):
-        return "%s" % self.id
-
-    def render(self, context):
-        """Renders the portlet as HTML.
-        """
-        return render_to_string("lfc/portlets/text_portlet.html", {
-            "title": self.title,
-            "text": self.text
-        })
-
-    def form(self, **kwargs):
-        """
-        """
-        return TextPortletForm(instance=self, **kwargs)
-
-
-class TextPortletForm(forms.ModelForm):
-    """Add/Edit form for the text portlet.
-    """
-    class Meta:
-        model = TextPortlet

File lfc/templates/lfc/manage/role.html

         <ul>
             <li class="ui-tabs-nav-item"><a href="#data">{% trans "Data" %}</a></li>
             <li class="ui-tabs-nav-item"><a href="#users">{% trans "Users" %}</a></li>
-            <li class="ui-tabs-nav-item"><a href="#groups">{% trans "Groups" %}</a></li>
         </ul>
 
         <div id="data">
+            <h2 class="first-heading">{% trans "Data" %}</h2>
             <form action="{% url lfc_manage_save_role role.id %}"
                   method="POST">
                 {% csrf_token %}
                 <input type="submit" value='{% trans "Save role" %}' />
             </form>
         </div>
-        <div id="users"></div>
-        <div id="groups"></div>
+        <div id="users">
+            <h2 class="first-heading">{% trans "Users" %}</h2>
+
+            <table class="lfs-manage-table">
+                <th>
+                    {% trans "Username" %}
+                </th>
+                <th>
+                    {% trans "Name" %}
+                </th>
+                {% for user in users %}
+                    <tr>
+                        <td>
+                            <a href="{% url lfc_manage_user user.id %}">
+                                {{ user.username }}
+                            </a>
+                        </td>
+                        <td>
+                            {% if user.first_name %}
+                                {{ user.first_name }}
+                            {% endif %}
+                            {% if user.last_name %}
+                                {{ user.last_name }}
+                            {% endif %}
+                        </td>
+                    </tr>
+                {% endfor %}
+            </table>
+        </div>
     </div>
 
 

File lfc/templates/lfc/manage/user.html

         </ul>
 
         <div id="data">
+            <h2 class="first-heading">{% trans "Data" %}</h2>
             {{ data }}
         </div>
 
         <div id="password">
+            <h2>{% trans "Change password" %}</h2>
             {{ password }}
         </div>
 
     </div>
-{% endblock %}
+{% endblock %}

File lfc/tests/permission_tests.py

         self.failUnless(result._headers["location"][1].startswith("http://testserver/login"))
 
         # object portlets
-        from lfc.models import PagesPortlet
-        self.portlet = PagesPortlet()
+        from lfc.models import ContentPortlet
+        self.portlet = ContentPortlet()
         self.portlet.id = 1
 
         # Assign the portlet to th page
         self.failUnless(result._headers["location"][1].startswith("http://testserver/login"))
 
         # object portlets
-        from lfc.models import PagesPortlet
-        self.portlet = PagesPortlet()
+        from lfc.models import ContentPortlet
+        self.portlet = ContentPortlet()
         self.portlet.id = 1
 
         # Assign the portlet to th page

File lfc/tests/portlets_tests.py

 import lfc.manage.views
 import lfc.utils.registration
 from lfc.models import BaseContent
-from lfc.models import NavigationPortlet
 from lfc.models import Portal
-from lfc.models import TextPortlet
 from lfc.tests.utils import create_request
 
 # lfc_page imports
 from portlets.models import PortletAssignment
 from portlets.models import Slot
 
+# lfc_portlets imports
+from lfc_portlets.models import NavigationPortlet
+from lfc_portlets.models import TextPortlet
 
 class PortletsTestCase(TestCase):
     """

File lfc/utils/initialize.py

 
 # lfc imports
 from lfc.utils.registration import register_template
-from lfc.models import NavigationPortlet
-from lfc.models import PagesPortlet
-from lfc.models import RandomPortlet
-from lfc.models import TextPortlet
+from lfc_portlets.models import NavigationPortlet
+from lfc_portlets.models import ContentPortlet
+from lfc_portlets.models import RandomPortlet
+from lfc_portlets.models import TextPortlet
 
 # resources imports
 import resources.utils
     """
     # Portlets
     register_portlet(NavigationPortlet, "Navigation")
-    register_portlet(PagesPortlet, "Pages")
+    register_portlet(ContentPortlet, "Pages")
     register_portlet(RandomPortlet, "Random")
     register_portlet(TextPortlet, "Text")