def render_group_filter(self): # group orders groups = vocabs.groups_vocab() group_selector = factory( 'label:select', name='group', value=self.request.form.get('group', ''), props={ 'vocabulary': groups, 'label': _('group_orders_by', default=u'Group orders by'), } ) filter = group_selector(request=self.request) return filter
def query(self, soup): self._get_buyables_in_context() req_group_id = self.request.get('group_by', 'email') # if no req group is set if len(req_group_id) == 0: req_group_id = 'email' if req_group_id not in vocabs.groups_vocab(): raise InternalError('Group not allowed!') # not pretty but, otherwise there a problems with vocab + soup attrs # that need many places to refactor to make it work if req_group_id == 'buyable': req_group_id = 'buyable_uid' group_index = soup.catalog[req_group_id] # TODO: This was the fastest produceable way for me to # have an empty query to start with and concenate the rest. queries = [] # fetch user vendor uids vendor_uids = get_vendor_uids_for() # filter by given vendor uid or user vendor uids vendor_uid = self.request.form.get('vendor') if vendor_uid: vendor_uid = uuid.UUID(vendor_uid) # raise if given vendor uid not in user vendor uids if vendor_uid not in vendor_uids: raise Unauthorized queries.append(Eq('vendor_uid', vendor_uid)) # filter by customer if given customer = self.request.form.get('customer') if customer: queries.append(Eq('creator', customer)) # Filter by state if given state = self.request.form.get('state') if state: queries.append(Eq('state', state)) # Filter by salaried if given salaried = self.request.form.get('salaried') if salaried: queries.append(Eq('salaried', salaried)) req_from_date = self.request.get('from_date', '') req_to_date = self.request.get('to_date', '') date_query = self._datetime_checker(req_from_date, req_to_date) if date_query: queries.append(date_query) req_text = self.request.get('search[value]', '') text_query = self._text_checker(req_text) if text_query: queries.append(text_query) if queries: # TODO: See the TODO above. query = queries[0] for q in queries[1:]: query = query & q dummysize, bookings_set = soup.catalog.query(query) else: booking_uids = soup.catalog['uid'] bookings_set = booking_uids._rev_index.keys() bookings_set = set(bookings_set) buyable_index = soup.catalog['buyable_uid'] buyables_set = set() # get all buyables for the current context/path for buyable_uid in self._get_buyables_in_context(): try: buyables_set.update(buyable_index._fwd_index[buyable_uid]) except KeyError: continue # filter bookings_set to only match the current context/path bookings_set = bookings_set.intersection(buyables_set) unique_group_ids = [] # for each booking which matches the query append the qroup id once for group_id, group_booking_ids in group_index._fwd_index.items(): if bookings_set.intersection(group_booking_ids): unique_group_ids.append(group_id) res = odict() size = len(unique_group_ids) # for each unique group id get the matching group booking ids for group_id in self.slice(unique_group_ids): res[group_id] = [] for group_booking_id in group_index._fwd_index[group_id]: if group_booking_id in bookings_set: res[group_id].append(soup.get(group_booking_id)) return size, res
def render_filter(self): filter_widgets = "" # vendor areas of current user vendors = vendors_form_vocab() vendor_selector = None # vendor selection, include if more than one vendor if len(vendors) > 2: vendor_selector = factory( 'div:label:select', name='vendor', value=self.request.form.get('vendor', ''), props={ 'vocabulary': vendors, 'label': _('filter_for_vendors', default=u'Filter for vendors'), } ) filter_widgets += vendor_selector(request=self.request) # customers of current user customers = customers_form_vocab() customer_selector = None # customers selection, include if more than one customer if len(customers) > 2: customer_selector = factory( 'div:label:select', name='customer', value=self.request.form.get('customer', ''), props={ 'vocabulary': customers, 'label': _('filter_for_customers', default=u'Filter for customers'), } ) filter_widgets += customer_selector(request=self.request) states = states_form_vocab() state_selector = factory( 'div:label:select', name='state', value=self.request.form.get('state', ''), props={ 'vocabulary': states, 'label': _('filter_for_state', default=u'Filter for states'), } ) filter_widgets += state_selector(request=self.request) salaried = salaried_form_vocab() salaried_selector = factory( 'div:label:select', name='salaried', value=self.request.form.get('salaried', ''), props={ 'vocabulary': salaried, 'label': _('filter_for_salaried', default=u'Filter for salaried state'), } ) filter_widgets += salaried_selector(request=self.request) # From date filter. from_date = factory( 'div:label:text', name='from_date', value=self.request.form.get('from_date', ''), props={ 'div.class': 'date_from_filter', 'label': _('filter_from_date', default=u'Filter from date'), } ) filter_widgets += from_date(request=self.request) # To date filter. to_date = factory( 'div:label:text', name='to_date', value=self.request.form.get('to_date', ''), props={ 'div.class': 'date_to_filter', 'label': _('filter_to_date', default=u'Filter to date'), } ) filter_widgets += to_date(request=self.request) # group orders groups = vocabs.groups_vocab() group_selector = factory( 'div:label:select', name='group_by', value=self.request.form.get('group_by', 'buyable'), props={ 'div.class': 'group_filter', 'vocabulary': groups, 'label': _('group_orders_by', default=u'Group orders by'), } ) filter_widgets += group_selector(request=self.request) return filter_widgets
def query(self, soup): self._get_buyables_in_context() req_group_id = self.request.get('group_by', 'email') # if no req group is set if len(req_group_id) == 0: req_group_id = 'email' if req_group_id not in vocabs.groups_vocab(): raise InternalError('Group not allowed!') # not pretty but, otherwise there a problems with vocab + soup attrs # that need many places to refactor to make it work if req_group_id == 'buyable': req_group_id = 'buyable_uid' group_index = soup.catalog[req_group_id] booking_uids = soup.catalog['uid'] req_from_date = self.request.get('from_date', '') req_to_date = self.request.get('to_date', '') req_text = self.request.get('search[value]', '') date_query = self._datetime_checker(req_from_date, req_to_date) text_query = self._text_checker(req_text) if date_query and not text_query: dummysize, bookings_set = soup.catalog.query(date_query) elif text_query and not date_query: dummysize, bookings_set = soup.catalog.query(text_query) elif date_query and text_query: query = date_query & text_query dummysize, bookings_set = soup.catalog.query(query) else: bookings_set = booking_uids._rev_index.keys() bookings_set = set(bookings_set) buyable_index = soup.catalog['buyable_uid'] buyables_set = set() # get all buyables for the current context/path for buyable_uid in self._get_buyables_in_context(): try: buyables_set.update(buyable_index._fwd_index[buyable_uid]) except KeyError: continue # filter bookings_set to only match the current context/path bookings_set = bookings_set.intersection(buyables_set) unique_group_ids = [] # for each booking which matches the query append the qroup id once for group_id, group_booking_ids in group_index._fwd_index.items(): if bookings_set.intersection(group_booking_ids): unique_group_ids.append(group_id) res = odict() size = len(unique_group_ids) # for each unique group id get the matching group booking ids for group_id in self.slice(unique_group_ids): res[group_id] = [] for group_booking_id in group_index._fwd_index[group_id]: if group_booking_id in bookings_set: res[group_id].append(soup.get(group_booking_id)) return size, res