def add_vessel_ret(self, haul_id, vessel_ret): """ Add a vessel retained ListElement to our model and save to DB @param haul_id: Haul DB Id (Fishing Activity) @param vessel_ret: ListElement QJSValue @return: """ if isinstance(vessel_ret, QJSValue): # convert QJSValue to QVariant (then to dict) vessel_ret = vessel_ret.toVariant() found_cc_code = CatchCategories.get( CatchCategories.catch_category_code == vessel_ret['cc_code']) catch_num = ObserverCatches.get_next_catch_num_for_this_haul( haul_id, self._logger) Catches.create( fishing_activity=haul_id, catch_category=found_cc_code.catch_category, catch_weight=vessel_ret['weight'], catch_weight_method='7', catch_purity=None, catch_weight_um='LB', catch_disposition='R', catch_num=catch_num, created_by=ObserverDBUtil.get_current_user_id(), created_date=ObserverDBUtil.get_arrow_datestr(), ) self.appendItem(vessel_ret)
def verify_freq_cc_codes(self, code_list): catch_category_q = CatchCategories.select().where(CatchCategories.active >> None). \ order_by(CatchCategories.catch_category_code) full_cc_list = [c.catch_category_code for c in catch_category_q] for code in code_list: if code not in full_cc_list: self._logger.info( f'Unable to find CC {code}, removing from frequent list.') code_list.remove(code)
def _get_catch_categories_orm(self, rebuild=False): """ To support autocomplete, get catch categories from database via ORM, store DB keys """ if self._catch_categories is not None and not rebuild: # only build this once return self._catch_categories = list() catch_q = CatchCategories.select().where(CatchCategories.active.is_null(True)) for cc in catch_q: self._catch_categories.append('{} {}'.format(cc.catch_category_code, cc.catch_category_name)) self._catch_categories = sorted(self._catch_categories) # Sort Alphabetically
def _get_target_code(self, target_id: int): """ Find pacfic code for id @param target_id: catch_category_id @return: int ID """ if target_id is None: return '' try: found = CatchCategories.get(CatchCategories.catch_category == target_id) return found.catch_category_code except CatchCategories.DoesNotExist: self._logger.warning('Did not find code for {}'.format(target_id)) return None
def _get_catch_categories_from_db(): """ Load add catch categories from DB :return: list of dict """ active_catch_categories = [] catch_category_q = CatchCategories.select().where( (CatchCategories.active >> None) & (~fn.Lower(CatchCategories.catch_category_name).contains( '- target')) # FIELD-1219 ).order_by(CatchCategories.catch_category_code) for cc in catch_category_q: active_catch_categories.append(model_to_dict(cc)) return active_catch_categories
def _lookup_target_strat_id(self, strat: str): """ Use first 4 chars as a code and look up CATCH_CATEGORY_ID @param strat: code + optional text @return: int ID """ if strat is None or len(strat) < 3: self._logger.debug('Got bad strat ID to look up: {}'.format(strat)) return find_code = strat.split(' ')[0] try: found = CatchCategories.get(CatchCategories.catch_category_code % find_code) self._logger.info('Found ID {} for {}'.format(found.catch_category, find_code)) return found.catch_category except CatchCategories.DoesNotExist: self._logger.warning('Did not find ID for {}'.format(find_code)) return None