| Total Complexity | 7 | 
| Total Lines | 46 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
| 1 | # -*- coding: utf-8 -*-  | 
            ||
| 2 | |||
| 3 | from bika.lims import api  | 
            ||
| 4 | from bika.lims.permissions import AddAnalysisRequest  | 
            ||
| 5 | from bika.lims.utils import get_client  | 
            ||
| 6 | from plone.app.layout.viewlets import ViewletBase  | 
            ||
| 7 | from plone.memoize.instance import memoize  | 
            ||
| 8 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile  | 
            ||
| 9 | from senaite.core.interfaces import ISamplesView  | 
            ||
| 10 | from zope.component import getMultiAdapter  | 
            ||
| 11 | |||
| 12 | |||
| 13 | class AddSamplesViewlet(ViewletBase):  | 
            ||
| 14 |     index = ViewPageTemplateFile("templates/add_samples.pt") | 
            ||
| 15 | |||
| 16 | def available(self):  | 
            ||
| 17 | """Check if the add samples viewlet should be rendered  | 
            ||
| 18 | """  | 
            ||
| 19 | if not ISamplesView.providedBy(self.view):  | 
            ||
| 20 | return False  | 
            ||
| 21 | if not api.security.check_permission(AddAnalysisRequest, self.context):  | 
            ||
| 22 | return False  | 
            ||
| 23 | return True  | 
            ||
| 24 | |||
| 25 | def get_sample_add_number(self):  | 
            ||
| 26 | """Return the default number of Samples to add  | 
            ||
| 27 | """  | 
            ||
| 28 | setup = api.get_setup()  | 
            ||
| 29 | return setup.getDefaultNumberOfARsToAdd() or 1  | 
            ||
| 30 | |||
| 31 | def get_add_url(self):  | 
            ||
| 32 | """Return the sample add URL  | 
            ||
| 33 | """  | 
            ||
| 34 |         url = "{}/ar_add" | 
            ||
| 35 | client = get_client(self.context)  | 
            ||
| 36 | if client:  | 
            ||
| 37 | return url.format(api.get_url(client))  | 
            ||
| 38 |         return "{}/ar_add".format(api.get_url(self.context)) | 
            ||
| 39 | |||
| 40 | @property  | 
            ||
| 41 | @memoize  | 
            ||
| 42 | def theme_view(self):  | 
            ||
| 43 | return getMultiAdapter(  | 
            ||
| 44 | (self.context, self.request),  | 
            ||
| 45 | name="senaite_theme")  | 
            ||
| 46 |