def _create_biolist_comment(self): """ Replaces _save_notes_biolist_num func Adds biolist string to comment table instead of saving directly to fishing_activity.notes. Comment record is picked up for comment parsing later (FIELD-2071) :return: None """ if not self._current_haul: self._logger.error( 'Tried to save biolist num, but current haul not set.') return BIOLIST_NOTE_PREFIX = 'Biolist #' APPSTATE = f"haul_details_state::Haul {self.currentHaulId} Details" # could make a param, but shouldn't change # check if biolist comment already exists existing_comments = Comment.select().where( (Comment.trip == self._current_haul.trip) & (Comment.fishing_activity == self._current_haul.fishing_activity) & (fn.Lower(Comment.comment).contains(BIOLIST_NOTE_PREFIX.lower()))) if not existing_comments: Comment.create( username=ObserverDBUtil.get_setting('current_user'), comment_date=ObserverDBUtil.get_arrow_datestr(), comment=f"{BIOLIST_NOTE_PREFIX}{self.currentBiolistNum}", appstateinfo=APPSTATE, trip=self._current_haul.trip, fishing_activity=self._current_haul.fishing_activity) self._logger.debug( f"{BIOLIST_NOTE_PREFIX}{self.currentBiolistNum} comment created." ) else: # not sure if this will ever get used for trawl, but will update if necessary query = Comment.update( username=ObserverDBUtil.get_setting('current_user'), comment_date=ObserverDBUtil.get_arrow_datestr(), comment=f"{BIOLIST_NOTE_PREFIX}{self.currentBiolistNum}", appstateinfo=APPSTATE, ).where((Comment.fishing_activity == self._current_haul.fishing_activity) & (Comment.trip == self._current_haul.trip) & (Comment.comment.regexp('^' + BIOLIST_NOTE_PREFIX + '\d+$') ) # starts with Biolist #, ends with nums ) query.execute() self._logger.debug( f"{BIOLIST_NOTE_PREFIX}{self.currentBiolistNum} comment updated." )
def create_trip(self, vessel_id, observer_id, program_id): """ Create a new trip in the DB @param vessel_id: ID of vessel @param observer_id: ID of user @param program_id: ID of program @return: new trip cursor (peewee) """ try: is_fixed_gear = ObserverTrip.get_fg_value() newtrip = Trips.create( user=observer_id, vessel=vessel_id, program=program_id, partial_trip='F', trip_status='FALSE', created_by=observer_id, created_date=ObserverDBUtil.get_arrow_datestr(), is_fg_trip_local=is_fixed_gear) self.add_trip(newtrip) return newtrip except Exception as e: self._logger.error(e) return None
def add_comment(self, comment, appstate): """ Adds date, username, and comment to Comments :return: """ if not self.currentTripId: self._logger.error( 'No trip selected, comment NOT saved: {}'.format(comment)) return self._logger.info( f'Adding comment "{comment}" to current trip {self.currentTripId}') # TODO Add to trips, not Comment if self.isFixedGear: haul_db_id = self.sets.currentSetDBId if self.sets else None else: haul_db_id = self.hauls.currentHaulDBId if self.hauls else None newcomment = Comment.create( username=self.currentObserver, comment_date=ObserverDBUtil.get_arrow_datestr(), appstateinfo=appstate, comment=comment, trip=self.currentTripId, fishing_activity=haul_db_id) newcomment.save() self.update_comments()
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 _add_species_basket(self, weight, count): """ Add species comp item to DB and model Check for 0-weight basket @param weight: lbs @param count: fish num """ self._logger.debug('Add species basket. wt: {}, ct: {}'.format( weight, count)) if self._current_species_comp_item is None: self._logger.error('Species ID / Current Species Comp ID is None') return try: new_basket = SpeciesCompositionBaskets.create( species_comp_item=self._current_species_comp_item, basket_weight_itq=weight, fish_number_itq=count, created_by=ObserverDBUtil.get_current_user_id(), created_date=ObserverDBUtil.get_arrow_datestr( date_format=ObserverDBUtil.oracle_date_format), is_fg_tally_local=1 if self._is_fixed_gear else None) self._baskets_model.add_basket(new_basket) self._logger.info(f'Added basket wt: {weight} ct: {count}') finally: self._calculate_totals() self.basketAdded.emit()
def upsert_comment(self, comment_prefix, comment, appstate): """ Use for comment update/insert oustide of Comment dialog box. Find comment with prefix, if exists, replace, else insert :param comment_prefix: str, e.g. CollectionMethod= :param comment: str, e.g. string after prefix :param appstate: str, e.g. state of app + title of current screen :return: None """ new_comment_date = ObserverDBUtil.get_arrow_datestr() new_comment = f"{comment_prefix}{comment}" # try to get comment model and update try: c = Comment.get(Comment.comment.contains(comment_prefix), Comment.trip == self.currentTripId) Comment.update(comment=new_comment, comment_date=new_comment_date, username=self.currentObserver).where( Comment.comment_id == c.comment_id).execute() except ValueError: # trip id is not defined yet return # if existing comment not found, create a new one except Comment.DoesNotExist: Comment.create(comment=new_comment, comment_date=new_comment_date, username=self.currentObserver, trip=self.currentTripId, appstateinfo=appstate) # parse comments to trips/fishing_activities self.update_comments()
def addTripCert(self, cert_num): """ Add Trip Certificate (Permit / License #) @param cert_num: trip or license number """ # TODO certification_id? if self._current_trip is not None: user_id = ObserverDBUtil.get_current_user_id() created_date = ObserverDBUtil.get_arrow_datestr() cert = TripCertificates.create(certificate_number=cert_num, trip=self._current_trip.trip, created_by=user_id, created_date=created_date) self._certs_model.add_cert(cert)
def addFishTicket(self): if self.fishTicketNum is None or self.fishTicketDate is None or self.fishTicketState is None: return user_id = ObserverDBUtil.get_current_user_id() created_date = ObserverDBUtil.get_arrow_datestr() fish_ticket_number = FishTickets.create(fish_ticket_number=self.fishTicketNum, fish_ticket_date=self.fishTicketDate, state_agency=self.fishTicketState, trip=self._current_trip.trip, created_by=user_id, created_date=created_date) self._tickets_model.add_ticket(fish_ticket_number) self.fishTicketDate = None self.fishTicketNum = None
def get_free_comment_space_after_proposed_add(self, proposed_comment, proposed_appstate, max_text_allowed): size_before = self._get_size_of_existing_comments() # Instantiate a new model instance in memory, but don't add to database (no create, no save) newcomment = Comment(username=self.currentObserver, comment_date=ObserverDBUtil.get_arrow_datestr(), appstateinfo=proposed_appstate, comment=proposed_comment, trip=self.currentTripId) size_of_new_comment = 0 if newcomment is None else len(self._db_format_one_comment(newcomment)) size_after = size_before + size_of_new_comment return max_text_allowed - size_after, size_of_new_comment
def create_set(self, set_num): """ @param set_num: ID local to this trip @return: haul db ID """ self.load_sets(trip_id=self._trip_id) observer_id = ObserverDBUtil.get_current_user_id() newset = FishingActivities.create( trip=self._trip_id, fishing_activity_num=set_num, created_by=observer_id, created_date=ObserverDBUtil.get_arrow_datestr()) logging.info('Created FishingActivities (set {}) for trip={}'.format( newset.fishing_activity_num, self._trip_id)) self.SetsModel.add_set(newset) self.currentSetId = newset.fishing_activity_num return int(newset.fishing_activity)
def _add_additional_basket(self, basket_weight, basket_type): """ Utility for adding weighed and unweighed baskets to CATCH_ADDITIONAL_BASKETS for use in WM3 calculation of catch weight. :param basket_weight: :param basket_type: digit as text. :return: """ if basket_type not in self._catch_additional_basket_types: raise Exception(f"Unrecognized catch additional basket type {basket_type}") if self._weight_method != '3': msg = f"Weight Method is '{self._weight_method}', not '3'; additional baskets not allowed." self._logger.error(msg) raise Exception(msg) # Consistency check: if basket type is unweighed, basket weight should be 0 or None if basket_type == ObserverCatchBaskets.LOOKUP_VALUE_CAB_BASKET_TYPE_UNWEIGHED_FULL and \ basket_weight is not None and basket_weight != 0.0: msg = f"Basket type unweighed should have no or zero basket weight." self._logger.error(f"Basket type unweighed should have no or zero basket weight.") raise Exception(msg) basket_type_description = self._catch_additional_basket_types[basket_type] self._logger.debug(f'Add catch additional basket with wt={basket_weight} and ' f'type={basket_type_description}.') if basket_weight is None: basket_weight = 0.0 self._logger.debug(f"Unweighted baskets will be given weight of 0.0") new_basket = None try: new_basket = CatchAdditionalBaskets.create( catch=self._current_catch.catch, basket_weight=float(basket_weight), basket_type=basket_type, created_by=ObserverDBUtil.get_current_user_id(), created_date=ObserverDBUtil.get_arrow_datestr(date_format=ObserverDBUtil.oracle_date_format) ) except Exception as e: self._logger.error(e) finally: return new_basket
def add_update_location_haul_id(self, haul_id, position, date, latitude, longitude, depth): # depth_um assumed to be "ftm" try: try: location_item = FishingLocations.get( (FishingLocations.fishing_activity == haul_id) & (FishingLocations.position == position)) self._logger.debug( 'Fishing location haul ID={}, position={} found, updating.' .format(haul_id, position)) location_item.location_date = date location_item.latitude = latitude location_item.longitude = longitude location_item.depth = depth location_item.depth_um = 'FM' location_item.position = position location_item.save() # Update the database # Update location positions in DB and the view model to handle possible shift in position. self._update_location_positions() except FishingLocations.DoesNotExist: self._logger.debug( 'Create fishing location haul ID={}, position={}'.format( haul_id, position)) user_id = ObserverDBUtil.get_current_user_id() location_item = FishingLocations.create( fishing_activity=haul_id, location_date=date, latitude=latitude, longitude=longitude, depth=depth, depth_um='FM', position=position, created_by=user_id, created_date=ObserverDBUtil.get_arrow_datestr()) self._logger.debug( 'Fishing location position {} created.'.format( location_item.position)) # New entry added, but position number sequence may be off, depending on datetime of new entry. # Update location positions in DB and the view model to handle possible insertion. self._update_location_positions() except Exception as e: self._logger.error(e) return location_item.fishing_location ## Primary key index of location
def create_haul(self, haul_num): """ @param haul_num: ID local to this trip @return: haul db ID """ self.load_hauls(trip_id=self._trip_id) observer_id = ObserverDBUtil.get_current_user_id() newhaul = FishingActivities.create(trip=self._trip_id, fishing_activity_num=haul_num, created_by=observer_id, created_date=ObserverDBUtil.get_arrow_datestr()) logging.info( 'Created FishingActivities (haul {}) for trip={}'.format(newhaul.fishing_activity_num, newhaul.trip)) self.HaulsModel.add_haul(newhaul) self.currentHaulId = newhaul.fishing_activity_num self._create_biolist_comment() return int(newhaul.fishing_activity)
def _check_insert_db_pw_history(username, user_id, password): """ Check DB for old pw, if not in history, insert it @param user_id: Users USER_ID @param password: un-hashed pw @return: True if in DB, False if not """ # Check for PASSWORD_HISTORY table if existing (note: not updated via APPLIED_TRANACTIONS) pw_hashed = ObserverSoap.hash_pw(username.upper(), password) try: PasswordHistory.get((PasswordHistory.user == user_id) & (PasswordHistory.password == pw_hashed)) return True except PasswordHistory.DoesNotExist: # entry not found, insert PasswordHistory.create( user=user_id, created_by=user_id, created_date=ObserverDBUtil.get_arrow_datestr(), password=pw_hashed) return False except: return False
def _create_mix_species_if_not_present(self): """ Check SPECIES table for 'MIX' pacfin code, if not there, create it. Scientific name, commmon name, and PacFIN code are all 'MIX'. Species ID and species code are both 99999. """ current_user_id = ObserverDBUtil.get_current_user_id() created_date = ObserverDBUtil.get_arrow_datestr(date_format=ObserverDBUtil.oracle_date_format) mix_species_info = { 'species': ObserverData.MIX_SPECIES_CODE, 'scientific_name': ObserverData.MIX_PACFIN_CODE, 'common_name': ObserverData.MIX_PACFIN_CODE, 'species_code': ObserverData.MIX_SPECIES_CODE, 'pacfin_code': ObserverData.MIX_PACFIN_CODE, 'created_by': current_user_id if current_user_id else 1, 'created_date': created_date, } try: Species.get(Species.pacfin_code == 'MIX') self._logger.info('MIX exists in SPECIES table.') except Species.DoesNotExist: self._logger.info('Adding MIX to SPECIES table (one-time operation)') Species.create(**mix_species_info)
def _add_catch_basket(self, weight): """ Used only for special-case species MIX, a pseudo-species whose use indicates the basket data should be added to catch-level bucket, CATCH_ADDITIONAL_BASKETS rather than to a species-specific bucket, SPECIES_COMP_BASKETS. Add basket item to DB and model. Note: CATCH_ADDITIONAL_BASKETS does not have a field for count. @param weight: lbs """ self._logger.debug(f'Add catch (not species) basket. wt: {weight}') # Get the current catch and exit if not defined current_catch_id = self._current_species_comp_item.species_composition.catch.catch if current_catch_id is None: self._logger.error('Catch ID is None') return if weight is None: weight = 0.0 try: new_basket = CatchAdditionalBaskets.create( catch=current_catch_id, basket_weight=weight, created_by=ObserverDBUtil.get_current_user_id(), # FIELD-2087: Using default dateformat for time display; reformat before sync later created_date=ObserverDBUtil.get_arrow_datestr( date_format=ObserverDBUtil.default_dateformat), ) self._baskets_model.add_basket(new_basket) self._logger.info(f'Added addl basket wt: {weight}') finally: self._calculate_totals() self.basketAdded.emit()