Added favorites contents in user's profile
authorThierry Florac <thierry.florac@onf.fr>
Mon, 18 Jan 2016 16:08:56 +0100
changeset 8 4a2b5dd3d69b
parent 7 cbc55162b64e
child 9 f6739a79912f
Added favorites contents in user's profile
src/pyams_content/profile/admin.py
src/pyams_content/profile/interfaces/__init__.py
src/pyams_content/profile/zmi/__init__.py
--- a/src/pyams_content/profile/admin.py	Mon Jan 18 16:08:07 2016 +0100
+++ b/src/pyams_content/profile/admin.py	Mon Jan 18 16:08:56 2016 +0100
@@ -34,6 +34,7 @@
 class AdminProfile(Persistent):
     """Admin profile persistent class"""
 
+    favorites = FieldProperty(IAdminProfile['favorites'])
     table_page_length = FieldProperty(IAdminProfile['table_page_length'])
 
 
--- a/src/pyams_content/profile/interfaces/__init__.py	Mon Jan 18 16:08:07 2016 +0100
+++ b/src/pyams_content/profile/interfaces/__init__.py	Mon Jan 18 16:08:56 2016 +0100
@@ -19,7 +19,7 @@
 
 # import packages
 from zope.interface import Interface
-from zope.schema import Choice
+from zope.schema import Choice, List, TextLine
 
 from pyams_content import _
 
@@ -30,6 +30,10 @@
 class IAdminProfile(Interface):
     """User admin profile preferences"""
 
+    favorites = List(title=_("User favorites"),
+                     description=_("List of internal numbers of shared contents stored for quick access"),
+                     value_type=TextLine())
+
     table_page_length = Choice(title=_("Default table length"),
                                description=_("Default length used for inner tables and dashboards"),
                                values=(10, 25, 50, 100),
--- a/src/pyams_content/profile/zmi/__init__.py	Mon Jan 18 16:08:07 2016 +0100
+++ b/src/pyams_content/profile/zmi/__init__.py	Mon Jan 18 16:08:56 2016 +0100
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyramid.view import view_config
 
 __docformat__ = 'restructuredtext'
 
@@ -21,9 +22,9 @@
 from pyams_skin.layer import IPyAMSLayer
 
 # import packages
-from pyams_form.form import InnerEditForm
 from pyams_security.zmi.profile import UserProfileEditForm
 from pyams_utils.adapter import adapter_config
+from pyams_zmi.form import InnerAdminEditForm
 from z3c.form import field
 from zope.interface import Interface
 
@@ -33,7 +34,7 @@
 @adapter_config(name='admin_profile',
                 context=(Interface, IPyAMSLayer, UserProfileEditForm),
                 provides=IInnerTabForm)
-class AdminProfileTabForm(InnerEditForm):
+class AdminProfileTabForm(InnerAdminEditForm):
     """Admin profile tab form"""
 
     tab_label = _("Admin. profile")
@@ -49,3 +50,20 @@
 
     def getContent(self):
         return IAdminProfile(self.request.principal)
+
+
+@view_config(name='switch-user-favorite.json', context=Interface, request_type=IPyAMSLayer,
+             renderer='json', xhr=True)
+def switch_favorite(request):
+    """Add or remove reference from user favorites"""
+    profile = IAdminProfile(request.principal)
+    favorites = profile.favorites or []
+    oid = request.params.get('oid')
+    if oid:
+        if oid in favorites:
+            favorites.remove(oid)
+        else:
+            favorites.append(oid)
+        profile.favorites = favorites
+        return {'oid': oid,
+                'favorite': oid in favorites}