Commits

Sylvain Thénault  committed 6494d1b

dumb share box, need improvments

  • Participants
  • Parent commits e5e6f41

Comments (0)

Files changed (1)

 
 from uuid import uuid4
 
-from cubicweb.predicates import relation_possible
-from cubicweb.view import EntityAdapter
-from cubicweb.web import Redirect
+from cubicweb import view, tags
+from cubicweb.predicates import relation_possible, match_user_groups, adaptable
+from cubicweb.web import Redirect, component
 from cubicweb.web.views import urlpublishing, uicfg
 
+
 _pvs = uicfg.primaryview_section
 _afs = uicfg.autoform_section
 
+_afs.tag_object_of(('*', 'share_key_of', '*'), 'main', 'hidden')
+_pvs.tag_object_of(('*', 'share_key_of', '*'), 'hidden')
 
-class IRESTComputedPathAdapter(EntityAdapter):
+
+class IURLShareableAdapter(view.EntityAdapter):
     __regid__ = 'IURLShareable'
     __select__ = relation_possible('share_key_of', 'object')
 
                                in_group=self.user_groups())
         return key
 
+
 class SharedURLPathEvaluator(urlpublishing.URLPathEvaluator):
     priority = 0.5 # insert after RawPathEvaluator but before EidPathEvaluator
     def evaluate_path(self, req, parts):
         # then redirecto to the url as if the key part wasn't there
         raise Redirect(req.build_url('/'.join(parts[1:])))
 
-_afs.tag_object_of(('*', 'share_key_of', '*'), 'main', 'hidden')
-_pvs.tag_object_of(('*', 'share_key_of', '*'), 'hidden')
+
+class ShareBox(component.EntityCtxComponent):
+    __regid__ = 'shareurl.box'
+    __select__ = (component.EntityCtxComponent.__select__
+                  & adaptable('IURLShareable')
+                  & match_user_groups('owners'))
+    order = 10
+    context = 'left'
+    title = _('share')
+
+    def render_body(self, w):
+        ishare = self.entity.cw_adapt_to('IURLShareable')
+        url = ishare.share_url()
+        if url:
+            w(u'%s <b>%s</b>' %
+              (self._cw._('You may share this content through this url:'), url))
+        else:
+            w(tags.a(self._cw._('Share this %s') % self.entity.dc_type().lower(),
+                     href=self.entity.absolute_url(vid='shareurl.share')))
+
+
+class ShareView(view.EntityView):
+    __regid__ = 'shareurl.share'    # no download box for images
+    __select__ = (component.EntityCtxComponent.__select__
+                  & adaptable('IURLShareable')
+                  & match_user_groups('owners'))
+
+    def entity_call(self, entity):
+        ishare = entity.cw_adapt_to('IURLShareable')
+        assert not ishare.share_url()
+        ishare.share()
+        raise Redirect(entity.absolute_url())