def edit(self, validated_user_id, edit_dict, row_id): # {'experience': [{column: value}]} for section_name, changes in edit_dict.items(): dao = self.dao_map.get(section_name) if dao is not None: formatted_changes = Formatting.format_datetime_strings_in_dict( changes, keys=dao.date_time_columns, input_format=Formatting.DATETIME_JS_FORMAT, output_format=Formatting.DATETIME_DB_FORMAT) formatted_changes = Formatting.remove_nones_from_dict( formatted_changes) # Only allow changes to rows owned by validated_user_id. if row_id == validated_user_id: where_condition = {SCHEMA.ID: validated_user_id} else: where_condition = { SCHEMA.ID: row_id, SCHEMA.USER_ID: validated_user_id } dao.update(None, formatted_changes, where_condition=where_condition)
def get_full_user_data(self, user_id, date_format=Formatting.DATETIME_JS_FORMAT, replace_none=False): # TODO Maybe formatting should be optional as edit_modal uses it to get current data and having None # date times would be lush. # Fetch user data from Users table, return None immediately if user_id is not found. user_dict = self.get_user_data(user_id=user_id) if user_dict is None: return None # Read bio and add to dictionary. bio_rows = self.bios_dao.read( None, where_condition={SCHEMA.USER_ID: user_id}, distinct=True) bio_dict = Formatting.map_rows_to_schema(bio_rows, self.bios_dao.SCHEMA) user_dict['bio'] = bio_dict[0] if bio_dict else None # Add any experiences. experience_rows = self.experiences_dao.read( None, where_condition={SCHEMA.USER_ID: user_id}, sortby=SCHEMA.END_DATE) experiences_dicts = Formatting.map_rows_to_schema( experience_rows, self.experiences_dao.SCHEMA) formatted_experiences_dict = Formatting.format_datetime_strings_in_dict( experiences_dicts, keys=self.experiences_dao.date_time_columns, input_format=Formatting.DATETIME_DB_FORMAT, output_format=date_format, replace_none=replace_none) user_dict[ 'experiences'] = formatted_experiences_dict if formatted_experiences_dict else None # Add any qualifications. qualification_rows = self.qualifications_dao.read( None, where_condition={SCHEMA.USER_ID: user_id}, sortby=SCHEMA.END_DATE) qualification_dict = Formatting.map_rows_to_schema( qualification_rows, self.qualifications_dao.SCHEMA) formatted_qualification_dict = Formatting.format_datetime_strings_in_dict( qualification_dict, keys=self.qualifications_dao.date_time_columns, input_format=Formatting.DATETIME_DB_FORMAT, output_format=date_format, replace_none=replace_none) user_dict[ 'qualifications'] = formatted_qualification_dict if formatted_qualification_dict else None return user_dict
def add(self, validated_user_id, add_dict): for section_name, change in add_dict.items(): dao = self.dao_map.get(section_name) # TODO maybe remove all Nones, nulls and '' here. if dao is not None: # Add validated user ID as user_id change[SCHEMA.USER_ID] = validated_user_id formatted_changes = Formatting.format_datetime_strings_in_dict( change, keys=dao.date_time_columns, input_format=Formatting.DATETIME_JS_FORMAT, output_format=Formatting.DATETIME_DB_FORMAT) dao.new(formatted_changes)
def validate_session(self, session_key, user_id): # Fetch sessions for this user. session_rows = self.sessions_dao.read( None, where_condition={SCHEMA.SESSION_KEY: session_key}, sortby=SCHEMA.DATE_CREATED) # Return False if no sessions have been started. if not session_rows: return False # Return True if latest user_id matches. latest_dict = Formatting.map_rows_to_schema( session_rows, self.sessions_dao.SCHEMA)[0] if user_id == latest_dict.get(SCHEMA.USER_ID): return True return False
def date(input_id=None, classes=None, value=None, max_today=True): input_html = HTMLComponents.HTML.get('input') # TODO if is none, tick no date checkbox, date readonly, value = today # TODO if is not none, clear no date checkbox, date write # Maybe? Could sort this somewhere else. today = Formatting.datetime_to_string(datetime.datetime.now(), format_string=Formatting.DATETIME_JS_FORMAT) readonly = False if HTMLComponents.is_none(value): # readonly = True value = today date_div = input_html.get('date').format( date_id=HTMLComponents.null(input_id), classes=HTMLComponents.null(classes), value=value, readonly='readonly' if readonly else '', max='max=' + today if max_today else '' ) return HTMLComponents.div(date_div, class_name='form-group')
def new(self, data_dict): data_dict[SCHEMA.DATE_CREATED] = Formatting.datetime_to_string( datetime.datetime.now()) return super().new(data_dict)
def new(self, data_dict): data_dict[SCHEMA.LAST_CHANGED] = Formatting.datetime_to_string( datetime.datetime.now()) return super().new(data_dict)