Changeset 1182 in main


Ignore:
Timestamp:
06/06/12 13:55:16 (7 years ago)
Author:
pcosquer
Message:

publication: adds buttons to publish/unpublish and tests

Location:
trunk/openPLM
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/openPLM/plmapp/tests/base.py

    r1109 r1182  
    3535    def get_contributor(self, username="user2"): 
    3636        """ Returns a new contributor""" 
    37         user = User(username="user2") 
     37        user = User(username=username) 
    3838        user.save() 
    3939        user.get_profile().is_contributor = True 
     40        user.get_profile().save() 
     41        user.groups.add(self.group) 
     42        return user 
     43     
     44    def get_publisher(self, username="publisher"): 
     45        """ Returns a new contributor""" 
     46        user = User(username=username) 
     47        user.save() 
     48        user.get_profile().can_publish = True 
    4049        user.get_profile().save() 
    4150        user.groups.add(self.group) 
  • trunk/openPLM/plmapp/tests/controllers/plmobject.py

    r1109 r1182  
    467467                ctrl.check_readable()         
    468468 
     469    # publication tests 
     470 
     471    def assertPublish(self, ctrl): 
     472        self.assertTrue(ctrl.check_publish()) 
     473        self.assertTrue(ctrl.can_publish()) 
     474        ctrl.publish() 
     475        self.assertTrue(ctrl.published) 
     476 
     477    def assertPublishError(self, ctrl): 
     478        self.assertRaises(exc.PermissionError, ctrl.check_publish) 
     479        self.assertFalse(ctrl.can_publish()) 
     480        self.assertRaises(exc.PermissionError, ctrl.publish) 
     481        self.assertFalse(ctrl.published) 
     482 
     483    def test_publish_not_official(self): 
     484        """ Tests that a non official object can *not* be published.""" 
     485        controller = self.create("P1") 
     486        publisher = self.get_publisher() 
     487        ctrl = self.CONTROLLER(controller.object, publisher) 
     488        self.assertPublishError(ctrl) 
     489 
     490    def test_publish_official(self): 
     491        """ Tests that an official object can be published.""" 
     492        controller = self.create("P1") 
     493        self.promote_to_official(controller) 
     494        publisher = self.get_publisher() 
     495        ctrl = self.CONTROLLER(controller.object, publisher) 
     496        self.assertPublish(ctrl) 
     497 
     498    def test_publish_deprecated(self): 
     499        """ Tests that a deprecated object can *not* be published.""" 
     500        controller = self.create("P1") 
     501        self.promote_to_deprecated(controller) 
     502        publisher = self.get_publisher() 
     503        ctrl = self.CONTROLLER(controller.object, publisher) 
     504        self.assertPublishError(ctrl) 
     505 
     506    def test_publish_published(self): 
     507        """ Tests that a published object can *not* be published.""" 
     508        controller = self.create("P1") 
     509        self.promote_to_official(controller) 
     510        publisher = self.get_publisher() 
     511        ctrl = self.CONTROLLER(controller.object, publisher) 
     512        self.assertPublish(ctrl) 
     513        self.assertFalse(ctrl.can_publish()) 
     514        self.assertRaises(ValueError, ctrl.check_publish) 
     515        self.assertRaises(ValueError, ctrl.publish) 
     516 
     517    def test_publish_not_publisher(self): 
     518        """ Tests that only a publisher can publish.""" 
     519        controller = self.create("P1") 
     520        self.promote_to_official(controller) 
     521        self.assertFalse(controller._user.get_profile().can_publish) 
     522        self.assertPublishError(controller) 
     523 
     524    def test_publish_not_in_group(self): 
     525        """ Tests that only a publisher who does not belongs to the objects 
     526        group can *not* publish.""" 
     527        controller = self.create("P1") 
     528        self.promote_to_official(controller) 
     529        publisher = self.get_publisher() 
     530        publisher.groups.remove(self.group) 
     531        ctrl = self.CONTROLLER(controller.object, publisher) 
     532        self.assertPublishError(ctrl) 
     533 
     534    # unpublication tests 
     535 
     536    def get_published_ctrl(self): 
     537        controller = self.create("P1") 
     538        controller.object.published = True 
     539        controller.object.save() 
     540        return controller 
     541 
     542    def assertUnpublish(self, ctrl): 
     543        self.assertTrue(ctrl.check_unpublish()) 
     544        self.assertTrue(ctrl.can_unpublish()) 
     545        ctrl.unpublish() 
     546        self.assertFalse(ctrl.published) 
     547 
     548    def assertUnpublishError(self, ctrl): 
     549        self.assertRaises(exc.PermissionError, ctrl.check_unpublish) 
     550        self.assertFalse(ctrl.can_unpublish()) 
     551        self.assertRaises(exc.PermissionError, ctrl.unpublish) 
     552        self.assertTrue(ctrl.published) 
     553 
     554    def test_unpublish_not_official(self): 
     555        """ Tests that a non official published object can be unpublished.""" 
     556        publisher = self.get_publisher() 
     557        ctrl = self.CONTROLLER(self.get_published_ctrl().object, publisher) 
     558        self.assertUnpublish(ctrl) 
     559 
     560    def test_unpublish_official(self): 
     561        """ Tests that an official published object can be unpublished.""" 
     562        controller = self.get_published_ctrl() 
     563        self.promote_to_official(controller) 
     564        publisher = self.get_publisher() 
     565        ctrl = self.CONTROLLER(controller.object, publisher) 
     566        self.assertUnpublish(ctrl) 
     567 
     568    def test_unpublish_deprecated(self): 
     569        """ Tests that a deprecated opublished bject can be unpublished.""" 
     570        controller = self.get_published_ctrl() 
     571        self.promote_to_deprecated(controller) 
     572        publisher = self.get_publisher() 
     573        ctrl = self.CONTROLLER(controller.object, publisher) 
     574        self.assertUnpublish(ctrl) 
     575 
     576    def test_unpublish_published(self): 
     577        """ Tests that an unpublished object can *not* be unpublished.""" 
     578        controller = self.create("P1") 
     579        publisher = self.get_publisher() 
     580        ctrl = self.CONTROLLER(controller.object, publisher) 
     581        self.assertFalse(ctrl.can_unpublish()) 
     582        self.assertRaises(ValueError, ctrl.check_unpublish) 
     583        self.assertRaises(ValueError, ctrl.unpublish) 
     584 
     585    def test_unpublish_not_publisher(self): 
     586        """ Tests that only a publisher can unpublish.""" 
     587        controller = self.get_published_ctrl() 
     588        self.assertFalse(controller._user.get_profile().can_publish) 
     589        self.assertUnpublishError(controller) 
     590 
     591    def test_unpublish_not_in_group(self): 
     592        """ Tests that only a publisher who does not belongs to the objects 
     593        group can *not* unpublish.""" 
     594        controller = self.get_published_ctrl() 
     595        publisher = self.get_publisher() 
     596        publisher.groups.remove(self.group) 
     597        ctrl = self.CONTROLLER(controller.object, publisher) 
     598        self.assertUnpublishError(ctrl) 
     599 
  • trunk/openPLM/plmapp/tests/views.py

    r1176 r1182  
    3939from django.utils import translation 
    4040 
     41import lxml.html 
     42 
    4143from openPLM.plmapp import forms 
    4244from openPLM.plmapp.utils import level_to_sign_str 
     
    6163        self.controller = self.CONTROLLER.create(self.REFERENCE, self.TYPE, "a", 
    6264                                                 self.user, self.DATA) 
    63         self.base_url = "/object/%s/%s/%s/" % (self.controller.type, 
    64                                               self.controller.reference, 
    65                                               self.controller.revision) 
     65        self.base_url = self.controller.plmobject_url  
    6666        brian = User.objects.create_user(username="Brian", password="life", 
    6767                email="brian@example.net") 
     
    419419        self.assertFalse(m.PLMObjectUserLink.objects.filter(plmobject=self.controller.object, 
    420420            user=self.brian, role=m.ROLE_NOTIFIED)) 
     421 
     422    def test_publish_post(self): 
     423        """ Tests a publication. """ 
     424        self.controller.object.state = m.State.objects.get(name="official") 
     425        self.controller.object.save() 
     426        self.user.get_profile().can_publish = True 
     427        self.user.get_profile().save() 
     428        response = self.post(self.base_url + "lifecycle/apply/",  
     429                {"publish" : "on", "password" : "password"}) 
     430        self.assertTrue(response.context["obj"].published) 
     431        # check that the public link is displayed 
     432        root = lxml.html.fromstring(response.content.decode("utf-8")) 
     433        self.assertTrue(root.xpath('//input[@name="unpublish"]')) 
     434        self.assertFalse(root.xpath('//input[@name="publish"]')) 
     435        self.assertTrue(root.xpath(u'//a[@href=$url]', url=self.base_url+"public/")) 
     436     
     437    def test_publish_post_error_not_official(self): 
     438        """ Tests a publication: error: object not official. """ 
     439        self.user.get_profile().can_publish = True 
     440        self.user.get_profile().save() 
     441        response = self.client.post(self.base_url + "lifecycle/apply/",  
     442                data={"publish" : "on", "password" : "password"}) 
     443        self.assertTemplateUsed(response, "error.html") 
     444        response2 = self.get(self.base_url + "lifecycle/apply/") 
     445        self.assertFalse(response2.context["obj"].published) 
     446        # check that the public link is not displayed 
     447        root = lxml.html.fromstring(response2.content.decode("utf-8")) 
     448        self.assertFalse(root.xpath('//input[@name="unpublish"]')) 
     449        self.assertFalse(root.xpath('//input[@name="publish"]')) 
     450        self.assertFalse(root.xpath(u'//a[@href=$url]', url=self.base_url+"public/")) 
     451    
     452    def test_publish_post_error_published(self): 
     453        """ Tests a publication: error: object is already published. """ 
     454        self.user.get_profile().can_publish = True 
     455        self.user.get_profile().save() 
     456        self.controller.object.state = m.State.objects.get(name="official") 
     457        self.controller.object.published = True 
     458        self.controller.object.save() 
     459        response = self.client.post(self.base_url + "lifecycle/apply/",  
     460                data={"publish" : "on", "password" : "password"}) 
     461        self.assertTemplateUsed(response, "error.html") 
     462        response2 = self.get(self.base_url + "lifecycle/apply/") 
     463        self.assertTrue(response2.context["obj"].published) 
     464        # check that the publish button is not displayed 
     465        root = lxml.html.fromstring(response2.content.decode("utf-8")) 
     466        self.assertTrue(root.xpath('//input[@name="unpublish"]')) 
     467        self.assertFalse(root.xpath('//input[@name="publish"]')) 
     468        self.assertTrue(root.xpath(u'//a[@href=$url]', url=self.base_url+"public/")) 
     469     
     470    def test_unpublish_post(self): 
     471        """ Tests an unpublication. """ 
     472        self.controller.object.published = True 
     473        self.controller.object.state = m.State.objects.get(name="official") 
     474        self.controller.object.save() 
     475        self.user.get_profile().can_publish = True 
     476        self.user.get_profile().save() 
     477        response = self.post(self.base_url + "lifecycle/apply/",  
     478                {"unpublish" : "on", "password" : "password"}) 
     479        self.assertFalse(response.context["obj"].published) 
     480        # check that the public link is not displayed 
     481        root = lxml.html.fromstring(response.content.decode("utf-8")) 
     482        self.assertFalse(root.xpath('//input[@name="unpublish"]')) 
     483        self.assertTrue(root.xpath('//input[@name="publish"]')) 
     484        self.assertFalse(root.xpath(u'//a[@href=$url]', url=self.base_url+"public/")) 
     485     
     486    def test_unpublish_post_error_unpublished(self): 
     487        """ Tests an unpublication: errror: object is unpublished. """ 
     488        self.controller.object.save() 
     489        self.user.get_profile().can_publish = True 
     490        self.user.get_profile().save() 
     491        response = self.client.post(self.base_url + "lifecycle/apply/",  
     492                {"unpublish" : "on", "password" : "password"}) 
     493        self.assertTemplateUsed(response, "error.html") 
     494        response2 = self.get(self.base_url + "lifecycle/apply/") 
     495        self.assertFalse(response2.context["obj"].published) 
     496        # check that the unpublish button is not displayed 
     497        root = lxml.html.fromstring(response.content.decode("utf-8")) 
     498        self.assertFalse(root.xpath('//input[@name="unpublish"]')) 
     499        self.assertFalse(root.xpath('//input[@name="publish"]')) 
     500        self.assertFalse(root.xpath(u'//a[@href=$url]', url=self.base_url+"public/")) 
     501    
     502    def test_public_get(self): 
     503        """ Tests anonymous access to a published object. """ 
     504        self.controller.object.published = True 
     505        self.controller.object.save() 
     506        revb = self.controller.revise("b") 
     507        self.assertFalse(revb.published) 
     508        revc = revb.revise("c") 
     509        revc.object.published = True 
     510        revc.object.save() 
     511        self.client.logout() 
     512        response = self.client.get(self.base_url + "public/") 
     513        self.assertTrue(response.context["obj"].published) 
     514        # checks that some private data are not displayed 
     515        root = lxml.html.fromstring(response.content.decode("utf-8")) 
     516        self.assertNotContains(response, self.user.username) 
     517        self.assertEqual(["a", "c"], [o.revision for o in response.context["revisions"]]) 
     518        self.assertFalse(root.xpath('//div[@id="SearchBox"]')) 
     519        self.assertFalse(root.xpath('//div[@id="DisplayBox"]')) 
     520 
     521    def test_public_error(self): 
     522        """ Tests anonymous access to an unpublished object: error. """ 
     523        self.client.logout() 
     524        response = self.client.get(self.base_url + "public/", follow=True) 
     525        self.assertTemplateUsed(response, "login.html") 
    421526 
    422527 
     
    18491954        data = dict(sponsor=self.user.id,  
    18501955                    username="loser", first_name="You", last_name="Lost", 
    1851                     email="you.lost@example.com", groups=[self.group.pk], language=self.user.get_profile().language) 
     1956                    email="you.lost@example.com", groups=[self.group.pk], 
     1957                    language=self.user.get_profile().language) 
    18521958        response = self.post(self.user_url + "delegation/sponsor/", data) 
    18531959        user = User.objects.get(username=data["username"]) 
  • trunk/openPLM/plmapp/views/main.py

    r1175 r1182  
    267267    if request.method == 'POST': 
    268268        password_form = forms.ConfirmPasswordForm(request.user, request.POST) 
     269        actions = (("demote", obj.demote), ("promote", obj.promote), 
     270                   ("publish", obj.publish), ("unpublish", obj.unpublish,)) 
    269271        if password_form.is_valid(): 
    270             if "demote" in request.POST: 
    271                 obj.demote() 
    272             elif "promote" in request.POST: 
    273                 obj.promote() 
     272            for action_name, method in actions: 
     273                if action_name in request.POST: 
     274                    method() 
     275                    break 
    274276            return HttpResponseRedirect("..") 
    275         if "demote" in request.POST: 
    276             ctx["action"] = "demote" 
    277         elif "promote" in request.POST: 
    278             ctx["action"] = "promote" 
     277        for action_name, method in actions: 
     278            if action_name in request.POST: 
     279                ctx["action"] = action_name 
     280                break 
    279281    else:  
    280282        password_form = forms.ConfirmPasswordForm(request.user) 
  • trunk/openPLM/templates/lifecycle.html

    r1161 r1182  
    2020 
    2121    {# promotion form #} 
    22     <form id ="form-promote" class="confirmation"  action="{{obj.plmobject_url}}lifecycle/apply/" method="POST"> 
     22    <form id ="form-promote" class="confirmation" style="display: inline;" 
     23        action="{{obj.plmobject_url}}lifecycle/apply/" method="POST"> 
    2324        {% if is_signer or is_signer_dm %} 
    2425            <div id="form-promote-dialog" 
     
    9899 
    99100    </form> 
     101 
     102    {# publish #} 
     103    {% if obj.can_publish %} 
     104        <form id ="form-publish" class="confirmation" style="display: inline;" 
     105            action="{{obj.plmobject_url}}lifecycle/apply/" method="POST"> 
     106            <div id="form-publish-dialog" 
     107                title="{% trans "Are you sure?" %}" 
     108                {% if password_form.is_bound and not password_form.is_valid %}  
     109                    class="c-error action-{{ action }}" 
     110                {% endif %} 
     111                > 
     112                <p> 
     113                <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span> 
     114                {% trans "Warning, if you publish this object, it will be accessible to everyone, including anonymous users." %}</p> 
     115 
     116                <div class="password"> 
     117                    {{ password_form.as_p }} 
     118                </div> 
     119            </div> 
     120 
     121            {# button #} 
     122            <input name="publish" type="submit" class="{{"Button"|button}}" value="{% trans "PUBLISH" %}" /> 
     123        </form> 
     124    {% endif %} 
     125 
     126    {# unpublish #} 
     127    {% if obj.can_unpublish %} 
     128        <form id ="form-unpublish" class="confirmation" style="display: inline;" 
     129            action="{{obj.plmobject_url}}lifecycle/apply/" method="POST"> 
     130            <div id="form-unpublish-dialog" 
     131                title="{% trans "Are you sure?" %}" 
     132                {% if password_form.is_bound and not password_form.is_valid %}  
     133                    class="c-error action-{{ action }}" 
     134                {% endif %} 
     135                > 
     136                <p> 
     137                <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span> 
     138                {% trans "Warning, if you unpublish this object, it won't be anymore accessible to everyone." %}</p> 
     139 
     140                <div class="password"> 
     141                    {{ password_form.as_p }} 
     142                </div> 
     143            </div> 
     144 
     145            {# button #} 
     146            <input name="unpublish" type="submit" class="{{"Button"|button}}" value="{% trans "UNPUBLISH" %}" /> 
     147        </form> 
     148    {% endif %} 
     149 
     150    {% if obj.published %} 
     151        <div id="published-info"> 
     152            <p> 
     153            {% trans "This part has been published:" %} 
     154            <a href="{{obj.plmobject_url}}public/">{% trans "Public link" %}</a> 
     155            </p> 
     156        </div> 
     157    {% endif %} 
     158 
    100159 
    101160    {# lifecycle : display all states #} 
Note: See TracChangeset for help on using the changeset viewer.