|
3 | 3 | # Copyright 2011-2016 by it's authors.
|
4 | 4 | # Some rights reserved. See LICENSE.txt, AUTHORS.txt.
|
5 | 5 |
|
| 6 | +import datetime |
| 7 | +import json |
| 8 | +from calendar import monthrange |
| 9 | + |
| 10 | +from DateTime import DateTime |
| 11 | +from Products.Archetypes.public import DisplayList |
6 | 12 | from Products.CMFCore.utils import getToolByName
|
7 | 13 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
|
| 14 | + |
| 15 | +from bika.lims import bikaMessageFactory as _ |
| 16 | +from bika.lims import logger |
8 | 17 | from bika.lims.browser import BrowserView
|
9 |
| -from bika.lims.catalog import CATALOG_ANALYSIS_REQUEST_LISTING |
10 | 18 | from bika.lims.catalog import CATALOG_ANALYSIS_LISTING
|
| 19 | +from bika.lims.catalog import CATALOG_ANALYSIS_REQUEST_LISTING |
11 | 20 | from bika.lims.catalog import CATALOG_WORKSHEET_LISTING
|
12 |
| -from bika.lims import bikaMessageFactory as _ |
13 |
| -from bika.lims import logger |
14 |
| -from calendar import monthrange |
15 |
| -from DateTime import DateTime |
16 |
| -import plone |
17 |
| -import json |
18 |
| -import datetime |
| 21 | +from bika.lims.utils import get_strings |
| 22 | + |
| 23 | +DASHBOARD_FILTER_COOKIE = 'dashboard_filter_cookie' |
19 | 24 |
|
20 | 25 |
|
21 | 26 | class DashboardView(BrowserView):
|
22 | 27 | template = ViewPageTemplateFile("templates/dashboard.pt")
|
23 | 28 |
|
| 29 | + def __init__(self, context, request): |
| 30 | + BrowserView.__init__(self, context, request) |
| 31 | + self.dashboard_cookie = None |
| 32 | + self.member = None |
| 33 | + |
24 | 34 | def __call__(self):
|
25 | 35 | tofrontpage = True
|
26 | 36 | mtool=getToolByName(self.context, 'portal_membership')
|
27 | 37 | if not mtool.isAnonymousUser() and self.context.bika_setup.getDashboardByDefault():
|
28 | 38 | # If authenticated user with labman role,
|
29 | 39 | # display the Main Dashboard view
|
30 | 40 | pm = getToolByName(self.context, "portal_membership")
|
31 |
| - member = pm.getAuthenticatedMember() |
32 |
| - roles = member.getRoles() |
| 41 | + self.member = pm.getAuthenticatedMember() |
| 42 | + roles = self.member.getRoles() |
33 | 43 | tofrontpage = 'Manager' not in roles and 'LabManager' not in roles
|
34 | 44 |
|
35 |
| - if tofrontpage == True: |
| 45 | + if tofrontpage: |
36 | 46 | self.request.response.redirect(self.portal_url + "/bika-frontpage")
|
37 | 47 | else:
|
38 | 48 | self._init_date_range()
|
| 49 | + self.dashboard_cookie = self.check_dashboard_cookie() |
39 | 50 | return self.template()
|
40 | 51 |
|
| 52 | + def check_dashboard_cookie(self): |
| 53 | + """ |
| 54 | + Check if the dashboard cookie should exist through bikasetup |
| 55 | + configuration. |
| 56 | +
|
| 57 | + If it should exist but doesn't exist yet, the function creates it |
| 58 | + with all values as default. |
| 59 | + If it should exist and already exists, it returns the value. |
| 60 | + Otherwise, the function returns None. |
| 61 | +
|
| 62 | + :return: a dictionary of strings |
| 63 | + """ |
| 64 | + # Getting cookie |
| 65 | + cookie_raw = self.request.get(DASHBOARD_FILTER_COOKIE, None) |
| 66 | + # If it doesn't exist, create it with default values |
| 67 | + if cookie_raw is None: |
| 68 | + cookie_raw = self._create_raw_data() |
| 69 | + self.request.response.setCookie( |
| 70 | + DASHBOARD_FILTER_COOKIE, |
| 71 | + json.dumps(cookie_raw), |
| 72 | + quoted=False, |
| 73 | + path='/') |
| 74 | + return cookie_raw |
| 75 | + return get_strings(json.loads(cookie_raw)) |
| 76 | + |
| 77 | + def is_filter_selected(self, selection_id, value): |
| 78 | + """ |
| 79 | + Compares whether the 'selection_id' parameter value saved in the |
| 80 | + cookie is the same value as the "value" parameter. |
| 81 | +
|
| 82 | + :param selection_id: a string as a dashboard_cookie key. |
| 83 | + :param value: The value to compare against the value from |
| 84 | + dashboard_cookie key. |
| 85 | + :return: Boolean. |
| 86 | + """ |
| 87 | + selected = self.dashboard_cookie.get(selection_id) |
| 88 | + return selected == value |
| 89 | + |
| 90 | + def _create_raw_data(self): |
| 91 | + """ |
| 92 | + Gathers the different sections ids and creates a string as first |
| 93 | + cookie data. |
| 94 | +
|
| 95 | + :return: A dictionary like: |
| 96 | + {'analyses':'all','analysisrequest':'all','worksheets':'all'} |
| 97 | + """ |
| 98 | + result = {} |
| 99 | + for section in self.get_sections(): |
| 100 | + result[section.get('id')] = 'all' |
| 101 | + return result |
| 102 | + |
41 | 103 | def _init_date_range(self):
|
42 | 104 | """ Sets the date range from which the data must be retrieved.
|
43 | 105 | Sets the values to the class parameters 'date_from',
|
@@ -130,6 +192,17 @@ def get_sections(self):
|
130 | 192 | self.get_worksheets_section()]
|
131 | 193 | return sections
|
132 | 194 |
|
| 195 | + def get_filter_options(self): |
| 196 | + """ |
| 197 | + Returns dasboard filter options. |
| 198 | + :return: Boolean |
| 199 | + """ |
| 200 | + dash_opt = DisplayList(( |
| 201 | + ('all', _('All')), |
| 202 | + ('mine', _('Mine')), |
| 203 | + )) |
| 204 | + return dash_opt |
| 205 | + |
133 | 206 | def _getStatistics(self, name, description, url, catalog, criterias, total):
|
134 | 207 | out = {'type': 'simple-panel',
|
135 | 208 | 'name': name,
|
@@ -163,6 +236,10 @@ def get_analysisrequests_section(self):
|
163 | 236 | cookie_dep_uid = self.request.get('filter_by_department_info', '').split(',') if filtering_allowed else ''
|
164 | 237 | query['getDepartmentUIDs'] = { "query": cookie_dep_uid,"operator":"or" }
|
165 | 238 |
|
| 239 | + # Check if dashboard_cookie contains any values to query |
| 240 | + # elements by |
| 241 | + query = self._update_criteria_with_filters(query, 'analysisrequests') |
| 242 | + |
166 | 243 | # Active Analysis Requests (All)
|
167 | 244 | total = len(catalog(query))
|
168 | 245 |
|
@@ -256,6 +333,10 @@ def get_worksheets_section(self):
|
256 | 333 | cookie_dep_uid = self.request.get('filter_by_department_info', '').split(',') if filtering_allowed else ''
|
257 | 334 | query['getDepartmentUIDs'] = { "query": cookie_dep_uid,"operator":"or" }
|
258 | 335 |
|
| 336 | + # Check if dashboard_cookie contains any values to query |
| 337 | + # elements by |
| 338 | + query = self._update_criteria_with_filters(query, 'worksheets') |
| 339 | + |
259 | 340 | # Active Worksheets (all)
|
260 | 341 | total = len(bc(query))
|
261 | 342 |
|
@@ -316,6 +397,9 @@ def get_analyses_section(self):
|
316 | 397 | cookie_dep_uid = self.request.get('filter_by_department_info', '').split(',') if filtering_allowed else ''
|
317 | 398 | query['getDepartmentUID'] = { "query": cookie_dep_uid,"operator":"or" }
|
318 | 399 |
|
| 400 | + # Check if dashboard_cookie contains any values to query elements by |
| 401 | + query = self._update_criteria_with_filters(query, 'analyses') |
| 402 | + |
319 | 403 | # Active Analyses (All)
|
320 | 404 | total = len(bc(query))
|
321 | 405 |
|
@@ -522,3 +606,19 @@ def _fill_dates_evo(self, catalog, query):
|
522 | 606 | del o[r]
|
523 | 607 |
|
524 | 608 | return outevo
|
| 609 | + |
| 610 | + def _update_criteria_with_filters(self, query, section_name): |
| 611 | + """ |
| 612 | + This method updates the 'query' dictionary with the criteria stored in |
| 613 | + dashboard cookie. |
| 614 | +
|
| 615 | + :param query: A dictionary with search criteria. |
| 616 | + :param section_name: The dashboard section name |
| 617 | + :return: The 'query' dictionary |
| 618 | + """ |
| 619 | + if self.dashboard_cookie is None: |
| 620 | + return query |
| 621 | + cookie_criteria = self.dashboard_cookie.get(section_name) |
| 622 | + if cookie_criteria == 'mine': |
| 623 | + query['Creator'] = self.member.getId() |
| 624 | + return query |
0 commit comments